fix(transaction): use eth_gasPrice RPC for Legacy transaction gas estimation
- 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 <noreply@anthropic.com>
This commit is contained in:
parent
0f8e9cf228
commit
c0229a1139
|
|
@ -317,8 +317,8 @@ export default function Home() {
|
||||||
};
|
};
|
||||||
|
|
||||||
// 格式化 gas 费用显示
|
// 格式化 gas 费用显示
|
||||||
const formatGasFee = (gasLimit: bigint, maxFeePerGas: bigint): string => {
|
const formatGasFee = (gasLimit: bigint, gasPrice: bigint): string => {
|
||||||
const maxFee = gasLimit * maxFeePerGas;
|
const maxFee = gasLimit * gasPrice;
|
||||||
const feeKava = Number(maxFee) / 1e18;
|
const feeKava = Number(maxFee) / 1e18;
|
||||||
return feeKava.toFixed(8).replace(/\.?0+$/, '');
|
return feeKava.toFixed(8).replace(/\.?0+$/, '');
|
||||||
};
|
};
|
||||||
|
|
@ -583,7 +583,7 @@ export default function Home() {
|
||||||
<div className={styles.confirmRow}>
|
<div className={styles.confirmRow}>
|
||||||
<span className={styles.confirmLabel}>预估 Gas 费</span>
|
<span className={styles.confirmLabel}>预估 Gas 费</span>
|
||||||
<span className={styles.confirmValue}>
|
<span className={styles.confirmValue}>
|
||||||
~{formatGasFee(preparedTx.gasLimit, preparedTx.maxFeePerGas)} KAVA
|
~{formatGasFee(preparedTx.gasLimit, preparedTx.gasPrice)} KAVA
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<div className={styles.confirmRow}>
|
<div className={styles.confirmRow}>
|
||||||
|
|
|
||||||
|
|
@ -335,33 +335,37 @@ export async function getNonce(address: string): Promise<number> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过 RPC 获取当前 gas 价格
|
* 通过 RPC 获取当前 gas 价格
|
||||||
|
* 使用 eth_gasPrice 方法获取 Legacy 交易的 gasPrice
|
||||||
*/
|
*/
|
||||||
export async function getGasPrice(): Promise<{ maxFeePerGas: bigint; maxPriorityFeePerGas: bigint }> {
|
export async function getGasPrice(): Promise<{ maxFeePerGas: bigint; maxPriorityFeePerGas: bigint }> {
|
||||||
const rpcUrl = getCurrentRpcUrl();
|
const rpcUrl = getCurrentRpcUrl();
|
||||||
|
|
||||||
// 获取基础费用
|
// 使用 eth_gasPrice 获取建议的 gas 价格
|
||||||
const blockResponse = await fetch(rpcUrl, {
|
const response = await fetch(rpcUrl, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
jsonrpc: '2.0',
|
jsonrpc: '2.0',
|
||||||
method: 'eth_getBlockByNumber',
|
method: 'eth_gasPrice',
|
||||||
params: ['latest', false],
|
params: [],
|
||||||
id: 1,
|
id: 1,
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
const blockData = await blockResponse.json();
|
const data = await response.json();
|
||||||
const baseFeePerGas = blockData.result?.baseFeePerGas
|
let gasPrice: bigint;
|
||||||
? BigInt(blockData.result.baseFeePerGas)
|
|
||||||
: BigInt(1000000000); // 默认 1 gwei
|
|
||||||
|
|
||||||
// 优先费用设为 1 gwei
|
if (data.result) {
|
||||||
const maxPriorityFeePerGas = BigInt(1000000000);
|
gasPrice = BigInt(data.result);
|
||||||
// 最大费用 = 基础费用 * 2 + 优先费用
|
// 增加 10% 以确保交易能被打包
|
||||||
const maxFeePerGas = baseFeePerGas * 2n + maxPriorityFeePerGas;
|
gasPrice = gasPrice * 110n / 100n;
|
||||||
|
} else {
|
||||||
|
// 默认 1 gwei
|
||||||
|
gasPrice = BigInt(1000000000);
|
||||||
|
}
|
||||||
|
|
||||||
return { maxFeePerGas, maxPriorityFeePerGas };
|
// 为了兼容性,同时返回 maxFeePerGas (实际上用作 gasPrice)
|
||||||
|
return { maxFeePerGas: gasPrice, maxPriorityFeePerGas: gasPrice };
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue