diff --git a/src/abis/core-mx-life-bonding-sc.abi.json b/src/abis/core-mx-life-bonding-sc.abi.json index 46b3819..f27e3fa 100644 --- a/src/abis/core-mx-life-bonding-sc.abi.json +++ b/src/abis/core-mx-life-bonding-sc.abi.json @@ -106,6 +106,39 @@ ], "outputs": [] }, + { + "name": "setVaultNonce", + "mutability": "mutable", + "inputs": [ + { + "name": "token_identifier", + "type": "TokenIdentifier" + }, + { + "name": "nonce", + "type": "u64" + } + ], + "outputs": [] + }, + { + "name": "topUpVault", + "mutability": "mutable", + "payableInTokens": [ + "*" + ], + "inputs": [ + { + "name": "token_identifier", + "type": "TokenIdentifier" + }, + { + "name": "nonce", + "type": "u64" + } + ], + "outputs": [] + }, { "name": "getCompensationBlacklist", "mutability": "readonly", @@ -132,6 +165,25 @@ } ] }, + { + "name": "getAddressVaultNone", + "mutability": "readonly", + "inputs": [ + { + "name": "address", + "type": "Address" + }, + { + "name": "token_identifier", + "type": "TokenIdentifier" + } + ], + "outputs": [ + { + "type": "u64" + } + ] + }, { "name": "getBond", "mutability": "readonly", diff --git a/src/bond.ts b/src/bond.ts index 860c45c..4e6cbe6 100644 --- a/src/bond.ts +++ b/src/bond.ts @@ -16,6 +16,7 @@ import { import { EnvironmentsEnum, bondContractAddress, + dataNftTokenIdentifier, itheumTokenIdentifier } from './config'; import { ErrContractQuery } from './errors'; @@ -179,6 +180,36 @@ export class BondContract extends Contract { } } + /** + * Returns the address vault nonce for a specific address + * @param address address to query + * @param tokenIdentifier token identifier to query [default token identifier based on the {@link EnvironmentsEnum}] + */ + async viewAddressVaultNonce( + address: IAddress, + tokenIdentifier = itheumTokenIdentifier[this.env as EnvironmentsEnum] + ): Promise { + const interaction = this.contract.methodsExplicit.getAddressVaultNone([ + new AddressValue(address), + new TokenIdentifierValue(tokenIdentifier) + ]); + 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()) { + return firstValue?.valueOf().toNumber(); + } else { + throw new ErrContractQuery( + 'viewAddressVaultNonce', + returnCode.toString() + ); + } + } + /** * Returns the contract owner address */ @@ -1129,6 +1160,40 @@ export class BondContract extends Contract { return bondTx; } + /** + * Builds a `topUpVault` transaction + * @param senderAddress the address of the sender + * @param payment the payment for the top up (tokenIdentifier, nonce and amount) + * @param nonce the nonce of the Data Nft + * @param tokenIdentifier the token identifier of the Data Nft [default is the Data Nft token identifier based on {@link EnvironmentsEnum}] + */ + topUpVault( + senderAddress: IAddress, + payment: { + tokenIdentifier: string; + amount: BigNumber.Value; + }, + nonce: number, + tokenIdentifier = dataNftTokenIdentifier[this.env as EnvironmentsEnum] + ): Transaction { + const topUpVaultTx = new Transaction({ + value: 0, + data: new ContractCallPayloadBuilder() + .setFunction(new ContractFunction('ESDTTransfer')) + .addArg(new TokenIdentifierValue(payment.tokenIdentifier)) + .addArg(new BigUIntValue(payment.amount)) + .addArg(new StringValue('topUpVault')) + .addArg(new TokenIdentifierValue(tokenIdentifier)) + .addArg(new U64Value(nonce)) + .build(), + receiver: this.contract.getAddress(), + sender: senderAddress, + gasLimit: 40_000_000, + chainID: this.chainID + }); + return topUpVaultTx; + } + /** * Builds a `bond` transaction with NFT/SFT transfer * @param senderAddress the address of the sender @@ -1293,6 +1358,32 @@ export class BondContract extends Contract { return renewTx; } + /** + * Builds a `setVaultNonce` transaction + * @param senderAddress the address of the sender + * @param nonce the nonce to set + * @param tokenIdentifier the Data Nft token identifier [default is the Data Nft token identifier based on {@link EnvironmentsEnum}] + */ + setVaultNonce( + senderAddress: IAddress, + nonce: number, + tokenIdentifier = dataNftTokenIdentifier[this.env as EnvironmentsEnum] + ): Transaction { + const tx = new Transaction({ + value: 0, + data: new ContractCallPayloadBuilder() + .setFunction('setVaultNonce') + .addArg(new TokenIdentifierValue(tokenIdentifier)) + .addArg(new U64Value(nonce)) + .build(), + receiver: this.contract.getAddress(), + sender: senderAddress, + gasLimit: 20_000_000, + chainID: this.chainID + }); + return tx; + } + /** * Builds a `proof` transaction * @param senderAddress the address of the sender