Skip to content

Commit

Permalink
feat: bond contract transaction method builders implementation
Browse files Browse the repository at this point in the history
Refs: #99
  • Loading branch information
bucurdavid committed Feb 22, 2024
1 parent d7eb098 commit cd08ff3
Show file tree
Hide file tree
Showing 3 changed files with 226 additions and 2 deletions.
220 changes: 219 additions & 1 deletion src/bond.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import {
AbiRegistry,
Address,
AddressValue,
ContractCallPayloadBuilder,
IAddress,
ResultsParser,
SmartContract,
TokenIdentifierValue,
Transaction,
U64Value
} from '@multiversx/sdk-core/out';
import { ApiNetworkProvider } from '@multiversx/sdk-network-providers/out';
Expand All @@ -17,7 +19,7 @@ import {
} from './config';

import bondContractAbi from './abis/core-mx-life-bonding-sc.abi.json';
import { Bond, Compensation, State } from './interfaces';
import { Bond, Compensation, PenaltyType, State } from './interfaces';
import {
parseBond,
parseCompensation,
Expand Down Expand Up @@ -326,4 +328,220 @@ export class BondContract {
throw new ErrContractQuery('getCompensation', returnCode.toString());
}
}

/**
* Builds a `setAdministrator` transaction
* @param senderAddress address of the sender (must be the owner of the contract)
* @param newAdministrator new administrator address
*/
setAdministrator(
senderAddress: IAddress,
newAdministrator: IAddress
): Transaction {
const setAdministratorTx = new Transaction({
value: 0,
data: new ContractCallPayloadBuilder()
.setFunction('setAdministrator')
.addArg(new AddressValue(newAdministrator))
.build(),
receiver: this.contract.getAddress(),
sender: senderAddress,
gasLimit: 10_000_000,
chainID: this.chainID
});
return setAdministratorTx;
}

/**
* Builds a `sanction` transaction
* @param senderAddress address of the sender (must be the owner of the contract or the administrator)
* @param tokenIdentifier token identifier to sanction
* @param nonce nonce to sanction
* @param penalty penalty type
* @param customPenalty custom penalty amount (required if penalty is `Custom`)
*/
sanction(
senderAddress: IAddress,
tokenIdentifier: string,
nonce: number,
penalty: PenaltyType,
customPenalty?: number
): Transaction {
let data;
if (penalty === PenaltyType.Custom && customPenalty) {
data = new ContractCallPayloadBuilder()
.setFunction('sanction')
.addArg(new TokenIdentifierValue(tokenIdentifier))
.addArg(new U64Value(nonce))
.addArg(new U64Value(penalty))
.addArg(new U64Value(customPenalty))
.build();
} else {
data = new ContractCallPayloadBuilder()
.setFunction('sanction')
.addArg(new TokenIdentifierValue(tokenIdentifier))
.addArg(new U64Value(nonce))
.addArg(new U64Value(penalty))
.build();
}

const sanctionTx = new Transaction({
value: 0,
data,
receiver: this.contract.getAddress(),
sender: senderAddress,
gasLimit: 30_000_000,
chainID: this.chainID
});
return sanctionTx;
}

/**
* Builds a `modifyBond` transaction
* @param senderAddress address of the sender (must be the owner of the contract or the administrator)
* @param tokenIdentifier token identifier to modify the bond for
* @param nonce nonce to modify the bond for
*/
modifyBond(
senderAddress: IAddress,
tokenIdentifier: string,
nonce: number
): Transaction {
const modifyBondTx = new Transaction({
value: 0,
data: new ContractCallPayloadBuilder()
.setFunction('modifyBond')
.addArg(new TokenIdentifierValue(tokenIdentifier))
.addArg(new U64Value(nonce))
.build(),
receiver: this.contract.getAddress(),
sender: senderAddress,
gasLimit: 10_000_000,
chainID: this.chainID
});
return modifyBondTx;
}

/**
* Builds a `setContractState` transaction
* @param senderAddress address of the sender (must be the owner of the contract or the administrator)
* @param state state to set the contract to
*/
setContractState(senderAddress: IAddress, state: State): Transaction {
let data;
if (state === State.Inactive) {
data = new ContractCallPayloadBuilder()
.setFunction('setContractStateInactive')
.build();
} else {
data = new ContractCallPayloadBuilder()
.setFunction('setContractStateActive')
.build();
}

const setContractStateTx = new Transaction({
value: 0,
data: data,
receiver: this.contract.getAddress(),
sender: senderAddress,
gasLimit: 10_000_000,
chainID: this.chainID
});
return setContractStateTx;
}

setAcceptedCallers(senderAddress: IAddress, addresses: IAddress[]) {
throw new Error('Not implemented');
}

setBondToken(senderAddress: IAddress, tokenIdentifier: string) {
throw new Error('Not implemented');
}

setPeriodsBonds(
senderAddress: IAddress,
periods: number[],
bonds: BigNumber.Value[]
) {
throw new Error('Not implemented');
}

setMinimumPenalty(senderAddress: IAddress, penalty: number) {
throw new Error('Not implemented');
}

setMaximumPenalty(senderAddress: IAddress, penalty: number) {
throw new Error('Not implemented');
}

setWithdrawPenalty(senderAddress: IAddress, penalty: number) {
throw new Error('Not implemented');
}

/**
* Builds a `withdraw` transaction
* @param senderAddress address of the sender
* @param tokenIdentifier token identifier to withdraw the bond for
* @param nonce nonce to withdraw the bond for
*/
withdraw(
senderAddress: IAddress,
tokenIdentifier: string,
nonce: number
): Transaction {
const withdrawTx = new Transaction({
value: 0,
data: new ContractCallPayloadBuilder()
.setFunction('withdraw')
.addArg(new TokenIdentifierValue(tokenIdentifier))
.addArg(new U64Value(nonce))
.addArg(new U64Value(nonce))
.build(),
receiver: this.contract.getAddress(),
sender: senderAddress,
gasLimit: 10_000_000,
chainID: this.chainID
});
return withdrawTx;
}

/**
* Builds a `renew` transaction
* @param senderAddress address of the sender
* @param tokenIdentifier token identifier for the bond to renew
* @param nonce nonce for the bond to renew
* @param newlockPeriod new lock period for the bond
*/
renew(
senderAddress: IAddress,
tokenIdentifier: string,
nonce: number,
newLockPeriod?: number
): Transaction {
let data;
if (newLockPeriod) {
data = new ContractCallPayloadBuilder()
.setFunction('renew')
.addArg(new TokenIdentifierValue(tokenIdentifier))
.addArg(new U64Value(nonce))
.addArg(new U64Value(newLockPeriod))
.build();
} else {
data = new ContractCallPayloadBuilder()
.setFunction('renew')
.addArg(new TokenIdentifierValue(tokenIdentifier))
.addArg(new U64Value(nonce))
.build();
}

const renewTx = new Transaction({
value: 0,
data,
receiver: this.contract.getAddress(),
sender: senderAddress,
gasLimit: 10_000_000,
chainID: this.chainID
});
return renewTx;
}
}
6 changes: 6 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ export enum State {
Active = 1
}

export enum PenaltyType {
Minimum = 0,
Custom = 1,
Maximum = 2
}

export interface Compensation {
tokenIdentifier: string;
nonce: number;
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"resolveJsonModule": true,
"noUnusedParameters": true,
"noUnusedParameters": false,
"esModuleInterop": true,
"declaration": true
},
Expand Down

0 comments on commit cd08ff3

Please sign in to comment.