From 60b46e3fbeab4475b864ab8db9c499a6cd7fb395 Mon Sep 17 00:00:00 2001 From: Bucur David Date: Tue, 12 Mar 2024 08:55:04 +0200 Subject: [PATCH] feat: bond contract configuration view --- src/abis/core-mx-life-bonding-sc.abi.json | 47 +++++++++++++++++++++++ src/bond.ts | 47 ++++++++++++++++++++--- src/common/utils.ts | 29 +++++++++++++- src/interfaces.ts | 10 +++++ tests/bond.test.ts | 8 ++++ 5 files changed, 134 insertions(+), 7 deletions(-) diff --git a/src/abis/core-mx-life-bonding-sc.abi.json b/src/abis/core-mx-life-bonding-sc.abi.json index d6b224e..86f8d54 100644 --- a/src/abis/core-mx-life-bonding-sc.abi.json +++ b/src/abis/core-mx-life-bonding-sc.abi.json @@ -395,6 +395,16 @@ } ] }, + { + "name": "getContractConfiguration", + "mutability": "readonly", + "inputs": [], + "outputs": [ + { + "type": "ContractConfiguration" + } + ] + }, { "name": "setBlacklist", "mutability": "mutable", @@ -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" + }, + { + "name": "bond_amounts", + "type": "List" + }, + { + "name": "minimum_penalty", + "type": "u64" + }, + { + "name": "maximum_penalty", + "type": "u64" + }, + { + "name": "withdraw_penalty", + "type": "u64" + }, + { + "name": "accepted_callers", + "type": "List
" + } + ] + }, "EsdtTokenPayment": { "type": "struct", "fields": [ diff --git a/src/bond.ts b/src/bond.ts index f5b0629..5207b1d 100644 --- a/src/bond.ts +++ b/src/bond.ts @@ -9,7 +9,8 @@ import { TokenIdentifierValue, Transaction, TypedValue, - U64Value + U64Value, + VariadicValue } from '@multiversx/sdk-core/out'; import { EnvironmentsEnum, @@ -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 { /** @@ -71,6 +79,33 @@ export class BondContract extends Contract { } } + /** + * Returns the `bond` contract configuration + */ + async viewContractConfiguration(): Promise { + 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 */ @@ -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(); @@ -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 }); } diff --git a/src/common/utils.ts b/src/common/utils.ts index c6f81db..f9bc199 100644 --- a/src/common/utils.ts +++ b/src/common/utils.ts @@ -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'; @@ -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(), diff --git a/src/interfaces.ts b/src/interfaces.ts index 91a9e9b..f960a12 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -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; diff --git a/tests/bond.test.ts b/tests/bond.test.ts index a6984bb..8c099e5 100644 --- a/tests/bond.test.ts +++ b/tests/bond.test.ts @@ -1,4 +1,5 @@ import { + BondConfiguration, BondContract, Compensation, State, @@ -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; + }); test('#test view methods', async () => { const bondContract = new BondContract('devnet');