Contract SwapAtUniswap on Remix Failing with gas costs

Hello Super Shadowy Devs

Hi, I am trying to push a Transaction for my newly created contract SwapAtUniswap (deployed on Kovan) but it fails due to the following reasons :point_down:

**Please also have a look at my code :point_down: **

//SPDX-License-Identifier: MIT
pragma solidity ^0.6.6;
import '@uniswap/v2-periphery/contracts/UniswapV2Router02.sol';
import "@chainlink/contracts/src/v0.6/interfaces/AggregatorV3Interface.sol";
//import "@openzeppelin/contracts/access/Ownable.sol";

contract SwapAtUniswap {
    
  
   UniswapV2Router02 internal router;
   AggregatorV3Interface internal ethUsdPriceFeed;
   AggregatorV3Interface internal linkUsdPriceFeed;
   address [] public wethLinkKovanPath;// = ["0xd0A1E359811322d97991E03f863a0C30C2cF029C", "0xa36085F69e2889c224210F603D836748e7dC0088"];
   uint256 constant denom = 1000;
   uint256 deadline;
   
   
    
   constructor(address payable _router, address _ethUsdPriceFeed, address _linkUsdPriceFeed) public {
       router = UniswapV2Router02 (_router);
       ethUsdPriceFeed = AggregatorV3Interface(_ethUsdPriceFeed);
       linkUsdPriceFeed = AggregatorV3Interface(_linkUsdPriceFeed);
    }
    
    function swapETHForLink(uint _amountOut, address[] memory _wethLinkKovanPath, address to, uint _deadline) public  {
        uint256 ethUsdPrice = getLatestEthUsdPrice();// will return 8 decimals
        uint256 linkUsdPrice = getLatestLinkUsdPrice(); // will return 8 decimals 
        deadline = _deadline;
        wethLinkKovanPath = _wethLinkKovanPath;
        uint256 i;
        for (i = 0; i < 1; i ++) {
            if ( (linkUsdPrice/ethUsdPrice) <= 9/denom ) {
                router.swapETHForExactTokens(_amountOut, wethLinkKovanPath, to , deadline);
           
            }
        }
    }
    
    function getLatestEthUsdPrice() public view returns(uint256) {
    (
      uint80 roundID,
      int price,
      uint startedAt,
      uint timeStamp,
      uint80 answeredInRound
    ) = ethUsdPriceFeed.latestRoundData();
    return uint256(price);
  }
    
    function getLatestLinkUsdPrice() public view returns(uint256) {
    (
      uint80 roundID,
      int price,
      uint startedAt,
      uint timeStamp,
      uint80 answeredInRound
    ) = linkUsdPriceFeed.latestRoundData();
    return uint256(price);
  }
       
      
}

:computer: Environment - Remix

**
Please help check my Code too (as I am new to solidity) if i am going wrong anywhere because I am just creating a simple Transaction of swapping ETH for Link whenever the price of Link < 0.009 ETH....... But as you see "Gas estimation failed Warning" is showing on Remix and even if i force send the Transaction I get a Revert Error by UniswapV2Router02 of Gas Estimation costs ??
So how to solve this ??

Meanwhile, also see my Kovan.etherscan.io snapshot regarding my Contract Creation, it says that Gas Limit:

530,913

Gas Used by Transaction:

530,913 (100%)

:thinking:

Maybe because the contract creation has used all gas?, and hence now i cannot send any transactions forward or if not, then what does this really mean ? I am unable to solve this , so kindly :pray: help

I am also getting a compilation warning as follows: :point_down: Unable to understand what it means: :thinking:

Thanks & regards

Suveett Kalra
**

You have 2 problems:

  1. Your swapETHForLink() function in the contract is missing the payable keyword so it cannot receive ETH.

  2. You forgot to send ETH when you broadcasted your transaction. If you look at your transaction receipt screenshot, it says value = 0 ether.


Separately, do take note that when you're testing on testnets, you want to make sure that the token address of Link that is used by Chainlink is the same as the one on uniswap.

Hi @STYJ

