0

I'm just trying to create a simple smart contract which can swap eth for tokens, and then swap those tokens for ETH back.

This is my code:

// SPDX-License-Identifier: agpl-3.0
pragma solidity ^0.8.7;

interface IUniswap {
    function swapExactTokensForETH(
        uint amountIn, 
        uint amountOutMin, 
        address[] calldata path, 
        address to, 
        uint deadline)
    external
    returns (uint[] memory amounts);
    
    function swapExactETHForTokens(
            uint amountOutMin, 
            address[] calldata path, 
            address to, 
            uint deadline)
        external
        payable
    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 EzSwap {
    IUniswap uniswap;
    
    constructor(address _uniswap) {
        uniswap = IUniswap(_uniswap);
    }
    
    function swapExactETHForTokens(
        address token,
        uint amountOutMin, 
        uint deadline) external payable {
            
            address[] memory path = new address[](2);
            path[0] = uniswap.WETH();
            path[1] = token;
            
            uniswap.swapExactETHForTokens{value: msg.value}(
                amountOutMin,
                path,
                msg.sender,
                deadline);
    }
        
    // uniswap address 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D
    function swapTokensForETH(
        address token, 
        uint amountIn, 
        uint amountOutMin, 
        uint deadline) external {
            IERC20(token).transferFrom(msg.sender, address(this), amountIn);
            address[] memory path = new address[](2);
            path[0] = token;
            path[1] = uniswap.WETH();
            IERC20(token).approve(address(uniswap), amountIn);
            uniswap.swapExactTokensForETH(amountIn, amountOutMin, path, msg.sender, deadline);
    }
}
 

When I try to execute swapExactETHForTokens I get an error:

false Transaction mined but execution failed

Edit:

I'm using this address for the UniswapV2(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D)

token = '0x5592EC0cfb4dbc12D3aB100b257153436a1f0FEa' //DAI Address
amountOutMin = 1   //Arbirary value that should always pass
Deadline = Math.floor(Date.now() / 1000) + 60 * 10 // calculating deadline from current timestamp
Value = 90 // In ETH
1

I don't see anything, it should work, maybe router address is incorrect IUniswapV2Router02(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D) or maybe transaction value is not enough to buy amountOutMin or that token does not have Pool or Liquidity Provider

Make sure you're using the right RPC. Change the Environment to InjectedWeb3Provider and Set Your Infura RPC in the window

2
  • i'm using that address, I set the value to 99 eth and amountOut for Dai as 1. I just want any transaction to pass so I know it works :/
    – johnny 5
    Nov 1 at 13:52
  • calculate amountOutMin by uint256[] memory amounts = _router.getAmountsOut(value, path); and send ETH as WEI 90ETH = 90000000000000000000. amountOutMin = amounts[1]; Nov 2 at 11:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.