After spending hours of research, I finally found the code from this forum that will do the trick for me. I've successfully compiled the code in Remix (Kovan Test Environment), but I'm getting this gas error message:

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

Here's the code that I'm re-using:

pragma solidity 0.7.1;

import "https://github.com/Uniswap/uniswap-v2-periphery/blob/master/contracts/interfaces/IUniswapV2Router02.sol"; 
 contract BuyToken{
    IUniswapV2Router02 usi = IUniswapV2Router02(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D);
function buyTokenWithExactEth() public payable returns(uint256) {
        address cryptoToken = 0x4F96Fe3b7A6Cf9725f59d353F723c1bDb64CA6Aa;

        uint deadline = block.timestamp + 15; // I am only using 'now' for convenience, for mainnet I will pass deadline from frontend
        usi.swapExactETHForTokens{value: msg.value}(0, getPathForETHToToken(cryptoToken), address(this), deadline);

        // no need to refund ETH
        return 1000;

  function getPathForETHToToken(address crypto) private view returns (address[] memory) {
    address[] memory path = new address[](2);
    path[0] = usi.WETH();
    path[1] = crypto;
    return path;


Here's the screenshot of my remix environment:


Any advice on how to fix this? thanks in advance!

Edit: new error when deploying the contract in Mainnet

what is this error?


Okay it seems you are already failing in the deployment part. The transaction you posted: https://kovan.etherscan.io/tx/0xeef83dae54ba0143aa1a654f328496af154775a3877e30af6cc5e8c520688bdc is a failed deployment.

The reason it fails is because you are sending 1 Wei along with the deployment. But the constructor is correctly not declared as payable and thus the deployment fails.

To get it working:

  1. Enter '0 Wei' in the top left.
  2. Deploy and wait for it to succeed.
  3. Enter 'x Wei' in the top left.
  4. Under 'Deployed Contracts' find the new contract. Now click 'buyTokenWithExactEth'.
  • thanks alot Markus! it worked perfectly. just some questions: 1) instead of "X" Wei in the top left, can I switch to Eth? I rather type .1 eth instead of 100000000000000000 every time i make a transaction. 2) can you recommend some solidity courses for getting the basics down. 3) since i have your email, can i contact you for questions going forward? Thanks again. you're awesome dude! – python_rok Oct 25 '20 at 23:45
  • Hey Markus, I tried to deploy this in Mainnet (step #2) and I see this error message: creation of BuyToken errored: while converting number to string, invalid number value '', should be a number matching (^-?[0-9.]+). It won't let me deploy the contract. any idea what went wrong? – python_rok Oct 26 '20 at 0:30
  • @python_rok For new questions please open up a new one. One thread on Stackexchange is supposed to be for one question only. – Markus - soliditydeveloper.com Oct 26 '20 at 9:09
  • Sounds fair, thanks! – python_rok Oct 28 '20 at 0:30

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.