0

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?

3
  • Where are you testing your contracts? The 1.5M gas returned by estimateGas is not that much, mainnet now has around 12M. Gas costs depends on the contract state so it is possible it will be higher in your contracts if the state is not the same. – Ismael Oct 12 '20 at 5:36
  • Looking at the depositAll code it calls transferFrom from the underlying token, for that to work you need to approve depositHelper for the amount transferred. – Ismael Oct 12 '20 at 6:04
  • @Ismael Apologies, that should say 11,5M. I figured out that I need pass some extra variables into the estimateGas call. I was able to resolve this issue which I've detailed in my answer below. – Megan Oct 12 '20 at 14:44
0

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 => {});

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.