如何解决为令牌错误交换确切的令牌
一段时间以来,我一直在尝试通过 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,amountIn),'transferFrom failed.');
// approve
require(USDC.approve(UNISWAP_ROUTER_ADDRESS,'approve failed.');
// swap
return uniswapRouter.swapExactTokensForTokens(amountIn,amountOutMin,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;
}
}
我也尝试复制实际合约中的功能,但随后我收到错误消息,提示必须将整个合约标记为抽象。
任何建议/指导将不胜感激!
解决方法
您正在尝试对 transferFrom()
类型(未定义)执行 address
函数。
<address>.transferFrom()
它需要位于定义 transferFrom()
函数的接口上。
interface IERC20 {
function transferFrom(address _from,address _to,uint256 _amount) external returns (bool);
}
contract UniswapExample2 {
IERC20 private USDC = IERC20(0x2F375e94FC336Cdec2Dc0cCB5277FE59CBf1cAe5);
function swapTokenForETH() public {
USDC.transferFrom();
}
}
注意:您已经(正确地)使用 IUniswapV2Router02
接口、uniswapRouter
属性和方法 swapExactTokensForTokens()
做同样的事情。 >
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。