【发布时间】:2021-06-06 02:10:19
【问题描述】:
一段时间以来,我一直在尝试通过 uniswap 进行 Swap ExactTokenforToken 交换。不断提出问题。
我确实有一个成功部署的合约,但它不断收到传输错误。通过查看文档、帖子、此论坛等,我的理解是我没有转移/批准事件。
这是已部署但不能交换的代码,供参考:
pragma solidity 0.7.1;
import "https://github.com/Uniswap/uniswap-v2-periphery/blob/master/contracts/interfaces/IUniswapV2Router02.sol";
contract UniswapExample {
address internal constant UNISWAP_ROUTER_ADDRESS = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D ;
IUniswapV2Router02 public uniswapRouter;
address private multiDaiKovan = 0x4F96Fe3b7A6Cf9725f59d353F723c1bDb64CA6Aa;
address private USDC = 0x2F375e94FC336Cdec2Dc0cCB5277FE59CBf1cAe5;
constructor() {
uniswapRouter = IUniswapV2Router02(UNISWAP_ROUTER_ADDRESS);
}
function convertUSDCtoEthtoDai(uint daiAmount, uint USDCAmount) public payable {
uint deadline = block.timestamp + 15; // using 'now' for convenience, for mainnet pass deadline from frontend!
uniswapRouter.swapExactTokensForTokens(USDCAmount, daiAmount, getPathForETHtoDAI(), address(this), deadline);
// refund leftover ETH to user
(bool success,) = msg.sender.call{ value: address(this).balance }("");
require(success, "refund failed");
}
function getEstimatedETHforDAI(uint daiAmount) public view returns (uint[] memory) {
return uniswapRouter.getAmountsIn(daiAmount, getPathForETHtoDAI());
}
function getPathForETHtoDAI() private view returns (address[] memory) {
address[] memory path = new address[](3);
path[0] = USDC;
path[1] = uniswapRouter.WETH();
path[2] = multiDaiKovan;
return path;
}
// important to receive ETH
receive() payable external {}
}
所以我按照自己的方式编写了以下代码,但是,我在“.transferFrom”上收到一个错误,提示“TypeError: Member “transferFrom” not found or not visible after argument-dependent lookup in address。”
.approve 也是如此。我已经为 Router02 和 IERC20 导入了 IERC20 接口。所以我无法部署合同来测试我是否接近。
这是新代码:
import "https://github.com/Uniswap/uniswap-v2-periphery/blob/master/contracts/interfaces/IUniswapV2Router02.sol";
import "https://github.com/Uniswap/uniswap-v2-periphery/blob/master/contracts/interfaces/IERC20.sol";
contract UniswapExample2 {
address internal constant UNISWAP_ROUTER_ADDRESS = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D ;
IUniswapV2Router02 public uniswapRouter;
address private multiDaiKovan = 0x4F96Fe3b7A6Cf9725f59d353F723c1bDb64CA6Aa;
address private USDC = 0x2F375e94FC336Cdec2Dc0cCB5277FE59CBf1cAe5;
constructor() {
uniswapRouter = IUniswapV2Router02(UNISWAP_ROUTER_ADDRESS);
}
function swapTokenForETH(uint amountIn, uint amountOutMin, uint deadline) public payable returns(uint[] memory) {
// transfer
require(USDC.transferFrom(msg.sender, address(this), amountIn), 'transferFrom failed.');
// approve
require(USDC.approve(UNISWAP_ROUTER_ADDRESS, amountIn), 'approve failed.');
// swap
return uniswapRouter.swapExactTokensForTokens(amountIn, amountOutMin, getPathForETHtoDAI(), msg.sender, deadline);
}
function getPathForETHtoDAI() private view returns (address[] memory) {
address[] memory path = new address[](3);
path[0] = USDC;
path[1] = uniswapRouter.WETH();
path[2] = multiDaiKovan;
return path;
}
}
我也尝试在实际合约中复制函数,但随后我收到一条错误消息,提示必须将整个合约标记为抽象。
任何建议/指导将不胜感激!
【问题讨论】: