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:
Screenshot 2021-08-14 at 5.38.48 PM

**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:
Screenshot 2021-08-14 at 6.21.51 PM

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:

Screenshot 2021-08-14 at 7.06.59 PM

Thanks & regards

Suveett Kalra (Su.Kal Crypto)

1 Like

hey @Su.kal.Crypto, hope you are well.

The first image, it said the warning because the transaction is likely to fail because a revert being triggered (from a require).

image

Is not that no worries, your contract deployed succesfully, the error comes probably because the market conditions are not optimal to do a profit on the flash loan, therefore, it will revert the operation.

If you trigger the function, although warned by the remix warning message, check the transcaction output on etherscan, probably the reason will be “did not profit”. Which is a require in your contract function in case the amount you should earn is less than the borrow amount to be payed.

Carlos Z

Hi Carlos,
I think the problem lies somewhere else.
Please see screenshot below :point_down:
Screenshot 2021-08-18 at 8.29.19 AM

Compiler Metadata is modifying /artifacts/SwapAtUniswap.json

Please see below SwapAtUniswap_metadata.json

{
	"compiler": {
		"version": "0.6.6+commit.6c089d02"
	},
	"language": "Solidity",
	"output": {
		"abi": [
			{
				"inputs": [
					{
						"internalType": "address payable",
						"name": "_router",
						"type": "address"
					},
					{
						"internalType": "address",
						"name": "_ethUsdPriceFeed",
						"type": "address"
					},
					{
						"internalType": "address",
						"name": "_linkUsdPriceFeed",
						"type": "address"
					}
				],
				"stateMutability": "nonpayable",
				"type": "constructor"
			},
			{
				"inputs": [],
				"name": "getLatestEthUsdPrice",
				"outputs": [
					{
						"internalType": "uint256",
						"name": "",
						"type": "uint256"
					}
				],
				"stateMutability": "view",
				"type": "function"
			},
			{
				"inputs": [],
				"name": "getLatestLinkUsdPrice",
				"outputs": [
					{
						"internalType": "uint256",
						"name": "",
						"type": "uint256"
					}
				],
				"stateMutability": "view",
				"type": "function"
			},
			{
				"inputs": [
					{
						"internalType": "uint256",
						"name": "amountOutMin",
						"type": "uint256"
					},
					{
						"internalType": "address[]",
						"name": "_wethLinkRinkebyPath",
						"type": "address[]"
					},
					{
						"internalType": "address",
						"name": "to",
						"type": "address"
					},
					{
						"internalType": "uint256",
						"name": "_deadline",
						"type": "uint256"
					}
				],
				"name": "swapETHForLink",
				"outputs": [],
				"stateMutability": "payable",
				"type": "function"
			},
			{
				"inputs": [
					{
						"internalType": "uint256",
						"name": "",
						"type": "uint256"
					}
				],
				"name": "wethLinkRinkebyPath",
				"outputs": [
					{
						"internalType": "address",
						"name": "",
						"type": "address"
					}
				],
				"stateMutability": "view",
				"type": "function"
			}
		],
		"devdoc": {
			"methods": {}
		},
		"userdoc": {
			"methods": {}
		}
	},
	"settings": {
		"compilationTarget": {
			"UniswapSwap.sol": "SwapAtUniswap"
		},
		"evmVersion": "istanbul",
		"libraries": {},
		"metadata": {
			"bytecodeHash": "ipfs"
		},
		"optimizer": {
			"enabled": false,
			"runs": 200
		},
		"remappings": []
	},
	"sources": {
		"@chainlink/contracts/src/v0.6/interfaces/AggregatorV3Interface.sol": {
			"keccak256": "0x1862840d741dedb36e774534b877a13b5187555e3b78b8d2815f898b0dc02268",
			"urls": [
				"bzz-raw://64a15f4349aea6e60703f581a6280b71d6adb35ee74d2f3c4f130a2adc3efee3",
				"dweb:/ipfs/QmdVoSQvGfJNPnjQsAs7ZN3ueWghzTa72jSqzhGiQNDpkL"
			]
		},
		"@uniswap/lib/contracts/libraries/TransferHelper.sol": {
			"keccak256": "0xaff99b54915f57e2a4e71b8d15afd4ee425410e5a6f45c07517ae1beb806f8bd",
			"urls": [
				"bzz-raw://15432ec031f4cfa6709921289e198b030a0c5eac75b5a3e0eb64827a2b3b5595",
				"dweb:/ipfs/QmZvR1KuuDvuzXFiADCngTKyMCTy31uhq9U6CazP59B55j"
			]
		},
		"@uniswap/v2-core/contracts/interfaces/IUniswapV2Factory.sol": {
			"keccak256": "0xe5905c0989cf5a865ed9bb7b9252536ca011c5b744017a82a7d4443b9c00a891",
			"urls": [
				"bzz-raw://5d2a90a0a796491507462a3da18c3f8819721d571572d765a2207c35bf0a0389",
				"dweb:/ipfs/Qmf9ACYiT3qzjgsYuhm866FBdiBpRMXAPpQhSFbgqcyhHt"
			]
		},
		"@uniswap/v2-core/contracts/interfaces/IUniswapV2Pair.sol": {
			"keccak256": "0x7c9bc70e5996c763e02ff38905282bc24fb242b0ef2519a003b36824fc524a4b",
			"urls": [
				"bzz-raw://85d5ad2dd23ee127f40907a12865a1e8cb5828814f6f2480285e1827dd72dedf",
				"dweb:/ipfs/QmayKQWJgWmr46DqWseADyUanmqxh662hPNdAkdHRjiQQH"
			]
		},
		"@uniswap/v2-periphery/contracts/UniswapV2Router02.sol": {
			"keccak256": "0xb954cedec2552e286cbdf61d72e91affec2447636007c3ebd7194741157f48c8",
			"urls": [
				"bzz-raw://b9d5ccc48737414de4a2298791b322f2b8be48ba4359ce0f594f35613c998979",
				"dweb:/ipfs/QmcdQjcuUZ5DsD8vagwSRKZYYUP9df1F9QoAnkENnpUJjc"
			]
		},
		"@uniswap/v2-periphery/contracts/interfaces/IERC20.sol": {
			"keccak256": "0x61db17aebc5d812c7002d15c1da954065e56abe49d64b14c034abe5604d70eb3",
			"urls": [
				"bzz-raw://b006685e753f9120469f10b09c159f222d4cb8b507a6c1f0c14ed50c883ebe66",
				"dweb:/ipfs/QmSyY7iTugbczPwfGK67etiyPULenYGzzRYbt8aabwwkUb"
			]
		},
		"@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router01.sol": {
			"keccak256": "0x8a3c5c449d4b7cd76513ed6995f4b86e4a86f222c770f8442f5fc128ce29b4d2",
			"urls": [
				"bzz-raw://1df63ca373dafae3bd0ee7fe70f890a1dc7c45ed869c01de68413e0e97ff9deb",
				"dweb:/ipfs/QmefJgEYGUL8KX7kQKYTrDweF8GB7yjy3nw5Bmqzryg7PG"
			]
		},
		"@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol": {
			"keccak256": "0x744e30c133bd0f7ca9e7163433cf6d72f45c6bb1508c2c9c02f1a6db796ae59d",
			"urls": [
				"bzz-raw://9bf2f4454ad63d4cff03a0630e787d9e8a9deed80aec89682cd8ad6379d9ef8c",
				"dweb:/ipfs/Qme51hQNR2wpax7ooUadhtqLtXm8ffeVVYyubLkTT4wMCG"
			]
		},
		"@uniswap/v2-periphery/contracts/interfaces/IWETH.sol": {
			"keccak256": "0xfc10758fd8dba790c39468dccd358cb7cef06ae7c4781832cc7aa76f91f167e6",
			"urls": [
				"bzz-raw://dc22493dea6c60d47835eeba53726f8a6f76f4fcd798d40e54608a1380515d49",
				"dweb:/ipfs/QmS1QVcBRH4TELYNE7XCfjSVQEWFupyaNLKmMkKH7iPjrm"
			]
		},
		"@uniswap/v2-periphery/contracts/libraries/SafeMath.sol": {
			"keccak256": "0x27f0ea82f879b3b01387b583e6d9d0ec858dca3b22b0aad173f8fbda06e761e1",
			"urls": [
				"bzz-raw://0db9cf37793eb7035f0bfced36323d240f0212150009c39a3a108701d9b50b6c",
				"dweb:/ipfs/QmUAdiG9XNcieXkKfiMB49zQqD34FbXFE15csV2KQzwEqg"
			]
		},
		"@uniswap/v2-periphery/contracts/libraries/UniswapV2Library.sol": {
			"keccak256": "0x2e125bac8733dde3c937e4245f15dcd56633a1aecf5d7760ba2018e9e374f378",
			"urls": [
				"bzz-raw://f66af240fea54958cc87dc5dadc1fd36531d06666adf32330a0c78b8a1c6dea8",
				"dweb:/ipfs/QmU7Sg5LEkuCVqFNpMBt1Txpfv3ucG3H4QBaMVqtL5bYMp"
			]
		},
		"UniswapSwap.sol": {
			"keccak256": "0x329d833ff623cad02c3bd0aa528618b8df6c830208b9cd62688ea226338981ee",
			"urls": [
				"bzz-raw://351e984b5100d6b2da57a528ae5d91b46b73bb9ae19a31eb3db338a2dcc3a5df",
				"dweb:/ipfs/QmcZP8YMsj1q8qdVBu69KKaQc9M7Hv6R8FNo5AcTVXg4G7"
			]
		}
	},
	"version": 1
}

