From c0229a11396b752724491232a566004d8d980596 Mon Sep 17 00:00:00 2001 From: hailin Date: Wed, 31 Dec 2025 12:48:41 -0800 Subject: [PATCH] fix(transaction): use eth_gasPrice RPC for Legacy transaction gas estimation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Changed getGasPrice() to use eth_gasPrice RPC method instead of calculating from baseFeePerGas (which is for EIP-1559 transactions) - Added 10% buffer to gas price to ensure transaction gets included - Updated Home.tsx to use gasPrice instead of maxFeePerGas for display KAVA doesn't support EIP-1559, so we must use Legacy (Type 0) transactions with gasPrice from eth_gasPrice RPC. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- .../service-party-app/src/pages/Home.tsx | 6 ++-- .../src/utils/transaction.ts | 30 +++++++++++-------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/backend/mpc-system/services/service-party-app/src/pages/Home.tsx b/backend/mpc-system/services/service-party-app/src/pages/Home.tsx index ed72ee0a..fe02c7ce 100644 --- a/backend/mpc-system/services/service-party-app/src/pages/Home.tsx +++ b/backend/mpc-system/services/service-party-app/src/pages/Home.tsx @@ -317,8 +317,8 @@ export default function Home() { }; // 格式化 gas 费用显示 - const formatGasFee = (gasLimit: bigint, maxFeePerGas: bigint): string => { - const maxFee = gasLimit * maxFeePerGas; + const formatGasFee = (gasLimit: bigint, gasPrice: bigint): string => { + const maxFee = gasLimit * gasPrice; const feeKava = Number(maxFee) / 1e18; return feeKava.toFixed(8).replace(/\.?0+$/, ''); }; @@ -583,7 +583,7 @@ export default function Home() {
预估 Gas 费 - ~{formatGasFee(preparedTx.gasLimit, preparedTx.maxFeePerGas)} KAVA + ~{formatGasFee(preparedTx.gasLimit, preparedTx.gasPrice)} KAVA
diff --git a/backend/mpc-system/services/service-party-app/src/utils/transaction.ts b/backend/mpc-system/services/service-party-app/src/utils/transaction.ts index d11483e9..f6b301cb 100644 --- a/backend/mpc-system/services/service-party-app/src/utils/transaction.ts +++ b/backend/mpc-system/services/service-party-app/src/utils/transaction.ts @@ -335,33 +335,37 @@ export async function getNonce(address: string): Promise { /** * 通过 RPC 获取当前 gas 价格 + * 使用 eth_gasPrice 方法获取 Legacy 交易的 gasPrice */ export async function getGasPrice(): Promise<{ maxFeePerGas: bigint; maxPriorityFeePerGas: bigint }> { const rpcUrl = getCurrentRpcUrl(); - // 获取基础费用 - const blockResponse = await fetch(rpcUrl, { + // 使用 eth_gasPrice 获取建议的 gas 价格 + const response = await fetch(rpcUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ jsonrpc: '2.0', - method: 'eth_getBlockByNumber', - params: ['latest', false], + method: 'eth_gasPrice', + params: [], id: 1, }), }); - const blockData = await blockResponse.json(); - const baseFeePerGas = blockData.result?.baseFeePerGas - ? BigInt(blockData.result.baseFeePerGas) - : BigInt(1000000000); // 默认 1 gwei + const data = await response.json(); + let gasPrice: bigint; - // 优先费用设为 1 gwei - const maxPriorityFeePerGas = BigInt(1000000000); - // 最大费用 = 基础费用 * 2 + 优先费用 - const maxFeePerGas = baseFeePerGas * 2n + maxPriorityFeePerGas; + if (data.result) { + gasPrice = BigInt(data.result); + // 增加 10% 以确保交易能被打包 + gasPrice = gasPrice * 110n / 100n; + } else { + // 默认 1 gwei + gasPrice = BigInt(1000000000); + } - return { maxFeePerGas, maxPriorityFeePerGas }; + // 为了兼容性,同时返回 maxFeePerGas (实际上用作 gasPrice) + return { maxFeePerGas: gasPrice, maxPriorityFeePerGas: gasPrice }; } /**