ethereumblockchainweb3jsmetamaskethers.js

Sending BEP20 Token with ethers.js


I'm getting error while sending BEP20 Token using ethers.js.

TypeError: invalid BigNumberish value (argument="value", value={ "hex": "0x8b4d", "type": "BigNumber" }, code=INVALID_ARGUMENT, version=6.9.2)
    at makeError (project/node_modules/ethers/lib.commonjs/utils/errors.js:122:21)
    at assert (project/node_modules/ethers/lib.commonjs/utils/errors.js:149:15)
    at assertArgument (project/node_modules/ethers/lib.commonjs/utils/errors.js:161:5)
    at getBigInt (project/node_modules/ethers/lib.commonjs/utils/maths.js:96:36)
    at set gasLimit [as gasLimit] (project/node_modules/ethers/lib.commonjs/transaction/transaction.js:330:69)
    at Transaction.from (project/node_modules/ethers/lib.commonjs/transaction/transaction.js:670:29)
    at Wallet.sendTransaction (project/node_modules/ethers/lib.commonjs/providers/abstract-signer.js:184:46)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async send (project/node_modules/ethers/lib.commonjs/contract/contract.js:228:20)
    at async Proxy.transfer (project/node_modules/ethers/lib.commonjs/contract/contract.js:261:16) {
  code: 'INVALID_ARGUMENT',
  argument: 'value',
  value: BigNumber { _hex: '0x8b4d', _isBigNumber: true },
  shortMessage: 'invalid BigNumberish value'
}

As per ethers.js documentation, I used wallet and Contract to send tokens to another account like this.

const provider = new JsonRpcProvider("https://data-seed-prebsc-1-s1.binance.org:8545");

const wallet = new ethers.Wallet(walletPrivate);
const connectedWallet = wallet.connect(provider);

const erc20_rw = new ethers.Contract(process.env.TOKEN_ADDRESS, require('./tokenAbi.json'), connectedWallet);

const tx = await erc20_rw.transfer(recieverAddress, ethers.parseUnits('10', 18));
await tx.wait();

Solution

  • Instead of using ethers.parseUnits('10', 18), You should try ethers.parseEther('10'). It will parse your amount to ethers using 18 decimals.

    Learn more on ethers Documentation

    Bonus tip

    You can directly add provide to wallet while declaration.

    const wallet = new ethers.Wallet(walletPrivate, provider);