我在使用 ethers.js 发送 BEP20 令牌时遇到错误。
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'
}
根据 ethers.js 文档,我使用钱包和合约将代币发送到另一个帐户,如下所示。
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();
错误信息显示转账金额:
erc20_rw.transfer(recieverAddress, ethers.parseUnits('10', 18))
您可以使用 javascript BigInt 算术,而不是依赖第 3 方工具
const amount = 10n * 10n ** 18n
10n
:这是代表数字 10 的 BigInt 文字。
10n ** 18n
:这是 10 的 18 次方,其中 ** 运算符在 JavaScript 中表示求幂。结果是一个 BigInt,代表一个非常大的数字,特别是 10 后跟 18 个零。
然后将金额传递给函数
erc20_rw.transfer(recieverAddress,amount)