[英]Swap Exact Tokens for Tokens Errors
一段時間以來,我一直在嘗試通過 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.”
on.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;
}
}
我也嘗試在實際合同中復制這些功能,但隨后我收到一個錯誤,提示必須將整個合同標記為抽象。
任何建議/指導將不勝感激!
您正在嘗試對address
類型(未定義)執行transferFrom()
function。
<address>.transferFrom()
它需要位於定義transferFrom()
function 的接口上。
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()
) 做同樣的事情。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.