Transferring a token
This is an example of sending 1 token to a recipient.
import React from 'react';
import useWeb3Provider from 'hooks/useWeb3Provider';
import { getMintTokenContract } from 'utils/contractHelpers';
import { useWeb3React } from '@web3-react/core';
import BigNumber from 'bignumber.js';
const BIG_TEN = new BigNumber(10);
const DEFAULT_TOKEN_DECIMAL = 18;
function TransferComponent() {
const address = '[SOME MINT TOKEN ADDRESS]';
const provider = useWeb3Provider();
const { account } = useWeb3React();
const recipient = '[RECIPIENT ADDRESS]';
return (
<button
onClick={async () => {
try {
const tokenContract = getMintTokenContract(address, provider.getSigner(account));
const bn = new BigNumber(1).times(BIG_TEN.pow(DEFAULT_TOKEN_DECIMAL)).toString();
const tx = await tokenContract.transfer(recipient, bn);
await tx.wait();
} catch (e) {
console.error(e);
}
}}
>
Transfer 1 Token
</button>
);
}