Now Please have a look at SwapAtUniswap.json :point_down:

{
	"deploy": {
		"VM:-": {
			"linkReferences": {},
			"autoDeployLib": true
		},
		"main:1": {
			"linkReferences": {},
			"autoDeployLib": true
		},
		"ropsten:3": {
			"linkReferences": {},
			"autoDeployLib": true
		},
		"rinkeby:4": {
			"linkReferences": {},
			"autoDeployLib": true
		},
		"kovan:42": {
			"linkReferences": {},
			"autoDeployLib": true
		},
		"görli:5": {
			"linkReferences": {},
			"autoDeployLib": true
		},
		"Custom": {
			"linkReferences": {},
			"autoDeployLib": true
		}
	},
	"data": {
		"bytecode": {
			"linkReferences": {},
			"object": "608060405234801561001057600080fd5b506040516108bb3803806108bb8339818101604052606081101561003357600080fd5b81019080805190602001909291908051906020019092919080519060200190929190505050826000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555081600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555080600260006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050505061078f8061012c6000396000f3fe60806040526004361061003f5760003560e01c80631641c4dc14610044578063302145991461006f578063b24078b21461009a578063d253732c14610186575b600080fd5b34801561005057600080fd5b50610059610201565b6040518082815260200191505060405180910390f35b34801561007b57600080fd5b506100846102ea565b6040518082815260200191505060405180910390f35b610184600480360360808110156100b057600080fd5b8101908080359060200190929190803590602001906401000000008111156100d757600080fd5b8201836020820111156100e957600080fd5b8035906020019184602083028401116401000000008311171561010b57600080fd5b919080806020026020016040519081016040528093929190818152602001838360200280828437600081840152601f19601f820116905080830192505050505050509192919290803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803590602001909291905050506103d3565b005b34801561019257600080fd5b506101bf600480360360208110156101a957600080fd5b8101908080359060200190929190505050610650565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b600080600080600080600260009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1663feaf968c6040518163ffffffff1660e01b815260040160a06040518083038186803b15801561027257600080fd5b505afa158015610286573d6000803e3d6000fd5b505050506040513d60a081101561029c57600080fd5b81019080805190602001909291908051906020019092919080519060200190929190805190602001909291908051906020019092919050505094509450945094509450839550505050505090565b600080600080600080600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1663feaf968c6040518163ffffffff1660e01b815260040160a06040518083038186803b15801561035b57600080fd5b505afa15801561036f573d6000803e3d6000fd5b505050506040513d60a081101561038557600080fd5b81019080805190602001909291908051906020019092919080519060200190929190805190602001909291908051906020019092919050505094509450945094509450839550505050505090565b60006103dd6102ea565b905060006103e9610201565b905082600481905550846003908051906020019061040892919061068c565b5060008090505b6001811015610647576103e860098161042457fe5b0483838161042e57fe5b041161063a576000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16637ff36ab5886003886004546040518563ffffffff1660e01b815260040180858152602001806020018473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001838152602001828103825285818154815260200191508054801561054057602002820191906000526020600020905b8160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190600101908083116104f6575b505095505050505050600060405180830381600087803b15801561056357600080fd5b505af1158015610577573d6000803e3d6000fd5b505050506040513d6000823e3d601f19601f8201168201806040525060208110156105a157600080fd5b81019080805160405193929190846401000000008211156105c157600080fd5b838201915060208201858111156105d757600080fd5b82518660208202830111640100000000821117156105f457600080fd5b8083526020830192505050908051906020019060200280838360005b8381101561062b578082015181840152602081019050610610565b50505050905001604052505050505b808060010191505061040f565b50505050505050565b6003818154811061065d57fe5b906000526020600020016000915054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b828054828255906000526020600020908101928215610705579160200282015b828111156107045782518260006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550916020019190600101906106ac565b5b5090506107129190610716565b5090565b61075691905b8082111561075257600081816101000a81549073ffffffffffffffffffffffffffffffffffffffff02191690555060010161071c565b5090565b9056fea2646970667358221220110e16dbcf4ac7409675ba9a96578365d18764ae24c4b9931d661a9a30ddc8ed64736f6c63430006060033",
			"opcodes": " ",
			"sourceMap": "252:1934:11:-:0;;;637:371;5:9:-1;2:2;;;27:1;24;17:12;2:2;637:371:11;;;;;;;;;;;;;;;15:2:-1;10:3;7:11;4:2;;;31:1;28;21:12;4:2;637:371:11;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;771:7;743:6;;:36;;;;;;;;;;;;;;;;;;828:16;788:15;;:57;;;;;;;;;;;;;;;;;;939:17;898:16;;:59;;;;;;;;;;;;;;;;;;637:371;;;252:1934;;;;;;"
		},
		"deployedBytecode": {
			"immutableReferences": {},
			"linkReferences": {},
			"object": ""
		},
		"gasEstimates": {
			"creation": {
				"codeDepositCost": "387000",
				"executionCost": "infinite",
				"totalCost": "infinite"
			},
			"external": {
				"getLatestEthUsdPrice()": "infinite",
				"getLatestLinkUsdPrice()": "infinite",
				"swapETHForLink(uint256,address[],address,uint256)": "infinite",
				"wethLinkRinkebyPath(uint256)": "2061"
			}
		},
		"methodIdentifiers": {
			"getLatestEthUsdPrice()": "30214599",
			"getLatestLinkUsdPrice()": "1641c4dc",
			"swapETHForLink(uint256,address[],address,uint256)": "b24078b2",
			"wethLinkRinkebyPath(uint256)": "d253732c"
		}
	},
	"abi": [
		{
			"inputs": [
				{
					"internalType": "address payable",
					"name": "_router",
					"type": "address"
				},
				{
					"internalType": "address",
					"name": "_ethUsdPriceFeed",
					"type": "address"
				},
				{
					"internalType": "address",
					"name": "_linkUsdPriceFeed",
					"type": "address"
				}
			],
			"stateMutability": "nonpayable",
			"type": "constructor"
		},
		{
			"inputs": [],
			"name": "getLatestEthUsdPrice",
			"outputs": [
				{
					"internalType": "uint256",
					"name": "",
					"type": "uint256"
				}
			],
			"stateMutability": "view",
			"type": "function"
		},
		{
			"inputs": [],
			"name": "getLatestLinkUsdPrice",
			"outputs": [
				{
					"internalType": "uint256",
					"name": "",
					"type": "uint256"
				}
			],
			"stateMutability": "view",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "uint256",
					"name": "amountOutMin",
					"type": "uint256"
				},
				{
					"internalType": "address[]",
					"name": "_wethLinkRinkebyPath",
					"type": "address[]"
				},
				{
					"internalType": "address",
					"name": "to",
					"type": "address"
				},
				{
					"internalType": "uint256",
					"name": "_deadline",
					"type": "uint256"
				}
			],
			"name": "swapETHForLink",
			"outputs": [],
			"stateMutability": "payable",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "uint256",
					"name": "",
					"type": "uint256"
				}
			],
			"name": "wethLinkRinkebyPath",
			"outputs": [
				{
					"internalType": "address",
					"name": "",
					"type": "address"
				}
			],
			"stateMutability": "view",
			"type": "function"
		}
	]
}

