Skip to content

Commit 2b1ffc4

Browse files
committed
Add fallback for eth_estimateGas with state override
Attempts to estimate gas using a balance override for the sender address. Falls back to the standard call if the node does not support state overrides, improving compatibility across different Ethereum nodes.
1 parent aca31e2 commit 2b1ffc4

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

Thirdweb/Thirdweb.Transactions/ThirdwebTransaction.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,19 @@ public static async Task<BigInteger> EstimateGasLimit(ThirdwebTransaction transa
272272
}
273273
else
274274
{
275-
var hex = await rpc.SendRequestAsync<string>("eth_estimateGas", transaction.Input).ConfigureAwait(false);
276-
baseGas = hex.HexToNumber();
275+
try
276+
{
277+
// With balance override
278+
var stateOverride = new Dictionary<string, object> { [transaction.Input.From] = new Dictionary<string, string> { ["balance"] = "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" } };
279+
var hex = await rpc.SendRequestAsync<string>("eth_estimateGas", transaction.Input, "latest", stateOverride).ConfigureAwait(false);
280+
baseGas = hex.HexToNumber();
281+
}
282+
catch
283+
{
284+
// Without balance override - fallback for nodes that don't support state overrides
285+
var hex = await rpc.SendRequestAsync<string>("eth_estimateGas", transaction.Input).ConfigureAwait(false);
286+
baseGas = hex.HexToNumber();
287+
}
277288
}
278289
return baseGas * 10 / divider;
279290
}

0 commit comments

Comments
 (0)