From 01c23cd4a22de06efc37ea805473338cf64d9bc3 Mon Sep 17 00:00:00 2001 From: haosenwang1018 <1293965075@qq.com> Date: Tue, 24 Feb 2026 15:37:24 +0800 Subject: [PATCH] fix: encode calldata in preflight gas estimation The preflight() function passed data: undefined to estimateGas(), which estimates gas for a plain ETH transfer rather than the actual contract call. This always fails for contract interactions, falling through to the catch() fallback of 200,000 gas. The balance check then uses an inaccurate estimate, providing no real protection against out-of-gas reverts. Encode the calldata with encodeFunctionData() so estimateGas() simulates the actual contract call and returns a meaningful estimate. Co-Authored-By: Claude Sonnet 4.6 --- src/registry/erc8004.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/registry/erc8004.ts b/src/registry/erc8004.ts index 5a21a7d6..a7ed4b3e 100644 --- a/src/registry/erc8004.ts +++ b/src/registry/erc8004.ts @@ -18,6 +18,7 @@ import { parseAbi, keccak256, toBytes, + encodeFunctionData, type Address, type PrivateKeyAccount, } from "viem"; @@ -98,12 +99,19 @@ async function preflight( transport: http(), }); + // Encode calldata for accurate gas estimation + const data = encodeFunctionData({ + abi: functionData.abi, + functionName: functionData.functionName, + args: functionData.args, + }); + // Estimate gas const gasEstimate = await publicClient .estimateGas({ account: account.address, to: functionData.address, - data: undefined, // Will be encoded by the client + data, }) .catch(() => BigInt(200_000)); // Fallback estimate