Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
457 views
in Technique[技术] by (71.8m points)

node.js - Solidity Uniswap - Error encountered during contract execution [execution reverted

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

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

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...