Buy & Sell

This is an example of buying / selling tokens.

Constants

Following are the constant values used in examples below:

export const BIG_TEN = new BigNumber(10);
export const DEFAULT_TOKEN_DECIMAL = 18;

Buy

Buy contract call is as follows:

mintClubBondContract.buy(
    tokenAddress,
    amount,
    minReward,
    beneficiary,
);

In order to calculate the minReward, we utilize the getMintReward function from mintClubBondContract.

const contract = getMintClubBondContract();
const result = await contract.getMintReward(
  tokenAddress,
  truncateDecimals(new BigNumber(mintAmount).times(BIG_TEN.pow(DEFAULT_TOKEN_DECIMAL)).toString(), 0),
); // here mintAmount is amount of mint user is willing to spend

const outBN = new BigNumber(result[0].toString());
const minReward = truncateDecimals(
  outBN
    .times((100 - 2 /* slippage percentage */) / 100)
    .toFixed(0, 1)
    .toString(10),
  0,
);

truncateDecimals is a number helper function:

With the calculated values, we can now call the contract:

Sell

Sell contract call is as follows:

In order to calculate the minRefund, we utilize the getBurnRefund function from mintClubBondContract.

With the calculated minRefund value, we can now call the sell function on mintClubBondContract.