web3jsethers.jsbinance-smart-chainbsc

Ethers.js: How to send BEP20 Token Transaction


I'm new to Ethers.js and I'm trying to send a transaction from a signer account (on MetaMask) to another account.

This code allows me to send 0.001 BNB through BSC normally:

const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
const tx = await signer.sendTransaction({
  to: "0xc705.......1f",
  value: ethers.utils.parseEther("0.001")
});

What I'm looking for is a way to send another BEP-20 token (not BNB) on BSC; I really couldn't find an example of this in any docs.


Solution

  • First, you should define the Contract object of the BEP-20 Token:

    const token = new ethers.Contract(tokenAddress, tokenABI, signer);
    

    then the transaction goes like this:

    await token.transfer(addr, amount)