@metamask-previews/gas-fee-controller 13.0.2-preview.e5c396b → 14.0.1-preview-d4ae797
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.
- package/CHANGELOG.md +24 -1
- package/dist/GasFeeController.js +19 -300
- package/dist/GasFeeController.js.map +1 -1
- package/dist/GasFeeController.mjs +20 -0
- package/dist/GasFeeController.mjs.map +1 -0
- package/dist/chunk-4T54ULFA.mjs +367 -0
- package/dist/chunk-4T54ULFA.mjs.map +1 -0
- package/dist/chunk-5INBFZXY.js +127 -0
- package/dist/chunk-5INBFZXY.js.map +1 -0
- package/dist/chunk-AQN4AQEF.mjs +127 -0
- package/dist/chunk-AQN4AQEF.mjs.map +1 -0
- package/dist/chunk-C4I7KKIU.mjs +12 -0
- package/dist/chunk-C4I7KKIU.mjs.map +1 -0
- package/dist/chunk-CCRUODGE.mjs +129 -0
- package/dist/chunk-CCRUODGE.mjs.map +1 -0
- package/dist/chunk-EXCWMMNV.mjs +39 -0
- package/dist/chunk-EXCWMMNV.mjs.map +1 -0
- package/dist/chunk-EZVGDV5H.js +39 -0
- package/dist/chunk-EZVGDV5H.js.map +1 -0
- package/dist/chunk-F46NZXRQ.js +129 -0
- package/dist/chunk-F46NZXRQ.js.map +1 -0
- package/dist/chunk-GE7RMDFV.js +77 -0
- package/dist/chunk-GE7RMDFV.js.map +1 -0
- package/dist/chunk-GUWY6WP6.mjs +77 -0
- package/dist/chunk-GUWY6WP6.mjs.map +1 -0
- package/dist/chunk-LO7OP5FM.js +19 -0
- package/dist/chunk-LO7OP5FM.js.map +1 -0
- package/dist/chunk-N5BANBTW.js +367 -0
- package/dist/chunk-N5BANBTW.js.map +1 -0
- package/dist/chunk-S2EH2EPS.js +12 -0
- package/dist/chunk-S2EH2EPS.js.map +1 -0
- package/dist/chunk-SINQOHIN.mjs +19 -0
- package/dist/chunk-SINQOHIN.mjs.map +1 -0
- package/dist/chunk-XUI43LEZ.mjs +30 -0
- package/dist/chunk-XUI43LEZ.mjs.map +1 -0
- package/dist/chunk-Z4BLTVTB.js +30 -0
- package/dist/chunk-Z4BLTVTB.js.map +1 -0
- package/dist/determineGasFeeCalculations.js +13 -86
- package/dist/determineGasFeeCalculations.js.map +1 -1
- package/dist/determineGasFeeCalculations.mjs +14 -0
- package/dist/determineGasFeeCalculations.mjs.map +1 -0
- package/dist/fetchBlockFeeHistory.js +7 -204
- package/dist/fetchBlockFeeHistory.js.map +1 -1
- package/dist/fetchBlockFeeHistory.mjs +8 -0
- package/dist/fetchBlockFeeHistory.mjs.map +1 -0
- package/dist/fetchGasEstimatesViaEthFeeHistory/calculateGasFeeEstimatesForPriorityLevels.js +8 -88
- package/dist/fetchGasEstimatesViaEthFeeHistory/calculateGasFeeEstimatesForPriorityLevels.js.map +1 -1
- package/dist/fetchGasEstimatesViaEthFeeHistory/calculateGasFeeEstimatesForPriorityLevels.mjs +9 -0
- package/dist/fetchGasEstimatesViaEthFeeHistory/calculateGasFeeEstimatesForPriorityLevels.mjs.map +1 -0
- package/dist/fetchGasEstimatesViaEthFeeHistory/fetchLatestBlock.js +7 -31
- package/dist/fetchGasEstimatesViaEthFeeHistory/fetchLatestBlock.js.map +1 -1
- package/dist/fetchGasEstimatesViaEthFeeHistory/fetchLatestBlock.mjs +8 -0
- package/dist/fetchGasEstimatesViaEthFeeHistory/fetchLatestBlock.mjs.map +1 -0
- package/dist/fetchGasEstimatesViaEthFeeHistory/medianOf.js +7 -16
- package/dist/fetchGasEstimatesViaEthFeeHistory/medianOf.js.map +1 -1
- package/dist/fetchGasEstimatesViaEthFeeHistory/medianOf.mjs +8 -0
- package/dist/fetchGasEstimatesViaEthFeeHistory/medianOf.mjs.map +1 -0
- package/dist/fetchGasEstimatesViaEthFeeHistory/types.js +1 -3
- package/dist/fetchGasEstimatesViaEthFeeHistory/types.js.map +1 -1
- package/dist/fetchGasEstimatesViaEthFeeHistory/types.mjs +1 -0
- package/dist/fetchGasEstimatesViaEthFeeHistory/types.mjs.map +1 -0
- package/dist/fetchGasEstimatesViaEthFeeHistory.js +11 -52
- package/dist/fetchGasEstimatesViaEthFeeHistory.js.map +1 -1
- package/dist/fetchGasEstimatesViaEthFeeHistory.mjs +12 -0
- package/dist/fetchGasEstimatesViaEthFeeHistory.mjs.map +1 -0
- package/dist/gas-util.js +15 -142
- package/dist/gas-util.js.map +1 -1
- package/dist/gas-util.mjs +16 -0
- package/dist/gas-util.mjs.map +1 -0
- package/dist/index.js +17 -17
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +18 -0
- package/dist/index.mjs.map +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/dist/types/GasFeeController.d.ts.map +1 -0
- package/dist/types/determineGasFeeCalculations.d.ts.map +1 -0
- package/dist/types/fetchBlockFeeHistory.d.ts.map +1 -0
- package/dist/types/fetchGasEstimatesViaEthFeeHistory/calculateGasFeeEstimatesForPriorityLevels.d.ts.map +1 -0
- package/dist/types/fetchGasEstimatesViaEthFeeHistory/fetchLatestBlock.d.ts.map +1 -0
- package/dist/types/fetchGasEstimatesViaEthFeeHistory/medianOf.d.ts.map +1 -0
- package/dist/types/fetchGasEstimatesViaEthFeeHistory/types.d.ts.map +1 -0
- package/dist/types/fetchGasEstimatesViaEthFeeHistory.d.ts.map +1 -0
- package/dist/types/gas-util.d.ts.map +1 -0
- package/dist/{index.d.ts.map → types/index.d.ts.map} +1 -1
- package/package.json +17 -7
- package/dist/GasFeeController.d.ts.map +0 -1
- package/dist/determineGasFeeCalculations.d.ts.map +0 -1
- package/dist/fetchBlockFeeHistory.d.ts.map +0 -1
- package/dist/fetchGasEstimatesViaEthFeeHistory/calculateGasFeeEstimatesForPriorityLevels.d.ts.map +0 -1
- package/dist/fetchGasEstimatesViaEthFeeHistory/fetchLatestBlock.d.ts.map +0 -1
- package/dist/fetchGasEstimatesViaEthFeeHistory/medianOf.d.ts.map +0 -1
- package/dist/fetchGasEstimatesViaEthFeeHistory/types.d.ts.map +0 -1
- package/dist/fetchGasEstimatesViaEthFeeHistory.d.ts.map +0 -1
- package/dist/gas-util.d.ts.map +0 -1
- /package/dist/{GasFeeController.d.ts → types/GasFeeController.d.ts} +0 -0
- /package/dist/{determineGasFeeCalculations.d.ts → types/determineGasFeeCalculations.d.ts} +0 -0
- /package/dist/{fetchBlockFeeHistory.d.ts → types/fetchBlockFeeHistory.d.ts} +0 -0
- /package/dist/{fetchGasEstimatesViaEthFeeHistory → types/fetchGasEstimatesViaEthFeeHistory}/calculateGasFeeEstimatesForPriorityLevels.d.ts +0 -0
- /package/dist/{fetchGasEstimatesViaEthFeeHistory → types/fetchGasEstimatesViaEthFeeHistory}/fetchLatestBlock.d.ts +0 -0
- /package/dist/{fetchGasEstimatesViaEthFeeHistory → types/fetchGasEstimatesViaEthFeeHistory}/medianOf.d.ts +0 -0
- /package/dist/{fetchGasEstimatesViaEthFeeHistory → types/fetchGasEstimatesViaEthFeeHistory}/types.d.ts +0 -0
- /package/dist/{fetchGasEstimatesViaEthFeeHistory.d.ts → types/fetchGasEstimatesViaEthFeeHistory.d.ts} +0 -0
- /package/dist/{gas-util.d.ts → types/gas-util.d.ts} +0 -0
- /package/dist/{index.d.ts → types/index.d.ts} +0 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});var __accessCheck = (obj, member, msg) => {
|
|
2
|
+
if (!member.has(obj))
|
|
3
|
+
throw TypeError("Cannot " + msg);
|
|
4
|
+
};
|
|
5
|
+
var __privateGet = (obj, member, getter) => {
|
|
6
|
+
__accessCheck(obj, member, "read from private field");
|
|
7
|
+
return getter ? getter.call(obj) : member.get(obj);
|
|
8
|
+
};
|
|
9
|
+
var __privateAdd = (obj, member, value) => {
|
|
10
|
+
if (member.has(obj))
|
|
11
|
+
throw TypeError("Cannot add the same private member more than once");
|
|
12
|
+
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
13
|
+
};
|
|
14
|
+
var __privateSet = (obj, member, value, setter) => {
|
|
15
|
+
__accessCheck(obj, member, "write to private field");
|
|
16
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
17
|
+
return value;
|
|
18
|
+
};
|
|
19
|
+
var __privateMethod = (obj, member, method) => {
|
|
20
|
+
__accessCheck(obj, member, "access private method");
|
|
21
|
+
return method;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
exports.__privateGet = __privateGet; exports.__privateAdd = __privateAdd; exports.__privateSet = __privateSet; exports.__privateMethod = __privateMethod;
|
|
30
|
+
//# sourceMappingURL=chunk-Z4BLTVTB.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":""}
|
|
@@ -1,87 +1,14 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
* Obtains a set of max base and priority fee estimates along with time estimates so that we
|
|
15
|
-
* can present them to users when they are sending transactions or making swaps.
|
|
16
|
-
*
|
|
17
|
-
* @param args - The arguments.
|
|
18
|
-
* @param args.isEIP1559Compatible - Governs whether or not we can use an EIP-1559-only method to
|
|
19
|
-
* produce estimates.
|
|
20
|
-
* @param args.isLegacyGasAPICompatible - Governs whether or not we can use a non-EIP-1559 method to
|
|
21
|
-
* produce estimates (for instance, testnets do not support estimates altogether).
|
|
22
|
-
* @param args.fetchGasEstimates - A function that fetches gas estimates using an EIP-1559-specific
|
|
23
|
-
* API.
|
|
24
|
-
* @param args.fetchGasEstimatesUrl - The URL for the API we can use to obtain EIP-1559-specific
|
|
25
|
-
* estimates.
|
|
26
|
-
* @param args.fetchGasEstimatesViaEthFeeHistory - A function that fetches gas estimates using
|
|
27
|
-
* `eth_feeHistory` (an EIP-1559 feature).
|
|
28
|
-
* @param args.fetchLegacyGasPriceEstimates - A function that fetches gas estimates using an
|
|
29
|
-
* non-EIP-1559-specific API.
|
|
30
|
-
* @param args.fetchLegacyGasPriceEstimatesUrl - The URL for the API we can use to obtain
|
|
31
|
-
* non-EIP-1559-specific estimates.
|
|
32
|
-
* @param args.fetchEthGasPriceEstimate - A function that fetches gas estimates using
|
|
33
|
-
* `eth_gasPrice`.
|
|
34
|
-
* @param args.calculateTimeEstimate - A function that determine time estimate bounds.
|
|
35
|
-
* @param args.clientId - An identifier that an API can use to know who is asking for estimates.
|
|
36
|
-
* @param args.ethQuery - An EthQuery instance we can use to talk to Ethereum directly.
|
|
37
|
-
* @returns The gas fee calculations.
|
|
38
|
-
*/
|
|
39
|
-
function determineGasFeeCalculations({ isEIP1559Compatible, isLegacyGasAPICompatible, fetchGasEstimates, fetchGasEstimatesUrl, fetchGasEstimatesViaEthFeeHistory, fetchLegacyGasPriceEstimates, fetchLegacyGasPriceEstimatesUrl, fetchEthGasPriceEstimate, calculateTimeEstimate, clientId, ethQuery, }) {
|
|
40
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
41
|
-
try {
|
|
42
|
-
if (isEIP1559Compatible) {
|
|
43
|
-
let estimates;
|
|
44
|
-
try {
|
|
45
|
-
estimates = yield fetchGasEstimates(fetchGasEstimatesUrl, clientId);
|
|
46
|
-
}
|
|
47
|
-
catch (_a) {
|
|
48
|
-
estimates = yield fetchGasEstimatesViaEthFeeHistory(ethQuery);
|
|
49
|
-
}
|
|
50
|
-
const { suggestedMaxPriorityFeePerGas, suggestedMaxFeePerGas } = estimates.medium;
|
|
51
|
-
const estimatedGasFeeTimeBounds = calculateTimeEstimate(suggestedMaxPriorityFeePerGas, suggestedMaxFeePerGas, estimates);
|
|
52
|
-
return {
|
|
53
|
-
gasFeeEstimates: estimates,
|
|
54
|
-
estimatedGasFeeTimeBounds,
|
|
55
|
-
gasEstimateType: GasFeeController_1.GAS_ESTIMATE_TYPES.FEE_MARKET,
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
else if (isLegacyGasAPICompatible) {
|
|
59
|
-
const estimates = yield fetchLegacyGasPriceEstimates(fetchLegacyGasPriceEstimatesUrl, clientId);
|
|
60
|
-
return {
|
|
61
|
-
gasFeeEstimates: estimates,
|
|
62
|
-
estimatedGasFeeTimeBounds: {},
|
|
63
|
-
gasEstimateType: GasFeeController_1.GAS_ESTIMATE_TYPES.LEGACY,
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
throw new Error('Main gas fee/price estimation failed. Use fallback');
|
|
67
|
-
}
|
|
68
|
-
catch (_b) {
|
|
69
|
-
try {
|
|
70
|
-
const estimates = yield fetchEthGasPriceEstimate(ethQuery);
|
|
71
|
-
return {
|
|
72
|
-
gasFeeEstimates: estimates,
|
|
73
|
-
estimatedGasFeeTimeBounds: {},
|
|
74
|
-
gasEstimateType: GasFeeController_1.GAS_ESTIMATE_TYPES.ETH_GASPRICE,
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
catch (error) {
|
|
78
|
-
if (error instanceof Error) {
|
|
79
|
-
throw new Error(`Gas fee/price estimation failed. Message: ${error.message}`);
|
|
80
|
-
}
|
|
81
|
-
throw error;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
exports.default = determineGasFeeCalculations;
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
+
|
|
3
|
+
var _chunkN5BANBTWjs = require('./chunk-N5BANBTW.js');
|
|
4
|
+
require('./chunk-EZVGDV5H.js');
|
|
5
|
+
require('./chunk-5INBFZXY.js');
|
|
6
|
+
require('./chunk-F46NZXRQ.js');
|
|
7
|
+
require('./chunk-GE7RMDFV.js');
|
|
8
|
+
require('./chunk-S2EH2EPS.js');
|
|
9
|
+
require('./chunk-LO7OP5FM.js');
|
|
10
|
+
require('./chunk-Z4BLTVTB.js');
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
exports.default = _chunkN5BANBTWjs.determineGasFeeCalculations;
|
|
87
14
|
//# sourceMappingURL=determineGasFeeCalculations.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":""}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import {
|
|
2
|
+
determineGasFeeCalculations
|
|
3
|
+
} from "./chunk-4T54ULFA.mjs";
|
|
4
|
+
import "./chunk-EXCWMMNV.mjs";
|
|
5
|
+
import "./chunk-AQN4AQEF.mjs";
|
|
6
|
+
import "./chunk-CCRUODGE.mjs";
|
|
7
|
+
import "./chunk-GUWY6WP6.mjs";
|
|
8
|
+
import "./chunk-C4I7KKIU.mjs";
|
|
9
|
+
import "./chunk-SINQOHIN.mjs";
|
|
10
|
+
import "./chunk-XUI43LEZ.mjs";
|
|
11
|
+
export {
|
|
12
|
+
determineGasFeeCalculations as default
|
|
13
|
+
};
|
|
14
|
+
//# sourceMappingURL=determineGasFeeCalculations.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -1,205 +1,8 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
const controller_utils_1 = require("@metamask/controller-utils");
|
|
16
|
-
const bn_js_1 = __importDefault(require("bn.js"));
|
|
17
|
-
const MAX_NUMBER_OF_BLOCKS_PER_ETH_FEE_HISTORY_CALL = 1024;
|
|
18
|
-
/**
|
|
19
|
-
* Uses `eth_feeHistory` (an EIP-1559 feature) to obtain information about gas fees from a range of
|
|
20
|
-
* blocks that have occurred recently on a network.
|
|
21
|
-
*
|
|
22
|
-
* To learn more, see these resources:
|
|
23
|
-
*
|
|
24
|
-
* - <https://infura.io/docs/ethereum#operation/eth_feeHistory>
|
|
25
|
-
* - <https://github.com/zsfelfoldi/feehistory/blob/main/docs/feeHistory.md>
|
|
26
|
-
* - <https://github.com/ethereum/go-ethereum/blob/57a3fab8a75eeb9c2f4fab770b73b51b9fe672c5/eth/gasprice/feehistory.go#L180>
|
|
27
|
-
* - <https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1559.md>
|
|
28
|
-
* - <https://gas-api.metaswap.codefi.network/testFeeHistory>
|
|
29
|
-
*
|
|
30
|
-
* @param args - The arguments to this function.
|
|
31
|
-
* @param args.ethQuery - An EthQuery instance that wraps a provider for the network in question.
|
|
32
|
-
* @param args.endBlock - The desired end of the requested block range. Can be "latest" if you want
|
|
33
|
-
* to start from the latest successful block or the number of a known past block.
|
|
34
|
-
* @param args.numberOfBlocks - How many total blocks to fetch. Note that if this is more than 1024,
|
|
35
|
-
* multiple calls to `eth_feeHistory` will be made.
|
|
36
|
-
* @param args.percentiles - A set of numbers between 1 and 100 which will dictate how
|
|
37
|
-
* `priorityFeesByPercentile` in each returned block will be formed. When Ethereum runs the
|
|
38
|
-
* `eth_feeHistory` method, for each block it is considering, it will first sort all transactions by
|
|
39
|
-
* the priority fee. It will then go through each transaction and add the total amount of gas paid
|
|
40
|
-
* for that transaction to a bucket which maxes out at the total gas used for the whole block. As
|
|
41
|
-
* the bucket fills, it will cross percentages which correspond to the percentiles specified here,
|
|
42
|
-
* and the priority fees of the first transactions which cause it to reach those percentages will be
|
|
43
|
-
* recorded. Hence, `priorityFeesByPercentile` represents the priority fees of transactions at key
|
|
44
|
-
* gas used contribution levels, where earlier levels have smaller contributions and later levels
|
|
45
|
-
* have higher contributions.
|
|
46
|
-
* @param args.includeNextBlock - Whether to include an extra block that represents the next
|
|
47
|
-
* block after the latest one. Only the `baseFeePerGas` will be filled in for this block (which is
|
|
48
|
-
* estimated).
|
|
49
|
-
* @returns The list of blocks and their fee data, sorted from oldest to newest.
|
|
50
|
-
*/
|
|
51
|
-
function fetchBlockFeeHistory({ ethQuery, numberOfBlocks: totalNumberOfBlocks, endBlock: givenEndBlock = 'latest', percentiles: givenPercentiles = [], includeNextBlock = false, }) {
|
|
52
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
53
|
-
const percentiles = givenPercentiles.length > 0
|
|
54
|
-
? Array.from(new Set(givenPercentiles)).sort((a, b) => a - b)
|
|
55
|
-
: [];
|
|
56
|
-
const finalEndBlockNumber = givenEndBlock === 'latest'
|
|
57
|
-
? (0, controller_utils_1.fromHex)(yield (0, controller_utils_1.query)(ethQuery, 'blockNumber'))
|
|
58
|
-
: givenEndBlock;
|
|
59
|
-
const requestChunkSpecifiers = determineRequestChunkSpecifiers(finalEndBlockNumber, totalNumberOfBlocks);
|
|
60
|
-
const blockChunks = yield Promise.all(requestChunkSpecifiers.map(({ numberOfBlocks, endBlockNumber }, i) => {
|
|
61
|
-
return i === requestChunkSpecifiers.length - 1
|
|
62
|
-
? makeRequestForChunk({
|
|
63
|
-
ethQuery,
|
|
64
|
-
numberOfBlocks,
|
|
65
|
-
endBlockNumber,
|
|
66
|
-
percentiles,
|
|
67
|
-
includeNextBlock,
|
|
68
|
-
})
|
|
69
|
-
: makeRequestForChunk({
|
|
70
|
-
ethQuery,
|
|
71
|
-
numberOfBlocks,
|
|
72
|
-
endBlockNumber,
|
|
73
|
-
percentiles,
|
|
74
|
-
includeNextBlock: false,
|
|
75
|
-
});
|
|
76
|
-
}));
|
|
77
|
-
return blockChunks.reduce((array, blocks) => [...array, ...blocks], []);
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
exports.default = fetchBlockFeeHistory;
|
|
81
|
-
/**
|
|
82
|
-
* Builds an ExistingFeeHistoryBlock.
|
|
83
|
-
*
|
|
84
|
-
* @param args - The args to this function.
|
|
85
|
-
* @param args.number - The number of the block.
|
|
86
|
-
* @param args.baseFeePerGas - The base fee per gas of the block.
|
|
87
|
-
* @param args.blockIndex - The index of the block in the source chunk.
|
|
88
|
-
* @param args.gasUsedRatios - The gas used ratios for the block.
|
|
89
|
-
* @param args.priorityFeePercentileGroups - The priority fee percentile groups for the block.
|
|
90
|
-
* @param args.percentiles - The percentiles used to fetch the source chunk.
|
|
91
|
-
* @returns The ExistingFeeHistoryBlock.
|
|
92
|
-
*/
|
|
93
|
-
function buildExistingFeeHistoryBlock({ baseFeePerGas, number, blockIndex, gasUsedRatios, priorityFeePercentileGroups, percentiles, }) {
|
|
94
|
-
const gasUsedRatio = gasUsedRatios[blockIndex];
|
|
95
|
-
const priorityFeesForEachPercentile = priorityFeePercentileGroups[blockIndex];
|
|
96
|
-
const priorityFeesByPercentile = percentiles.reduce((obj, percentile, percentileIndex) => {
|
|
97
|
-
const priorityFee = priorityFeesForEachPercentile[percentileIndex];
|
|
98
|
-
return Object.assign(Object.assign({}, obj), { [percentile]: (0, controller_utils_1.fromHex)(priorityFee) });
|
|
99
|
-
}, {});
|
|
100
|
-
return {
|
|
101
|
-
number,
|
|
102
|
-
baseFeePerGas,
|
|
103
|
-
gasUsedRatio,
|
|
104
|
-
priorityFeesByPercentile,
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Builds a NextFeeHistoryBlock.
|
|
109
|
-
*
|
|
110
|
-
* @param args - The args to this function.
|
|
111
|
-
* @param args.baseFeePerGas - The base fee per gas of the block.
|
|
112
|
-
* @param args.number - The number of the block.
|
|
113
|
-
* @returns The NextFeeHistoryBlock.
|
|
114
|
-
*/
|
|
115
|
-
function buildNextFeeHistoryBlock({ baseFeePerGas, number, }) {
|
|
116
|
-
return {
|
|
117
|
-
number,
|
|
118
|
-
baseFeePerGas,
|
|
119
|
-
gasUsedRatio: null,
|
|
120
|
-
priorityFeesByPercentile: null,
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* Uses eth_feeHistory to request historical data about a group of blocks (max size 1024).
|
|
125
|
-
*
|
|
126
|
-
* @param args - The arguments
|
|
127
|
-
* @param args.ethQuery - An EthQuery instance.
|
|
128
|
-
* @param args.numberOfBlocks - The number of blocks in the chunk. Must be at most 1024, as this is
|
|
129
|
-
* the maximum that `eth_feeHistory` can return in one call.
|
|
130
|
-
* @param args.endBlockNumber - The end of the requested block range.
|
|
131
|
-
* @param args.percentiles - A set of numbers between 1 and 100 that will be used to pull priority
|
|
132
|
-
* fees for each block.
|
|
133
|
-
* @param args.includeNextBlock - Whether to include an extra block that represents the next
|
|
134
|
-
* block after the latest one. Only the `baseFeePerGas` will be filled in for this block (which is
|
|
135
|
-
* estimated).
|
|
136
|
-
* @returns A list of block data.
|
|
137
|
-
*/
|
|
138
|
-
function makeRequestForChunk({ ethQuery, numberOfBlocks, endBlockNumber, percentiles, includeNextBlock, }) {
|
|
139
|
-
var _a;
|
|
140
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
141
|
-
const response = yield (0, controller_utils_1.query)(ethQuery, 'eth_feeHistory', [(0, controller_utils_1.toHex)(numberOfBlocks), (0, controller_utils_1.toHex)(endBlockNumber), percentiles]);
|
|
142
|
-
const startBlockNumber = (0, controller_utils_1.fromHex)(response.oldestBlock);
|
|
143
|
-
if (response.baseFeePerGas !== undefined &&
|
|
144
|
-
response.baseFeePerGas.length > 0 &&
|
|
145
|
-
response.gasUsedRatio.length > 0 &&
|
|
146
|
-
(response.reward === undefined || response.reward.length > 0)) {
|
|
147
|
-
// Per
|
|
148
|
-
// <https://github.com/ethereum/go-ethereum/blob/57a3fab8a75eeb9c2f4fab770b73b51b9fe672c5/eth/gasprice/feehistory.go#L191-L192>,
|
|
149
|
-
// baseFeePerGas will always include an extra item which is the calculated base fee for the
|
|
150
|
-
// next (future) block. We may or may not care about this; if we don't, chop it off.
|
|
151
|
-
const baseFeesPerGasAsHex = includeNextBlock
|
|
152
|
-
? response.baseFeePerGas
|
|
153
|
-
: response.baseFeePerGas.slice(0, numberOfBlocks);
|
|
154
|
-
const gasUsedRatios = response.gasUsedRatio;
|
|
155
|
-
const priorityFeePercentileGroups = (_a = response.reward) !== null && _a !== void 0 ? _a : [];
|
|
156
|
-
// Chain is allowed to return fewer number of block results
|
|
157
|
-
const numberOfExistingResults = gasUsedRatios.length;
|
|
158
|
-
return baseFeesPerGasAsHex.map((baseFeePerGasAsHex, blockIndex) => {
|
|
159
|
-
const baseFeePerGas = (0, controller_utils_1.fromHex)(baseFeePerGasAsHex);
|
|
160
|
-
const number = startBlockNumber.addn(blockIndex);
|
|
161
|
-
return blockIndex >= numberOfExistingResults
|
|
162
|
-
? buildNextFeeHistoryBlock({ baseFeePerGas, number })
|
|
163
|
-
: buildExistingFeeHistoryBlock({
|
|
164
|
-
baseFeePerGas,
|
|
165
|
-
number,
|
|
166
|
-
blockIndex,
|
|
167
|
-
gasUsedRatios,
|
|
168
|
-
priorityFeePercentileGroups,
|
|
169
|
-
percentiles,
|
|
170
|
-
});
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
|
-
return [];
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
/**
|
|
177
|
-
* Divides a block range (specified by a range size and the end of the range) into chunks based on
|
|
178
|
-
* the maximum number of blocks that `eth_feeHistory` can return in a single call.
|
|
179
|
-
*
|
|
180
|
-
* If the requested totalNumberOfBlocks exceed endBlockNumber, totalNumberOfBlocks is
|
|
181
|
-
* truncated to avoid requesting chunks with negative endBlockNumber.
|
|
182
|
-
*
|
|
183
|
-
* @param endBlockNumber - The final block in the complete desired block range after all
|
|
184
|
-
* `eth_feeHistory` requests have been made.
|
|
185
|
-
* @param totalNumberOfBlocks - The total number of desired blocks after all `eth_feeHistory`
|
|
186
|
-
* requests have been made.
|
|
187
|
-
* @returns A set of arguments that can be used to make requests to `eth_feeHistory` in order to
|
|
188
|
-
* retrieve all of the requested blocks, sorted from oldest block to newest block.
|
|
189
|
-
*/
|
|
190
|
-
function determineRequestChunkSpecifiers(endBlockNumber, totalNumberOfBlocks) {
|
|
191
|
-
if (endBlockNumber.lt(new bn_js_1.default(totalNumberOfBlocks))) {
|
|
192
|
-
totalNumberOfBlocks = endBlockNumber.toNumber();
|
|
193
|
-
}
|
|
194
|
-
const specifiers = [];
|
|
195
|
-
for (let chunkStartBlockNumber = endBlockNumber.subn(totalNumberOfBlocks); chunkStartBlockNumber.lt(endBlockNumber); chunkStartBlockNumber = chunkStartBlockNumber.addn(MAX_NUMBER_OF_BLOCKS_PER_ETH_FEE_HISTORY_CALL)) {
|
|
196
|
-
const distanceToEnd = endBlockNumber.sub(chunkStartBlockNumber).toNumber();
|
|
197
|
-
const numberOfBlocks = distanceToEnd < MAX_NUMBER_OF_BLOCKS_PER_ETH_FEE_HISTORY_CALL
|
|
198
|
-
? distanceToEnd
|
|
199
|
-
: MAX_NUMBER_OF_BLOCKS_PER_ETH_FEE_HISTORY_CALL;
|
|
200
|
-
const chunkEndBlockNumber = chunkStartBlockNumber.addn(numberOfBlocks);
|
|
201
|
-
specifiers.push({ numberOfBlocks, endBlockNumber: chunkEndBlockNumber });
|
|
202
|
-
}
|
|
203
|
-
return specifiers;
|
|
204
|
-
}
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
+
|
|
3
|
+
var _chunk5INBFZXYjs = require('./chunk-5INBFZXY.js');
|
|
4
|
+
require('./chunk-Z4BLTVTB.js');
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
exports.default = _chunk5INBFZXYjs.fetchBlockFeeHistory;
|
|
205
8
|
//# sourceMappingURL=fetchBlockFeeHistory.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetchBlockFeeHistory.js","sourceRoot":"","sources":["../src/fetchBlockFeeHistory.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,iEAAmE;AACnE,kDAAuB;AA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AC,CAAC;SACJ;QAED,OAAO,EAAE,CAAC;;CACX;AAED;;;;;;;;;;;;;GAaG;AACH,SAAS,+BAA+B,CACtC,cAAkB,EAClB,mBAA2B;IAE3B,IAAI,cAAc,CAAC,EAAE,CAAC,IAAI,eAAE,CAAC,mBAAmB,CAAC,CAAC,EAAE;QAClD,mBAAmB,GAAG,cAAc,CAAC,QAAQ,EAAE,CAAC;KACjD;IAED,MAAM,UAAU,GAAG,EAAE,CAAC;IACtB,KACE,IAAI,qBAAqB,GAAG,cAAc,CAAC,IAAI,CAAC,mBAAmB,CAAC,EACpE,qBAAqB,CAAC,EAAE,CAAC,cAAc,CAAC,EACxC,qBAAqB,GAAG,qBAAqB,CAAC,IAAI,CAChD,6CAA6C,CAC9C,EACD;QACA,MAAM,aAAa,GAAG,cAAc,CAAC,GAAG,CAAC,qBAAqB,CAAC,CAAC,QAAQ,EAAE,CAAC;QAC3E,MAAM,cAAc,GAClB,aAAa,GAAG,6CAA6C;YAC3D,CAAC,CAAC,aAAa;YACf,CAAC,CAAC,6CAA6C,CAAC;QACpD,MAAM,mBAAmB,GAAG,qBAAqB,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC;QACvE,UAAU,CAAC,IAAI,CAAC,EAAE,cAAc,EAAE,cAAc,EAAE,mBAAmB,EAAE,CAAC,CAAC;KAC1E;IACD,OAAO,UAAU,CAAC;AACpB,CAAC","sourcesContent":["import { query, fromHex, toHex } from '@metamask/controller-utils';\nimport BN from 'bn.js';\n\n// TODO: Replace `any` with type\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype EthQuery = any;\n\n/**\n * @type RequestChunkSpecifier\n *\n * Arguments to `eth_feeHistory` that can be used to fetch a set of historical data.\n * @property blockCount - The number of blocks requested.\n * @property endBlockNumber - The number of the block at the end of the requested range.\n */\ntype RequestChunkSpecifier = {\n numberOfBlocks: number;\n endBlockNumber: BN;\n};\n\n/**\n * @type EthFeeHistoryResponse\n *\n * Response data for `eth_feeHistory`.\n * @property oldestBlock - The id of the oldest block (in hex format) in the range of blocks\n * requested.\n * @property baseFeePerGas - Base fee per gas for each block in the range of blocks requested.\n * For go-ethereum based chains baseFeePerGas will not returned in case of empty results\n * <github.com/ethereum/go-ethereum/blob/v1.10.16/internal/ethapi/api.go#L87>\n * @property gasUsedRatio - A number between 0 and 1 that represents the gas used vs. gas limit for\n * each block in the range of blocks requested.\n * @property reward - The priority fee at the percentiles requested for each block in the range of\n * blocks requested.\n */\n\nexport type EthFeeHistoryResponse = {\n oldestBlock: string;\n baseFeePerGas?: string[];\n gasUsedRatio: number[];\n reward?: string[][];\n};\n\n/**\n * @type ExistingFeeHistoryBlock\n *\n * Historical data for a particular block that exists on the blockchain.\n * @property number - The number of the block, as a BN.\n * @property baseFeePerGas - The base fee per gas for the block in WEI, as a BN.\n * @property gasUsedRatio - A number between 0 and 1 that represents the ratio between the gas paid\n * for the block and its set gas limit.\n * @property priorityFeesByPercentile - The priority fees paid for the transactions in the block\n * that occurred at particular levels at which those transactions contributed to the overall gas\n * used for the block, indexed by those percentiles. (See docs for {@link fetchBlockFeeHistory} for more\n * on how this works.)\n */\ntype ExistingFeeHistoryBlock<Percentile extends number> = {\n number: BN;\n baseFeePerGas: BN;\n gasUsedRatio: number;\n priorityFeesByPercentile: Record<Percentile, BN>;\n};\n\n/**\n * @type NextFeeHistoryBlock\n *\n * Historical data for a theoretical block that could exist in the future.\n * @property number - The number of the block, as a BN.\n * @property baseFeePerGas - The estimated base fee per gas for the block in WEI, as a BN.\n */\ntype NextFeeHistoryBlock = {\n number: BN;\n baseFeePerGas: BN;\n};\n\n/**\n * @type FeeHistoryBlock\n *\n * Historical data for a particular block.\n * @property number - The number of the block, as a BN.\n * @property baseFeePerGas - The base fee per gas for the block in WEI, as a BN.\n * @property gasUsedRatio - A number between 0 and 1 that represents the ratio between the gas paid\n * for the block and its set gas limit.\n * @property priorityFeesByPercentile - The priority fees paid for the transactions in the block\n * that occurred at particular levels at which those transactions contributed to the overall gas\n * used for the block, indexed by those percentiles. (See docs for {@link fetchBlockFeeHistory} for more\n * on how this works.)\n */\nexport type FeeHistoryBlock<Percentile extends number> =\n | ExistingFeeHistoryBlock<Percentile>\n | NextFeeHistoryBlock;\n\n/**\n * @type ExtractPercentileFrom\n *\n * Extracts the percentiles that the type assigned to an array of FeeHistoryBlock has been created\n * with. This makes use of the `infer` keyword to read the type argument.\n */\nexport type ExtractPercentileFrom<T> = T extends FeeHistoryBlock<infer P>[]\n ? P\n : never;\n\nconst MAX_NUMBER_OF_BLOCKS_PER_ETH_FEE_HISTORY_CALL = 1024;\n\n/**\n * Uses `eth_feeHistory` (an EIP-1559 feature) to obtain information about gas fees from a range of\n * blocks that have occurred recently on a network.\n *\n * To learn more, see these resources:\n *\n * - <https://infura.io/docs/ethereum#operation/eth_feeHistory>\n * - <https://github.com/zsfelfoldi/feehistory/blob/main/docs/feeHistory.md>\n * - <https://github.com/ethereum/go-ethereum/blob/57a3fab8a75eeb9c2f4fab770b73b51b9fe672c5/eth/gasprice/feehistory.go#L180>\n * - <https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1559.md>\n * - <https://gas-api.metaswap.codefi.network/testFeeHistory>\n *\n * @param args - The arguments to this function.\n * @param args.ethQuery - An EthQuery instance that wraps a provider for the network in question.\n * @param args.endBlock - The desired end of the requested block range. Can be \"latest\" if you want\n * to start from the latest successful block or the number of a known past block.\n * @param args.numberOfBlocks - How many total blocks to fetch. Note that if this is more than 1024,\n * multiple calls to `eth_feeHistory` will be made.\n * @param args.percentiles - A set of numbers between 1 and 100 which will dictate how\n * `priorityFeesByPercentile` in each returned block will be formed. When Ethereum runs the\n * `eth_feeHistory` method, for each block it is considering, it will first sort all transactions by\n * the priority fee. It will then go through each transaction and add the total amount of gas paid\n * for that transaction to a bucket which maxes out at the total gas used for the whole block. As\n * the bucket fills, it will cross percentages which correspond to the percentiles specified here,\n * and the priority fees of the first transactions which cause it to reach those percentages will be\n * recorded. Hence, `priorityFeesByPercentile` represents the priority fees of transactions at key\n * gas used contribution levels, where earlier levels have smaller contributions and later levels\n * have higher contributions.\n * @param args.includeNextBlock - Whether to include an extra block that represents the next\n * block after the latest one. Only the `baseFeePerGas` will be filled in for this block (which is\n * estimated).\n * @returns The list of blocks and their fee data, sorted from oldest to newest.\n */\nexport default async function fetchBlockFeeHistory<Percentile extends number>({\n ethQuery,\n numberOfBlocks: totalNumberOfBlocks,\n endBlock: givenEndBlock = 'latest',\n percentiles: givenPercentiles = [],\n includeNextBlock = false,\n}: {\n ethQuery: EthQuery;\n numberOfBlocks: number;\n endBlock?: 'latest' | BN;\n percentiles?: readonly Percentile[];\n includeNextBlock?: boolean;\n}): Promise<FeeHistoryBlock<Percentile>[]> {\n const percentiles =\n givenPercentiles.length > 0\n ? Array.from(new Set(givenPercentiles)).sort((a, b) => a - b)\n : [];\n\n const finalEndBlockNumber =\n givenEndBlock === 'latest'\n ? fromHex(await query(ethQuery, 'blockNumber'))\n : givenEndBlock;\n\n const requestChunkSpecifiers = determineRequestChunkSpecifiers(\n finalEndBlockNumber,\n totalNumberOfBlocks,\n );\n\n const blockChunks = await Promise.all(\n requestChunkSpecifiers.map(({ numberOfBlocks, endBlockNumber }, i) => {\n return i === requestChunkSpecifiers.length - 1\n ? makeRequestForChunk({\n ethQuery,\n numberOfBlocks,\n endBlockNumber,\n percentiles,\n includeNextBlock,\n })\n : makeRequestForChunk({\n ethQuery,\n numberOfBlocks,\n endBlockNumber,\n percentiles,\n includeNextBlock: false,\n });\n }),\n );\n\n return blockChunks.reduce(\n (array, blocks) => [...array, ...blocks],\n [] as FeeHistoryBlock<Percentile>[],\n );\n}\n\n/**\n * Builds an ExistingFeeHistoryBlock.\n *\n * @param args - The args to this function.\n * @param args.number - The number of the block.\n * @param args.baseFeePerGas - The base fee per gas of the block.\n * @param args.blockIndex - The index of the block in the source chunk.\n * @param args.gasUsedRatios - The gas used ratios for the block.\n * @param args.priorityFeePercentileGroups - The priority fee percentile groups for the block.\n * @param args.percentiles - The percentiles used to fetch the source chunk.\n * @returns The ExistingFeeHistoryBlock.\n */\nfunction buildExistingFeeHistoryBlock<Percentile extends number>({\n baseFeePerGas,\n number,\n blockIndex,\n gasUsedRatios,\n priorityFeePercentileGroups,\n percentiles,\n}: {\n baseFeePerGas: BN;\n number: BN;\n blockIndex: number;\n gasUsedRatios: number[];\n priorityFeePercentileGroups: string[][];\n percentiles: readonly Percentile[];\n}): ExistingFeeHistoryBlock<Percentile> {\n const gasUsedRatio = gasUsedRatios[blockIndex];\n const priorityFeesForEachPercentile = priorityFeePercentileGroups[blockIndex];\n const priorityFeesByPercentile = percentiles.reduce(\n (obj, percentile, percentileIndex) => {\n const priorityFee = priorityFeesForEachPercentile[percentileIndex];\n return { ...obj, [percentile]: fromHex(priorityFee) };\n },\n {} as Record<Percentile, BN>,\n );\n\n return {\n number,\n baseFeePerGas,\n gasUsedRatio,\n priorityFeesByPercentile,\n };\n}\n\n/**\n * Builds a NextFeeHistoryBlock.\n *\n * @param args - The args to this function.\n * @param args.baseFeePerGas - The base fee per gas of the block.\n * @param args.number - The number of the block.\n * @returns The NextFeeHistoryBlock.\n */\nfunction buildNextFeeHistoryBlock({\n baseFeePerGas,\n number,\n}: {\n baseFeePerGas: BN;\n number: BN;\n}) {\n return {\n number,\n baseFeePerGas,\n gasUsedRatio: null,\n priorityFeesByPercentile: null,\n };\n}\n\n/**\n * Uses eth_feeHistory to request historical data about a group of blocks (max size 1024).\n *\n * @param args - The arguments\n * @param args.ethQuery - An EthQuery instance.\n * @param args.numberOfBlocks - The number of blocks in the chunk. Must be at most 1024, as this is\n * the maximum that `eth_feeHistory` can return in one call.\n * @param args.endBlockNumber - The end of the requested block range.\n * @param args.percentiles - A set of numbers between 1 and 100 that will be used to pull priority\n * fees for each block.\n * @param args.includeNextBlock - Whether to include an extra block that represents the next\n * block after the latest one. Only the `baseFeePerGas` will be filled in for this block (which is\n * estimated).\n * @returns A list of block data.\n */\nasync function makeRequestForChunk<Percentile extends number>({\n ethQuery,\n numberOfBlocks,\n endBlockNumber,\n percentiles,\n includeNextBlock,\n}: {\n ethQuery: EthQuery;\n numberOfBlocks: number;\n endBlockNumber: BN;\n percentiles: readonly Percentile[];\n includeNextBlock: boolean;\n}): Promise<FeeHistoryBlock<Percentile>[]> {\n const response: EthFeeHistoryResponse = await query(\n ethQuery,\n 'eth_feeHistory',\n [toHex(numberOfBlocks), toHex(endBlockNumber), percentiles],\n );\n\n const startBlockNumber = fromHex(response.oldestBlock);\n\n if (\n response.baseFeePerGas !== undefined &&\n response.baseFeePerGas.length > 0 &&\n response.gasUsedRatio.length > 0 &&\n (response.reward === undefined || response.reward.length > 0)\n ) {\n // Per\n // <https://github.com/ethereum/go-ethereum/blob/57a3fab8a75eeb9c2f4fab770b73b51b9fe672c5/eth/gasprice/feehistory.go#L191-L192>,\n // baseFeePerGas will always include an extra item which is the calculated base fee for the\n // next (future) block. We may or may not care about this; if we don't, chop it off.\n const baseFeesPerGasAsHex = includeNextBlock\n ? response.baseFeePerGas\n : response.baseFeePerGas.slice(0, numberOfBlocks);\n const gasUsedRatios = response.gasUsedRatio;\n const priorityFeePercentileGroups = response.reward ?? [];\n // Chain is allowed to return fewer number of block results\n const numberOfExistingResults = gasUsedRatios.length;\n\n return baseFeesPerGasAsHex.map((baseFeePerGasAsHex, blockIndex) => {\n const baseFeePerGas = fromHex(baseFeePerGasAsHex);\n const number = startBlockNumber.addn(blockIndex);\n\n return blockIndex >= numberOfExistingResults\n ? buildNextFeeHistoryBlock({ baseFeePerGas, number })\n : buildExistingFeeHistoryBlock({\n baseFeePerGas,\n number,\n blockIndex,\n gasUsedRatios,\n priorityFeePercentileGroups,\n percentiles,\n });\n });\n }\n\n return [];\n}\n\n/**\n * Divides a block range (specified by a range size and the end of the range) into chunks based on\n * the maximum number of blocks that `eth_feeHistory` can return in a single call.\n *\n * If the requested totalNumberOfBlocks exceed endBlockNumber, totalNumberOfBlocks is\n * truncated to avoid requesting chunks with negative endBlockNumber.\n *\n * @param endBlockNumber - The final block in the complete desired block range after all\n * `eth_feeHistory` requests have been made.\n * @param totalNumberOfBlocks - The total number of desired blocks after all `eth_feeHistory`\n * requests have been made.\n * @returns A set of arguments that can be used to make requests to `eth_feeHistory` in order to\n * retrieve all of the requested blocks, sorted from oldest block to newest block.\n */\nfunction determineRequestChunkSpecifiers(\n endBlockNumber: BN,\n totalNumberOfBlocks: number,\n): RequestChunkSpecifier[] {\n if (endBlockNumber.lt(new BN(totalNumberOfBlocks))) {\n totalNumberOfBlocks = endBlockNumber.toNumber();\n }\n\n const specifiers = [];\n for (\n let chunkStartBlockNumber = endBlockNumber.subn(totalNumberOfBlocks);\n chunkStartBlockNumber.lt(endBlockNumber);\n chunkStartBlockNumber = chunkStartBlockNumber.addn(\n MAX_NUMBER_OF_BLOCKS_PER_ETH_FEE_HISTORY_CALL,\n )\n ) {\n const distanceToEnd = endBlockNumber.sub(chunkStartBlockNumber).toNumber();\n const numberOfBlocks =\n distanceToEnd < MAX_NUMBER_OF_BLOCKS_PER_ETH_FEE_HISTORY_CALL\n ? distanceToEnd\n : MAX_NUMBER_OF_BLOCKS_PER_ETH_FEE_HISTORY_CALL;\n const chunkEndBlockNumber = chunkStartBlockNumber.addn(numberOfBlocks);\n specifiers.push({ numberOfBlocks, endBlockNumber: chunkEndBlockNumber });\n }\n return specifiers;\n}\n"]}
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -1,89 +1,9 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const medianOf_1 = __importDefault(require("./medianOf"));
|
|
10
|
-
const PRIORITY_LEVELS = ['low', 'medium', 'high'];
|
|
11
|
-
const PRIORITY_LEVEL_PERCENTILES = [10, 20, 30];
|
|
12
|
-
const SETTINGS_BY_PRIORITY_LEVEL = {
|
|
13
|
-
low: {
|
|
14
|
-
percentile: 10,
|
|
15
|
-
baseFeePercentageMultiplier: new bn_js_1.default(110),
|
|
16
|
-
priorityFeePercentageMultiplier: new bn_js_1.default(94),
|
|
17
|
-
minSuggestedMaxPriorityFeePerGas: new bn_js_1.default(1000000000),
|
|
18
|
-
estimatedWaitTimes: {
|
|
19
|
-
minWaitTimeEstimate: 15000,
|
|
20
|
-
maxWaitTimeEstimate: 30000,
|
|
21
|
-
},
|
|
22
|
-
},
|
|
23
|
-
medium: {
|
|
24
|
-
percentile: 20,
|
|
25
|
-
baseFeePercentageMultiplier: new bn_js_1.default(120),
|
|
26
|
-
priorityFeePercentageMultiplier: new bn_js_1.default(97),
|
|
27
|
-
minSuggestedMaxPriorityFeePerGas: new bn_js_1.default(1500000000),
|
|
28
|
-
estimatedWaitTimes: {
|
|
29
|
-
minWaitTimeEstimate: 15000,
|
|
30
|
-
maxWaitTimeEstimate: 45000,
|
|
31
|
-
},
|
|
32
|
-
},
|
|
33
|
-
high: {
|
|
34
|
-
percentile: 30,
|
|
35
|
-
baseFeePercentageMultiplier: new bn_js_1.default(125),
|
|
36
|
-
priorityFeePercentageMultiplier: new bn_js_1.default(98),
|
|
37
|
-
minSuggestedMaxPriorityFeePerGas: new bn_js_1.default(2000000000),
|
|
38
|
-
estimatedWaitTimes: {
|
|
39
|
-
minWaitTimeEstimate: 15000,
|
|
40
|
-
maxWaitTimeEstimate: 60000,
|
|
41
|
-
},
|
|
42
|
-
},
|
|
43
|
-
};
|
|
44
|
-
/**
|
|
45
|
-
* Calculates a set of estimates assigned to a particular priority level based on the data returned
|
|
46
|
-
* by `eth_feeHistory`.
|
|
47
|
-
*
|
|
48
|
-
* @param priorityLevel - The level of fees that dictates how soon a transaction may go through
|
|
49
|
-
* ("low", "medium", or "high").
|
|
50
|
-
* @param blocks - A set of blocks as obtained from {@link fetchBlockFeeHistory}.
|
|
51
|
-
* @returns The estimates.
|
|
52
|
-
*/
|
|
53
|
-
function calculateEstimatesForPriorityLevel(priorityLevel, blocks) {
|
|
54
|
-
const settings = SETTINGS_BY_PRIORITY_LEVEL[priorityLevel];
|
|
55
|
-
const latestBaseFeePerGas = blocks[blocks.length - 1].baseFeePerGas;
|
|
56
|
-
const adjustedBaseFee = latestBaseFeePerGas
|
|
57
|
-
.mul(settings.baseFeePercentageMultiplier)
|
|
58
|
-
.divn(100);
|
|
59
|
-
const priorityFees = blocks
|
|
60
|
-
.map((block) => {
|
|
61
|
-
return 'priorityFeesByPercentile' in block
|
|
62
|
-
? block.priorityFeesByPercentile[settings.percentile]
|
|
63
|
-
: null;
|
|
64
|
-
})
|
|
65
|
-
.filter(bn_js_1.default.isBN);
|
|
66
|
-
const medianPriorityFee = (0, medianOf_1.default)(priorityFees);
|
|
67
|
-
const adjustedPriorityFee = medianPriorityFee
|
|
68
|
-
.mul(settings.priorityFeePercentageMultiplier)
|
|
69
|
-
.divn(100);
|
|
70
|
-
const suggestedMaxPriorityFeePerGas = bn_js_1.default.max(adjustedPriorityFee, settings.minSuggestedMaxPriorityFeePerGas);
|
|
71
|
-
const suggestedMaxFeePerGas = adjustedBaseFee.add(suggestedMaxPriorityFeePerGas);
|
|
72
|
-
return Object.assign(Object.assign({}, settings.estimatedWaitTimes), { suggestedMaxPriorityFeePerGas: (0, ethjs_unit_1.fromWei)(suggestedMaxPriorityFeePerGas, controller_utils_1.GWEI), suggestedMaxFeePerGas: (0, ethjs_unit_1.fromWei)(suggestedMaxFeePerGas, controller_utils_1.GWEI) });
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Calculates a set of estimates suitable for different priority levels based on the data returned
|
|
76
|
-
* by `eth_feeHistory`.
|
|
77
|
-
*
|
|
78
|
-
* @param blocks - A set of blocks populated with data for priority fee percentiles 10, 20, and 30,
|
|
79
|
-
* obtained via {@link BlockFeeHistoryDatasetFetcher}.
|
|
80
|
-
* @returns The estimates.
|
|
81
|
-
*/
|
|
82
|
-
function calculateGasFeeEstimatesForPriorityLevels(blocks) {
|
|
83
|
-
return PRIORITY_LEVELS.reduce((obj, priorityLevel) => {
|
|
84
|
-
const gasEstimatesForPriorityLevel = calculateEstimatesForPriorityLevel(priorityLevel, blocks);
|
|
85
|
-
return Object.assign(Object.assign({}, obj), { [priorityLevel]: gasEstimatesForPriorityLevel });
|
|
86
|
-
}, {});
|
|
87
|
-
}
|
|
88
|
-
exports.default = calculateGasFeeEstimatesForPriorityLevels;
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
+
|
|
3
|
+
var _chunkGE7RMDFVjs = require('../chunk-GE7RMDFV.js');
|
|
4
|
+
require('../chunk-S2EH2EPS.js');
|
|
5
|
+
require('../chunk-Z4BLTVTB.js');
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
exports.default = _chunkGE7RMDFVjs.calculateGasFeeEstimatesForPriorityLevels;
|
|
89
9
|
//# sourceMappingURL=calculateGasFeeEstimatesForPriorityLevels.js.map
|
package/dist/fetchGasEstimatesViaEthFeeHistory/calculateGasFeeEstimatesForPriorityLevels.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":""}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import {
|
|
2
|
+
calculateGasFeeEstimatesForPriorityLevels
|
|
3
|
+
} from "../chunk-GUWY6WP6.mjs";
|
|
4
|
+
import "../chunk-C4I7KKIU.mjs";
|
|
5
|
+
import "../chunk-XUI43LEZ.mjs";
|
|
6
|
+
export {
|
|
7
|
+
calculateGasFeeEstimatesForPriorityLevels as default
|
|
8
|
+
};
|
|
9
|
+
//# sourceMappingURL=calculateGasFeeEstimatesForPriorityLevels.mjs.map
|
package/dist/fetchGasEstimatesViaEthFeeHistory/calculateGasFeeEstimatesForPriorityLevels.mjs.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -1,32 +1,8 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
const controller_utils_1 = require("@metamask/controller-utils");
|
|
13
|
-
/**
|
|
14
|
-
* Returns information about the latest completed block.
|
|
15
|
-
*
|
|
16
|
-
* @param ethQuery - An EthQuery instance
|
|
17
|
-
* @param includeFullTransactionData - Whether or not to include all data for transactions as
|
|
18
|
-
* opposed to merely hashes. False by default.
|
|
19
|
-
* @returns The block.
|
|
20
|
-
*/
|
|
21
|
-
function fetchLatestBlock(ethQuery, includeFullTransactionData = false) {
|
|
22
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
23
|
-
const blockNumber = yield (0, controller_utils_1.query)(ethQuery, 'blockNumber');
|
|
24
|
-
const block = yield (0, controller_utils_1.query)(ethQuery, 'getBlockByNumber', [
|
|
25
|
-
blockNumber,
|
|
26
|
-
includeFullTransactionData,
|
|
27
|
-
]);
|
|
28
|
-
return Object.assign(Object.assign({}, block), { number: (0, controller_utils_1.fromHex)(block.number), baseFeePerGas: (0, controller_utils_1.fromHex)(block.baseFeePerGas) });
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
exports.default = fetchLatestBlock;
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
+
|
|
3
|
+
var _chunkLO7OP5FMjs = require('../chunk-LO7OP5FM.js');
|
|
4
|
+
require('../chunk-Z4BLTVTB.js');
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
exports.default = _chunkLO7OP5FMjs.fetchLatestBlock;
|
|
32
8
|
//# sourceMappingURL=fetchLatestBlock.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|