Your swapETHForLink() function in the contract is missing the payable keyword so it cannot receive ETH. You forgot to send ETH when you broadcasted your transaction. If you look at your transaction receipt screenshot, it says value = 0 ether.

First of all Thanks a ton correcting me and showing me the Path. I corrected it, but its still not working with the following REVERT ERROR

Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending?
execution reverted } }

Also, I have double checked and entered the following Addresses on Rinkeby:

["0xc778417E063141139Fce010982780140Aa0cD5Ab", "0x01BE23585060835E02B77ef475b0Cc51aA1e0709"]
[WETH, LINK]

So what could possibly be going wrong ? ? Its the same Spurious Dragon Gas issue as in my last question (Refer to the screenshot) :point_up_2: :point_up_2:or something else ??
How to correct it and get this contract's transactions going ??
See also below screenshots :pray: :point_down:



As you can see i was trying to swap 0.1 ETH to 11 Link (The Input amount is also more than the Min OutputAmount , I really don't understand what i am doing wrong :thinking: )

swapETHForLink
amountOutMin:
11
_wethLinkRinkebyPath:
["0xc778417E063141139Fce010982780140Aa0cD5Ab", "0x01BE23585060835E02B77ef475b0Cc51aA1e0709"]
to:
0xCc00FDd001B9644E65BaE1760DF82465607F73FB
_deadline:
1000000000000000000

The code below: :point_down: (again for your referrence)

//SPDX-License-Identifier: MIT
pragma solidity ^0.6.6;
import '@uniswap/v2-periphery/contracts/UniswapV2Router02.sol';
import "@chainlink/contracts/src/v0.6/interfaces/AggregatorV3Interface.sol";
//import "@openzeppelin/contracts/access/Ownable.sol";

contract SwapAtUniswap {
    
  
   UniswapV2Router02 internal router;
   AggregatorV3Interface internal ethUsdPriceFeed;
   AggregatorV3Interface internal linkUsdPriceFeed;
   address [] public wethLinkRinkebyPath;// = ["0xc778417E063141139Fce010982780140Aa0cD5Ab", "0x01BE23585060835E02B77ef475b0Cc51aA1e0709"];
   uint256 constant denom = 1000;
   uint256 deadline;
   
   
    
   constructor(address payable _router, address _ethUsdPriceFeed, address _linkUsdPriceFeed) public {
       router = UniswapV2Router02 (_router);
       ethUsdPriceFeed = AggregatorV3Interface(_ethUsdPriceFeed);//0x8A753747A1Fa494EC906cE90E9f37563A8AF630e
       linkUsdPriceFeed = AggregatorV3Interface(_linkUsdPriceFeed);//0xd8bD0a1cB028a31AA859A21A3758685a95dE4623
    }
    
    function swapETHForLink(uint amountOutMin, address[] memory _wethLinkRinkebyPath, address to, uint _deadline) public payable {
        uint256 ethUsdPrice = getLatestEthUsdPrice();// will return 8 decimals
        uint256 linkUsdPrice = getLatestLinkUsdPrice(); // will return 8 decimals 
        deadline = _deadline;
        wethLinkRinkebyPath = _wethLinkRinkebyPath;
        uint256 i;
        for (i = 0; i < 1; i ++) {
            if ( (linkUsdPrice/ethUsdPrice) <= 9/denom ) {
                router.swapExactETHForTokens(amountOutMin, wethLinkRinkebyPath, to , deadline);
           
            }
        }
    }
    
    function getLatestEthUsdPrice() public view returns(uint256) {
    (
      uint80 roundID,
      int price,
      uint startedAt,
      uint timeStamp,
      uint80 answeredInRound
    ) = ethUsdPriceFeed.latestRoundData();
    return uint256(price);
  }
    
    function getLatestLinkUsdPrice() public view returns(uint256) {
    (
      uint80 roundID,
      int price,
      uint startedAt,
      uint timeStamp,
      uint80 answeredInRound
    ) = linkUsdPriceFeed.latestRoundData();
    return uint256(price);
  }
       
      
}

Thanks and Awaiting your Kind Revert

Suveett Kalra


when you want to do the swap, you need to enter 0.1 ETH on top before trying to broadcast your transaction