@metamask-previews/gas-fee-controller 26.3.2-preview-4f4c98e → 26.3.2-preview-5b293ee50
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 +0 -4
- package/dist/GasFeeController-method-action-types.cjs +7 -0
- package/dist/GasFeeController-method-action-types.cjs.map +1 -0
- package/dist/{GasFeeController-method-action-types.d.ts → GasFeeController-method-action-types.d.cts} +2 -2
- package/dist/GasFeeController-method-action-types.d.cts.map +1 -0
- package/dist/GasFeeController-method-action-types.d.mts +75 -0
- package/dist/GasFeeController-method-action-types.d.mts.map +1 -0
- package/dist/{GasFeeController-method-action-types.js → GasFeeController-method-action-types.mjs} +1 -1
- package/dist/GasFeeController-method-action-types.mjs.map +1 -0
- package/dist/GasFeeController.cjs +370 -0
- package/dist/GasFeeController.cjs.map +1 -0
- package/dist/{GasFeeController.d.ts → GasFeeController.d.cts} +18 -18
- package/dist/GasFeeController.d.cts.map +1 -0
- package/dist/GasFeeController.d.mts +297 -0
- package/dist/GasFeeController.d.mts.map +1 -0
- package/dist/{GasFeeController.js → GasFeeController.mjs} +14 -7
- package/dist/GasFeeController.mjs.map +1 -0
- package/dist/determineGasFeeCalculations.cjs +109 -0
- package/dist/determineGasFeeCalculations.cjs.map +1 -0
- package/dist/{determineGasFeeCalculations.d.ts → determineGasFeeCalculations.d.cts} +2 -2
- package/dist/determineGasFeeCalculations.d.cts.map +1 -0
- package/dist/determineGasFeeCalculations.d.mts +42 -0
- package/dist/determineGasFeeCalculations.d.mts.map +1 -0
- package/dist/{determineGasFeeCalculations.js → determineGasFeeCalculations.mjs} +2 -2
- package/dist/determineGasFeeCalculations.mjs.map +1 -0
- package/dist/gas-util.cjs +143 -0
- package/dist/gas-util.cjs.map +1 -0
- package/dist/{gas-util.d.ts → gas-util.d.cts} +3 -3
- package/dist/gas-util.d.cts.map +1 -0
- package/dist/gas-util.d.mts +43 -0
- package/dist/gas-util.d.mts.map +1 -0
- package/dist/{gas-util.js → gas-util.mjs} +10 -3
- package/dist/gas-util.mjs.map +1 -0
- package/dist/index.cjs +18 -0
- package/dist/index.cjs.map +1 -0
- package/dist/{index.d.ts → index.d.cts} +3 -3
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +3 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +16 -12
- package/dist/GasFeeController-method-action-types.d.ts.map +0 -1
- package/dist/GasFeeController-method-action-types.js.map +0 -1
- package/dist/GasFeeController.d.ts.map +0 -1
- package/dist/GasFeeController.js.map +0 -1
- package/dist/determineGasFeeCalculations.d.ts.map +0 -1
- package/dist/determineGasFeeCalculations.js.map +0 -1
- package/dist/gas-util.d.ts.map +0 -1
- package/dist/gas-util.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -2
- package/dist/index.js.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -7,10 +7,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
-
### Changed
|
|
11
|
-
|
|
12
|
-
- **BREAKING:** Bump minimum Node.js version to 22 ([#9168](https://github.com/MetaMask/core/pull/9168))
|
|
13
|
-
|
|
14
10
|
## [26.3.2]
|
|
15
11
|
|
|
16
12
|
### Changed
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GasFeeController-method-action-types.cjs","sourceRoot":"","sources":["../src/GasFeeController-method-action-types.ts"],"names":[],"mappings":";AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated.\n * Do not edit manually.\n */\n\nimport type { GasFeeController } from './GasFeeController.js';\n\n/**\n * Resets the polling interval by stopping and restarting polling\n * with the existing poll tokens.\n */\nexport type GasFeeControllerResetPollingAction = {\n type: `GasFeeController:resetPolling`;\n handler: GasFeeController['resetPolling'];\n};\n\n/**\n * Fetches gas fee estimates.\n *\n * @param options - The gas fee estimate options.\n * @returns The gas fee estimates.\n */\nexport type GasFeeControllerFetchGasFeeEstimatesAction = {\n type: `GasFeeController:fetchGasFeeEstimates`;\n handler: GasFeeController['fetchGasFeeEstimates'];\n};\n\n/**\n * Gets gas fee estimates and starts polling for updates.\n *\n * @param pollToken - An existing poll token to reuse, or undefined to\n * generate a new one.\n * @returns The poll token that can be used to stop polling.\n */\nexport type GasFeeControllerGetGasFeeEstimatesAndStartPollingAction = {\n type: `GasFeeController:getGasFeeEstimatesAndStartPolling`;\n handler: GasFeeController['getGasFeeEstimatesAndStartPolling'];\n};\n\n/**\n * Remove the poll token, and stop polling if the set of poll tokens is empty.\n *\n * @param pollToken - The poll token to disconnect.\n */\nexport type GasFeeControllerDisconnectPollerAction = {\n type: `GasFeeController:disconnectPoller`;\n handler: GasFeeController['disconnectPoller'];\n};\n\n/**\n * Stops polling for gas fee estimates and clears all poll tokens.\n */\nexport type GasFeeControllerStopPollingAction = {\n type: `GasFeeController:stopPolling`;\n handler: GasFeeController['stopPolling'];\n};\n\n/**\n * Gets the estimated time for a transaction based on the given gas parameters.\n *\n * @param maxPriorityFeePerGas - The maximum priority fee per gas in GWEI.\n * @param maxFeePerGas - The maximum fee per gas in GWEI.\n * @returns The estimated time bounds, or an empty object if fee market\n * estimates are not available.\n */\nexport type GasFeeControllerGetTimeEstimateAction = {\n type: `GasFeeController:getTimeEstimate`;\n handler: GasFeeController['getTimeEstimate'];\n};\n\nexport type GasFeeControllerEnableNonRPCGasFeeApisAction = {\n type: `GasFeeController:enableNonRPCGasFeeApis`;\n handler: GasFeeController['enableNonRPCGasFeeApis'];\n};\n\nexport type GasFeeControllerDisableNonRPCGasFeeApisAction = {\n type: `GasFeeController:disableNonRPCGasFeeApis`;\n handler: GasFeeController['disableNonRPCGasFeeApis'];\n};\n\n/**\n * Union of all GasFeeController action types.\n */\nexport type GasFeeControllerMethodActions =\n | GasFeeControllerResetPollingAction\n | GasFeeControllerFetchGasFeeEstimatesAction\n | GasFeeControllerGetGasFeeEstimatesAndStartPollingAction\n | GasFeeControllerDisconnectPollerAction\n | GasFeeControllerStopPollingAction\n | GasFeeControllerGetTimeEstimateAction\n | GasFeeControllerEnableNonRPCGasFeeApisAction\n | GasFeeControllerDisableNonRPCGasFeeApisAction;\n"]}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* This file is auto generated.
|
|
3
3
|
* Do not edit manually.
|
|
4
4
|
*/
|
|
5
|
-
import type { GasFeeController } from
|
|
5
|
+
import type { GasFeeController } from "./GasFeeController.cjs";
|
|
6
6
|
/**
|
|
7
7
|
* Resets the polling interval by stopping and restarting polling
|
|
8
8
|
* with the existing poll tokens.
|
|
@@ -72,4 +72,4 @@ export type GasFeeControllerDisableNonRPCGasFeeApisAction = {
|
|
|
72
72
|
* Union of all GasFeeController action types.
|
|
73
73
|
*/
|
|
74
74
|
export type GasFeeControllerMethodActions = GasFeeControllerResetPollingAction | GasFeeControllerFetchGasFeeEstimatesAction | GasFeeControllerGetGasFeeEstimatesAndStartPollingAction | GasFeeControllerDisconnectPollerAction | GasFeeControllerStopPollingAction | GasFeeControllerGetTimeEstimateAction | GasFeeControllerEnableNonRPCGasFeeApisAction | GasFeeControllerDisableNonRPCGasFeeApisAction;
|
|
75
|
-
//# sourceMappingURL=GasFeeController-method-action-types.d.
|
|
75
|
+
//# sourceMappingURL=GasFeeController-method-action-types.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GasFeeController-method-action-types.d.cts","sourceRoot":"","sources":["../src/GasFeeController-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,+BAA8B;AAE9D;;;GAGG;AACH,MAAM,MAAM,kCAAkC,GAAG;IAC/C,IAAI,EAAE,+BAA+B,CAAC;IACtC,OAAO,EAAE,gBAAgB,CAAC,cAAc,CAAC,CAAC;CAC3C,CAAC;AAEF;;;;;GAKG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
|
|
4
|
+
*/
|
|
5
|
+
import type { GasFeeController } from "./GasFeeController.mjs";
|
|
6
|
+
/**
|
|
7
|
+
* Resets the polling interval by stopping and restarting polling
|
|
8
|
+
* with the existing poll tokens.
|
|
9
|
+
*/
|
|
10
|
+
export type GasFeeControllerResetPollingAction = {
|
|
11
|
+
type: `GasFeeController:resetPolling`;
|
|
12
|
+
handler: GasFeeController['resetPolling'];
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Fetches gas fee estimates.
|
|
16
|
+
*
|
|
17
|
+
* @param options - The gas fee estimate options.
|
|
18
|
+
* @returns The gas fee estimates.
|
|
19
|
+
*/
|
|
20
|
+
export type GasFeeControllerFetchGasFeeEstimatesAction = {
|
|
21
|
+
type: `GasFeeController:fetchGasFeeEstimates`;
|
|
22
|
+
handler: GasFeeController['fetchGasFeeEstimates'];
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Gets gas fee estimates and starts polling for updates.
|
|
26
|
+
*
|
|
27
|
+
* @param pollToken - An existing poll token to reuse, or undefined to
|
|
28
|
+
* generate a new one.
|
|
29
|
+
* @returns The poll token that can be used to stop polling.
|
|
30
|
+
*/
|
|
31
|
+
export type GasFeeControllerGetGasFeeEstimatesAndStartPollingAction = {
|
|
32
|
+
type: `GasFeeController:getGasFeeEstimatesAndStartPolling`;
|
|
33
|
+
handler: GasFeeController['getGasFeeEstimatesAndStartPolling'];
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Remove the poll token, and stop polling if the set of poll tokens is empty.
|
|
37
|
+
*
|
|
38
|
+
* @param pollToken - The poll token to disconnect.
|
|
39
|
+
*/
|
|
40
|
+
export type GasFeeControllerDisconnectPollerAction = {
|
|
41
|
+
type: `GasFeeController:disconnectPoller`;
|
|
42
|
+
handler: GasFeeController['disconnectPoller'];
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Stops polling for gas fee estimates and clears all poll tokens.
|
|
46
|
+
*/
|
|
47
|
+
export type GasFeeControllerStopPollingAction = {
|
|
48
|
+
type: `GasFeeController:stopPolling`;
|
|
49
|
+
handler: GasFeeController['stopPolling'];
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Gets the estimated time for a transaction based on the given gas parameters.
|
|
53
|
+
*
|
|
54
|
+
* @param maxPriorityFeePerGas - The maximum priority fee per gas in GWEI.
|
|
55
|
+
* @param maxFeePerGas - The maximum fee per gas in GWEI.
|
|
56
|
+
* @returns The estimated time bounds, or an empty object if fee market
|
|
57
|
+
* estimates are not available.
|
|
58
|
+
*/
|
|
59
|
+
export type GasFeeControllerGetTimeEstimateAction = {
|
|
60
|
+
type: `GasFeeController:getTimeEstimate`;
|
|
61
|
+
handler: GasFeeController['getTimeEstimate'];
|
|
62
|
+
};
|
|
63
|
+
export type GasFeeControllerEnableNonRPCGasFeeApisAction = {
|
|
64
|
+
type: `GasFeeController:enableNonRPCGasFeeApis`;
|
|
65
|
+
handler: GasFeeController['enableNonRPCGasFeeApis'];
|
|
66
|
+
};
|
|
67
|
+
export type GasFeeControllerDisableNonRPCGasFeeApisAction = {
|
|
68
|
+
type: `GasFeeController:disableNonRPCGasFeeApis`;
|
|
69
|
+
handler: GasFeeController['disableNonRPCGasFeeApis'];
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Union of all GasFeeController action types.
|
|
73
|
+
*/
|
|
74
|
+
export type GasFeeControllerMethodActions = GasFeeControllerResetPollingAction | GasFeeControllerFetchGasFeeEstimatesAction | GasFeeControllerGetGasFeeEstimatesAndStartPollingAction | GasFeeControllerDisconnectPollerAction | GasFeeControllerStopPollingAction | GasFeeControllerGetTimeEstimateAction | GasFeeControllerEnableNonRPCGasFeeApisAction | GasFeeControllerDisableNonRPCGasFeeApisAction;
|
|
75
|
+
//# sourceMappingURL=GasFeeController-method-action-types.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GasFeeController-method-action-types.d.mts","sourceRoot":"","sources":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CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,6BAA6B,GACrC,kCAAkC,GAClC,0CAA0C,GAC1C,uDAAuD,GACvD,sCAAsC,GACtC,iCAAiC,GACjC,qCAAqC,GACrC,4CAA4C,GAC5C,6CAA6C,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GasFeeController-method-action-types.mjs","sourceRoot":"","sources":["../src/GasFeeController-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated.\n * Do not edit manually.\n */\n\nimport type { GasFeeController } from './GasFeeController.js';\n\n/**\n * Resets the polling interval by stopping and restarting polling\n * with the existing poll tokens.\n */\nexport type GasFeeControllerResetPollingAction = {\n type: `GasFeeController:resetPolling`;\n handler: GasFeeController['resetPolling'];\n};\n\n/**\n * Fetches gas fee estimates.\n *\n * @param options - The gas fee estimate options.\n * @returns The gas fee estimates.\n */\nexport type GasFeeControllerFetchGasFeeEstimatesAction = {\n type: `GasFeeController:fetchGasFeeEstimates`;\n handler: GasFeeController['fetchGasFeeEstimates'];\n};\n\n/**\n * Gets gas fee estimates and starts polling for updates.\n *\n * @param pollToken - An existing poll token to reuse, or undefined to\n * generate a new one.\n * @returns The poll token that can be used to stop polling.\n */\nexport type GasFeeControllerGetGasFeeEstimatesAndStartPollingAction = {\n type: `GasFeeController:getGasFeeEstimatesAndStartPolling`;\n handler: GasFeeController['getGasFeeEstimatesAndStartPolling'];\n};\n\n/**\n * Remove the poll token, and stop polling if the set of poll tokens is empty.\n *\n * @param pollToken - The poll token to disconnect.\n */\nexport type GasFeeControllerDisconnectPollerAction = {\n type: `GasFeeController:disconnectPoller`;\n handler: GasFeeController['disconnectPoller'];\n};\n\n/**\n * Stops polling for gas fee estimates and clears all poll tokens.\n */\nexport type GasFeeControllerStopPollingAction = {\n type: `GasFeeController:stopPolling`;\n handler: GasFeeController['stopPolling'];\n};\n\n/**\n * Gets the estimated time for a transaction based on the given gas parameters.\n *\n * @param maxPriorityFeePerGas - The maximum priority fee per gas in GWEI.\n * @param maxFeePerGas - The maximum fee per gas in GWEI.\n * @returns The estimated time bounds, or an empty object if fee market\n * estimates are not available.\n */\nexport type GasFeeControllerGetTimeEstimateAction = {\n type: `GasFeeController:getTimeEstimate`;\n handler: GasFeeController['getTimeEstimate'];\n};\n\nexport type GasFeeControllerEnableNonRPCGasFeeApisAction = {\n type: `GasFeeController:enableNonRPCGasFeeApis`;\n handler: GasFeeController['enableNonRPCGasFeeApis'];\n};\n\nexport type GasFeeControllerDisableNonRPCGasFeeApisAction = {\n type: `GasFeeController:disableNonRPCGasFeeApis`;\n handler: GasFeeController['disableNonRPCGasFeeApis'];\n};\n\n/**\n * Union of all GasFeeController action types.\n */\nexport type GasFeeControllerMethodActions =\n | GasFeeControllerResetPollingAction\n | GasFeeControllerFetchGasFeeEstimatesAction\n | GasFeeControllerGetGasFeeEstimatesAndStartPollingAction\n | GasFeeControllerDisconnectPollerAction\n | GasFeeControllerStopPollingAction\n | GasFeeControllerGetTimeEstimateAction\n | GasFeeControllerEnableNonRPCGasFeeApisAction\n | GasFeeControllerDisableNonRPCGasFeeApisAction;\n"]}
|
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
6
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
+
};
|
|
8
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
+
};
|
|
13
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
|
+
};
|
|
16
|
+
var _GasFeeController_instances, _GasFeeController_getProvider, _GasFeeController_getChainId, _GasFeeController_onNetworkControllerDidChange, _GasFeeController_getEthQuery, _GasFeeController_getCurrentChainId, _GasFeeController_getChainIdFromNetworkController, _GasFeeController_getChainIdForNetworkClient;
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.GasFeeController = exports.GAS_ESTIMATE_TYPES = exports.LEGACY_GAS_PRICES_API_URL = void 0;
|
|
19
|
+
const controller_utils_1 = require("@metamask/controller-utils");
|
|
20
|
+
const eth_query_1 = __importDefault(require("@metamask/eth-query"));
|
|
21
|
+
const polling_controller_1 = require("@metamask/polling-controller");
|
|
22
|
+
const uuid_1 = require("uuid");
|
|
23
|
+
const determineGasFeeCalculations_js_1 = __importDefault(require("./determineGasFeeCalculations.cjs"));
|
|
24
|
+
const gas_util_js_1 = require("./gas-util.cjs");
|
|
25
|
+
exports.LEGACY_GAS_PRICES_API_URL = `https://api.metaswap.codefi.network/gasPrices`;
|
|
26
|
+
/**
|
|
27
|
+
* Indicates which type of gasEstimate the controller is currently returning.
|
|
28
|
+
* This is useful as a way of asserting that the shape of gasEstimates matches
|
|
29
|
+
* expectations. NONE is a special case indicating that no previous gasEstimate
|
|
30
|
+
* has been fetched.
|
|
31
|
+
*/
|
|
32
|
+
exports.GAS_ESTIMATE_TYPES = {
|
|
33
|
+
FEE_MARKET: 'fee-market',
|
|
34
|
+
LEGACY: 'legacy',
|
|
35
|
+
ETH_GASPRICE: 'eth_gasPrice',
|
|
36
|
+
NONE: 'none',
|
|
37
|
+
};
|
|
38
|
+
const metadata = {
|
|
39
|
+
gasFeeEstimatesByChainId: {
|
|
40
|
+
includeInStateLogs: true,
|
|
41
|
+
persist: true,
|
|
42
|
+
includeInDebugSnapshot: false,
|
|
43
|
+
usedInUi: true,
|
|
44
|
+
},
|
|
45
|
+
gasFeeEstimates: {
|
|
46
|
+
includeInStateLogs: true,
|
|
47
|
+
persist: true,
|
|
48
|
+
includeInDebugSnapshot: false,
|
|
49
|
+
usedInUi: true,
|
|
50
|
+
},
|
|
51
|
+
estimatedGasFeeTimeBounds: {
|
|
52
|
+
includeInStateLogs: true,
|
|
53
|
+
persist: true,
|
|
54
|
+
includeInDebugSnapshot: false,
|
|
55
|
+
usedInUi: true,
|
|
56
|
+
},
|
|
57
|
+
gasEstimateType: {
|
|
58
|
+
includeInStateLogs: true,
|
|
59
|
+
persist: true,
|
|
60
|
+
includeInDebugSnapshot: false,
|
|
61
|
+
usedInUi: true,
|
|
62
|
+
},
|
|
63
|
+
nonRPCGasFeeApisDisabled: {
|
|
64
|
+
includeInStateLogs: true,
|
|
65
|
+
persist: true,
|
|
66
|
+
includeInDebugSnapshot: false,
|
|
67
|
+
usedInUi: false,
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
const name = 'GasFeeController';
|
|
71
|
+
const MESSENGER_EXPOSED_METHODS = [
|
|
72
|
+
'disableNonRPCGasFeeApis',
|
|
73
|
+
'disconnectPoller',
|
|
74
|
+
'enableNonRPCGasFeeApis',
|
|
75
|
+
'fetchGasFeeEstimates',
|
|
76
|
+
'getGasFeeEstimatesAndStartPolling',
|
|
77
|
+
'getTimeEstimate',
|
|
78
|
+
'resetPolling',
|
|
79
|
+
'stopPolling',
|
|
80
|
+
];
|
|
81
|
+
const defaultState = {
|
|
82
|
+
gasFeeEstimatesByChainId: {},
|
|
83
|
+
gasFeeEstimates: {},
|
|
84
|
+
estimatedGasFeeTimeBounds: {},
|
|
85
|
+
gasEstimateType: exports.GAS_ESTIMATE_TYPES.NONE,
|
|
86
|
+
nonRPCGasFeeApisDisabled: false,
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* Controller that retrieves gas fee estimate data and polls for updated data on a set interval
|
|
90
|
+
*/
|
|
91
|
+
class GasFeeController extends (0, polling_controller_1.StaticIntervalPollingController)() {
|
|
92
|
+
/**
|
|
93
|
+
* Creates a GasFeeController instance.
|
|
94
|
+
*
|
|
95
|
+
* @param options - The controller options.
|
|
96
|
+
* @param options.interval - The time in milliseconds to wait between polls.
|
|
97
|
+
* @param options.messenger - The controller messenger.
|
|
98
|
+
* @param options.state - The initial state.
|
|
99
|
+
* @param options.getCurrentNetworkEIP1559Compatibility - Determines whether or not the current
|
|
100
|
+
* network is EIP-1559 compatible.
|
|
101
|
+
* @param options.getCurrentNetworkLegacyGasAPICompatibility - Determines whether or not the
|
|
102
|
+
* current network is compatible with the legacy gas price API.
|
|
103
|
+
* @param options.getCurrentAccountEIP1559Compatibility - Determines whether or not the current
|
|
104
|
+
* account is EIP-1559 compatible.
|
|
105
|
+
* @param options.getChainId - Returns the current chain ID.
|
|
106
|
+
* @param options.getProvider - Returns a network provider for the current network.
|
|
107
|
+
* @param options.onNetworkDidChange - A function for registering an event handler for the
|
|
108
|
+
* network state change event.
|
|
109
|
+
* @param options.legacyAPIEndpoint - The legacy gas price API URL. This option is primarily for
|
|
110
|
+
* testing purposes.
|
|
111
|
+
* @param options.EIP1559APIEndpoint - The EIP-1559 gas price API URL.
|
|
112
|
+
* @param options.clientId - The client ID used to identify to the gas estimation API who is
|
|
113
|
+
* asking for estimates.
|
|
114
|
+
*/
|
|
115
|
+
constructor({ interval = 15000, messenger, state, getCurrentNetworkEIP1559Compatibility, getCurrentAccountEIP1559Compatibility, getChainId, getCurrentNetworkLegacyGasAPICompatibility, getProvider, onNetworkDidChange, legacyAPIEndpoint = exports.LEGACY_GAS_PRICES_API_URL, EIP1559APIEndpoint, clientId, }) {
|
|
116
|
+
super({
|
|
117
|
+
name,
|
|
118
|
+
metadata,
|
|
119
|
+
messenger,
|
|
120
|
+
state: { ...defaultState, ...state },
|
|
121
|
+
});
|
|
122
|
+
_GasFeeController_instances.add(this);
|
|
123
|
+
_GasFeeController_getProvider.set(this, void 0);
|
|
124
|
+
_GasFeeController_getChainId.set(this, void 0);
|
|
125
|
+
this.intervalDelay = interval;
|
|
126
|
+
this.setIntervalLength(interval);
|
|
127
|
+
this.pollTokens = new Set();
|
|
128
|
+
this.getCurrentNetworkEIP1559Compatibility =
|
|
129
|
+
getCurrentNetworkEIP1559Compatibility;
|
|
130
|
+
this.getCurrentNetworkLegacyGasAPICompatibility =
|
|
131
|
+
getCurrentNetworkLegacyGasAPICompatibility;
|
|
132
|
+
this.getCurrentAccountEIP1559Compatibility =
|
|
133
|
+
getCurrentAccountEIP1559Compatibility;
|
|
134
|
+
__classPrivateFieldSet(this, _GasFeeController_getProvider, getProvider, "f");
|
|
135
|
+
this.EIP1559APIEndpoint = EIP1559APIEndpoint;
|
|
136
|
+
this.legacyAPIEndpoint = legacyAPIEndpoint;
|
|
137
|
+
this.clientId = clientId;
|
|
138
|
+
this.messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
|
|
139
|
+
if (onNetworkDidChange && getChainId) {
|
|
140
|
+
__classPrivateFieldSet(this, _GasFeeController_getChainId, getChainId, "f");
|
|
141
|
+
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
142
|
+
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
143
|
+
onNetworkDidChange(async (networkControllerState) => {
|
|
144
|
+
await __classPrivateFieldGet(this, _GasFeeController_instances, "m", _GasFeeController_onNetworkControllerDidChange).call(this, networkControllerState);
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
this.messenger.subscribe('NetworkController:networkDidChange',
|
|
149
|
+
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
150
|
+
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
151
|
+
async (networkControllerState) => {
|
|
152
|
+
await __classPrivateFieldGet(this, _GasFeeController_instances, "m", _GasFeeController_onNetworkControllerDidChange).call(this, networkControllerState);
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Resets the polling interval by stopping and restarting polling
|
|
158
|
+
* with the existing poll tokens.
|
|
159
|
+
*/
|
|
160
|
+
async resetPolling() {
|
|
161
|
+
if (this.pollTokens.size !== 0) {
|
|
162
|
+
const tokens = Array.from(this.pollTokens);
|
|
163
|
+
this.stopPolling();
|
|
164
|
+
await this.getGasFeeEstimatesAndStartPolling(tokens[0]);
|
|
165
|
+
tokens.slice(1).forEach((token) => {
|
|
166
|
+
this.pollTokens.add(token);
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Fetches gas fee estimates.
|
|
172
|
+
*
|
|
173
|
+
* @param options - The gas fee estimate options.
|
|
174
|
+
* @returns The gas fee estimates.
|
|
175
|
+
*/
|
|
176
|
+
async fetchGasFeeEstimates(options) {
|
|
177
|
+
return await this._fetchGasFeeEstimateData(options);
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Gets gas fee estimates and starts polling for updates.
|
|
181
|
+
*
|
|
182
|
+
* @param pollToken - An existing poll token to reuse, or undefined to
|
|
183
|
+
* generate a new one.
|
|
184
|
+
* @returns The poll token that can be used to stop polling.
|
|
185
|
+
*/
|
|
186
|
+
async getGasFeeEstimatesAndStartPolling(pollToken) {
|
|
187
|
+
const _pollToken = pollToken || (0, uuid_1.v1)();
|
|
188
|
+
this.pollTokens.add(_pollToken);
|
|
189
|
+
if (this.pollTokens.size === 1) {
|
|
190
|
+
await this._fetchGasFeeEstimateData();
|
|
191
|
+
this._poll();
|
|
192
|
+
}
|
|
193
|
+
return _pollToken;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Gets and sets gasFeeEstimates in state.
|
|
197
|
+
*
|
|
198
|
+
* @param options - The gas fee estimate options.
|
|
199
|
+
* @param options.shouldUpdateState - Determines whether the state should be updated with the
|
|
200
|
+
* updated gas estimates.
|
|
201
|
+
* @returns The gas fee estimates.
|
|
202
|
+
*/
|
|
203
|
+
async _fetchGasFeeEstimateData(options = {}) {
|
|
204
|
+
const { shouldUpdateState = true, networkClientId } = options;
|
|
205
|
+
let ethQuery, isEIP1559Compatible, isLegacyGasAPICompatible, decimalChainId;
|
|
206
|
+
if (networkClientId !== undefined) {
|
|
207
|
+
const networkClient = this.messenger.call('NetworkController:getNetworkClientById', networkClientId);
|
|
208
|
+
isLegacyGasAPICompatible = networkClient.configuration.chainId === '0x38';
|
|
209
|
+
decimalChainId = (0, controller_utils_1.convertHexToDecimal)(networkClient.configuration.chainId);
|
|
210
|
+
try {
|
|
211
|
+
const result = await this.messenger.call('NetworkController:getEIP1559Compatibility', networkClientId);
|
|
212
|
+
isEIP1559Compatible = result || false;
|
|
213
|
+
}
|
|
214
|
+
catch {
|
|
215
|
+
isEIP1559Compatible = false;
|
|
216
|
+
}
|
|
217
|
+
ethQuery = new eth_query_1.default(networkClient.provider);
|
|
218
|
+
}
|
|
219
|
+
ethQuery ?? (ethQuery = __classPrivateFieldGet(this, _GasFeeController_instances, "m", _GasFeeController_getEthQuery).call(this));
|
|
220
|
+
isLegacyGasAPICompatible ?? (isLegacyGasAPICompatible = this.getCurrentNetworkLegacyGasAPICompatibility());
|
|
221
|
+
decimalChainId ?? (decimalChainId = (0, controller_utils_1.convertHexToDecimal)(__classPrivateFieldGet(this, _GasFeeController_instances, "m", _GasFeeController_getCurrentChainId).call(this)));
|
|
222
|
+
try {
|
|
223
|
+
isEIP1559Compatible ?? (isEIP1559Compatible = await this.getEIP1559Compatibility());
|
|
224
|
+
}
|
|
225
|
+
catch (e) {
|
|
226
|
+
console.error(e);
|
|
227
|
+
isEIP1559Compatible ?? (isEIP1559Compatible = false);
|
|
228
|
+
}
|
|
229
|
+
const gasFeeCalculations = await (0, determineGasFeeCalculations_js_1.default)({
|
|
230
|
+
isEIP1559Compatible,
|
|
231
|
+
isLegacyGasAPICompatible,
|
|
232
|
+
fetchGasEstimates: gas_util_js_1.fetchGasEstimates,
|
|
233
|
+
fetchGasEstimatesUrl: this.EIP1559APIEndpoint.replace('<chain_id>', `${decimalChainId}`),
|
|
234
|
+
fetchLegacyGasPriceEstimates: gas_util_js_1.fetchLegacyGasPriceEstimates,
|
|
235
|
+
fetchLegacyGasPriceEstimatesUrl: this.legacyAPIEndpoint.replace('<chain_id>', `${decimalChainId}`),
|
|
236
|
+
fetchEthGasPriceEstimate: gas_util_js_1.fetchEthGasPriceEstimate,
|
|
237
|
+
calculateTimeEstimate: gas_util_js_1.calculateTimeEstimate,
|
|
238
|
+
clientId: this.clientId,
|
|
239
|
+
ethQuery,
|
|
240
|
+
nonRPCGasFeeApisDisabled: this.state.nonRPCGasFeeApisDisabled,
|
|
241
|
+
});
|
|
242
|
+
if (shouldUpdateState) {
|
|
243
|
+
const chainId = (0, controller_utils_1.toHex)(decimalChainId);
|
|
244
|
+
const currentChainId = __classPrivateFieldGet(this, _GasFeeController_instances, "m", _GasFeeController_getCurrentChainId).call(this);
|
|
245
|
+
this.update((state) => {
|
|
246
|
+
if (currentChainId === chainId) {
|
|
247
|
+
state.gasFeeEstimates = gasFeeCalculations.gasFeeEstimates;
|
|
248
|
+
state.estimatedGasFeeTimeBounds =
|
|
249
|
+
gasFeeCalculations.estimatedGasFeeTimeBounds;
|
|
250
|
+
state.gasEstimateType = gasFeeCalculations.gasEstimateType;
|
|
251
|
+
}
|
|
252
|
+
state.gasFeeEstimatesByChainId ?? (state.gasFeeEstimatesByChainId = {});
|
|
253
|
+
state.gasFeeEstimatesByChainId[chainId] = {
|
|
254
|
+
gasFeeEstimates: gasFeeCalculations.gasFeeEstimates,
|
|
255
|
+
estimatedGasFeeTimeBounds: gasFeeCalculations.estimatedGasFeeTimeBounds,
|
|
256
|
+
gasEstimateType: gasFeeCalculations.gasEstimateType,
|
|
257
|
+
};
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
return gasFeeCalculations;
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Remove the poll token, and stop polling if the set of poll tokens is empty.
|
|
264
|
+
*
|
|
265
|
+
* @param pollToken - The poll token to disconnect.
|
|
266
|
+
*/
|
|
267
|
+
disconnectPoller(pollToken) {
|
|
268
|
+
this.pollTokens.delete(pollToken);
|
|
269
|
+
if (this.pollTokens.size === 0) {
|
|
270
|
+
this.stopPolling();
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Stops polling for gas fee estimates and clears all poll tokens.
|
|
275
|
+
*/
|
|
276
|
+
stopPolling() {
|
|
277
|
+
if (this.intervalId) {
|
|
278
|
+
clearInterval(this.intervalId);
|
|
279
|
+
}
|
|
280
|
+
this.pollTokens.clear();
|
|
281
|
+
this.resetState();
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Prepare to discard this controller.
|
|
285
|
+
*
|
|
286
|
+
* This stops any active polling.
|
|
287
|
+
*/
|
|
288
|
+
destroy() {
|
|
289
|
+
super.destroy();
|
|
290
|
+
this.stopPolling();
|
|
291
|
+
}
|
|
292
|
+
_poll() {
|
|
293
|
+
if (this.intervalId) {
|
|
294
|
+
clearInterval(this.intervalId);
|
|
295
|
+
}
|
|
296
|
+
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
297
|
+
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
298
|
+
this.intervalId = setInterval(async () => {
|
|
299
|
+
await (0, controller_utils_1.safelyExecute)(() => this._fetchGasFeeEstimateData());
|
|
300
|
+
}, this.intervalDelay);
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Fetching token list from the Token Service API.
|
|
304
|
+
*
|
|
305
|
+
* @param input - The input for the poll.
|
|
306
|
+
* @param input.networkClientId - The ID of the network client triggering the fetch.
|
|
307
|
+
* @returns A promise that resolves when this operation completes.
|
|
308
|
+
*/
|
|
309
|
+
async _executePoll({ networkClientId }) {
|
|
310
|
+
await this._fetchGasFeeEstimateData({ networkClientId });
|
|
311
|
+
}
|
|
312
|
+
resetState() {
|
|
313
|
+
this.update(() => {
|
|
314
|
+
return defaultState;
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
async getEIP1559Compatibility() {
|
|
318
|
+
const currentNetworkIsEIP1559Compatible = await this.getCurrentNetworkEIP1559Compatibility();
|
|
319
|
+
const currentAccountIsEIP1559Compatible = this.getCurrentAccountEIP1559Compatibility?.() ?? true;
|
|
320
|
+
return (currentNetworkIsEIP1559Compatible && currentAccountIsEIP1559Compatible);
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Gets the estimated time for a transaction based on the given gas parameters.
|
|
324
|
+
*
|
|
325
|
+
* @param maxPriorityFeePerGas - The maximum priority fee per gas in GWEI.
|
|
326
|
+
* @param maxFeePerGas - The maximum fee per gas in GWEI.
|
|
327
|
+
* @returns The estimated time bounds, or an empty object if fee market
|
|
328
|
+
* estimates are not available.
|
|
329
|
+
*/
|
|
330
|
+
getTimeEstimate(maxPriorityFeePerGas, maxFeePerGas) {
|
|
331
|
+
if (!this.state.gasFeeEstimates ||
|
|
332
|
+
this.state.gasEstimateType !== exports.GAS_ESTIMATE_TYPES.FEE_MARKET) {
|
|
333
|
+
return {};
|
|
334
|
+
}
|
|
335
|
+
return (0, gas_util_js_1.calculateTimeEstimate)(maxPriorityFeePerGas, maxFeePerGas, this.state.gasFeeEstimates);
|
|
336
|
+
}
|
|
337
|
+
enableNonRPCGasFeeApis() {
|
|
338
|
+
this.update((state) => {
|
|
339
|
+
state.nonRPCGasFeeApisDisabled = false;
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
disableNonRPCGasFeeApis() {
|
|
343
|
+
this.update((state) => {
|
|
344
|
+
state.nonRPCGasFeeApisDisabled = true;
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
exports.GasFeeController = GasFeeController;
|
|
349
|
+
_GasFeeController_getProvider = new WeakMap(), _GasFeeController_getChainId = new WeakMap(), _GasFeeController_instances = new WeakSet(), _GasFeeController_onNetworkControllerDidChange = async function _GasFeeController_onNetworkControllerDidChange({ selectedNetworkClientId, }) {
|
|
350
|
+
const newChainId = __classPrivateFieldGet(this, _GasFeeController_instances, "m", _GasFeeController_getChainIdForNetworkClient).call(this, selectedNetworkClientId);
|
|
351
|
+
if (newChainId !== this.currentChainId) {
|
|
352
|
+
// Reset so the next fetch rebuilds it from the new network's provider.
|
|
353
|
+
this.ethQuery = undefined;
|
|
354
|
+
await this.resetPolling();
|
|
355
|
+
this.currentChainId = newChainId;
|
|
356
|
+
}
|
|
357
|
+
}, _GasFeeController_getEthQuery = function _GasFeeController_getEthQuery() {
|
|
358
|
+
this.ethQuery ?? (this.ethQuery = new eth_query_1.default(__classPrivateFieldGet(this, _GasFeeController_getProvider, "f").call(this)));
|
|
359
|
+
return this.ethQuery;
|
|
360
|
+
}, _GasFeeController_getCurrentChainId = function _GasFeeController_getCurrentChainId() {
|
|
361
|
+
this.currentChainId ?? (this.currentChainId = __classPrivateFieldGet(this, _GasFeeController_getChainId, "f")?.call(this) ?? __classPrivateFieldGet(this, _GasFeeController_instances, "m", _GasFeeController_getChainIdFromNetworkController).call(this));
|
|
362
|
+
return this.currentChainId;
|
|
363
|
+
}, _GasFeeController_getChainIdFromNetworkController = function _GasFeeController_getChainIdFromNetworkController() {
|
|
364
|
+
const { selectedNetworkClientId } = this.messenger.call('NetworkController:getState');
|
|
365
|
+
return __classPrivateFieldGet(this, _GasFeeController_instances, "m", _GasFeeController_getChainIdForNetworkClient).call(this, selectedNetworkClientId);
|
|
366
|
+
}, _GasFeeController_getChainIdForNetworkClient = function _GasFeeController_getChainIdForNetworkClient(networkClientId) {
|
|
367
|
+
return this.messenger.call('NetworkController:getNetworkClientById', networkClientId).configuration.chainId;
|
|
368
|
+
};
|
|
369
|
+
exports.default = GasFeeController;
|
|
370
|
+
//# sourceMappingURL=GasFeeController.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GasFeeController.cjs","sourceRoot":"","sources":["../src/GasFeeController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAKA,iEAIoC;AACpC,oEAA2C;AAW3C,qEAA+E;AAE/E,+BAAoC;AAEpC,uGAA2E;AAC3E,gDAKuB;AAGV,QAAA,yBAAyB,GAAG,+CAA+C,CAAC;AAoBzF;;;;;GAKG;AACU,QAAA,kBAAkB,GAAG;IAChC,UAAU,EAAE,YAAqC;IACjD,MAAM,EAAE,QAA8B;IACtC,YAAY,EAAE,cAAyC;IACvD,IAAI,EAAE,MAAwB;CAC/B,CAAC;AAqGF,MAAM,QAAQ,GAA+B;IAC3C,wBAAwB,EAAE;QACxB,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,KAAK;QAC7B,QAAQ,EAAE,IAAI;KACf;IACD,eAAe,EAAE;QACf,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,KAAK;QAC7B,QAAQ,EAAE,IAAI;KACf;IACD,yBAAyB,EAAE;QACzB,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,KAAK;QAC7B,QAAQ,EAAE,IAAI;KACf;IACD,eAAe,EAAE;QACf,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,KAAK;QAC7B,QAAQ,EAAE,IAAI;KACf;IACD,wBAAwB,EAAE;QACxB,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,KAAK;QAC7B,QAAQ,EAAE,KAAK;KAChB;CACF,CAAC;AAsDF,MAAM,IAAI,GAAG,kBAAkB,CAAC;AAEhC,MAAM,yBAAyB,GAAG;IAChC,yBAAyB;IACzB,kBAAkB;IAClB,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/base-controller';\nimport {\n convertHexToDecimal,\n safelyExecute,\n toHex,\n} from '@metamask/controller-utils';\nimport EthQuery from '@metamask/eth-query';\nimport type { Messenger } from '@metamask/messenger';\nimport type {\n NetworkClientId,\n NetworkControllerGetEIP1559CompatibilityAction,\n NetworkControllerGetNetworkClientByIdAction,\n NetworkControllerGetStateAction,\n NetworkControllerNetworkDidChangeEvent,\n NetworkState,\n ProviderProxy,\n} from '@metamask/network-controller';\nimport { StaticIntervalPollingController } from '@metamask/polling-controller';\nimport type { Hex } from '@metamask/utils';\nimport { v1 as random } from 'uuid';\n\nimport determineGasFeeCalculations from './determineGasFeeCalculations.js';\nimport {\n fetchGasEstimates,\n fetchLegacyGasPriceEstimates,\n fetchEthGasPriceEstimate,\n calculateTimeEstimate,\n} from './gas-util.js';\nimport type { GasFeeControllerMethodActions } from './GasFeeController-method-action-types.js';\n\nexport const LEGACY_GAS_PRICES_API_URL = `https://api.metaswap.codefi.network/gasPrices`;\n\nexport type unknownString = 'unknown';\n\n// Fee Market describes the way gas is set after the london hardfork, and was\n// defined by EIP-1559.\nexport type FeeMarketEstimateType = 'fee-market';\n// Legacy describes gasPrice estimates from before london hardfork, when the\n// user is connected to mainnet and are presented with fast/average/slow\n// estimate levels to choose from.\nexport type LegacyEstimateType = 'legacy';\n// EthGasPrice describes a gasPrice estimate received from eth_gasPrice. Post\n// london this value should only be used for legacy type transactions when on\n// networks that support EIP-1559. This type of estimate is the most accurate\n// to display on custom networks that don't support EIP-1559.\nexport type EthGasPriceEstimateType = 'eth_gasPrice';\n// NoEstimate describes the state of the controller before receiving its first\n// estimate.\nexport type NoEstimateType = 'none';\n\n/**\n * Indicates which type of gasEstimate the controller is currently returning.\n * This is useful as a way of asserting that the shape of gasEstimates matches\n * expectations. NONE is a special case indicating that no previous gasEstimate\n * has been fetched.\n */\nexport const GAS_ESTIMATE_TYPES = {\n FEE_MARKET: 'fee-market' as FeeMarketEstimateType,\n LEGACY: 'legacy' as LegacyEstimateType,\n ETH_GASPRICE: 'eth_gasPrice' as EthGasPriceEstimateType,\n NONE: 'none' as NoEstimateType,\n};\n\nexport type GasEstimateType =\n | FeeMarketEstimateType\n | EthGasPriceEstimateType\n | LegacyEstimateType\n | NoEstimateType;\n\nexport type EstimatedGasFeeTimeBounds = {\n lowerTimeBound: number | null;\n upperTimeBound: number | unknownString;\n};\n\n/**\n * @type EthGasPriceEstimate\n *\n * A single gas price estimate for networks and accounts that don't support EIP-1559\n * This estimate comes from eth_gasPrice but is converted to dec gwei to match other\n * return values\n *\n * @property gasPrice - A GWEI dec string\n */\n\nexport type EthGasPriceEstimate = {\n gasPrice: string;\n};\n\n/**\n * @type LegacyGasPriceEstimate\n *\n * A set of gas price estimates for networks and accounts that don't support EIP-1559\n * These estimates include low, medium and high all as strings representing gwei in\n * decimal format.\n *\n * @property high - gasPrice, in decimal gwei string format, suggested for fast inclusion\n * @property medium - gasPrice, in decimal gwei string format, suggested for avg inclusion\n * @property low - gasPrice, in decimal gwei string format, suggested for slow inclusion\n */\nexport type LegacyGasPriceEstimate = {\n high: string;\n medium: string;\n low: string;\n};\n\n/**\n * @type Eip1559GasFee\n *\n * Data necessary to provide an estimate of a gas fee with a specific tip\n *\n * @property minWaitTimeEstimate - The fastest the transaction will take, in milliseconds\n * @property maxWaitTimeEstimate - The slowest the transaction will take, in milliseconds\n * @property suggestedMaxPriorityFeePerGas - A suggested \"tip\", a GWEI hex number\n * @property suggestedMaxFeePerGas - A suggested max fee, the most a user will pay. a GWEI hex number\n */\nexport type Eip1559GasFee = {\n minWaitTimeEstimate: number; // a time duration in milliseconds\n maxWaitTimeEstimate: number; // a time duration in milliseconds\n suggestedMaxPriorityFeePerGas: string; // a GWEI decimal number\n suggestedMaxFeePerGas: string; // a GWEI decimal number\n};\n\n/**\n * @type GasFeeEstimates\n *\n * Data necessary to provide multiple GasFee estimates, and supporting information, to the user\n *\n * @property low - A GasFee for a minimum necessary combination of tip and maxFee\n * @property medium - A GasFee for a recommended combination of tip and maxFee\n * @property high - A GasFee for a high combination of tip and maxFee\n * @property estimatedBaseFee - An estimate of what the base fee will be for the pending/next block. A GWEI dec number\n * @property networkCongestion - A normalized number that can be used to gauge the congestion\n * level of the network, with 0 meaning not congested and 1 meaning extremely congested\n */\nexport type GasFeeEstimates = SourcedGasFeeEstimates | FallbackGasFeeEstimates;\n\ntype SourcedGasFeeEstimates = {\n low: Eip1559GasFee;\n medium: Eip1559GasFee;\n high: Eip1559GasFee;\n estimatedBaseFee: string;\n historicalBaseFeeRange: [string, string];\n baseFeeTrend: 'up' | 'down' | 'level';\n latestPriorityFeeRange: [string, string];\n historicalPriorityFeeRange: [string, string];\n priorityFeeTrend: 'up' | 'down' | 'level';\n networkCongestion: number;\n};\n\ntype FallbackGasFeeEstimates = {\n low: Eip1559GasFee;\n medium: Eip1559GasFee;\n high: Eip1559GasFee;\n estimatedBaseFee: string;\n historicalBaseFeeRange: null;\n baseFeeTrend: null;\n latestPriorityFeeRange: null;\n historicalPriorityFeeRange: null;\n priorityFeeTrend: null;\n networkCongestion: null;\n};\n\nconst metadata: StateMetadata<GasFeeState> = {\n gasFeeEstimatesByChainId: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n gasFeeEstimates: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n estimatedGasFeeTimeBounds: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n gasEstimateType: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: false,\n usedInUi: true,\n },\n nonRPCGasFeeApisDisabled: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: false,\n usedInUi: false,\n },\n};\n\nexport type GasFeeStateEthGasPrice = {\n gasFeeEstimates: EthGasPriceEstimate;\n estimatedGasFeeTimeBounds: Record<string, never>;\n gasEstimateType: EthGasPriceEstimateType;\n};\n\nexport type GasFeeStateFeeMarket = {\n gasFeeEstimates: GasFeeEstimates;\n estimatedGasFeeTimeBounds: EstimatedGasFeeTimeBounds | Record<string, never>;\n gasEstimateType: FeeMarketEstimateType;\n};\n\nexport type GasFeeStateLegacy = {\n gasFeeEstimates: LegacyGasPriceEstimate;\n estimatedGasFeeTimeBounds: Record<string, never>;\n gasEstimateType: LegacyEstimateType;\n};\n\nexport type GasFeeStateNoEstimates = {\n gasFeeEstimates: Record<string, never>;\n estimatedGasFeeTimeBounds: Record<string, never>;\n gasEstimateType: NoEstimateType;\n};\n\nexport type FetchGasFeeEstimateOptions = {\n shouldUpdateState?: boolean;\n networkClientId?: NetworkClientId;\n};\n\n/**\n * @type GasFeeState\n *\n * Gas Fee controller state\n *\n * @property gasFeeEstimates - Gas fee estimate data based on new EIP-1559 properties\n * @property estimatedGasFeeTimeBounds - Estimates representing the minimum and maximum\n */\nexport type SingleChainGasFeeState =\n | GasFeeStateEthGasPrice\n | GasFeeStateFeeMarket\n | GasFeeStateLegacy\n | GasFeeStateNoEstimates;\n\nexport type GasFeeEstimatesByChainId = {\n gasFeeEstimatesByChainId?: Record<string, SingleChainGasFeeState>;\n};\n\nexport type GasFeeState = GasFeeEstimatesByChainId &\n SingleChainGasFeeState & {\n nonRPCGasFeeApisDisabled?: boolean;\n };\n\nconst name = 'GasFeeController';\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'disableNonRPCGasFeeApis',\n 'disconnectPoller',\n 'enableNonRPCGasFeeApis',\n 'fetchGasFeeEstimates',\n 'getGasFeeEstimatesAndStartPolling',\n 'getTimeEstimate',\n 'resetPolling',\n 'stopPolling',\n] as const;\n\nexport type GasFeeStateChange = ControllerStateChangeEvent<\n typeof name,\n GasFeeState\n>;\n\nexport type GetGasFeeState = ControllerGetStateAction<typeof name, GasFeeState>;\n\nexport type GasFeeControllerActions =\n | GetGasFeeState\n | GasFeeControllerMethodActions;\n\nexport type GasFeeControllerEvents = GasFeeStateChange;\n\ntype AllowedActions =\n | NetworkControllerGetStateAction\n | NetworkControllerGetNetworkClientByIdAction\n | NetworkControllerGetEIP1559CompatibilityAction;\n\nexport type GasFeeMessenger = Messenger<\n typeof name,\n GasFeeControllerActions | AllowedActions,\n GasFeeControllerEvents | NetworkControllerNetworkDidChangeEvent\n>;\n\nconst defaultState: GasFeeState = {\n gasFeeEstimatesByChainId: {},\n gasFeeEstimates: {},\n estimatedGasFeeTimeBounds: {},\n gasEstimateType: GAS_ESTIMATE_TYPES.NONE,\n nonRPCGasFeeApisDisabled: false,\n};\n\n/** The input to start polling for the {@link GasFeeController} */\ntype GasFeePollingInput = {\n networkClientId: NetworkClientId;\n};\n\n/**\n * Controller that retrieves gas fee estimate data and polls for updated data on a set interval\n */\nexport class GasFeeController extends StaticIntervalPollingController<GasFeePollingInput>()<\n typeof name,\n GasFeeState,\n GasFeeMessenger\n> {\n private intervalId?: ReturnType<typeof setTimeout>;\n\n private readonly intervalDelay;\n\n private readonly pollTokens: Set<string>;\n\n private readonly legacyAPIEndpoint: string;\n\n private readonly EIP1559APIEndpoint: string;\n\n private readonly getCurrentNetworkEIP1559Compatibility;\n\n private readonly getCurrentNetworkLegacyGasAPICompatibility;\n\n private readonly getCurrentAccountEIP1559Compatibility;\n\n private currentChainId?: Hex;\n\n private ethQuery?: EthQuery;\n\n private readonly clientId?: string;\n\n readonly #getProvider: () => ProviderProxy;\n\n readonly #getChainId?: () => Hex;\n\n /**\n * Creates a GasFeeController instance.\n *\n * @param options - The controller options.\n * @param options.interval - The time in milliseconds to wait between polls.\n * @param options.messenger - The controller messenger.\n * @param options.state - The initial state.\n * @param options.getCurrentNetworkEIP1559Compatibility - Determines whether or not the current\n * network is EIP-1559 compatible.\n * @param options.getCurrentNetworkLegacyGasAPICompatibility - Determines whether or not the\n * current network is compatible with the legacy gas price API.\n * @param options.getCurrentAccountEIP1559Compatibility - Determines whether or not the current\n * account is EIP-1559 compatible.\n * @param options.getChainId - Returns the current chain ID.\n * @param options.getProvider - Returns a network provider for the current network.\n * @param options.onNetworkDidChange - A function for registering an event handler for the\n * network state change event.\n * @param options.legacyAPIEndpoint - The legacy gas price API URL. This option is primarily for\n * testing purposes.\n * @param options.EIP1559APIEndpoint - The EIP-1559 gas price API URL.\n * @param options.clientId - The client ID used to identify to the gas estimation API who is\n * asking for estimates.\n */\n constructor({\n interval = 15000,\n messenger,\n state,\n getCurrentNetworkEIP1559Compatibility,\n getCurrentAccountEIP1559Compatibility,\n getChainId,\n getCurrentNetworkLegacyGasAPICompatibility,\n getProvider,\n onNetworkDidChange,\n legacyAPIEndpoint = LEGACY_GAS_PRICES_API_URL,\n EIP1559APIEndpoint,\n clientId,\n }: {\n interval?: number;\n messenger: GasFeeMessenger;\n state?: GasFeeState;\n getCurrentNetworkEIP1559Compatibility: () => Promise<boolean>;\n getCurrentNetworkLegacyGasAPICompatibility: () => boolean;\n getCurrentAccountEIP1559Compatibility?: () => boolean;\n getChainId?: () => Hex;\n getProvider: () => ProviderProxy;\n onNetworkDidChange?: (listener: (state: NetworkState) => void) => void;\n legacyAPIEndpoint?: string;\n EIP1559APIEndpoint: string;\n clientId?: string;\n }) {\n super({\n name,\n metadata,\n messenger,\n state: { ...defaultState, ...state },\n });\n this.intervalDelay = interval;\n this.setIntervalLength(interval);\n this.pollTokens = new Set();\n this.getCurrentNetworkEIP1559Compatibility =\n getCurrentNetworkEIP1559Compatibility;\n this.getCurrentNetworkLegacyGasAPICompatibility =\n getCurrentNetworkLegacyGasAPICompatibility;\n this.getCurrentAccountEIP1559Compatibility =\n getCurrentAccountEIP1559Compatibility;\n this.#getProvider = getProvider;\n this.EIP1559APIEndpoint = EIP1559APIEndpoint;\n this.legacyAPIEndpoint = legacyAPIEndpoint;\n this.clientId = clientId;\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n\n if (onNetworkDidChange && getChainId) {\n this.#getChainId = getChainId;\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n onNetworkDidChange(async (networkControllerState) => {\n await this.#onNetworkControllerDidChange(networkControllerState);\n });\n } else {\n this.messenger.subscribe(\n 'NetworkController:networkDidChange',\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n async (networkControllerState) => {\n await this.#onNetworkControllerDidChange(networkControllerState);\n },\n );\n }\n }\n\n /**\n * Resets the polling interval by stopping and restarting polling\n * with the existing poll tokens.\n */\n async resetPolling() {\n if (this.pollTokens.size !== 0) {\n const tokens = Array.from(this.pollTokens);\n this.stopPolling();\n await this.getGasFeeEstimatesAndStartPolling(tokens[0]);\n tokens.slice(1).forEach((token) => {\n this.pollTokens.add(token);\n });\n }\n }\n\n /**\n * Fetches gas fee estimates.\n *\n * @param options - The gas fee estimate options.\n * @returns The gas fee estimates.\n */\n async fetchGasFeeEstimates(options?: FetchGasFeeEstimateOptions) {\n return await this._fetchGasFeeEstimateData(options);\n }\n\n /**\n * Gets gas fee estimates and starts polling for updates.\n *\n * @param pollToken - An existing poll token to reuse, or undefined to\n * generate a new one.\n * @returns The poll token that can be used to stop polling.\n */\n async getGasFeeEstimatesAndStartPolling(\n pollToken: string | undefined,\n ): Promise<string> {\n const _pollToken = pollToken || random();\n\n this.pollTokens.add(_pollToken);\n\n if (this.pollTokens.size === 1) {\n await this._fetchGasFeeEstimateData();\n this._poll();\n }\n\n return _pollToken;\n }\n\n /**\n * Gets and sets gasFeeEstimates in state.\n *\n * @param options - The gas fee estimate options.\n * @param options.shouldUpdateState - Determines whether the state should be updated with the\n * updated gas estimates.\n * @returns The gas fee estimates.\n */\n async _fetchGasFeeEstimateData(\n options: FetchGasFeeEstimateOptions = {},\n ): Promise<GasFeeState> {\n const { shouldUpdateState = true, networkClientId } = options;\n\n let ethQuery,\n isEIP1559Compatible,\n isLegacyGasAPICompatible,\n decimalChainId: number;\n\n if (networkClientId !== undefined) {\n const networkClient = this.messenger.call(\n 'NetworkController:getNetworkClientById',\n networkClientId,\n );\n isLegacyGasAPICompatible = networkClient.configuration.chainId === '0x38';\n\n decimalChainId = convertHexToDecimal(networkClient.configuration.chainId);\n\n try {\n const result = await this.messenger.call(\n 'NetworkController:getEIP1559Compatibility',\n networkClientId,\n );\n isEIP1559Compatible = result || false;\n } catch {\n isEIP1559Compatible = false;\n }\n ethQuery = new EthQuery(networkClient.provider);\n }\n\n ethQuery ??= this.#getEthQuery();\n\n isLegacyGasAPICompatible ??=\n this.getCurrentNetworkLegacyGasAPICompatibility();\n\n decimalChainId ??= convertHexToDecimal(this.#getCurrentChainId());\n\n try {\n isEIP1559Compatible ??= await this.getEIP1559Compatibility();\n } catch (e) {\n console.error(e);\n isEIP1559Compatible ??= false;\n }\n\n const gasFeeCalculations = await determineGasFeeCalculations({\n isEIP1559Compatible,\n isLegacyGasAPICompatible,\n fetchGasEstimates,\n fetchGasEstimatesUrl: this.EIP1559APIEndpoint.replace(\n '<chain_id>',\n `${decimalChainId}`,\n ),\n fetchLegacyGasPriceEstimates,\n fetchLegacyGasPriceEstimatesUrl: this.legacyAPIEndpoint.replace(\n '<chain_id>',\n `${decimalChainId}`,\n ),\n fetchEthGasPriceEstimate,\n calculateTimeEstimate,\n clientId: this.clientId,\n ethQuery,\n nonRPCGasFeeApisDisabled: this.state.nonRPCGasFeeApisDisabled,\n });\n\n if (shouldUpdateState) {\n const chainId = toHex(decimalChainId);\n const currentChainId = this.#getCurrentChainId();\n this.update((state) => {\n if (currentChainId === chainId) {\n state.gasFeeEstimates = gasFeeCalculations.gasFeeEstimates;\n state.estimatedGasFeeTimeBounds =\n gasFeeCalculations.estimatedGasFeeTimeBounds;\n state.gasEstimateType = gasFeeCalculations.gasEstimateType;\n }\n state.gasFeeEstimatesByChainId ??= {};\n state.gasFeeEstimatesByChainId[chainId] = {\n gasFeeEstimates: gasFeeCalculations.gasFeeEstimates,\n estimatedGasFeeTimeBounds:\n gasFeeCalculations.estimatedGasFeeTimeBounds,\n gasEstimateType: gasFeeCalculations.gasEstimateType,\n } as SingleChainGasFeeState;\n });\n }\n\n return gasFeeCalculations;\n }\n\n /**\n * Remove the poll token, and stop polling if the set of poll tokens is empty.\n *\n * @param pollToken - The poll token to disconnect.\n */\n disconnectPoller(pollToken: string) {\n this.pollTokens.delete(pollToken);\n if (this.pollTokens.size === 0) {\n this.stopPolling();\n }\n }\n\n /**\n * Stops polling for gas fee estimates and clears all poll tokens.\n */\n stopPolling() {\n if (this.intervalId) {\n clearInterval(this.intervalId);\n }\n this.pollTokens.clear();\n this.resetState();\n }\n\n /**\n * Prepare to discard this controller.\n *\n * This stops any active polling.\n */\n override destroy() {\n super.destroy();\n this.stopPolling();\n }\n\n private _poll() {\n if (this.intervalId) {\n clearInterval(this.intervalId);\n }\n\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n this.intervalId = setInterval(async () => {\n await safelyExecute(() => this._fetchGasFeeEstimateData());\n }, this.intervalDelay);\n }\n\n /**\n * Fetching token list from the Token Service API.\n *\n * @param input - The input for the poll.\n * @param input.networkClientId - The ID of the network client triggering the fetch.\n * @returns A promise that resolves when this operation completes.\n */\n async _executePoll({ networkClientId }: GasFeePollingInput): Promise<void> {\n await this._fetchGasFeeEstimateData({ networkClientId });\n }\n\n private resetState() {\n this.update(() => {\n return defaultState;\n });\n }\n\n private async getEIP1559Compatibility() {\n const currentNetworkIsEIP1559Compatible =\n await this.getCurrentNetworkEIP1559Compatibility();\n const currentAccountIsEIP1559Compatible =\n this.getCurrentAccountEIP1559Compatibility?.() ?? true;\n\n return (\n currentNetworkIsEIP1559Compatible && currentAccountIsEIP1559Compatible\n );\n }\n\n /**\n * Gets the estimated time for a transaction based on the given gas parameters.\n *\n * @param maxPriorityFeePerGas - The maximum priority fee per gas in GWEI.\n * @param maxFeePerGas - The maximum fee per gas in GWEI.\n * @returns The estimated time bounds, or an empty object if fee market\n * estimates are not available.\n */\n getTimeEstimate(\n maxPriorityFeePerGas: string,\n maxFeePerGas: string,\n ): EstimatedGasFeeTimeBounds | Record<string, never> {\n if (\n !this.state.gasFeeEstimates ||\n this.state.gasEstimateType !== GAS_ESTIMATE_TYPES.FEE_MARKET\n ) {\n return {};\n }\n return calculateTimeEstimate(\n maxPriorityFeePerGas,\n maxFeePerGas,\n this.state.gasFeeEstimates,\n );\n }\n\n async #onNetworkControllerDidChange({\n selectedNetworkClientId,\n }: NetworkState) {\n const newChainId = this.#getChainIdForNetworkClient(\n selectedNetworkClientId,\n );\n\n if (newChainId !== this.currentChainId) {\n // Reset so the next fetch rebuilds it from the new network's provider.\n this.ethQuery = undefined;\n await this.resetPolling();\n\n this.currentChainId = newChainId;\n }\n }\n\n #getEthQuery(): EthQuery {\n this.ethQuery ??= new EthQuery(this.#getProvider());\n return this.ethQuery;\n }\n\n #getCurrentChainId(): Hex {\n this.currentChainId ??=\n this.#getChainId?.() ?? this.#getChainIdFromNetworkController();\n return this.currentChainId;\n }\n\n #getChainIdFromNetworkController(): Hex {\n const { selectedNetworkClientId } = this.messenger.call(\n 'NetworkController:getState',\n );\n return this.#getChainIdForNetworkClient(selectedNetworkClientId);\n }\n\n #getChainIdForNetworkClient(networkClientId: NetworkClientId): Hex {\n return this.messenger.call(\n 'NetworkController:getNetworkClientById',\n networkClientId,\n ).configuration.chainId;\n }\n\n enableNonRPCGasFeeApis() {\n this.update((state) => {\n state.nonRPCGasFeeApisDisabled = false;\n });\n }\n\n disableNonRPCGasFeeApis() {\n this.update((state) => {\n state.nonRPCGasFeeApisDisabled = true;\n });\n }\n}\n\nexport default GasFeeController;\n"]}
|