我正在尝试通过在测试网上使用 pancakeswap 路由器 v2 将我的 busd 令牌换成 bnb(wbnb)。虽然我有 CALL_EXCEPTION。
在我深入研究代码之前,我在bscscan上遇到了同样的问题。我不知道这是否相关,但即使我成功批准了合同,它仍然返回 0 作为津贴。这是我尝试的方法,我从代码中获取了值。无论输入如何,总支付金额(金额 + 汽油费)都不会改变。
这是我在测试网中的资产。
amountIn 值填充了 busdContract.balanceOf 方法的返回值
const abi = await retrieveAbi(tokenIn);
erc = new ethers.Contract(
tokenIn,
abi,
account
);
const balance = await erc.balanceOf(process.env.WALLET_ADDRESS);
amountOutMin 由 pancakeswapRouter.getAmountsOut 方法计算
let amountOutMin = 0;
if (parseInt(slippage) !== 0) {
const amounts = await router.getAmountsOut(amountIn, [tokenIn, tokenOut]);
amountOutMin = amounts[1].sub(amounts[1].mul(slippage).div(100));
}
return amountOutMin;
批准令牌:
const tx = await erc.approve(
tokenIn,
tokenAmount,
{
gasPrice: gasPrice,
gasLimit: gasLimit
}
);
const receipt = await tx.wait();
Sell 函数(它实际上会引发 CALL_EXCEPTION 错误):
const tx = await router.swapExactTokensForETH(
amountIn,
amountOutMin,
[tokenIn, tokenOut],
process.env.WALLET_ADDRESS,
Date.now() + 1000 * 60 * 5, // 5 minutes
{
gasPrice: gasPrice,
gasLimit: gasLimit
}
);
const receipt = await tx.wait();
我还尝试提供自定义金额,并使用检索到的 abi 的(busd abi)小数值进行计算,但没有运气;它给出了同样的错误:CALL_EXCEPTION。
const amountIn = ethers.utils.parseUnits('100', decimals);
错误消息和失败的交易都没有给我任何线索。它只说代码= CALL_EXCEPTION。这让我无处可去。
我怀疑问题出在批准和津贴上。即使批准交易成功,我的钱包的busd津贴总是为零。提前谢谢了。
这是变量,在图像中看到:
211064843184329663888 amountIn
468509127086739224 amountOutMin
0x8301f2213c0eed49a7e28ae4c3e91722919b8b47 tokenIn (BUSD)
0xae13d989dac2f0debff460ac112a837c89baa7cd tokenOut (WBNB)