Skip to content

Commit

Permalink
feat: bond contract configuration view
Browse files Browse the repository at this point in the history
  • Loading branch information
bucurdavid committed Mar 12, 2024
1 parent 22ea11c commit 60b46e3
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 7 deletions.
47 changes: 47 additions & 0 deletions src/abis/core-mx-life-bonding-sc.abi.json
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,16 @@
}
]
},
{
"name": "getContractConfiguration",
"mutability": "readonly",
"inputs": [],
"outputs": [
{
"type": "ContractConfiguration"
}
]
},
{
"name": "setBlacklist",
"mutability": "mutable",
Expand Down Expand Up @@ -1041,6 +1051,43 @@
}
]
},
"ContractConfiguration": {
"type": "struct",
"fields": [
{
"name": "contract_state",
"type": "State"
},
{
"name": "bond_payment_token_identifier",
"type": "TokenIdentifier"
},
{
"name": "lock_periods",
"type": "List<u64>"
},
{
"name": "bond_amounts",
"type": "List<BigUint>"
},
{
"name": "minimum_penalty",
"type": "u64"
},
{
"name": "maximum_penalty",
"type": "u64"
},
{
"name": "withdraw_penalty",
"type": "u64"
},
{
"name": "accepted_callers",
"type": "List<Address>"
}
]
},
"EsdtTokenPayment": {
"type": "struct",
"fields": [
Expand Down
47 changes: 41 additions & 6 deletions src/bond.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
TokenIdentifierValue,
Transaction,
TypedValue,
U64Value
U64Value,
VariadicValue
} from '@multiversx/sdk-core/out';
import {
EnvironmentsEnum,
Expand All @@ -22,12 +23,19 @@ import BigNumber from 'bignumber.js';
import bondContractAbi from './abis/core-mx-life-bonding-sc.abi.json';
import {
parseBond,
parseBondConfiguration,
parseCompensation,
parseRefund,
parseTokenIdentifier
} from './common/utils';
import { Contract } from './contract';
import { Bond, Compensation, PenaltyType, State } from './interfaces';
import {
Bond,
BondConfiguration,
Compensation,
PenaltyType,
State
} from './interfaces';

export class BondContract extends Contract {
/**
Expand Down Expand Up @@ -71,6 +79,33 @@ export class BondContract extends Contract {
}
}

/**
* Returns the `bond` contract configuration
*/
async viewContractConfiguration(): Promise<BondConfiguration> {
const interaction = this.contract.methodsExplicit.getContractConfiguration(
[]
);
const query = interaction.buildQuery();
const queryResponse = await this.networkProvider.queryContract(query);
const endpointDefinition = interaction.getEndpoint();
const { firstValue, returnCode } = new ResultsParser().parseQueryResponse(
queryResponse,
endpointDefinition
);
if (returnCode.isSuccess()) {
const firstValueAsVariadic = firstValue as VariadicValue;
const returnValue = firstValueAsVariadic?.valueOf();
const bondConfiguration = parseBondConfiguration(returnValue);
return bondConfiguration;
} else {
throw new ErrContractQuery(
'viewContractConfiguration',
returnCode.toString()
);
}
}

/**
* Returns the contract owner address
*/
Expand Down Expand Up @@ -142,7 +177,7 @@ export class BondContract extends Contract {
* Returns the contract lock periods and bond amounts
*/
async viewLockPeriodsWithBonds(): Promise<
{ lockPeriod: string; amount: string }[]
{ lockPeriod: number; amount: BigNumber.Value }[]
> {
const interaction = this.contract.methodsExplicit.getLockPeriodsBonds([]);
const query = interaction.buildQuery();
Expand All @@ -157,10 +192,10 @@ export class BondContract extends Contract {
const bondAmounts: BigNumber[] = firstValue?.valueOf().field1;

// Construct array of objects containing lock period and bond amount
const result: { lockPeriod: string; amount: string }[] = [];
const result: { lockPeriod: number; amount: BigNumber.Value }[] = [];
for (let i = 0; i < lockPeriods.length; i++) {
const lockPeriod = lockPeriods[i].toString();
const bondAmount = bondAmounts[i].toString();
const lockPeriod = lockPeriods[i].toNumber();
const bondAmount = bondAmounts[i];
result.push({ lockPeriod: lockPeriod, amount: bondAmount });
}

Expand Down
29 changes: 28 additions & 1 deletion src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ import {
} from '../errors';
import {
Bond,
BondConfiguration,
Compensation,
ContractConfiguration,
NftEnumType,
NftType,
Offer,
Refund
Refund,
State
} from '../interfaces';
import { EnvironmentsEnum, dataMarshalUrlOverride } from '../config';

Expand Down Expand Up @@ -98,6 +101,30 @@ export function parseBond(value: any): Bond {
};
}

export function parseBondConfiguration(value: any): BondConfiguration {
const lockPeriods: BigNumber[] = value.lock_periods;
const bondAmounts: BigNumber[] = value.bond_amounts;

// Construct array of objects containing lock period and bond amount
const result: { lockPeriod: number; amount: BigNumber.Value }[] = [];
for (let i = 0; i < lockPeriods.length; i++) {
const lockPeriod = lockPeriods[i].toNumber();
const bondAmount = bondAmounts[i].toFixed(0);
result.push({ lockPeriod: lockPeriod, amount: bondAmount });
}
return {
contractState: value.contract_state.name as State,
bondPaymentTokenIdentifier: value.bond_payment_token_identifier.toString(),
lockPeriodsWithBonds: result,
minimumPenalty: value.minimum_penalty.toNumber(),
maximumPenalty: value.maximum_penalty.toNumber(),
withdrawPenalty: value.withdraw_penalty.toNumber(),
acceptedCallers: value.accepted_callers.map((address: any) =>
address.toString()
)
};
}

export function parseCompensation(value: any): Compensation {
return {
compensationId: value.compensation_id.toNumber(),
Expand Down
10 changes: 10 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@ export interface Bond {
remainingAmount: BigNumber.Value;
}

export interface BondConfiguration {
contractState: State;
bondPaymentTokenIdentifier: string;
lockPeriodsWithBonds: { lockPeriod: number; amount: BigNumber.Value }[];
minimumPenalty: number;
maximumPenalty: number;
withdrawPenalty: number;
acceptedCallers: string[];
}

export interface Refund {
compensationId: number;
address: string;
Expand Down
8 changes: 8 additions & 0 deletions tests/bond.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
BondConfiguration,
BondContract,
Compensation,
State,
Expand All @@ -16,6 +17,13 @@ describe('Bond test', () => {
expect(e.message).toBe('Contract address is not deployed on testnet');
}
});

test('#view bond configuration', async () => {
const bondContract = new BondContract('devnet');
const bondConfiguration = await bondContract.viewContractConfiguration();
console.log(bondConfiguration);
expect(bondConfiguration).toMatchObject<BondConfiguration>;
});
test('#test view methods', async () => {
const bondContract = new BondContract('devnet');

Expand Down

0 comments on commit 60b46e3

Please sign in to comment.