Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add fallback method for EIP-1559 maxPriorityFee #6393

Merged
merged 1 commit into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 4 additions & 14 deletions packages/kit-bg/src/vaults/impls/evm/sdkEvm/ClientEvm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,10 @@ export class ClientEvm extends JsonRPCRequest {
baseFeePerGas: string;
}>('eth_getBlockByNumber', ['latest', false]);
const baseFeePerGas = new BigNumber(hexBlock.baseFeePerGas);

// 0 also means not 1559
if (baseFeePerGas.isNaN() || baseFeePerGas.isEqualTo(0)) {
return { isEIP1559FeeEnabled: false };
}

let maxPriorityFeeSupported = true;
try {
await this.call('eth_maxPriorityFeePerGas');
} catch (error) {
maxPriorityFeeSupported = false;
}

return { isEIP1559FeeEnabled: maxPriorityFeeSupported };
const isEIP1559FeeEnabled = !(
baseFeePerGas.isNaN() || baseFeePerGas.isEqualTo(0)
); // 0 also means not 1559
return { isEIP1559FeeEnabled };
}

async broadcastTransaction(rawTx: string): Promise<string> {
Expand Down
71 changes: 65 additions & 6 deletions packages/kit-bg/src/vaults/impls/evm/sdkEvm/EvmApiProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,15 +413,28 @@ class EvmApiProvider extends BaseApiProvider {
let maxPriorityFeePerGas: string;
let baseFeePerGas: BigNumber;
try {
const [maxPriorityFeePerGasResult, hexBlock] =
await this.client.batchCall<[string, any]>([
['eth_maxPriorityFeePerGas', []],
['eth_getBlockByNumber', ['latest', false]],
]);
const hexBlock = await this.client.call<any>('eth_getBlockByNumber', [
'latest',
false,
]);
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
baseFeePerGas = new B(hexBlock.baseFeePerGas);
isEIP1559 = !(baseFeePerGas.isNaN() || baseFeePerGas.isEqualTo(0)); // 0 also means not 1559
maxPriorityFeePerGas = maxPriorityFeePerGasResult;
if (isEIP1559) {
try {
maxPriorityFeePerGas = await this.client.call<string>(
'eth_maxPriorityFeePerGas',
[],
);
} catch (error) {
// if native method is not available, use fallback estimate method
maxPriorityFeePerGas =
await this.fallbackEstimateMaxPriorityFeePerGas();
console.log(
`[ProviderEVMFork.getGasPriceEIP1559] fallbackEstimateMaxPriorityFeePerGas: ${maxPriorityFeePerGas}`,
);
}
}
} catch (error) {
console.error(
`[ProviderEVMFork.getGasPriceEIP1559] get maxPriorityFeePerGas error: ${
Expand Down Expand Up @@ -462,6 +475,52 @@ class EvmApiProvider extends BaseApiProvider {
};
}

protected async fallbackEstimateMaxPriorityFeePerGas(): Promise<string> {
const BLOCK_COUNT = 3;
const PRIORITY_FEE_RATIO = 0.25; // use 25% of base fee as priority fee

try {
// get latest block
const latestBlock = await this.client.call<any>('eth_getBlockByNumber', [
'latest',
false,
]);
const latestNumber = parseInt(latestBlock.number, 16);

// get recent blocks
const blockPromises = Array.from({ length: BLOCK_COUNT }, (_, i) => {
const blockNumber = `0x${(latestNumber - i).toString(16)}`;
return this.client.call<any>('eth_getBlockByNumber', [
blockNumber,
false,
]);
});

const blocks = await Promise.all(blockPromises);

// calculate max base fee
const baseFees = blocks
.map((block) => new B(block.baseFeePerGas))
.filter((fee) => !fee.isNaN());

if (baseFees.length === 0) {
throw new Error('No valid base fees found in recent blocks');
}

const maxBaseFee = B.max(...baseFees);

// calculate suggested priority fee
return maxBaseFee.multipliedBy(PRIORITY_FEE_RATIO).toFixed(0);
} catch (error) {
console.error(
`[ProviderEVMFork.estimateMaxPriorityFeePerGas] error: ${
(error as Error).message
}`,
);
throw error;
}
}

originalix marked this conversation as resolved.
Show resolved Hide resolved
override async getGasFee(
params: IServerGasFeeParams,
): Promise<IServerGasFeeResponse> {
Expand Down
Loading