问题描述
我之前发布了一个关于为什么我无法在脚本中增加 gas 而不导致 txn 失败的问题。但我发现它是我传入的变量。如果我在字符串“100”中传入一个金额,则 ethers.utils.parseUnits
分配的返回值是正确的,交易没有问题。但是,如果我将用户定义的值分配给 const 或 var 并尝试使用它,则任何超过 10 的值都会导致事务失败。
获取 gasLimit
的值时这无关紧要
// This code will work:
const gasPriceWei = ethers.utils.parseUnits('100','gwei');
const gas = {
gasPrice: gasPriceWei,gasLimit: gasLimit
}
// This code will NOT work:
userDefinedValue = prompt("enter the price you want to pay for gas.");
const gasPriceWei = ethers.utils.parseUnits(userDefinedValue,gasLimit: gasLimit
}
我只是想知道为什么 parseUnits
会在我传入的任何没有硬编码到脚本中的东西上窒息。变量 userDefinedValue
的值是通过 prompt-sync Nodesjs prompt 获取的。
这些值被传递给:
const tx = await router.swapExactETHForTokens(
0,//dont really care about slippage
[addresses.WBNB,token],addresses.target,Math.floor(Date.Now() / 1000) + 60 * 10,// 10 minutes from Now
{
...gas,//
value: BNB2Swap
}
);
谢谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)