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:
- Which token I want to borrow
- Which amount I want to borrow
- 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
eth_estimateGas
(named EstimateGas() in golang ethclient) , just call it and it will give you the gas limit you have to use