2

I wrote a smart contract, that is able to borrow a flashloan, make a few trades and pays the flashloan back after that. I tested the smart contract with hardhat on a mainnet fork and everything worked fine, so I deployed on mainnet. Now I wanted to test the smart contract on mainnet, so I funded the smart contract with a little bit of tokens. The idea is, I can test it with a small trade (1$ maybe) that is even not profitable, because the amount I funded the smart contract with will be used to pay back the flashloan.

For my flashLoan function I need 3 arguments:

  1. Which token I want to borrow
  2. Which amount I want to borrow
  3. The data I need for the trades (encoded)

So I set up a new ethers contract like this:

const signer = new ethers.Wallet(private_key, provider);

let flashloan_contract = new ethers.Contract(
  contract_addr,
  contractABI,
  signer 
);

Now I estimate the gas for the transaction:

const gasLimit = 1500000;
const gasPrice = await provider.getGasPrice();
const extraGas = ethers.utils.parseUnits("100", "gwei");
console.log("gasLimit: ", gasLimit);
console.log("gasPrice: ", gasPrice.toString());
console.log(
   "TransactionPrice: ",
   ethers.utils.formatUnits(gasPrice.mul(gasLimit), 18)
);

What leads to:

gasLimit:  1500000
gasPrice:  59093502898
TransactionPrice:  0.068360709408

So the price for the whole transaction should be 0.068 (Token has 18 decimals), my wallet does have enough funds for that (I do have 2.6 in it).

Now I try to call my smart contract function flashLoan:

try {
   const tx = await flashloan_contract.flashLoan(
      [token],
      [amount],
      trade_data,
      {
         gasLimit: gasLimit,
         gasPrice: gasPrice.add(extraGas),
       }
    );
    let receipt = await tx.wait();
    console.log("Receipt");
    console.log(receipt);
} catch (err) {
   console.error(err);
}

I tried many different gas prices but noone is working.I get the error:

<ref *1> Error: cannot estimate gas; transaction may fail or may require manual gas limit [ See: https://links.ethers.org/v5-errors-UNPREDICTABLE_GAS_LIMIT ] (error={"reason":"execution reverted","code":"UNPREDICTABLE_GAS_LIMIT","method":"estimateGas","transaction":{"from":"0x...2","maxPriorityFeePerGas":{"type":"BigNumber","hex":"0x59682f00"},"maxFeePerGas":{"type":"BigNumber","hex":"0x0b3c48ff8c"},"to":"0x...4",...

I searched for this error for days, but cannot find a solution that works for me. I hope someone here can help me with this issue.

Thank you very much.

Best regards, Daniel

2

3 Answers 3

2

Ok I solved it. I tested my smart contract with hardhat and to check the states I used console.log. For sure that does not work on mainnet (Or even testnet), but on a hardhat forked mainnet it does. So I deleted all console.log(...) commands and now the code is working. Thank you guys for your answers/support.

Merry X-Mas

0

This might have plenty of reasons and (according to what you explained) I think it has nothing to do with the gas limit. It's highly likely that your transaction is getting failed because of a runtime error, and as the result, estimateGas can't get completed successfully. If you send your transaction without a dry run, it will really fail and reverts on Mainnet (and of course, it costs you precious eth). I think the best thing you can do is to first run it on the goearly testnet (if possible), in addition, you might want to handle all possible failures in your smart contract and add suitable error messages to it so that you can find the reason of any failure on the real situation.

1
  • But what I did is forking mainnet with hardhat and run tests there (included calling the function mentianed above). What would be the benefit to run it on testnet now?
    – Dani
    Dec 23, 2022 at 5:57
0

This errors pops for many reasons, not only related to gas costs. It may indicates that it is impossible to calculate gas costs due to an unknown function call for example.

Last times I got this error was :

  • I did spelling mistake in my function call (for example I was trying to call the function mint() instead of Mint() as written in my contract)
  • I was trying to call a non payable function with a transaction value

Hope it can help...

1
  • I am calling few internal functions in the process, could this lead to an error because I cannot set them payable? Is there a possibility to get information which line is throwing the error in my smart contract? Hard to understand for me because in forked mainnet everything works fine By the way, I checked, that I have spelled everythin correctly, error still exists
    – Dani
    Dec 23, 2022 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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