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 费用显示
|
||||
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() {
|
|||
<div className={styles.confirmRow}>
|
||||
<span className={styles.confirmLabel}>预估 Gas 费</span>
|
||||
<span className={styles.confirmValue}>
|
||||
~{formatGasFee(preparedTx.gasLimit, preparedTx.maxFeePerGas)} KAVA
|
||||
~{formatGasFee(preparedTx.gasLimit, preparedTx.gasPrice)} KAVA
|
||||
</span>
|
||||
</div>
|
||||
<div className={styles.confirmRow}>
|
||||
|
|
|
|||
|
|
@ -335,33 +335,37 @@ export async function getNonce(address: string): Promise<number> {
|
|||
|
||||
/**
|
||||
* 通过 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 };
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in New Issue