Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Simulating transactions on Ganache v7 fork throws insufficient funds error #2162

Closed
haseebrabbani opened this issue Jan 23, 2022 · 7 comments
Assignees

Comments

@haseebrabbani
Copy link

hey there, appreciate your work on this amazing tool! when trying to integrate the latest version (ganache 7.0.0) into my previously working code (which used ganache-core 2.13.2) I am seeing some non-deterministic behaviour. Sometimes when simulating a transaction on a fork it will throw an insufficient funds for intrinsic transaction cost error. But other times, running the exact same code will work as expected. Here is what I am doing:

const provider = new ethers.providers.Web3Provider(ganache.provider({
    fork: {
      url: MY_INFURA_URL,
      blockNumber: SOME_BLOCK_NUMBER
    },
    wallet: {
      unlockedAccounts: [USER_ADDRESS]
    }
  }) as any)
const tetherContract = new ethers.Contract(TETHER_ADDRESS, TETHER_ABI, provider.getSigner(USER_ADDRESS))
const userTetherBalance = await tetherContract.balanceOf(USER_ADDRESS)
const tx = await tetherContract.transfer(USER2_ADDRESS, userTetherBalance) //<---- throws error here
@haseebrabbani haseebrabbani changed the title Simulating transactions on Ganache v7 fork throws insufficient gas error Simulating transactions on Ganache v7 fork throws insufficient funds error Jan 23, 2022
@davidmurdoch davidmurdoch added this to the 7.0.x milestone Jan 23, 2022
@MicaiahReid
Copy link
Contributor

Hi @haseebrabbani, thanks for opening this issue. We're investigating this now. Is there any chance you could send a repo to help us reproduce this?

@MicaiahReid MicaiahReid self-assigned this Jan 25, 2022
@haseebrabbani
Copy link
Author

@MicaiahReid for sure, here is a repo to reproduce: https://github.com/haseebrabbani/ganache-v7-forking-issue-repro

i'm simulating the transaction on a fork over a fixed range of 10 blocks. it seems that the error is thrown on the same blocks everytime. when i initially encountered this issue i was running it on increasing block numbers which made it appear non-deterministic

@MicaiahReid
Copy link
Contributor

@haseebrabbani This is perfect, thank you! We're looking into this now.

@MicaiahReid
Copy link
Contributor

MicaiahReid commented Jan 27, 2022

Hi @haseebrabbani, we did some looking into this and think there's a combination of factors contributing to the issue. If you change your transfer call to include a maxFeePerGas, as follows:

const tx = await tetherContract.transfer(
  USER2_ADDRESS,
  userTetherBalance,
  { maxFeePerGas: 90417983619 }
);

all of the transfers work. Whenever you fork from a block, the new "latest" block is used to set the baseFeePerGas of the next block. When no gas information is specified on a transaction, the default base fee is set by Ganache. This is an expensive gas premium because it ensures it will make it onto the block - but you're overspending on gas in that case. So, this was leading to insufficient funds to perform the transfer.

We plan to improve upon this in a number of ways:

@haseebrabbani Let me know if the above fix doesn't work for you and we can investigate further. If it does work, feel free to close this issue!

@haseebrabbani
Copy link
Author

Appreciate the explanation! However, since this code will be client-facing I wouldn't want to use this workaround (and possibly link to this issue in a comment to explain why I'm setting maxFeePerGas). As such, I will hold off on integrating Ganache v7 and stick to ganache-core for the time being until a longer-term fix is implemented. Ideally, since users did not have to set this parameter before, they shouldn't have to set it in the latest version either

@davidmurdoch
Copy link
Member

If you want the revert to ganache-core's gas behavior and you don't care about type 2 transactions (eip-1559) you can set the hardfork option to "berlin", or even back to istanbul.

@cds-amal
Copy link
Member

cds-amal commented Feb 2, 2022

We're closing since it's working as intended. Please subscribe to the linked issues above to track progress.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants