gpt4 book ai didi

ethereum - 与 Uni V2 交互时如何修复 'TransferHelper: ETH_TRANSFER_FAILED'

转载 作者:行者123 更新时间:2023-12-05 01:05:42 29 4
gpt4 key购买 nike

我正在处理 Uniswap V2 路由器合约中的 safeTransferETH 辅助函数的一个奇怪问题。

我正在尝试使用 Uniswap V2 路由器提供的 swapExactTokensForETH 函数将合约持有的代币交换为 Uniswap for Ether。 (功能代码在 Uniswap's github in router1 上)。被调用的函数是:

function swapExactTokensForETH(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)
external
override
ensure(deadline)
returns (uint[] memory amounts)
{
require(path[path.length - 1] == WETH, 'UniswapV2Router: INVALID_PATH');
amounts = UniswapV2Library.getAmountsOut(factory, amountIn, path);
require(amounts[amounts.length - 1] >= amountOutMin, 'UniswapV2Router: INSUFFICIENT_OUTPUT_AMOUNT');
TransferHelper.safeTransferFrom(path[0], msg.sender, UniswapV2Library.pairFor(factory, path[0], path[1]), amounts[0]);
_swap(amounts, path, address(this));
IWETH(WETH).withdraw(amounts[amounts.length - 1]);
TransferHelper.safeTransferETH(to, amounts[amounts.length - 1]);
}

此代码中唯一引发错误的部分是 TransferHelper.safeTransferETH 函数,即:

function safeTransferETH(address to, uint value) internal {
(bool success,) = to.call{value:value}(new bytes(0));
require(success, 'TransferHelper: ETH_TRANSFER_FAILED');
}

我的代码是:

function uniV2ReceiveETH(address _token0, uint _amount0) public payable returns (uint[] memory amountsReceived) {
require(_amount0 > 0, "Must provide tokens if we want tokens in return!");
address[] memory path = new address[](2);
path[0] = _token0;
path[1] = WETH;

IERC20 token;
token = IERC20(_token0);

if (token.balanceOf(address(this)) > 0) {
_amount0 = token.balanceOf(address(this));
}

require(token.approve(address(uniV2Router), _amount0 + 10000), "approval failed");

// Swap logic
uint amountOutMin = UniswapV2Library.getAmountsOut(address(uniV2Factory), _amount0, path)[1];
amountsReceived = uniV2Router.swapExactTokensForETH(_amount0, amountOutMin, path, address(this), deadline);
uint endBalance = address(this).balance;

// Let everyone know we're done!
emit Swap(msg.sender, _amount0, endBalance);
}

其他一些注意事项是:

  1. 合约确实从其他地址收到了 ETH,没有问题。
  2. 我正在使用安全帽和主网的 fork 版本进行测试。
  3. 该合约还适用于 Uniswap 路由器的其他交换功能,包括 SwapExactETHForTokensSwapExactTokensForTokens

最佳答案

我通过将 payable fallback 函数定义为:

解决了这个问题:
fallback() external payable { }

在我的智能合约中。

关于ethereum - 与 Uni V2 交互时如何修复 'TransferHelper: ETH_TRANSFER_FAILED',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70441441/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com