wagmi icon indicating copy to clipboard operation
wagmi copied to clipboard

hook useSimulateContract always fails if maxFeePerGas and maxPriorityFeePerGas are provided

Open Linch1 opened this issue 1 year ago • 3 comments

Check existing issues

Describe the bug

I'm calling the hook useSimulateContract passing also the maxFeePerGas and maxPriorityFeePerGas parameters:

const simulateRes = useSimulateContract({
    address: usdtContractAddress,
    abi: usdtABI,
    functionName: "transfer",
    args: ["0x000000000000000000000000000000000000dead", "1"],
    query: { enabled: false },
    maxFeePerGas: parseGwei("5"),
    maxPriorityFeePerGas: parseGwei("5"),
  });

Since I am using standard values for these two fields, I expected the simulation to succeed. However, it always fails with an error similar to the following:

err: insufficient funds for gas * price + value: address 0xasd...asd have 195844648662880000 want 2750000000000000000 (supplied gas 550000000)

The strange thing is the excessively high gas amount.

Link to Minimal Reproducible Example

https://codesandbox.io/p/sandbox/wagmi-usedisconnect-bug-forked-xc969n?file=%2Fsrc%2FTransfer.js

Steps To Reproduce

In the provided CodeSandbox link, there is a minimal React app.

  1. Open the link.
  2. Click the Simulate Transfer button.
  3. After 5-10 seconds, the error will appear.

The component handling the simulation is Transfer.js. To test the different behaviors:

  • Comment out the maxFeePerGas and maxPriorityFeePerGas parameters from the hook to make the simulation work.
  • Uncomment them to make the simulation fail.

What Wagmi package(s) are you using?

wagmi

Wagmi Package(s) Version(s)

2.9.2

Viem Version

2.10.11

TypeScript Version

No response

Anything else?

My goal was to call the contract function with slightly higher values so that it could be mined faster and to avoid transactions getting stuck and never being mined. As a workaround, I provide these values directly when I call the writeContract instead of in the simulation hook.

So, instead of using () => writeContract(simulateData?.request), I would use:

() => writeContract({
    ...simulateData?.request, 
    maxFeePerGas: parseGwei("5"), 
    maxPriorityFeePerGas: parseGwei("5")
})

Linch1 avatar Jul 09 '24 20:07 Linch1

Hi @Linch1 , i'm getting the same error. Have you found anything helpful? Thanks in advance. I'm trying with this hook useEstimatefeespergas but still not work throwing this.

{ "jsonrpc": "2.0", "id": 10, "error": { "code": -32000, "message": "execution reverted" } }

Icaro3422 avatar Jul 26 '24 21:07 Icaro3422

Hey @Icaro3422 unfortunately nope, the only way to force those information is not to use them in the estimate but to directly add them in the writeContract function as i said at the end of the issue post, other than that i did not found something else :c

Linch1 avatar Jul 28 '24 12:07 Linch1

how do I estimate result of gas used? It was possible to get them through Ethers v5, but I have no idea on this one.

hskang9 avatar Aug 09 '24 03:08 hskang9

This is because the connected account does not have enough funds to cover the maximum fees (as stated in the error message).

jxom avatar Sep 09 '24 06:09 jxom

Hi, i hope you all are doing well. Finally managed to solve this issue as follow

  1. Created a react function to calculate the gas amount like this
  const calculateGasFee = async ({
    address,
    abi,
    functionName,
    args,
    account,
  }: {
    address: `0x${string}`;
    abi: any;
    functionName: string;
    args: any[];
    account: `0x${string}`;
  }) => {
    const gasFee = await publicClient?.estimateContractGas({
      address,
      abi,
      functionName,
      args,
      account,
    });

    return gasFee;
  };
  1. Compute FeesPerGas with hook useEstimateFeesPerGas from Wagmi with the specific config for WagmiProvider as parameter.
  const feesPerGas = useEstimateFeesPerGas({
    config,
  });
  1. Create a hook to compute the abi function you want to execute so you can calculate the fees ans then pass it as parameters to the writeContract function from @wagmi/core.
      const gas = await calculateGasFee({
        ...contracts?.[contract],
        functionName,
        args,
        account: otherAddress || (address as `0x${string}`),
      });

      if (!gas) return;

      const hash = await writeContract(config, {
        ...contracts?.[contract],
        functionName,
        args,
        account: otherAddress || (address as `0x${string}`),
        gas: Number(gas), // you can add up to this number by 25 percent or so
        maxFeePerGas: feesPerGas?.data?.maxFeePerGas || BigInt(0),
        maxPriorityFeePerGas: feesPerGas?.data?.maxPriorityFeePerGas || BigInt(0),
      });

      const resTransaction = await waitForTransactionReceipt(config, {
        hash: hash as `0x${string}`,
      });

I hope this can help you out. In case you want to reach out to explain this through a quick call, here is my email [email protected].

Icaro3422 avatar Sep 09 '24 13:09 Icaro3422

This issue has been locked since it has been closed for more than 14 days.

If you found a concrete bug or regression related to it, please open a new bug report with a reproduction against the latest Wagmi version. If you have any questions or comments you can create a new discussion thread.

github-actions[bot] avatar Sep 24 '24 00:09 github-actions[bot]