0

I have a smart contract that I've deployed to the RinkyBy Network Via Remix:

// 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);
    }
}
 

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

I've setup remix to use an inject web3 provider which is attached to an infura url. I've verified that I can swap ETH for Tokens, However I'm having trouble swapping tokens back for ETH.

token = '0x5592EC0cfb4dbc12D3aB100b257153436a1f0FEa' //DAI Address
amountIn = 1000000000000000 // 1 Finney
amountOutMin = 1   //Arbirary value that should always pass 1 x 10^-19
Deadline = Math.floor(Date.now() / 1000) + 60 * 10 // calculating deadline from current timestamp

Remix is claiming it's having trouble determining the Gas Fees. So I've jacked up the gas by to the point where it should always pass but the transaction fails to mine.

Edit: I've also tried to do this using link, I manually approve the spending of link for that on the rinkby ethercan scan, but I'm not sure if that will work because I'm not sure what block the infura RPC is point to?

2
  • Hi Jhonny, the amounts you swapped for ETH -> Tokens and the amount you try to swap Tokens -> ETH will not be the same. So you should try to swap for a less ETH value as it may cause the supply to rebalance affecting the price of your token. That's how Uniswap works under the hood Nov 5 at 16:07
  • @SujithSomraaj I understand that that why I put the minOutAmount = 1 meaning I just want the transaction to occur at the best price possible
    – johnny 5
    Nov 5 at 16:17

Your Answer

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

Browse other questions tagged or ask your own question.