Mint Club V1 Docs
  • Introduction
  • Fair Launch Tokens
  • To-do list
  • Contracts
  • FAQ
  • Announcement
  • Get Started
    • User Guide
      • Create Smart Token
      • Swap Smart Tokens
      • Buy MINT
      • User Guide (Russian, not official)
    • Trading Fees / Referral
    • Widget Link
    • Add MINT to your wallet
  • Developers
    • Code Snippets for Node scripts
    • Connect Wallet (React.js)
    • Mint.club Javascript SDK
      • Contract Interaction (React.js)
        • Minting a new token
        • Transferring a token
        • Buy & Sell
  • Tokenomics
    • Smart Token Bonding Curve
    • MINT and Smart Tokens
    • CREATOR Token
  • Others
    • Terms
    • Protocol Disclaimer
  • Resources
    • Mint.club (website)
    • Buy MINT (PancakeSwap)
    • Buy MINT (StealthEX)
    • Github
    • Audit Report
    • Twitter
    • Telegram
  • Old Docs
    • To-do list (deprecated)
    • Pre-sign Up (finished)
      • Swap HUNT to MINT Token
      • MINT Community Airdrop
    • Swap HUNT to MINT Terms (finished)
    • MINT Airdrop Terms (finished)
Powered by GitBook
On this page
  • Constants
  • Buy
  • Sell
  1. Developers
  2. Mint.club Javascript SDK
  3. Contract Interaction (React.js)

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:

export function truncateDecimals(num, digits = 18) {
  const str = num?.toString();

  if (str?.includes('.')) {
    const numbers = str.split('.');
    const decimals = numbers[1];
    if (digits === 0) {
      return numbers[0];
    } else if (decimals.length > digits) {
      return `${numbers[0]}.${numbers[1].toString().substring(0, digits)}`;
    }

    return num;
  }

  return num;
}

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

import { AddressZero } from '@ethersproject/constants'; // use null address if there is no referrer wallet address
const tx = await mintClubBondContract.buy(tokenAddress, buyAmount, minReward, referrerWalletAddress);

Sell

Sell contract call is as follows:

mintClubBondContract.sell(
    tokenAddress,
    amount,
    minRefund, 
    beneficiary,
);

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

const contract = getMintClubBondContract();
const result = await contract.getBurnRefund(
  tokenAddress,
  new BigNumber(amount).times(BIG_TEN.pow(DEFAULT_TOKEN_DECIMAL)).toString(),
); // here amount is amount of token user is willing to sell

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

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

import { AddressZero } from '@ethersproject/constants'; // use null address if there is no referrer wallet address
const tx = await mintClubBondContract.sell(tokenAddress, sellAmount, minRefund, referrerWalletAddress);
PreviousTransferring a tokenNextSmart Token Bonding Curve