How to Send Custom Token With Ethers.js ( Binance Smart Chain )

Johnson awah Alfred's photo
Johnson awah Alfred
ยทFeb 9, 2022ยท

2 min read

Subscribe to my newsletter and never miss my upcoming articles

Below is a snippet that you can use to transfer some tokens using ethers.js v 4.0.0. For this demonstration, we are connecting to the Binance Smart Chain ( BSC) test net via JSON RPC URL. So if you have deployed a contract to BSC, this is a clean code to transfer value via smart contract calls.


const ethers = require('ethers'); // import ethers.js

let privateKey = ""; // set privateKey

const bsc_jsonRPC_testnet = "https://data-seed-prebsc-1-s1.binance.org:8545/" // json RPC url
const provider = new ethers.providers.JsonRpcProvider(bsc_jsonRPC_testnet) // provider for signing transaction
let wallet = new ethers.Wallet(privateKey,provider); // initiating a wallet using private key and provider

const CONTRACT_ABI = []; // set ABI

// The address from the above deployment example
const CONTRACT_ADDRESS = '0x6AbB39Cb07E152096B489d010455fe3356fA6c18';

let contract = new ethers.Contract(CONTRACT_ADDRESS, CONTRACT_ABI, provider); // initiate contract

let contractWithSigner = contract.connect(wallet); // connect our wallet and provider to be able to update values on the contract.

var numberOfDecimals = 8; // for proper parsing
const receiver = "" // set receiver
const value = ethers.utils.parseEther('0.00000001',numberOfDecimals) // set correct value to transfer

contractWithSigner.transfer(receiver,value).then((txn) => {
    console.log(txn);

//     {
//   nonce: 11,
//   gasPrice: BigNumber { _hex: '0x02540be400', _isBigNumber: true },
//   gasLimit: BigNumber { _hex: '0xd32e', _isBigNumber: true },
//   to: '0x6AbB39Cb07E152096B489d010455fe3356fA6c18',
//   value: BigNumber { _hex: '0x00', _isBigNumber: true },
//   data: '0xa9059cbb000000000000000000000000121baceccc1300cd4d772fdbe14acf7a8f831f1800000000000000000000000000000000000000000000000000000002540be400',      
//   chainId: 97,
//   v: 230,
//   r: '0xb9bf7ac229944a37489f80b5e9b332d0e0d7f4ba42749ce8ee7db35ee27938db',
//   s: '0x550ece4d6b8efcb952e1083ec849e527d049cbc9e794e47581e3155305e06e67',
//   from: '0x687d0848507F90dCE542d13D5A824575Df5aBBe0',
//   hash: '0x651feab31ce16c677032fd8a3305135230a8851cc21e9647adef87471f0b7514',
//   type: null,
//   confirmations: 0,
//   wait: [Function (anonymous)]
// }
}).catch((err) => {
    console.log(err);
})
ย 
Share this