我在使用UniswapV2Router.sol时遇到了问题。我使用UniswapV2Router接口编写了一个契约,并创建了一个使用swapExactTokenForETH的函数。
这是密码。
function swapForETH(
uint amountInMax,
uint amountOut,
address[] calldata path,
address to
)external{
IUniswapV2Router02(ROUTER).swapExactTokensForETH(
amountInMax,
amountOut,
path,
to,
block.timestamp
);
}现在我写了一个测试,首先我批准了路由器,然后我尝试在ETH中交换一些沙子标记。
以下是代码:
it("should swap exact tokens for ETH", async()=>{
let ownerSandBalance = await SAND_TOKEN.balanceOf(owner.address);
console.log("Owner Sand Balance: ", (ownerSandBalance).toString());
let tx = await SAND_TOKEN.connect(owner).approve(ROUTER, ownerSandBalance);
await tx.wait();
let approvance = await SAND_TOKEN.allowance(owner.address, ROUTER);
console.log("Approval from owner to contract: ", (approvance).toString());
let result = await contract.connect(owner).swapForETH(
ownerSandBalance,
"100000000000000000",
[SAND, WETH],
owner.address,
);
console.log("", (result[0] / decimals).toString());
console.log("", (result[1] / decimals).toString());
console.log(result);
});现在我不明白为什么会出现"TransferHelper: TRANSFER_FROM_FAILED“这个错误。ownerSandBalance是18576609770834064884363。有人能解释一下我为什么会得到它吗?
发布于 2022-11-05 11:40:04
"100000000000000000“似乎不算什么问题。你试过web3.utils.toWei("0,1")了吗?
https://stackoverflow.com/questions/74319515
复制相似问题