Uniswap v3自定义ERC20令牌交换



我正试图通过UniswapV3实现自定义ERC20令牌的令牌交换

我使用的是Rinkeby以太坊网络。

我将令牌部署在地址:0x4646CB39EA04d4763BED770F80F0e0dE8efcdF0f

下我为这个令牌和ETH添加了流动性到Uniswap。

现在,我尝试在我的合约中执行swap,但它不起作用。我得到错误:

Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending?
execution reverted

我的Swap.sol合约将令牌的地址作为构造函数参数与ETH交换。当我使用DAI令牌地址部署它时,交换工作正常。

我认为这是一个与Uniswap流动性相关的问题,但我手动添加了流动性,我可以在他们的应用程序中交换我的令牌。

合同代码:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
pragma abicoder v2;
import "@uniswap/v3-periphery/contracts/libraries/TransferHelper.sol";
import "@uniswap/v3-periphery/contracts/interfaces/ISwapRouter.sol";
import "@uniswap/v3-periphery/contracts/interfaces/IQuoter.sol";

contract Swap {
address private constant SWAP_ROUTER =
0xE592427A0AEce92De3Edee1F18E0157C05861564;
address private constant WETH = 0xc778417E063141139Fce010982780140Aa0cD5Ab;
address public tokenAddress;
address public immutable _owner;
ISwapRouter public immutable swapRouter;
constructor(address token) {
_owner = msg.sender;
swapRouter = ISwapRouter(SWAP_ROUTER);
tokenAddress = token;
}
function swapExactInputSingle() external payable {
require(msg.value > 0, "Must pass non 0 ETH amount");
ISwapRouter.ExactInputSingleParams memory params = ISwapRouter
.ExactInputSingleParams({
tokenIn: WETH,
tokenOut: tokenAddress,
fee: 3000,
recipient: msg.sender,
deadline: block.timestamp,
amountIn: msg.value,
amountOutMinimum: 1,
sqrtPriceLimitX96: 0
});
swapRouter.exactInputSingle{value: msg.value}(params);
}
receive() external payable {}
}

我在uniswap上的swapExactInputMultihop函数也有同样的问题。对于你要经过的每个池/路径,你需要确保你设置了正确的池费。

您可以在uniswap网站上查看交换费用:V3-overview/fees

或视频教程,通过整个过程:智慧区块链在YouTube上

已成功修复

我在合同中设置了fee: 3000,但我以1%的费用创建了流动性,所以我不得不根据docs将其更改为fee: 10000:fee The fee tier of the pool, used to determine the correct pool contract in which to execute the swap

最新更新