rocketh 0.17.13 → 0.17.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,120 @@
1
+ import {EIP1193BlockTag, EIP1193FeeHistory, EIP1193ProviderWithoutEvents, EIP1193QUANTITY} from 'eip-1193';
2
+ import {logger} from '../internal/logging.js';
3
+
4
+ function avg(arr: bigint[]) {
5
+ const sum = arr.reduce((a: bigint, v: bigint) => a + v);
6
+ return sum / BigInt(arr.length);
7
+ }
8
+
9
+ export type EstimateGasPriceOptions = {
10
+ blockCount: number;
11
+ newestBlock: EIP1193BlockTag;
12
+ rewardPercentiles: number[];
13
+ };
14
+
15
+ export type RoughEstimateGasPriceOptions = {
16
+ blockCount: number;
17
+ newestBlock: EIP1193BlockTag;
18
+ rewardPercentiles: [number, number, number];
19
+ };
20
+
21
+ export type GasPrice = {maxFeePerGas: bigint; maxPriorityFeePerGas: bigint};
22
+ export type EstimateGasPriceResult = GasPrice[];
23
+ export type RoughEstimateGasPriceResult = {slow: GasPrice; average: GasPrice; fast: GasPrice};
24
+
25
+ export async function getGasPriceEstimate(
26
+ provider: EIP1193ProviderWithoutEvents,
27
+ options?: Partial<EstimateGasPriceOptions>,
28
+ ): Promise<EstimateGasPriceResult> {
29
+ const defaultOptions: EstimateGasPriceOptions = {
30
+ blockCount: 20,
31
+ newestBlock: 'pending',
32
+ rewardPercentiles: [10, 50, 80],
33
+ };
34
+ const optionsResolved = options ? {...defaultOptions, ...options} : defaultOptions;
35
+
36
+ const historicalBlocks = `0x${optionsResolved.blockCount.toString(16)}`;
37
+
38
+ let rawFeeHistory: EIP1193FeeHistory;
39
+ try {
40
+ rawFeeHistory = await provider.request({
41
+ method: 'eth_feeHistory',
42
+ params: [historicalBlocks as EIP1193QUANTITY, optionsResolved.newestBlock, optionsResolved.rewardPercentiles],
43
+ });
44
+ } catch (err: any) {
45
+ const message = 'details' in err && err.details ? err.details : err.message;
46
+ if (
47
+ message &&
48
+ (message.indexOf('unknown method eth_feeHistory') != -1 ||
49
+ message.indexOf('The method "eth_feeHistory" does not exist') != -1)
50
+ ) {
51
+ logger.warn(`eth_feeHistory not implemeted by node, falling back on "eth_gasPrice"`);
52
+ const gasPrice = await provider.request({method: 'eth_gasPrice'});
53
+ const result: EstimateGasPriceResult = [];
54
+ for (let i = 0; i < optionsResolved.rewardPercentiles.length; i++) {
55
+ result.push({
56
+ maxFeePerGas: BigInt(gasPrice),
57
+ maxPriorityFeePerGas: BigInt(gasPrice),
58
+ });
59
+ }
60
+ return result;
61
+ } else {
62
+ throw err;
63
+ }
64
+ }
65
+
66
+ let blockNum = Number(rawFeeHistory.oldestBlock);
67
+ const lastBlock = blockNum + rawFeeHistory.reward.length;
68
+ let index = 0;
69
+ const blocksHistory: {number: number; baseFeePerGas: bigint; gasUsedRatio: number; priorityFeePerGas: bigint[]}[] =
70
+ [];
71
+ while (blockNum < lastBlock) {
72
+ blocksHistory.push({
73
+ number: blockNum,
74
+ baseFeePerGas: BigInt(rawFeeHistory.baseFeePerGas[index]),
75
+ gasUsedRatio: Number(rawFeeHistory.gasUsedRatio[index]),
76
+ priorityFeePerGas: rawFeeHistory.reward[index].map((x) => BigInt(x)),
77
+ });
78
+ blockNum += 1;
79
+ index += 1;
80
+ }
81
+
82
+ const percentilePriorityFeeAverages: bigint[] = [];
83
+ for (let i = 0; i < optionsResolved.rewardPercentiles.length; i++) {
84
+ percentilePriorityFeeAverages.push(avg(blocksHistory.map((b) => b.priorityFeePerGas[i])));
85
+ }
86
+
87
+ const baseFeePerGas = BigInt(rawFeeHistory.baseFeePerGas[rawFeeHistory.baseFeePerGas.length - 1]);
88
+
89
+ const result: EstimateGasPriceResult = [];
90
+ for (let i = 0; i < optionsResolved.rewardPercentiles.length; i++) {
91
+ result.push({
92
+ maxFeePerGas: percentilePriorityFeeAverages[i] + baseFeePerGas,
93
+ maxPriorityFeePerGas: percentilePriorityFeeAverages[i],
94
+ });
95
+ }
96
+ return result;
97
+ }
98
+
99
+ export async function getRoughGasPriceEstimate(
100
+ provider: EIP1193ProviderWithoutEvents,
101
+ options?: Partial<RoughEstimateGasPriceOptions>,
102
+ ): Promise<RoughEstimateGasPriceResult> {
103
+ const defaultOptions: EstimateGasPriceOptions = {
104
+ blockCount: 20,
105
+ newestBlock: 'pending',
106
+ rewardPercentiles: [10, 50, 80],
107
+ };
108
+ const optionsResolved = options ? {...defaultOptions, ...options} : defaultOptions;
109
+
110
+ if (optionsResolved.rewardPercentiles.length !== 3) {
111
+ throw new Error(`rough gas estimate require 3 percentile, it defaults to [10,50,80]`);
112
+ }
113
+
114
+ const result = await getGasPriceEstimate(provider, optionsResolved);
115
+ return {
116
+ slow: result[0],
117
+ average: result[1],
118
+ fast: result[2],
119
+ };
120
+ }