Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I need to get the txn params from nodejs app to contract and then swap the token for ETH,

So the solidity function is getting params (tokenAddress, amoountIn, amountOutMin, deadline) then it's calling "swapExactTokensForETH" to this contract on rinkeby "0xdEcefC912c686b9c03597D706Cb17fc23506545c", Deadline is 30 minutes but it's failing in 1-2 seconds

Token should be swaped from contract's balance (Not from the sender's balance)

Please proivde a correct solidity function

Thanks in advance enter image description here

There is 14 UNI on contract balance and it should swap to ETH

Solidity:


pragma solidity ^0.7.0;


interface IUniswap {
    function swapExactTokensForETH( uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline ) external  returns (uint[] memory amounts); 
    function WETH() external pure returns(address);
 } 
 
interface IERC20 {
   function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
   function approve(address spender, uint256 amount) external returns (bool);
}
 

contract Swap {
    
    IUniswap uniswap;    
    constructor(address _uniswap){
        uniswap = IUniswap(_uniswap); 
    } 
    
    function swapTokensForETH( address token, uint amountIn, uint amountOutMin, uint deadline)external{  
        address[] memory path = new address[](2);
        path[0] = token;
        path[1] = uniswap.WETH();
        IERC20(token).approve(address(this), amountIn);
        uniswap.swapExactTokensForETH( amountIn, amountOutMin, path, address(this), deadline ); 
    }
}

Sending transaction from remix.ethereum.org at contract address "0x1b4c0a8AC1738391e145CAC66492f1716D93b93C"

Sending transaction (10 UNI for ETH, min out is 0): "0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984",10000000000000000000,0,1612465457

question from:https://stackoverflow.com/questions/66056774/solidity-uniswap-error-encountered-during-contract-execution-execution-revert

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
455 views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...