useDApp icon indicating copy to clipboard operation
useDApp copied to clipboard

Gas estimation on simple function fails

Open JCaraballo113 opened this issue 2 years ago • 2 comments

Describe the bug

So I have this function

function unstake(uint256 amount) public {
        require(_stakedAmounts[msg.sender] != 0, "Has not staked");
        require(_stakedAmounts[msg.sender] - (_unstakedAmounts[msg.sender] + amount) >= 0, "Cannot unstake more than total staked amount");
        _unstakedAmounts[msg.sender] += amount;
        emit Unstaked(msg.sender, amount);
        emit TotalUnstaked(msg.sender, _unstakedAmounts[msg.sender]);
    }

Which is pretty simple and it used to work just fine with useDapp on the GOERLI network. But after the merge on goerli or some kind of update I keep getting the following error:

Error: cannot estimate gas; transaction may fail or may require manual gas limit

This function has been the same for over 3 months and it used to work fine using the useContractFunction hook. It was just recently that this error started happening. I have confirmed the issue is within useDapp and not my smart contract code because you can call the unstake function on etherscan directly without any issues.

To Reproduce No idea how to reproduce since I don't know what actually causes it.

Config:

const readConfig =
    process.env.POLYGON_CHAIN_ID === '80001'
        ? {
              [Mumbai.chainId]: process.env.POLYGON_URL || '',
              [SkaleChain.chainId]: process.env.SKALE_URL || '',
          }
        : {
              [Polygon.chainId]: process.env.POLYGON_URL || '',
              [SkaleChain.chainId]: process.env.SKALE_URL || '',
          };

const dappConfig: Config = {
    networks: [...DEFAULT_SUPPORTED_CHAINS, SkaleChain],
    readOnlyUrls: {
        [Goerli.chainId]: process.env.INFURA_NETWORK || '',
        ...readConfig,
    },
};

Software versions

  • useDapp version : 1.1.5
  • Package manager : npm
  • Node version: 16.16.0

Additional context

Screen Shot 2022-08-15 at 9 43 57 AM

JCaraballo113 avatar Aug 15 '22 13:08 JCaraballo113

@JCaraballo113 It started happening recently - does it happen always, or randomly? There were no useDapp package upgrades in the meantime?

Trying to narrow down the problem.

rzadp avatar Aug 16 '22 11:08 rzadp

Does the error happen only on one chain - Goerli? Or does it happen on all other chains as well?

rzadp avatar Aug 16 '22 11:08 rzadp

@rzadp sorry I was travelling!. It happens always on Goerli at least. I have not tried with other chains. Unless useDapp has gone past 1.1.5 then there are no updates

JCaraballo113 avatar Aug 19 '22 14:08 JCaraballo113

I'm trying to reproduce it. On Goerli, with a simple WETH contract.

Example transaction initiated from Etherscan UI: https://goerli.etherscan.io/tx/0x319ad57b4c6d07f4ddd264f512c73ead8d097ba7ec0cd301223bb9302152e175 Screenshot 2022-08-19 at 19 16 31

Example transaction initiated from useDApp UI (no gasLimitBufferPercentage): https://goerli.etherscan.io/tx/0xb4b903b3271bcf4d04d380ffaa09039b0ce275d07872ef2db0015be6f4b1665d Screenshot 2022-08-19 at 19 16 50


So the transaction uses 98% of estimated gas, and is mined successfully. I have also tested a useDApp version from 3 months ago before any changes to gas estimation we did recently - works the same.

So, no success in reproduction so far.

@JCaraballo113 Perhaps you actually do need to set the gasLimitBufferPercentage? Can you set it to 10 for example (will add 10% on top of estimated gas limit) and see if that helps?

Also, since your contract is deployed on Goerli testnet, are you able to grant me some rights / stake to it so that I could try to reproduce it myself?

rzadp avatar Aug 19 '22 17:08 rzadp

@rzadp all you need to stake/unstake is some of our tokens. If you can plop your wallet here I can send you some and then you will be able to stake/unstake yourself

JCaraballo113 avatar Aug 22 '22 19:08 JCaraballo113

@rzadp all you need to stake/unstake is some of our tokens. If you can plop your wallet here I can send you some and then you will be able to stake/unstake yourself

My address for testing: 0x182E1b58bc86582eE1f582aD5Aa5BDfAbAf5b495

rzadp avatar Aug 23 '22 07:08 rzadp

@rzadp I have sent you some of our token for use in staking with @useDapp/core here is the tx receipt: https://goerli.etherscan.io/tx/0xb81a38639c9a36cb3b6f1208398d205e1327caadb7db5fa69d9fcb639a903bec

JCaraballo113 avatar Aug 25 '22 23:08 JCaraballo113

@JCaraballo113 OK, what about the address of the contract for staking and its ABI?

rzadp avatar Aug 26 '22 08:08 rzadp

@rzadp here is the contract address for staking, it's verified so should have everything you need? https://goerli.etherscan.io/address/0xBED33e388d6079A55E19FAD54C9668CF95631390

JCaraballo113 avatar Aug 26 '22 15:08 JCaraballo113

@JCaraballo113 I tried to reproduce the issue, but it worked for me. Here are my steps:

  1. increaseAllowance for my tokens on the contract
  2. stake amount 1000000 using etherscan - tx
  3. unstake amount 1000000 using etherscan - tx
  4. stake amount 1000000 using useDApp - tx
  5. unstake amount 1000000 using useDApp - tx

It all worked OK. My useDApp code snippets:

const CoveyStakingInterface = new utils.Interface(CoveyStakingabi)
const contractAddress = '0xBED33e388d6079A55E19FAD54C9668CF95631390'
const contract = new Contract(contractAddress, CoveyStakingInterface)

const stakeFn = useContractFunction(contract, 'stake')
const unstakeFn = useContractFunction(contract, 'unstake')

const handleStake = async () => {
  await stakeFn.send('1000000')
}

const handleUnstake = async () => {
  await unstakeFn.send('1000000')
}

What can be observed, is that useDApp by default estimates gas limit of a transaction with a very small margin (roughly 99% of estimation is consumed by the transaction). For larger buffers of safety, we added a gasLimitBufferPercentage configuration option. However, I did not use it, and my transaction were within limit and went through.

Estimation is done using ethers - perhaps there is a problem with the version of it? We're using version 5.6.9. @JCaraballo113 If you're using lower, can you try upgrading?

rzadp avatar Aug 26 '22 17:08 rzadp

That's probably it. Will test and let you know

JCaraballo113 avatar Aug 26 '22 18:08 JCaraballo113

@rzadp this was the issue. Closing.

JCaraballo113 avatar Aug 31 '22 15:08 JCaraballo113