Skip to content

Commit

Permalink
feat: vault setup and topup
Browse files Browse the repository at this point in the history
  • Loading branch information
bucurdavid committed Jul 18, 2024
1 parent 2932d0a commit 26828e2
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/abis/core-mx-life-bonding-sc.abi.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
91 changes: 91 additions & 0 deletions src/bond.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import {
EnvironmentsEnum,
bondContractAddress,
dataNftTokenIdentifier,
itheumTokenIdentifier
} from './config';
import { ErrContractQuery } from './errors';
Expand Down Expand Up @@ -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<number> {
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
*/
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 26828e2

Please sign in to comment.