hook useSimulateContract always fails if maxFeePerGas and maxPriorityFeePerGas are provided
Check existing issues
- [X] I checked there isn't already an issue for the bug I encountered.
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.
- Open the link.
- Click the Simulate Transfer button.
- 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")
})
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" } }
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
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.
This is because the connected account does not have enough funds to cover the maximum fees (as stated in the error message).
Hi, i hope you all are doing well. Finally managed to solve this issue as follow
- 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;
};
- Compute FeesPerGas with hook
useEstimateFeesPerGasfrom Wagmi with the specific config forWagmiProvideras parameter.
const feesPerGas = useEstimateFeesPerGas({
config,
});
- 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
writeContractfunction 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].
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.