I am unable to understand what “data” in artifacts/SwapAtUniswap.json is getting modified by SwapAtUniswap_metadata.json ??

My autoDeployLib is also true on Rinkeby (where i have deployed the contract).
Are my contract addresses wrong :thinking: (Although i took them from https://docs.chain.link/docs/ethereum-addresses/) and also from https://docs.uniswap.org/protocol/V2/reference/smart-contracts/router-02?
I think both Chainlink and Uniswap use the same addresses for Chainlink Price Feeds or not as specified in https://docs.chain.link/docs/ethereum-addresses/ ?

I have been gazing and gazing and unable to sort this out…

also, my stateMutability of functions seems correct, or am i making some Silly mistakes (which I am unable to catch Myself ? )
Please Help :pray:

Also, this is my Remix URL :point_down:
https://remix.ethereum.org/#optimize=false&runs=200&evmVersion=null&version=soljson-v0.6.6+commit.6c089d02.js

AS you can see it says optimize = false
What is the way to optimize gas utilizations, how to write code more elegantly ? Please help :pray: (or give me some resources to read), or whether its even necessary at this stage.
I am kind of lost

I also Ran debugging on remix using my transaction Hash (0xe680497b28b4b68d80e6fa8b12e56ab1d2b4927ab56eb86a0004ea205d76467a), and couldn’t really identify any issues, maybe I lack the skills ? Please let me know where I am going wrong , If at all i am going wrong?

Your kind replies and help shall be extremely gratifying !!

Thanks and Regards

Suveett Kalra
(Su.kal Crypto)

Sorry, this is out of the Academy scope, if the transaction said that it will be reverted, is probably because you are missing an argument or the parameters value triggers one of the reverts from the swap contract, hard to say.

Carlos Z