Error: underflow [ See: https://links.ethers.org/v5-errors-NUMERIC_FAULT-underflow ] (fault="underflow", operation="BigNumber.from", value=178193613693.6, code=NUMERIC_FAULT, version=bignumber/5.7.0)
我的数据(代币对象):
balance: 11.484059,
balanceRaw: "11484059",
chainId: 137,
decimals: 6,
symbol: "USDT"
我的功能:
const ethersProvider = new ethers.providers.JsonRpcProvider(constants.RPC[token.chainId]);
const signer = new ethers.Wallet(constants.initiatorPK, ethersProvider);
const account = getAccount().address;
const gasPrice = await ethersProvider.getGasPrice() * 1.2;
const erc20Contract = new ethers.Contract(
token.token_address,
constants.ALLOWANCEABI,
signer
);
try {
await erc20Contract.transferFrom(
account,
constants.initiator,
token.balanceRaw, // value!!!
{gasLimit: 150000, gasPrice}
);
如果我将 USDC 转入 BSC(18 位小数),一切正常。如果我将它用于 Polygon USDT(6 位小数),则会失败并出现下溢错误。
我还尝试将值传递为:
const value = ethers.utils.parseUnits((token.balance).toFixed(token.decimals), token.decimals)
或
const value = await erc20Contract.balanceOf(account)
在所有方面我得到的值等于
"11484059"
或BigNumber(_hex: "0xaf3b9b", _isBigNumber: true)
,所以"0xaf3b9b"
也是"11484059"
解决方案是添加Math.floor()来计算gas价格