Mint Club
Search…
Buy & Sell
This is an example of buying / selling tokens.

Constants

Following are the constant values used in examples below:
1
export const BIG_TEN = new BigNumber(10);
2
export const DEFAULT_TOKEN_DECIMAL = 18;
Copied!

Buy

Buy contract call is as follows:
1
mintClubBondContract.buy(
2
tokenAddress,
3
amount,
4
minReward,
5
beneficiary,
6
);
Copied!
In order to calculate the minReward, we utilize the getMintReward function from mintClubBondContract.
1
const contract = getMintClubBondContract();
2
const result = await contract.getMintReward(
3
tokenAddress,
4
truncateDecimals(new BigNumber(mintAmount).times(BIG_TEN.pow(DEFAULT_TOKEN_DECIMAL)).toString(), 0),
5
); // here mintAmount is amount of mint user is willing to spend
6
7
const outBN = new BigNumber(result[0].toString());
8
const minReward = truncateDecimals(
9
outBN
10
.times((100 - 2 /* slippage percentage */) / 100)
11
.toFixed(0, 1)
12
.toString(10),
13
0,
14
);
Copied!
truncateDecimals is a number helper function:
1
export function truncateDecimals(num, digits = 18) {
2
const str = num?.toString();
3
4
if (str?.includes('.')) {
5
const numbers = str.split('.');
6
const decimals = numbers[1];
7
if (digits === 0) {
8
return numbers[0];
9
} else if (decimals.length > digits) {
10
return `${numbers[0]}.${numbers[1].toString().substring(0, digits)}`;
11
}
12
13
return num;
14
}
15
16
return num;
17
}
Copied!
With the calculated values, we can now call the contract:
1
import { AddressZero } from '@ethersproject/constants'; // use null address if there is no referrer wallet address
2
const tx = await mintClubBondContract.buy(tokenAddress, buyAmount, minReward, referrerWalletAddress);
Copied!

Sell

Sell contract call is as follows:
1
mintClubBondContract.sell(
2
tokenAddress,
3
amount,
4
minRefund,
5
beneficiary,
6
);
Copied!
In order to calculate the minRefund, we utilize the getBurnRefund function from mintClubBondContract.
1
const contract = getMintClubBondContract();
2
const result = await contract.getBurnRefund(
3
tokenAddress,
4
new BigNumber(amount).times(BIG_TEN.pow(DEFAULT_TOKEN_DECIMAL)).toString(),
5
); // here amount is amount of token user is willing to sell
6
7
const outBN = new BigNumber(result[0].toString());
8
const minRefund = outBN
9
.times((100 - 2 /* slippage percentage */) / 100)
10
.toFixed(0, 1)
11
.toString(10);
Copied!
With the calculated minRefund value, we can now call the sell function on mintClubBondContract.
1
import { AddressZero } from '@ethersproject/constants'; // use null address if there is no referrer wallet address
2
const tx = await mintClubBondContract.sell(tokenAddress, sellAmount, minRefund, referrerWalletAddress);
Copied!
Last modified 1mo ago
Copy link