[Ethereum] Default gas limit set by web3 is unreasonably high

gas-estimategas-limitweb3js

I am trying to learn and understand solidity/defi by creating some copies of current popular protocols; I've created a small copy of the Curve Dai strategy from harvest finance, but I'm getting really high gas limits when I try to deposit funds using the depositAll() function on the DepositHelper contract.

Contract Links (Mainnet)

I thought this could've been because I did not verify my source code initially, but after verifying I am still seeing high gas prices in MetaMask for what should be relatively cheap (~$15-$20) transactions. As a test I deposited $40 DAI into harvest finance and the gas limit estimated ~$20 for the transaction cost, and final transaction cost was $10.

In contrast, when I called the same function on my version of the deployed contract the default gas limit set in MetaMask created a estimated transaction cost of ~$140.

I've tried to call estimateGas() so I can manually set an appropriate gas limit (not sure if this is best practice…) but this call fails with the following error:

{code: -32000, message: "gas required exceeds allowance (1500000) or always failing transaction"}

Code I wrote to call depositAll():

const depositHelper = new web3.eth.Contract(
    DepositHelper.abi, // abi for DepositHelper contract
    '0x4681ae82f6f575da63b77a6735f44c13fdc2e4df'
);

// construct depositAll method parameters
const amount = 40; // 40 DAI
const weiAmount = window.web3.utils.toWei(amount, 'ether'); // convert to wei
const amounts = [weiAmount];
const vaults = ['0x3EE8bD22Df6605c6a7074c56Abb0f3Fc1FA6a069'];

depositHelper.methods
    .depositAll(amounts, vaults).send({ from: account }) // gas limit set here by default seems unreasonably high ...
    .catch(error => {});

Code I wrote to attempt gas estimate that fails

depositHelper.methods
    .depositAll(amounts, vaults).estimateGas({ gas: 1500000 })
    .then(gasAmount => {
         console.log('gasAmount ::: ', gasAmount);
    })
    .catch(error => {});

Is there something I've done wrong in calling the depositHelper function that is causing this gas limit to be super high?

Best Answer

Not sure why the default gas estimate from web3 -> metamask was so high, but I have gotten around this by using the estimated gas from estimateGas() + a buffer to manually set the gas sent in the depositAll transaction.

Here is the code:

const depositHelper = new web3.eth.Contract(
    DepositHelper.abi, // abi for DepositHelper contract
    '0x4681ae82f6f575da63b77a6735f44c13fdc2e4df'
);

// construct depositAll method parameters
const amount = 40; // 40 DAI
const weiAmount = window.web3.utils.toWei(amount, 'ether'); // convert to wei
const amounts = [weiAmount];
const vaults = ['0x3EE8bD22Df6605c6a7074c56Abb0f3Fc1FA6a069'];

depositHelper.methods
    .depositAll([amount], vaults)
    .estimateGas({ from: account, gas: 50000000 })
    .then(gasEstimate => {
        // not sure if there is a best practice for adding a gas buffer
        // so I just set it to 450k as a default
        return depositHelper.methods
            .depositAll(amounts, vaults).send({ from: account, gas: gasEstimate + 450000 })
        })
    .catch(error => {});
Related Topic