@metamask-previews/sample-controllers 2.0.2-preview-0458fe94 → 2.0.2-preview-b6fecd10
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 -5
- package/dist/sample-gas-prices-controller.cjs +20 -29
- package/dist/sample-gas-prices-controller.cjs.map +1 -1
- package/dist/sample-gas-prices-controller.d.cts +16 -26
- package/dist/sample-gas-prices-controller.d.cts.map +1 -1
- package/dist/sample-gas-prices-controller.d.mts +16 -26
- package/dist/sample-gas-prices-controller.d.mts.map +1 -1
- package/dist/sample-gas-prices-controller.mjs +19 -28
- package/dist/sample-gas-prices-controller.mjs.map +1 -1
- package/dist/sample-gas-prices-service/sample-gas-prices-service.cjs +9 -14
- package/dist/sample-gas-prices-service/sample-gas-prices-service.cjs.map +1 -1
- package/dist/sample-gas-prices-service/sample-gas-prices-service.d.cts +11 -16
- package/dist/sample-gas-prices-service/sample-gas-prices-service.d.cts.map +1 -1
- package/dist/sample-gas-prices-service/sample-gas-prices-service.d.mts +11 -16
- package/dist/sample-gas-prices-service/sample-gas-prices-service.d.mts.map +1 -1
- package/dist/sample-gas-prices-service/sample-gas-prices-service.mjs +9 -14
- package/dist/sample-gas-prices-service/sample-gas-prices-service.mjs.map +1 -1
- package/dist/sample-petnames-controller.cjs +13 -18
- package/dist/sample-petnames-controller.cjs.map +1 -1
- package/dist/sample-petnames-controller.d.cts +12 -18
- package/dist/sample-petnames-controller.d.cts.map +1 -1
- package/dist/sample-petnames-controller.d.mts +12 -18
- package/dist/sample-petnames-controller.d.mts.map +1 -1
- package/dist/sample-petnames-controller.mjs +12 -17
- package/dist/sample-petnames-controller.mjs.map +1 -1
- package/package.json +1 -2
package/CHANGELOG.md
CHANGED
|
@@ -7,11 +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:** Migrate to new `Messenger` class ([#6335](https://github.com/MetaMask/core/pull/6335))
|
|
13
|
-
- **BREAKING:** Rename metadata property `anonymous` to `includeInDebugSnapshot` ([#6335](https://github.com/MetaMask/core/pull/6335))
|
|
14
|
-
|
|
15
10
|
## [2.0.2]
|
|
16
11
|
|
|
17
12
|
### Changed
|
|
@@ -13,7 +13,7 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (
|
|
|
13
13
|
var _SampleGasPricesController_instances, _SampleGasPricesController_selectedChainId, _SampleGasPricesController_onSelectedNetworkClientIdChange;
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.SampleGasPricesController = exports.getDefaultSampleGasPricesControllerState = exports.controllerName = void 0;
|
|
16
|
-
const
|
|
16
|
+
const base_controller_1 = require("@metamask/base-controller");
|
|
17
17
|
// === GENERAL ===
|
|
18
18
|
/**
|
|
19
19
|
* The name of the {@link SampleGasPricesController}, used to namespace the
|
|
@@ -26,9 +26,9 @@ exports.controllerName = 'SampleGasPricesController';
|
|
|
26
26
|
*/
|
|
27
27
|
const gasPricesControllerMetadata = {
|
|
28
28
|
gasPricesByChainId: {
|
|
29
|
-
includeInDebugSnapshot: false,
|
|
30
29
|
includeInStateLogs: true,
|
|
31
30
|
persist: true,
|
|
31
|
+
anonymous: false,
|
|
32
32
|
usedInUi: true,
|
|
33
33
|
},
|
|
34
34
|
};
|
|
@@ -55,7 +55,7 @@ const MESSENGER_EXPOSED_METHODS = ['updateGasPrices'];
|
|
|
55
55
|
* @example
|
|
56
56
|
*
|
|
57
57
|
* ``` ts
|
|
58
|
-
* import { Messenger } from '@metamask/
|
|
58
|
+
* import { Messenger } from '@metamask/base-controller';
|
|
59
59
|
* import type {
|
|
60
60
|
* NetworkControllerActions,
|
|
61
61
|
* NetworkControllerEvents,
|
|
@@ -72,23 +72,18 @@ const MESSENGER_EXPOSED_METHODS = ['updateGasPrices'];
|
|
|
72
72
|
* selectGasPrices,
|
|
73
73
|
* } from '@metamask/sample-controllers';
|
|
74
74
|
*
|
|
75
|
-
* const
|
|
76
|
-
* 'Root',
|
|
75
|
+
* const globalMessenger = new Messenger<
|
|
77
76
|
* SampleGasPricesServiceActions
|
|
78
77
|
* | SampleGasPricesControllerActions
|
|
79
78
|
* | NetworkControllerActions,
|
|
80
79
|
* SampleGasPricesServiceEvents
|
|
81
80
|
* | SampleGasPricesControllerEvents
|
|
82
81
|
* | NetworkControllerEvents
|
|
83
|
-
* >(
|
|
84
|
-
* const gasPricesServiceMessenger =
|
|
85
|
-
* 'SampleGasPricesService',
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
* typeof rootMessenger,
|
|
89
|
-
* >({
|
|
90
|
-
* namespace: 'SampleGasPricesService',
|
|
91
|
-
* parent: rootMessenger,
|
|
82
|
+
* >();
|
|
83
|
+
* const gasPricesServiceMessenger = globalMessenger.getRestricted({
|
|
84
|
+
* name: 'SampleGasPricesService',
|
|
85
|
+
* allowedActions: [],
|
|
86
|
+
* allowedEvents: [],
|
|
92
87
|
* });
|
|
93
88
|
* // Instantiate the service to register its actions on the messenger
|
|
94
89
|
* new SampleGasPricesService({
|
|
@@ -96,14 +91,10 @@ const MESSENGER_EXPOSED_METHODS = ['updateGasPrices'];
|
|
|
96
91
|
* // We assume you're using this in the browser.
|
|
97
92
|
* fetch,
|
|
98
93
|
* });
|
|
99
|
-
* const gasPricesControllerMessenger =
|
|
100
|
-
* 'SampleGasPricesController',
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
* typeof rootMessenger,
|
|
104
|
-
* >({
|
|
105
|
-
* namespace: 'SampleGasPricesController',
|
|
106
|
-
* parent: rootMessenger,
|
|
94
|
+
* const gasPricesControllerMessenger = globalMessenger.getRestricted({
|
|
95
|
+
* name: 'SampleGasPricesController',
|
|
96
|
+
* allowedActions: ['NetworkController:getNetworkClientById'],
|
|
97
|
+
* allowedEvents: ['NetworkController:stateChange'],
|
|
107
98
|
* });
|
|
108
99
|
* // Instantiate the controller to register its actions on the messenger
|
|
109
100
|
* new SampleGasPricesController({
|
|
@@ -111,18 +102,18 @@ const MESSENGER_EXPOSED_METHODS = ['updateGasPrices'];
|
|
|
111
102
|
* });
|
|
112
103
|
*
|
|
113
104
|
* // Later...
|
|
114
|
-
* await
|
|
105
|
+
* await globalMessenger.call(
|
|
115
106
|
* 'SampleGasPricesController:updateGasPrices',
|
|
116
107
|
* { chainId: '0x42' },
|
|
117
108
|
* );
|
|
118
|
-
* const gasPricesControllerState = await
|
|
109
|
+
* const gasPricesControllerState = await globalMessenger.call(
|
|
119
110
|
* 'SampleGasPricesController:getState',
|
|
120
111
|
* );
|
|
121
112
|
* gasPricesControllerState.gasPricesByChainId
|
|
122
113
|
* // => { '0x42': { low: 5, average: 10, high: 15, fetchedDate: '2024-01-02T00:00:00.000Z' } }
|
|
123
114
|
* ```
|
|
124
115
|
*/
|
|
125
|
-
class SampleGasPricesController extends
|
|
116
|
+
class SampleGasPricesController extends base_controller_1.BaseController {
|
|
126
117
|
/**
|
|
127
118
|
* Constructs a new {@link SampleGasPricesController}.
|
|
128
119
|
*
|
|
@@ -146,8 +137,8 @@ class SampleGasPricesController extends next_1.BaseController {
|
|
|
146
137
|
* The globally selected chain ID.
|
|
147
138
|
*/
|
|
148
139
|
_SampleGasPricesController_selectedChainId.set(this, void 0);
|
|
149
|
-
this.
|
|
150
|
-
this.
|
|
140
|
+
this.messagingSystem.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
|
|
141
|
+
this.messagingSystem.subscribe('NetworkController:stateChange', __classPrivateFieldGet(this, _SampleGasPricesController_instances, "m", _SampleGasPricesController_onSelectedNetworkClientIdChange).bind(this), (networkControllerState) => networkControllerState.selectedNetworkClientId);
|
|
151
142
|
}
|
|
152
143
|
/**
|
|
153
144
|
* Fetches the latest gas prices for the given chain and persists them to
|
|
@@ -157,7 +148,7 @@ class SampleGasPricesController extends next_1.BaseController {
|
|
|
157
148
|
* @param args.chainId - The chain ID for which to fetch gas prices.
|
|
158
149
|
*/
|
|
159
150
|
async updateGasPrices({ chainId }) {
|
|
160
|
-
const gasPricesResponse = await this.
|
|
151
|
+
const gasPricesResponse = await this.messagingSystem.call('SampleGasPricesService:fetchGasPrices', chainId);
|
|
161
152
|
this.update((state) => {
|
|
162
153
|
state.gasPricesByChainId[chainId] = {
|
|
163
154
|
...gasPricesResponse,
|
|
@@ -175,7 +166,7 @@ _SampleGasPricesController_selectedChainId = new WeakMap(), _SampleGasPricesCont
|
|
|
175
166
|
* @param selectedNetworkClientId - The globally selected network client ID.
|
|
176
167
|
*/
|
|
177
168
|
async function _SampleGasPricesController_onSelectedNetworkClientIdChange(selectedNetworkClientId) {
|
|
178
|
-
const { configuration: { chainId }, } = this.
|
|
169
|
+
const { configuration: { chainId }, } = this.messagingSystem.call('NetworkController:getNetworkClientById', selectedNetworkClientId);
|
|
179
170
|
if (chainId !== __classPrivateFieldGet(this, _SampleGasPricesController_selectedChainId, "f")) {
|
|
180
171
|
__classPrivateFieldSet(this, _SampleGasPricesController_selectedChainId, chainId, "f");
|
|
181
172
|
await this.updateGasPrices({ chainId });
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sample-gas-prices-controller.cjs","sourceRoot":"","sources":["../src/sample-gas-prices-controller.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAKA,yDAAgE;AAYhE,kBAAkB;AAElB;;;;GAIG;AACU,QAAA,cAAc,GAAG,2BAA2B,CAAC;AAsC1D;;GAEG;AACH,MAAM,2BAA2B,GAAG;IAClC,kBAAkB,EAAE;QAClB,sBAAsB,EAAE,KAAK;QAC7B,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,QAAQ,EAAE,IAAI;KACf;CACsD,CAAC;AAE1D;;;;;;;GAOG;AACH,SAAgB,wCAAwC;IACtD,OAAO;QACL,kBAAkB,EAAE,EAAE;KACvB,CAAC;AACJ,CAAC;AAJD,4F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AAa,EAAE,EAAE,OAAO,EAAE,GAC3B,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CACrB,wCAAwC,EACxC,uBAAuB,CACxB,CAAC;IAEF,IAAI,OAAO,KAAK,uBAAA,IAAI,kDAAiB,EAAE;QACrC,uBAAA,IAAI,8CAAoB,OAAO,MAAA,CAAC;QAChC,MAAM,IAAI,CAAC,eAAe,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC;KACzC;AACH,CAAC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller/next';\nimport { BaseController } from '@metamask/base-controller/next';\nimport type { Messenger } from '@metamask/messenger';\nimport type {\n NetworkClientId,\n NetworkControllerGetNetworkClientByIdAction,\n NetworkControllerStateChangeEvent,\n} from '@metamask/network-controller';\nimport type { Hex } from '@metamask/utils';\n\nimport type { SampleGasPricesControllerMethodActions } from './sample-gas-prices-controller-method-action-types';\nimport type { SampleGasPricesServiceFetchGasPricesAction } from './sample-gas-prices-service/sample-gas-prices-service-method-action-types';\n\n// === GENERAL ===\n\n/**\n * The name of the {@link SampleGasPricesController}, used to namespace the\n * controller's actions and events and to namespace the controller's state data\n * when composed with other controllers.\n */\nexport const controllerName = 'SampleGasPricesController';\n\n// === STATE ===\n\n/**\n * The collection of gas price data fetched periodically.\n */\ntype GasPrices = {\n /**\n * The total estimated gas in the \"low\" bucket.\n */\n low: number;\n /**\n * The total estimated gas in the \"average\" bucket.\n */\n average: number;\n /**\n * The total estimated gas in the \"high\" bucket.\n */\n high: number;\n /**\n * The date/time (in ISO-8601 format) when prices were fetched.\n */\n fetchedDate: string;\n};\n\n/**\n * Describes the shape of the state object for {@link SampleGasPricesController}.\n */\nexport type SampleGasPricesControllerState = {\n /**\n * Fetched gas prices categorized by chain ID.\n */\n gasPricesByChainId: {\n [chainId: Hex]: GasPrices;\n };\n};\n\n/**\n * The metadata for each property in {@link SampleGasPricesControllerState}.\n */\nconst gasPricesControllerMetadata = {\n gasPricesByChainId: {\n includeInDebugSnapshot: false,\n includeInStateLogs: true,\n persist: true,\n usedInUi: true,\n },\n} satisfies StateMetadata<SampleGasPricesControllerState>;\n\n/**\n * Constructs the default {@link SampleGasPricesController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link SampleGasPricesController} state.\n */\nexport function getDefaultSampleGasPricesControllerState(): SampleGasPricesControllerState {\n return {\n gasPricesByChainId: {},\n };\n}\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = ['updateGasPrices'] as const;\n\n/**\n * Retrieves the state of the {@link SampleGasPricesController}.\n */\nexport type SampleGasPricesControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n SampleGasPricesControllerState\n>;\n\n/**\n * Actions that {@link SampleGasPricesMessenger} exposes to other consumers.\n */\nexport type SampleGasPricesControllerActions =\n | SampleGasPricesControllerGetStateAction\n | SampleGasPricesControllerMethodActions;\n\n/**\n * Actions from other messengers that {@link SampleGasPricesMessenger} calls.\n */\ntype AllowedActions =\n | NetworkControllerGetNetworkClientByIdAction\n | SampleGasPricesServiceFetchGasPricesAction;\n\n/**\n * Published when the state of {@link SampleGasPricesController} changes.\n */\nexport type SampleGasPricesControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n SampleGasPricesControllerState\n >;\n\n/**\n * Events that {@link SampleGasPricesMessenger} exposes to other consumers.\n */\nexport type SampleGasPricesControllerEvents =\n SampleGasPricesControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link SampleGasPricesMessenger} subscribes\n * to.\n */\ntype AllowedEvents = NetworkControllerStateChangeEvent;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link SampleGasPricesController}.\n */\nexport type SampleGasPricesControllerMessenger = Messenger<\n typeof controllerName,\n SampleGasPricesControllerActions | AllowedActions,\n SampleGasPricesControllerEvents | AllowedEvents\n>;\n\n// === CONTROLLER DEFINITION ===\n\n/**\n * `SampleGasPricesController` fetches and persists gas prices for various chains.\n *\n * @example\n *\n * ``` ts\n * import { Messenger } from '@metamask/messenger';\n * import type {\n * NetworkControllerActions,\n * NetworkControllerEvents,\n * } from '@metamask/network-controller';\n * import type {\n * SampleGasPricesControllerActions,\n * SampleGasPricesControllerEvents,\n * SampleGasPricesServiceActions,\n * SampleGasPricesServiceEvents,\n * } from '@metamask/sample-controllers';\n * import {\n * SampleGasPricesController,\n * SampleGasPricesService,\n * selectGasPrices,\n * } from '@metamask/sample-controllers';\n *\n * const rootMessenger = new Messenger<\n * 'Root',\n * SampleGasPricesServiceActions\n * | SampleGasPricesControllerActions\n * | NetworkControllerActions,\n * SampleGasPricesServiceEvents\n * | SampleGasPricesControllerEvents\n * | NetworkControllerEvents\n * >({ namespace: 'Root' });\n * const gasPricesServiceMessenger = new Messenger<\n * 'SampleGasPricesService',\n * SampleGasPricesServiceActions,\n * SampleGasPricesServiceEvents,\n * typeof rootMessenger,\n * >({\n * namespace: 'SampleGasPricesService',\n * parent: rootMessenger,\n * });\n * // Instantiate the service to register its actions on the messenger\n * new SampleGasPricesService({\n * messenger: gasPricesServiceMessenger,\n * // We assume you're using this in the browser.\n * fetch,\n * });\n * const gasPricesControllerMessenger = new Messenger<\n * 'SampleGasPricesController',\n * SampleGasPricesControllerActions | NetworkControllerGetNetworkClientByIdAction,\n * SampleGasPricesControllerEvents | NetworkControllerStateChangeEvent,\n * typeof rootMessenger,\n * >({\n * namespace: 'SampleGasPricesController',\n * parent: rootMessenger,\n * });\n * // Instantiate the controller to register its actions on the messenger\n * new SampleGasPricesController({\n * messenger: gasPricesControllerMessenger,\n * });\n *\n * // Later...\n * await rootMessenger.call(\n * 'SampleGasPricesController:updateGasPrices',\n * { chainId: '0x42' },\n * );\n * const gasPricesControllerState = await rootMessenger.call(\n * 'SampleGasPricesController:getState',\n * );\n * gasPricesControllerState.gasPricesByChainId\n * // => { '0x42': { low: 5, average: 10, high: 15, fetchedDate: '2024-01-02T00:00:00.000Z' } }\n * ```\n */\nexport class SampleGasPricesController extends BaseController<\n typeof controllerName,\n SampleGasPricesControllerState,\n SampleGasPricesControllerMessenger\n> {\n /**\n * The globally selected chain ID.\n */\n #selectedChainId: Hex | undefined;\n\n /**\n * Constructs a new {@link SampleGasPricesController}.\n *\n * @param args - The constructor arguments.\n * @param args.messenger - The messenger suited for this controller.\n * @param args.state - The desired state with which to initialize this\n * controller. Missing properties will be filled in with defaults.\n */\n constructor({\n messenger,\n state,\n }: {\n messenger: SampleGasPricesControllerMessenger;\n state?: Partial<SampleGasPricesControllerState>;\n }) {\n super({\n messenger,\n metadata: gasPricesControllerMetadata,\n name: controllerName,\n state: {\n ...getDefaultSampleGasPricesControllerState(),\n ...state,\n },\n });\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n\n this.messenger.subscribe(\n 'NetworkController:stateChange',\n this.#onSelectedNetworkClientIdChange.bind(this),\n (networkControllerState) =>\n networkControllerState.selectedNetworkClientId,\n );\n }\n\n /**\n * Fetches the latest gas prices for the given chain and persists them to\n * state.\n *\n * @param args - The arguments to the function.\n * @param args.chainId - The chain ID for which to fetch gas prices.\n */\n async updateGasPrices({ chainId }: { chainId: Hex }) {\n const gasPricesResponse = await this.messenger.call(\n 'SampleGasPricesService:fetchGasPrices',\n chainId,\n );\n\n this.update((state) => {\n state.gasPricesByChainId[chainId] = {\n ...gasPricesResponse,\n fetchedDate: new Date().toISOString(),\n };\n });\n }\n\n /**\n * Callback to call when the globally selected network client ID changes,\n * ensuring that gas prices get updated.\n *\n * @param selectedNetworkClientId - The globally selected network client ID.\n */\n async #onSelectedNetworkClientIdChange(\n selectedNetworkClientId: NetworkClientId,\n ) {\n const {\n configuration: { chainId },\n } = this.messenger.call(\n 'NetworkController:getNetworkClientById',\n selectedNetworkClientId,\n );\n\n if (chainId !== this.#selectedChainId) {\n this.#selectedChainId = chainId;\n await this.updateGasPrices({ chainId });\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"sample-gas-prices-controller.cjs","sourceRoot":"","sources":["../src/sample-gas-prices-controller.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAMA,+DAA2D;AAW3D,kBAAkB;AAElB;;;;GAIG;AACU,QAAA,cAAc,GAAG,2BAA2B,CAAC;AAsC1D;;GAEG;AACH,MAAM,2BAA2B,GAAG;IAClC,kBAAkB,EAAE;QAClB,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,KAAK;QAChB,QAAQ,EAAE,IAAI;KACf;CACsD,CAAC;AAE1D;;;;;;;GAOG;AACH,SAAgB,wCAAwC;IACtD,OAAO;QACL,kBAAkB,EAAE,EAAE;KACvB,CAAC;AACJ,CAAC;AAJD,4FAIC;AAED,oBAAoB;AAEpB,MAAM,yBAAyB,GAAG,CAAC,iBAAiB,CAAU,CAAC;AAyD/D,gCAAgC;AAEhC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+DG;AACH,MAAa,yBAA0B,SAAQ,gCAI9C;IAMC;;;;;;;OAOG;IACH,YAAY,EACV,SAAS,EACT,KAAK,GAIN;QACC,KAAK,CAAC;YACJ,SAAS;YACT,QAAQ,EAAE,2BAA2B;YACrC,IAAI,EAAE,sBAAc;YACpB,KAAK,EAAE;gBACL,GAAG,wCAAwC,EAAE;gBAC7C,GAAG,KAAK;aACT;SACF,CAAC,CAAC;;QA5BL;;WAEG;QACH,6DAAkC;QA2BhC,IAAI,CAAC,eAAe,CAAC,4BAA4B,CAC/C,IAAI,EACJ,yBAAyB,CAC1B,CAAC;QAEF,IAAI,CAAC,eAAe,CAAC,SAAS,CAC5B,+BAA+B,EAC/B,uBAAA,IAAI,wGAAiC,CAAC,IAAI,CAAC,IAAI,CAAC,EAChD,CAAC,sBAAsB,EAAE,EAAE,CACzB,sBAAsB,CAAC,uBAAuB,CACjD,CAAC;IACJ,CAAC;IAED;;;;;;OAMG;IACH,KAAK,CAAC,eAAe,CAAC,EAAE,OAAO,EAAoB;QACjD,MAAM,iBAAiB,GAAG,MAAM,IAAI,CAAC,eAAe,CAAC,IAAI,CACvD,uCAAuC,EACvC,OAAO,CACR,CAAC;QAEF,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,kBAAkB,CAAC,OAAO,CAAC,GAAG;gBAClC,GAAG,iBAAiB;gBACpB,WAAW,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;aACtC,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;CAuBF;AA1FD,8DA0FC;;AArBC;;;;;GAKG;AACH,KAAK,qEACH,uBAAwC;IAExC,MAAM,EACJ,aAAa,EAAE,EAAE,OAAO,EAAE,GAC3B,GAAG,IAAI,CAAC,eAAe,CAAC,IAAI,CAC3B,wCAAwC,EACxC,uBAAuB,CACxB,CAAC;IAEF,IAAI,OAAO,KAAK,uBAAA,IAAI,kDAAiB,EAAE;QACrC,uBAAA,IAAI,8CAAoB,OAAO,MAAA,CAAC;QAChC,MAAM,IAAI,CAAC,eAAe,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC;KACzC;AACH,CAAC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n RestrictedMessenger,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type {\n NetworkClientId,\n NetworkControllerGetNetworkClientByIdAction,\n NetworkControllerStateChangeEvent,\n} from '@metamask/network-controller';\nimport type { Hex } from '@metamask/utils';\n\nimport type { SampleGasPricesControllerMethodActions } from './sample-gas-prices-controller-method-action-types';\nimport type { SampleGasPricesServiceFetchGasPricesAction } from './sample-gas-prices-service/sample-gas-prices-service-method-action-types';\n\n// === GENERAL ===\n\n/**\n * The name of the {@link SampleGasPricesController}, used to namespace the\n * controller's actions and events and to namespace the controller's state data\n * when composed with other controllers.\n */\nexport const controllerName = 'SampleGasPricesController';\n\n// === STATE ===\n\n/**\n * The collection of gas price data fetched periodically.\n */\ntype GasPrices = {\n /**\n * The total estimated gas in the \"low\" bucket.\n */\n low: number;\n /**\n * The total estimated gas in the \"average\" bucket.\n */\n average: number;\n /**\n * The total estimated gas in the \"high\" bucket.\n */\n high: number;\n /**\n * The date/time (in ISO-8601 format) when prices were fetched.\n */\n fetchedDate: string;\n};\n\n/**\n * Describes the shape of the state object for {@link SampleGasPricesController}.\n */\nexport type SampleGasPricesControllerState = {\n /**\n * Fetched gas prices categorized by chain ID.\n */\n gasPricesByChainId: {\n [chainId: Hex]: GasPrices;\n };\n};\n\n/**\n * The metadata for each property in {@link SampleGasPricesControllerState}.\n */\nconst gasPricesControllerMetadata = {\n gasPricesByChainId: {\n includeInStateLogs: true,\n persist: true,\n anonymous: false,\n usedInUi: true,\n },\n} satisfies StateMetadata<SampleGasPricesControllerState>;\n\n/**\n * Constructs the default {@link SampleGasPricesController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link SampleGasPricesController} state.\n */\nexport function getDefaultSampleGasPricesControllerState(): SampleGasPricesControllerState {\n return {\n gasPricesByChainId: {},\n };\n}\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = ['updateGasPrices'] as const;\n\n/**\n * Retrieves the state of the {@link SampleGasPricesController}.\n */\nexport type SampleGasPricesControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n SampleGasPricesControllerState\n>;\n\n/**\n * Actions that {@link SampleGasPricesMessenger} exposes to other consumers.\n */\nexport type SampleGasPricesControllerActions =\n | SampleGasPricesControllerGetStateAction\n | SampleGasPricesControllerMethodActions;\n\n/**\n * Actions from other messengers that {@link SampleGasPricesMessenger} calls.\n */\ntype AllowedActions =\n | NetworkControllerGetNetworkClientByIdAction\n | SampleGasPricesServiceFetchGasPricesAction;\n\n/**\n * Published when the state of {@link SampleGasPricesController} changes.\n */\nexport type SampleGasPricesControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n SampleGasPricesControllerState\n >;\n\n/**\n * Events that {@link SampleGasPricesMessenger} exposes to other consumers.\n */\nexport type SampleGasPricesControllerEvents =\n SampleGasPricesControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link SampleGasPricesMessenger} subscribes\n * to.\n */\ntype AllowedEvents = NetworkControllerStateChangeEvent;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link SampleGasPricesController}.\n */\nexport type SampleGasPricesControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n SampleGasPricesControllerActions | AllowedActions,\n SampleGasPricesControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\n// === CONTROLLER DEFINITION ===\n\n/**\n * `SampleGasPricesController` fetches and persists gas prices for various chains.\n *\n * @example\n *\n * ``` ts\n * import { Messenger } from '@metamask/base-controller';\n * import type {\n * NetworkControllerActions,\n * NetworkControllerEvents,\n * } from '@metamask/network-controller';\n * import type {\n * SampleGasPricesControllerActions,\n * SampleGasPricesControllerEvents,\n * SampleGasPricesServiceActions,\n * SampleGasPricesServiceEvents,\n * } from '@metamask/sample-controllers';\n * import {\n * SampleGasPricesController,\n * SampleGasPricesService,\n * selectGasPrices,\n * } from '@metamask/sample-controllers';\n *\n * const globalMessenger = new Messenger<\n * SampleGasPricesServiceActions\n * | SampleGasPricesControllerActions\n * | NetworkControllerActions,\n * SampleGasPricesServiceEvents\n * | SampleGasPricesControllerEvents\n * | NetworkControllerEvents\n * >();\n * const gasPricesServiceMessenger = globalMessenger.getRestricted({\n * name: 'SampleGasPricesService',\n * allowedActions: [],\n * allowedEvents: [],\n * });\n * // Instantiate the service to register its actions on the messenger\n * new SampleGasPricesService({\n * messenger: gasPricesServiceMessenger,\n * // We assume you're using this in the browser.\n * fetch,\n * });\n * const gasPricesControllerMessenger = globalMessenger.getRestricted({\n * name: 'SampleGasPricesController',\n * allowedActions: ['NetworkController:getNetworkClientById'],\n * allowedEvents: ['NetworkController:stateChange'],\n * });\n * // Instantiate the controller to register its actions on the messenger\n * new SampleGasPricesController({\n * messenger: gasPricesControllerMessenger,\n * });\n *\n * // Later...\n * await globalMessenger.call(\n * 'SampleGasPricesController:updateGasPrices',\n * { chainId: '0x42' },\n * );\n * const gasPricesControllerState = await globalMessenger.call(\n * 'SampleGasPricesController:getState',\n * );\n * gasPricesControllerState.gasPricesByChainId\n * // => { '0x42': { low: 5, average: 10, high: 15, fetchedDate: '2024-01-02T00:00:00.000Z' } }\n * ```\n */\nexport class SampleGasPricesController extends BaseController<\n typeof controllerName,\n SampleGasPricesControllerState,\n SampleGasPricesControllerMessenger\n> {\n /**\n * The globally selected chain ID.\n */\n #selectedChainId: Hex | undefined;\n\n /**\n * Constructs a new {@link SampleGasPricesController}.\n *\n * @param args - The constructor arguments.\n * @param args.messenger - The messenger suited for this controller.\n * @param args.state - The desired state with which to initialize this\n * controller. Missing properties will be filled in with defaults.\n */\n constructor({\n messenger,\n state,\n }: {\n messenger: SampleGasPricesControllerMessenger;\n state?: Partial<SampleGasPricesControllerState>;\n }) {\n super({\n messenger,\n metadata: gasPricesControllerMetadata,\n name: controllerName,\n state: {\n ...getDefaultSampleGasPricesControllerState(),\n ...state,\n },\n });\n\n this.messagingSystem.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n\n this.messagingSystem.subscribe(\n 'NetworkController:stateChange',\n this.#onSelectedNetworkClientIdChange.bind(this),\n (networkControllerState) =>\n networkControllerState.selectedNetworkClientId,\n );\n }\n\n /**\n * Fetches the latest gas prices for the given chain and persists them to\n * state.\n *\n * @param args - The arguments to the function.\n * @param args.chainId - The chain ID for which to fetch gas prices.\n */\n async updateGasPrices({ chainId }: { chainId: Hex }) {\n const gasPricesResponse = await this.messagingSystem.call(\n 'SampleGasPricesService:fetchGasPrices',\n chainId,\n );\n\n this.update((state) => {\n state.gasPricesByChainId[chainId] = {\n ...gasPricesResponse,\n fetchedDate: new Date().toISOString(),\n };\n });\n }\n\n /**\n * Callback to call when the globally selected network client ID changes,\n * ensuring that gas prices get updated.\n *\n * @param selectedNetworkClientId - The globally selected network client ID.\n */\n async #onSelectedNetworkClientIdChange(\n selectedNetworkClientId: NetworkClientId,\n ) {\n const {\n configuration: { chainId },\n } = this.messagingSystem.call(\n 'NetworkController:getNetworkClientById',\n selectedNetworkClientId,\n );\n\n if (chainId !== this.#selectedChainId) {\n this.#selectedChainId = chainId;\n await this.updateGasPrices({ chainId });\n }\n }\n}\n"]}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import type { ControllerGetStateAction, ControllerStateChangeEvent } from "@metamask/base-controller
|
|
2
|
-
import { BaseController } from "@metamask/base-controller
|
|
3
|
-
import type { Messenger } from "@metamask/messenger";
|
|
1
|
+
import type { ControllerGetStateAction, ControllerStateChangeEvent, RestrictedMessenger } from "@metamask/base-controller";
|
|
2
|
+
import { BaseController } from "@metamask/base-controller";
|
|
4
3
|
import type { NetworkControllerGetNetworkClientByIdAction, NetworkControllerStateChangeEvent } from "@metamask/network-controller";
|
|
5
4
|
import type { Hex } from "@metamask/utils";
|
|
6
5
|
import type { SampleGasPricesControllerMethodActions } from "./sample-gas-prices-controller-method-action-types.cjs";
|
|
@@ -81,14 +80,14 @@ type AllowedEvents = NetworkControllerStateChangeEvent;
|
|
|
81
80
|
* The messenger restricted to actions and events accessed by
|
|
82
81
|
* {@link SampleGasPricesController}.
|
|
83
82
|
*/
|
|
84
|
-
export type SampleGasPricesControllerMessenger =
|
|
83
|
+
export type SampleGasPricesControllerMessenger = RestrictedMessenger<typeof controllerName, SampleGasPricesControllerActions | AllowedActions, SampleGasPricesControllerEvents | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
|
|
85
84
|
/**
|
|
86
85
|
* `SampleGasPricesController` fetches and persists gas prices for various chains.
|
|
87
86
|
*
|
|
88
87
|
* @example
|
|
89
88
|
*
|
|
90
89
|
* ``` ts
|
|
91
|
-
* import { Messenger } from '@metamask/
|
|
90
|
+
* import { Messenger } from '@metamask/base-controller';
|
|
92
91
|
* import type {
|
|
93
92
|
* NetworkControllerActions,
|
|
94
93
|
* NetworkControllerEvents,
|
|
@@ -105,23 +104,18 @@ export type SampleGasPricesControllerMessenger = Messenger<typeof controllerName
|
|
|
105
104
|
* selectGasPrices,
|
|
106
105
|
* } from '@metamask/sample-controllers';
|
|
107
106
|
*
|
|
108
|
-
* const
|
|
109
|
-
* 'Root',
|
|
107
|
+
* const globalMessenger = new Messenger<
|
|
110
108
|
* SampleGasPricesServiceActions
|
|
111
109
|
* | SampleGasPricesControllerActions
|
|
112
110
|
* | NetworkControllerActions,
|
|
113
111
|
* SampleGasPricesServiceEvents
|
|
114
112
|
* | SampleGasPricesControllerEvents
|
|
115
113
|
* | NetworkControllerEvents
|
|
116
|
-
* >(
|
|
117
|
-
* const gasPricesServiceMessenger =
|
|
118
|
-
* 'SampleGasPricesService',
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
* typeof rootMessenger,
|
|
122
|
-
* >({
|
|
123
|
-
* namespace: 'SampleGasPricesService',
|
|
124
|
-
* parent: rootMessenger,
|
|
114
|
+
* >();
|
|
115
|
+
* const gasPricesServiceMessenger = globalMessenger.getRestricted({
|
|
116
|
+
* name: 'SampleGasPricesService',
|
|
117
|
+
* allowedActions: [],
|
|
118
|
+
* allowedEvents: [],
|
|
125
119
|
* });
|
|
126
120
|
* // Instantiate the service to register its actions on the messenger
|
|
127
121
|
* new SampleGasPricesService({
|
|
@@ -129,14 +123,10 @@ export type SampleGasPricesControllerMessenger = Messenger<typeof controllerName
|
|
|
129
123
|
* // We assume you're using this in the browser.
|
|
130
124
|
* fetch,
|
|
131
125
|
* });
|
|
132
|
-
* const gasPricesControllerMessenger =
|
|
133
|
-
* 'SampleGasPricesController',
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
* typeof rootMessenger,
|
|
137
|
-
* >({
|
|
138
|
-
* namespace: 'SampleGasPricesController',
|
|
139
|
-
* parent: rootMessenger,
|
|
126
|
+
* const gasPricesControllerMessenger = globalMessenger.getRestricted({
|
|
127
|
+
* name: 'SampleGasPricesController',
|
|
128
|
+
* allowedActions: ['NetworkController:getNetworkClientById'],
|
|
129
|
+
* allowedEvents: ['NetworkController:stateChange'],
|
|
140
130
|
* });
|
|
141
131
|
* // Instantiate the controller to register its actions on the messenger
|
|
142
132
|
* new SampleGasPricesController({
|
|
@@ -144,11 +134,11 @@ export type SampleGasPricesControllerMessenger = Messenger<typeof controllerName
|
|
|
144
134
|
* });
|
|
145
135
|
*
|
|
146
136
|
* // Later...
|
|
147
|
-
* await
|
|
137
|
+
* await globalMessenger.call(
|
|
148
138
|
* 'SampleGasPricesController:updateGasPrices',
|
|
149
139
|
* { chainId: '0x42' },
|
|
150
140
|
* );
|
|
151
|
-
* const gasPricesControllerState = await
|
|
141
|
+
* const gasPricesControllerState = await globalMessenger.call(
|
|
152
142
|
* 'SampleGasPricesController:getState',
|
|
153
143
|
* );
|
|
154
144
|
* gasPricesControllerState.gasPricesByChainId
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sample-gas-prices-controller.d.cts","sourceRoot":"","sources":["../src/sample-gas-prices-controller.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,
|
|
1
|
+
{"version":3,"file":"sample-gas-prices-controller.d.cts","sourceRoot":"","sources":["../src/sample-gas-prices-controller.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAC1B,mBAAmB,EAEpB,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAEV,2CAA2C,EAC3C,iCAAiC,EAClC,qCAAqC;AACtC,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAE3C,OAAO,KAAK,EAAE,sCAAsC,EAAE,+DAA2D;AACjH,OAAO,KAAK,EAAE,0CAA0C,EAAE,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ACH,qBAAa,yBAA0B,SAAQ,cAAc,CAC3D,OAAO,cAAc,EACrB,8BAA8B,EAC9B,kCAAkC,CACnC;;IAMC;;;;;;;OAOG;gBACS,EACV,SAAS,EACT,KAAK,GACN,EAAE;QACD,SAAS,EAAE,kCAAkC,CAAC;QAC9C,KAAK,CAAC,EAAE,OAAO,CAAC,8BAA8B,CAAC,CAAC;KACjD;IAwBD;;;;;;OAMG;IACG,eAAe,CAAC,EAAE,OAAO,EAAE,EAAE;QAAE,OAAO,EAAE,GAAG,CAAA;KAAE;CAmCpD"}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import type { ControllerGetStateAction, ControllerStateChangeEvent } from "@metamask/base-controller
|
|
2
|
-
import { BaseController } from "@metamask/base-controller
|
|
3
|
-
import type { Messenger } from "@metamask/messenger";
|
|
1
|
+
import type { ControllerGetStateAction, ControllerStateChangeEvent, RestrictedMessenger } from "@metamask/base-controller";
|
|
2
|
+
import { BaseController } from "@metamask/base-controller";
|
|
4
3
|
import type { NetworkControllerGetNetworkClientByIdAction, NetworkControllerStateChangeEvent } from "@metamask/network-controller";
|
|
5
4
|
import type { Hex } from "@metamask/utils";
|
|
6
5
|
import type { SampleGasPricesControllerMethodActions } from "./sample-gas-prices-controller-method-action-types.mjs";
|
|
@@ -81,14 +80,14 @@ type AllowedEvents = NetworkControllerStateChangeEvent;
|
|
|
81
80
|
* The messenger restricted to actions and events accessed by
|
|
82
81
|
* {@link SampleGasPricesController}.
|
|
83
82
|
*/
|
|
84
|
-
export type SampleGasPricesControllerMessenger =
|
|
83
|
+
export type SampleGasPricesControllerMessenger = RestrictedMessenger<typeof controllerName, SampleGasPricesControllerActions | AllowedActions, SampleGasPricesControllerEvents | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
|
|
85
84
|
/**
|
|
86
85
|
* `SampleGasPricesController` fetches and persists gas prices for various chains.
|
|
87
86
|
*
|
|
88
87
|
* @example
|
|
89
88
|
*
|
|
90
89
|
* ``` ts
|
|
91
|
-
* import { Messenger } from '@metamask/
|
|
90
|
+
* import { Messenger } from '@metamask/base-controller';
|
|
92
91
|
* import type {
|
|
93
92
|
* NetworkControllerActions,
|
|
94
93
|
* NetworkControllerEvents,
|
|
@@ -105,23 +104,18 @@ export type SampleGasPricesControllerMessenger = Messenger<typeof controllerName
|
|
|
105
104
|
* selectGasPrices,
|
|
106
105
|
* } from '@metamask/sample-controllers';
|
|
107
106
|
*
|
|
108
|
-
* const
|
|
109
|
-
* 'Root',
|
|
107
|
+
* const globalMessenger = new Messenger<
|
|
110
108
|
* SampleGasPricesServiceActions
|
|
111
109
|
* | SampleGasPricesControllerActions
|
|
112
110
|
* | NetworkControllerActions,
|
|
113
111
|
* SampleGasPricesServiceEvents
|
|
114
112
|
* | SampleGasPricesControllerEvents
|
|
115
113
|
* | NetworkControllerEvents
|
|
116
|
-
* >(
|
|
117
|
-
* const gasPricesServiceMessenger =
|
|
118
|
-
* 'SampleGasPricesService',
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
* typeof rootMessenger,
|
|
122
|
-
* >({
|
|
123
|
-
* namespace: 'SampleGasPricesService',
|
|
124
|
-
* parent: rootMessenger,
|
|
114
|
+
* >();
|
|
115
|
+
* const gasPricesServiceMessenger = globalMessenger.getRestricted({
|
|
116
|
+
* name: 'SampleGasPricesService',
|
|
117
|
+
* allowedActions: [],
|
|
118
|
+
* allowedEvents: [],
|
|
125
119
|
* });
|
|
126
120
|
* // Instantiate the service to register its actions on the messenger
|
|
127
121
|
* new SampleGasPricesService({
|
|
@@ -129,14 +123,10 @@ export type SampleGasPricesControllerMessenger = Messenger<typeof controllerName
|
|
|
129
123
|
* // We assume you're using this in the browser.
|
|
130
124
|
* fetch,
|
|
131
125
|
* });
|
|
132
|
-
* const gasPricesControllerMessenger =
|
|
133
|
-
* 'SampleGasPricesController',
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
* typeof rootMessenger,
|
|
137
|
-
* >({
|
|
138
|
-
* namespace: 'SampleGasPricesController',
|
|
139
|
-
* parent: rootMessenger,
|
|
126
|
+
* const gasPricesControllerMessenger = globalMessenger.getRestricted({
|
|
127
|
+
* name: 'SampleGasPricesController',
|
|
128
|
+
* allowedActions: ['NetworkController:getNetworkClientById'],
|
|
129
|
+
* allowedEvents: ['NetworkController:stateChange'],
|
|
140
130
|
* });
|
|
141
131
|
* // Instantiate the controller to register its actions on the messenger
|
|
142
132
|
* new SampleGasPricesController({
|
|
@@ -144,11 +134,11 @@ export type SampleGasPricesControllerMessenger = Messenger<typeof controllerName
|
|
|
144
134
|
* });
|
|
145
135
|
*
|
|
146
136
|
* // Later...
|
|
147
|
-
* await
|
|
137
|
+
* await globalMessenger.call(
|
|
148
138
|
* 'SampleGasPricesController:updateGasPrices',
|
|
149
139
|
* { chainId: '0x42' },
|
|
150
140
|
* );
|
|
151
|
-
* const gasPricesControllerState = await
|
|
141
|
+
* const gasPricesControllerState = await globalMessenger.call(
|
|
152
142
|
* 'SampleGasPricesController:getState',
|
|
153
143
|
* );
|
|
154
144
|
* gasPricesControllerState.gasPricesByChainId
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sample-gas-prices-controller.d.mts","sourceRoot":"","sources":["../src/sample-gas-prices-controller.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,
|
|
1
|
+
{"version":3,"file":"sample-gas-prices-controller.d.mts","sourceRoot":"","sources":["../src/sample-gas-prices-controller.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAC1B,mBAAmB,EAEpB,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAEV,2CAA2C,EAC3C,iCAAiC,EAClC,qCAAqC;AACtC,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAE3C,OAAO,KAAK,EAAE,sCAAsC,EAAE,+DAA2D;AACjH,OAAO,KAAK,EAAE,0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;;;;GA+DG;AACH,qBAAa,yBAA0B,SAAQ,cAAc,CAC3D,OAAO,cAAc,EACrB,8BAA8B,EAC9B,kCAAkC,CACnC;;IAMC;;;;;;;OAOG;gBACS,EACV,SAAS,EACT,KAAK,GACN,EAAE;QACD,SAAS,EAAE,kCAAkC,CAAC;QAC9C,KAAK,CAAC,EAAE,OAAO,CAAC,8BAA8B,CAAC,CAAC;KACjD;IAwBD;;;;;;OAMG;IACG,eAAe,CAAC,EAAE,OAAO,EAAE,EAAE;QAAE,OAAO,EAAE,GAAG,CAAA;KAAE;CAmCpD"}
|
|
@@ -10,7 +10,7 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (
|
|
|
10
10
|
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
11
11
|
};
|
|
12
12
|
var _SampleGasPricesController_instances, _SampleGasPricesController_selectedChainId, _SampleGasPricesController_onSelectedNetworkClientIdChange;
|
|
13
|
-
import { BaseController } from "@metamask/base-controller
|
|
13
|
+
import { BaseController } from "@metamask/base-controller";
|
|
14
14
|
// === GENERAL ===
|
|
15
15
|
/**
|
|
16
16
|
* The name of the {@link SampleGasPricesController}, used to namespace the
|
|
@@ -23,9 +23,9 @@ export const controllerName = 'SampleGasPricesController';
|
|
|
23
23
|
*/
|
|
24
24
|
const gasPricesControllerMetadata = {
|
|
25
25
|
gasPricesByChainId: {
|
|
26
|
-
includeInDebugSnapshot: false,
|
|
27
26
|
includeInStateLogs: true,
|
|
28
27
|
persist: true,
|
|
28
|
+
anonymous: false,
|
|
29
29
|
usedInUi: true,
|
|
30
30
|
},
|
|
31
31
|
};
|
|
@@ -51,7 +51,7 @@ const MESSENGER_EXPOSED_METHODS = ['updateGasPrices'];
|
|
|
51
51
|
* @example
|
|
52
52
|
*
|
|
53
53
|
* ``` ts
|
|
54
|
-
* import { Messenger } from '@metamask/
|
|
54
|
+
* import { Messenger } from '@metamask/base-controller';
|
|
55
55
|
* import type {
|
|
56
56
|
* NetworkControllerActions,
|
|
57
57
|
* NetworkControllerEvents,
|
|
@@ -68,23 +68,18 @@ const MESSENGER_EXPOSED_METHODS = ['updateGasPrices'];
|
|
|
68
68
|
* selectGasPrices,
|
|
69
69
|
* } from '@metamask/sample-controllers';
|
|
70
70
|
*
|
|
71
|
-
* const
|
|
72
|
-
* 'Root',
|
|
71
|
+
* const globalMessenger = new Messenger<
|
|
73
72
|
* SampleGasPricesServiceActions
|
|
74
73
|
* | SampleGasPricesControllerActions
|
|
75
74
|
* | NetworkControllerActions,
|
|
76
75
|
* SampleGasPricesServiceEvents
|
|
77
76
|
* | SampleGasPricesControllerEvents
|
|
78
77
|
* | NetworkControllerEvents
|
|
79
|
-
* >(
|
|
80
|
-
* const gasPricesServiceMessenger =
|
|
81
|
-
* 'SampleGasPricesService',
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
* typeof rootMessenger,
|
|
85
|
-
* >({
|
|
86
|
-
* namespace: 'SampleGasPricesService',
|
|
87
|
-
* parent: rootMessenger,
|
|
78
|
+
* >();
|
|
79
|
+
* const gasPricesServiceMessenger = globalMessenger.getRestricted({
|
|
80
|
+
* name: 'SampleGasPricesService',
|
|
81
|
+
* allowedActions: [],
|
|
82
|
+
* allowedEvents: [],
|
|
88
83
|
* });
|
|
89
84
|
* // Instantiate the service to register its actions on the messenger
|
|
90
85
|
* new SampleGasPricesService({
|
|
@@ -92,14 +87,10 @@ const MESSENGER_EXPOSED_METHODS = ['updateGasPrices'];
|
|
|
92
87
|
* // We assume you're using this in the browser.
|
|
93
88
|
* fetch,
|
|
94
89
|
* });
|
|
95
|
-
* const gasPricesControllerMessenger =
|
|
96
|
-
* 'SampleGasPricesController',
|
|
97
|
-
*
|
|
98
|
-
*
|
|
99
|
-
* typeof rootMessenger,
|
|
100
|
-
* >({
|
|
101
|
-
* namespace: 'SampleGasPricesController',
|
|
102
|
-
* parent: rootMessenger,
|
|
90
|
+
* const gasPricesControllerMessenger = globalMessenger.getRestricted({
|
|
91
|
+
* name: 'SampleGasPricesController',
|
|
92
|
+
* allowedActions: ['NetworkController:getNetworkClientById'],
|
|
93
|
+
* allowedEvents: ['NetworkController:stateChange'],
|
|
103
94
|
* });
|
|
104
95
|
* // Instantiate the controller to register its actions on the messenger
|
|
105
96
|
* new SampleGasPricesController({
|
|
@@ -107,11 +98,11 @@ const MESSENGER_EXPOSED_METHODS = ['updateGasPrices'];
|
|
|
107
98
|
* });
|
|
108
99
|
*
|
|
109
100
|
* // Later...
|
|
110
|
-
* await
|
|
101
|
+
* await globalMessenger.call(
|
|
111
102
|
* 'SampleGasPricesController:updateGasPrices',
|
|
112
103
|
* { chainId: '0x42' },
|
|
113
104
|
* );
|
|
114
|
-
* const gasPricesControllerState = await
|
|
105
|
+
* const gasPricesControllerState = await globalMessenger.call(
|
|
115
106
|
* 'SampleGasPricesController:getState',
|
|
116
107
|
* );
|
|
117
108
|
* gasPricesControllerState.gasPricesByChainId
|
|
@@ -142,8 +133,8 @@ export class SampleGasPricesController extends BaseController {
|
|
|
142
133
|
* The globally selected chain ID.
|
|
143
134
|
*/
|
|
144
135
|
_SampleGasPricesController_selectedChainId.set(this, void 0);
|
|
145
|
-
this.
|
|
146
|
-
this.
|
|
136
|
+
this.messagingSystem.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
|
|
137
|
+
this.messagingSystem.subscribe('NetworkController:stateChange', __classPrivateFieldGet(this, _SampleGasPricesController_instances, "m", _SampleGasPricesController_onSelectedNetworkClientIdChange).bind(this), (networkControllerState) => networkControllerState.selectedNetworkClientId);
|
|
147
138
|
}
|
|
148
139
|
/**
|
|
149
140
|
* Fetches the latest gas prices for the given chain and persists them to
|
|
@@ -153,7 +144,7 @@ export class SampleGasPricesController extends BaseController {
|
|
|
153
144
|
* @param args.chainId - The chain ID for which to fetch gas prices.
|
|
154
145
|
*/
|
|
155
146
|
async updateGasPrices({ chainId }) {
|
|
156
|
-
const gasPricesResponse = await this.
|
|
147
|
+
const gasPricesResponse = await this.messagingSystem.call('SampleGasPricesService:fetchGasPrices', chainId);
|
|
157
148
|
this.update((state) => {
|
|
158
149
|
state.gasPricesByChainId[chainId] = {
|
|
159
150
|
...gasPricesResponse,
|
|
@@ -170,7 +161,7 @@ _SampleGasPricesController_selectedChainId = new WeakMap(), _SampleGasPricesCont
|
|
|
170
161
|
* @param selectedNetworkClientId - The globally selected network client ID.
|
|
171
162
|
*/
|
|
172
163
|
async function _SampleGasPricesController_onSelectedNetworkClientIdChange(selectedNetworkClientId) {
|
|
173
|
-
const { configuration: { chainId }, } = this.
|
|
164
|
+
const { configuration: { chainId }, } = this.messagingSystem.call('NetworkController:getNetworkClientById', selectedNetworkClientId);
|
|
174
165
|
if (chainId !== __classPrivateFieldGet(this, _SampleGasPricesController_selectedChainId, "f")) {
|
|
175
166
|
__classPrivateFieldSet(this, _SampleGasPricesController_selectedChainId, chainId, "f");
|
|
176
167
|
await this.updateGasPrices({ chainId });
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sample-gas-prices-controller.mjs","sourceRoot":"","sources":["../src/sample-gas-prices-controller.ts"],"names":[],"mappings":";;;;;;;;;;;;AAKA,OAAO,EAAE,cAAc,EAAE,uCAAuC;AAYhE,kBAAkB;AAElB;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,2BAA2B,CAAC;AAsC1D;;GAEG;AACH,MAAM,2BAA2B,GAAG;IAClC,kBAAkB,EAAE;QAClB,sBAAsB,EAAE,KAAK;QAC7B,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,QAAQ,EAAE,IAAI;KACf;CACsD,CAAC;AAE1D;;;;;;;GAOG;AACH,MAAM,UAAU,wCAAwC;IACtD,OAAO;QACL,kBAAkB,EAAE,EAAE;KACvB,CAAC;AACJ,CAAC;AAED,oBAAoB;AAEpB,MAAM,yBAAyB,GAAG,CAAC,iBAAiB,CAAU,CAAC;AAuD/D,gCAAgC;AAEhC;;;;;;;;;;;;;;;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OAAO,KAAK,uBAAA,IAAI,kDAAiB,EAAE;QACrC,uBAAA,IAAI,8CAAoB,OAAO,MAAA,CAAC;QAChC,MAAM,IAAI,CAAC,eAAe,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC;KACzC;AACH,CAAC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller/next';\nimport { BaseController } from '@metamask/base-controller/next';\nimport type { Messenger } from '@metamask/messenger';\nimport type {\n NetworkClientId,\n NetworkControllerGetNetworkClientByIdAction,\n NetworkControllerStateChangeEvent,\n} from '@metamask/network-controller';\nimport type { Hex } from '@metamask/utils';\n\nimport type { SampleGasPricesControllerMethodActions } from './sample-gas-prices-controller-method-action-types';\nimport type { SampleGasPricesServiceFetchGasPricesAction } from './sample-gas-prices-service/sample-gas-prices-service-method-action-types';\n\n// === GENERAL ===\n\n/**\n * The name of the {@link SampleGasPricesController}, used to namespace the\n * controller's actions and events and to namespace the controller's state data\n * when composed with other controllers.\n */\nexport const controllerName = 'SampleGasPricesController';\n\n// === STATE ===\n\n/**\n * The collection of gas price data fetched periodically.\n */\ntype GasPrices = {\n /**\n * The total estimated gas in the \"low\" bucket.\n */\n low: number;\n /**\n * The total estimated gas in the \"average\" bucket.\n */\n average: number;\n /**\n * The total estimated gas in the \"high\" bucket.\n */\n high: number;\n /**\n * The date/time (in ISO-8601 format) when prices were fetched.\n */\n fetchedDate: string;\n};\n\n/**\n * Describes the shape of the state object for {@link SampleGasPricesController}.\n */\nexport type SampleGasPricesControllerState = {\n /**\n * Fetched gas prices categorized by chain ID.\n */\n gasPricesByChainId: {\n [chainId: Hex]: GasPrices;\n };\n};\n\n/**\n * The metadata for each property in {@link SampleGasPricesControllerState}.\n */\nconst gasPricesControllerMetadata = {\n gasPricesByChainId: {\n includeInDebugSnapshot: false,\n includeInStateLogs: true,\n persist: true,\n usedInUi: true,\n },\n} satisfies StateMetadata<SampleGasPricesControllerState>;\n\n/**\n * Constructs the default {@link SampleGasPricesController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link SampleGasPricesController} state.\n */\nexport function getDefaultSampleGasPricesControllerState(): SampleGasPricesControllerState {\n return {\n gasPricesByChainId: {},\n };\n}\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = ['updateGasPrices'] as const;\n\n/**\n * Retrieves the state of the {@link SampleGasPricesController}.\n */\nexport type SampleGasPricesControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n SampleGasPricesControllerState\n>;\n\n/**\n * Actions that {@link SampleGasPricesMessenger} exposes to other consumers.\n */\nexport type SampleGasPricesControllerActions =\n | SampleGasPricesControllerGetStateAction\n | SampleGasPricesControllerMethodActions;\n\n/**\n * Actions from other messengers that {@link SampleGasPricesMessenger} calls.\n */\ntype AllowedActions =\n | NetworkControllerGetNetworkClientByIdAction\n | SampleGasPricesServiceFetchGasPricesAction;\n\n/**\n * Published when the state of {@link SampleGasPricesController} changes.\n */\nexport type SampleGasPricesControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n SampleGasPricesControllerState\n >;\n\n/**\n * Events that {@link SampleGasPricesMessenger} exposes to other consumers.\n */\nexport type SampleGasPricesControllerEvents =\n SampleGasPricesControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link SampleGasPricesMessenger} subscribes\n * to.\n */\ntype AllowedEvents = NetworkControllerStateChangeEvent;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link SampleGasPricesController}.\n */\nexport type SampleGasPricesControllerMessenger = Messenger<\n typeof controllerName,\n SampleGasPricesControllerActions | AllowedActions,\n SampleGasPricesControllerEvents | AllowedEvents\n>;\n\n// === CONTROLLER DEFINITION ===\n\n/**\n * `SampleGasPricesController` fetches and persists gas prices for various chains.\n *\n * @example\n *\n * ``` ts\n * import { Messenger } from '@metamask/messenger';\n * import type {\n * NetworkControllerActions,\n * NetworkControllerEvents,\n * } from '@metamask/network-controller';\n * import type {\n * SampleGasPricesControllerActions,\n * SampleGasPricesControllerEvents,\n * SampleGasPricesServiceActions,\n * SampleGasPricesServiceEvents,\n * } from '@metamask/sample-controllers';\n * import {\n * SampleGasPricesController,\n * SampleGasPricesService,\n * selectGasPrices,\n * } from '@metamask/sample-controllers';\n *\n * const rootMessenger = new Messenger<\n * 'Root',\n * SampleGasPricesServiceActions\n * | SampleGasPricesControllerActions\n * | NetworkControllerActions,\n * SampleGasPricesServiceEvents\n * | SampleGasPricesControllerEvents\n * | NetworkControllerEvents\n * >({ namespace: 'Root' });\n * const gasPricesServiceMessenger = new Messenger<\n * 'SampleGasPricesService',\n * SampleGasPricesServiceActions,\n * SampleGasPricesServiceEvents,\n * typeof rootMessenger,\n * >({\n * namespace: 'SampleGasPricesService',\n * parent: rootMessenger,\n * });\n * // Instantiate the service to register its actions on the messenger\n * new SampleGasPricesService({\n * messenger: gasPricesServiceMessenger,\n * // We assume you're using this in the browser.\n * fetch,\n * });\n * const gasPricesControllerMessenger = new Messenger<\n * 'SampleGasPricesController',\n * SampleGasPricesControllerActions | NetworkControllerGetNetworkClientByIdAction,\n * SampleGasPricesControllerEvents | NetworkControllerStateChangeEvent,\n * typeof rootMessenger,\n * >({\n * namespace: 'SampleGasPricesController',\n * parent: rootMessenger,\n * });\n * // Instantiate the controller to register its actions on the messenger\n * new SampleGasPricesController({\n * messenger: gasPricesControllerMessenger,\n * });\n *\n * // Later...\n * await rootMessenger.call(\n * 'SampleGasPricesController:updateGasPrices',\n * { chainId: '0x42' },\n * );\n * const gasPricesControllerState = await rootMessenger.call(\n * 'SampleGasPricesController:getState',\n * );\n * gasPricesControllerState.gasPricesByChainId\n * // => { '0x42': { low: 5, average: 10, high: 15, fetchedDate: '2024-01-02T00:00:00.000Z' } }\n * ```\n */\nexport class SampleGasPricesController extends BaseController<\n typeof controllerName,\n SampleGasPricesControllerState,\n SampleGasPricesControllerMessenger\n> {\n /**\n * The globally selected chain ID.\n */\n #selectedChainId: Hex | undefined;\n\n /**\n * Constructs a new {@link SampleGasPricesController}.\n *\n * @param args - The constructor arguments.\n * @param args.messenger - The messenger suited for this controller.\n * @param args.state - The desired state with which to initialize this\n * controller. Missing properties will be filled in with defaults.\n */\n constructor({\n messenger,\n state,\n }: {\n messenger: SampleGasPricesControllerMessenger;\n state?: Partial<SampleGasPricesControllerState>;\n }) {\n super({\n messenger,\n metadata: gasPricesControllerMetadata,\n name: controllerName,\n state: {\n ...getDefaultSampleGasPricesControllerState(),\n ...state,\n },\n });\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n\n this.messenger.subscribe(\n 'NetworkController:stateChange',\n this.#onSelectedNetworkClientIdChange.bind(this),\n (networkControllerState) =>\n networkControllerState.selectedNetworkClientId,\n );\n }\n\n /**\n * Fetches the latest gas prices for the given chain and persists them to\n * state.\n *\n * @param args - The arguments to the function.\n * @param args.chainId - The chain ID for which to fetch gas prices.\n */\n async updateGasPrices({ chainId }: { chainId: Hex }) {\n const gasPricesResponse = await this.messenger.call(\n 'SampleGasPricesService:fetchGasPrices',\n chainId,\n );\n\n this.update((state) => {\n state.gasPricesByChainId[chainId] = {\n ...gasPricesResponse,\n fetchedDate: new Date().toISOString(),\n };\n });\n }\n\n /**\n * Callback to call when the globally selected network client ID changes,\n * ensuring that gas prices get updated.\n *\n * @param selectedNetworkClientId - The globally selected network client ID.\n */\n async #onSelectedNetworkClientIdChange(\n selectedNetworkClientId: NetworkClientId,\n ) {\n const {\n configuration: { chainId },\n } = this.messenger.call(\n 'NetworkController:getNetworkClientById',\n selectedNetworkClientId,\n );\n\n if (chainId !== this.#selectedChainId) {\n this.#selectedChainId = chainId;\n await this.updateGasPrices({ chainId });\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"sample-gas-prices-controller.mjs","sourceRoot":"","sources":["../src/sample-gas-prices-controller.ts"],"names":[],"mappings":";;;;;;;;;;;;AAMA,OAAO,EAAE,c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AAC,eAAe,CAAC,IAAI,CACvD,uCAAuC,EACvC,OAAO,CACR,CAAC;QAEF,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,kBAAkB,CAAC,OAAO,CAAC,GAAG;gBAClC,GAAG,iBAAiB;gBACpB,WAAW,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;aACtC,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;CAuBF;;AArBC;;;;;GAKG;AACH,KAAK,qEACH,uBAAwC;IAExC,MAAM,EACJ,aAAa,EAAE,EAAE,OAAO,EAAE,GAC3B,GAAG,IAAI,CAAC,eAAe,CAAC,IAAI,CAC3B,wCAAwC,EACxC,uBAAuB,CACxB,CAAC;IAEF,IAAI,OAAO,KAAK,uBAAA,IAAI,kDAAiB,EAAE;QACrC,uBAAA,IAAI,8CAAoB,OAAO,MAAA,CAAC;QAChC,MAAM,IAAI,CAAC,eAAe,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC;KACzC;AACH,CAAC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n RestrictedMessenger,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type {\n NetworkClientId,\n NetworkControllerGetNetworkClientByIdAction,\n NetworkControllerStateChangeEvent,\n} from '@metamask/network-controller';\nimport type { Hex } from '@metamask/utils';\n\nimport type { SampleGasPricesControllerMethodActions } from './sample-gas-prices-controller-method-action-types';\nimport type { SampleGasPricesServiceFetchGasPricesAction } from './sample-gas-prices-service/sample-gas-prices-service-method-action-types';\n\n// === GENERAL ===\n\n/**\n * The name of the {@link SampleGasPricesController}, used to namespace the\n * controller's actions and events and to namespace the controller's state data\n * when composed with other controllers.\n */\nexport const controllerName = 'SampleGasPricesController';\n\n// === STATE ===\n\n/**\n * The collection of gas price data fetched periodically.\n */\ntype GasPrices = {\n /**\n * The total estimated gas in the \"low\" bucket.\n */\n low: number;\n /**\n * The total estimated gas in the \"average\" bucket.\n */\n average: number;\n /**\n * The total estimated gas in the \"high\" bucket.\n */\n high: number;\n /**\n * The date/time (in ISO-8601 format) when prices were fetched.\n */\n fetchedDate: string;\n};\n\n/**\n * Describes the shape of the state object for {@link SampleGasPricesController}.\n */\nexport type SampleGasPricesControllerState = {\n /**\n * Fetched gas prices categorized by chain ID.\n */\n gasPricesByChainId: {\n [chainId: Hex]: GasPrices;\n };\n};\n\n/**\n * The metadata for each property in {@link SampleGasPricesControllerState}.\n */\nconst gasPricesControllerMetadata = {\n gasPricesByChainId: {\n includeInStateLogs: true,\n persist: true,\n anonymous: false,\n usedInUi: true,\n },\n} satisfies StateMetadata<SampleGasPricesControllerState>;\n\n/**\n * Constructs the default {@link SampleGasPricesController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link SampleGasPricesController} state.\n */\nexport function getDefaultSampleGasPricesControllerState(): SampleGasPricesControllerState {\n return {\n gasPricesByChainId: {},\n };\n}\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = ['updateGasPrices'] as const;\n\n/**\n * Retrieves the state of the {@link SampleGasPricesController}.\n */\nexport type SampleGasPricesControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n SampleGasPricesControllerState\n>;\n\n/**\n * Actions that {@link SampleGasPricesMessenger} exposes to other consumers.\n */\nexport type SampleGasPricesControllerActions =\n | SampleGasPricesControllerGetStateAction\n | SampleGasPricesControllerMethodActions;\n\n/**\n * Actions from other messengers that {@link SampleGasPricesMessenger} calls.\n */\ntype AllowedActions =\n | NetworkControllerGetNetworkClientByIdAction\n | SampleGasPricesServiceFetchGasPricesAction;\n\n/**\n * Published when the state of {@link SampleGasPricesController} changes.\n */\nexport type SampleGasPricesControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n SampleGasPricesControllerState\n >;\n\n/**\n * Events that {@link SampleGasPricesMessenger} exposes to other consumers.\n */\nexport type SampleGasPricesControllerEvents =\n SampleGasPricesControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link SampleGasPricesMessenger} subscribes\n * to.\n */\ntype AllowedEvents = NetworkControllerStateChangeEvent;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link SampleGasPricesController}.\n */\nexport type SampleGasPricesControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n SampleGasPricesControllerActions | AllowedActions,\n SampleGasPricesControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\n// === CONTROLLER DEFINITION ===\n\n/**\n * `SampleGasPricesController` fetches and persists gas prices for various chains.\n *\n * @example\n *\n * ``` ts\n * import { Messenger } from '@metamask/base-controller';\n * import type {\n * NetworkControllerActions,\n * NetworkControllerEvents,\n * } from '@metamask/network-controller';\n * import type {\n * SampleGasPricesControllerActions,\n * SampleGasPricesControllerEvents,\n * SampleGasPricesServiceActions,\n * SampleGasPricesServiceEvents,\n * } from '@metamask/sample-controllers';\n * import {\n * SampleGasPricesController,\n * SampleGasPricesService,\n * selectGasPrices,\n * } from '@metamask/sample-controllers';\n *\n * const globalMessenger = new Messenger<\n * SampleGasPricesServiceActions\n * | SampleGasPricesControllerActions\n * | NetworkControllerActions,\n * SampleGasPricesServiceEvents\n * | SampleGasPricesControllerEvents\n * | NetworkControllerEvents\n * >();\n * const gasPricesServiceMessenger = globalMessenger.getRestricted({\n * name: 'SampleGasPricesService',\n * allowedActions: [],\n * allowedEvents: [],\n * });\n * // Instantiate the service to register its actions on the messenger\n * new SampleGasPricesService({\n * messenger: gasPricesServiceMessenger,\n * // We assume you're using this in the browser.\n * fetch,\n * });\n * const gasPricesControllerMessenger = globalMessenger.getRestricted({\n * name: 'SampleGasPricesController',\n * allowedActions: ['NetworkController:getNetworkClientById'],\n * allowedEvents: ['NetworkController:stateChange'],\n * });\n * // Instantiate the controller to register its actions on the messenger\n * new SampleGasPricesController({\n * messenger: gasPricesControllerMessenger,\n * });\n *\n * // Later...\n * await globalMessenger.call(\n * 'SampleGasPricesController:updateGasPrices',\n * { chainId: '0x42' },\n * );\n * const gasPricesControllerState = await globalMessenger.call(\n * 'SampleGasPricesController:getState',\n * );\n * gasPricesControllerState.gasPricesByChainId\n * // => { '0x42': { low: 5, average: 10, high: 15, fetchedDate: '2024-01-02T00:00:00.000Z' } }\n * ```\n */\nexport class SampleGasPricesController extends BaseController<\n typeof controllerName,\n SampleGasPricesControllerState,\n SampleGasPricesControllerMessenger\n> {\n /**\n * The globally selected chain ID.\n */\n #selectedChainId: Hex | undefined;\n\n /**\n * Constructs a new {@link SampleGasPricesController}.\n *\n * @param args - The constructor arguments.\n * @param args.messenger - The messenger suited for this controller.\n * @param args.state - The desired state with which to initialize this\n * controller. Missing properties will be filled in with defaults.\n */\n constructor({\n messenger,\n state,\n }: {\n messenger: SampleGasPricesControllerMessenger;\n state?: Partial<SampleGasPricesControllerState>;\n }) {\n super({\n messenger,\n metadata: gasPricesControllerMetadata,\n name: controllerName,\n state: {\n ...getDefaultSampleGasPricesControllerState(),\n ...state,\n },\n });\n\n this.messagingSystem.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n\n this.messagingSystem.subscribe(\n 'NetworkController:stateChange',\n this.#onSelectedNetworkClientIdChange.bind(this),\n (networkControllerState) =>\n networkControllerState.selectedNetworkClientId,\n );\n }\n\n /**\n * Fetches the latest gas prices for the given chain and persists them to\n * state.\n *\n * @param args - The arguments to the function.\n * @param args.chainId - The chain ID for which to fetch gas prices.\n */\n async updateGasPrices({ chainId }: { chainId: Hex }) {\n const gasPricesResponse = await this.messagingSystem.call(\n 'SampleGasPricesService:fetchGasPrices',\n chainId,\n );\n\n this.update((state) => {\n state.gasPricesByChainId[chainId] = {\n ...gasPricesResponse,\n fetchedDate: new Date().toISOString(),\n };\n });\n }\n\n /**\n * Callback to call when the globally selected network client ID changes,\n * ensuring that gas prices get updated.\n *\n * @param selectedNetworkClientId - The globally selected network client ID.\n */\n async #onSelectedNetworkClientIdChange(\n selectedNetworkClientId: NetworkClientId,\n ) {\n const {\n configuration: { chainId },\n } = this.messagingSystem.call(\n 'NetworkController:getNetworkClientById',\n selectedNetworkClientId,\n );\n\n if (chainId !== this.#selectedChainId) {\n this.#selectedChainId = chainId;\n await this.updateGasPrices({ chainId });\n }\n }\n}\n"]}
|
|
@@ -29,25 +29,20 @@ const MESSENGER_EXPOSED_METHODS = ['fetchGasPrices'];
|
|
|
29
29
|
* @example
|
|
30
30
|
*
|
|
31
31
|
* ``` ts
|
|
32
|
-
* import { Messenger } from '@metamask/
|
|
32
|
+
* import { Messenger } from '@metamask/base-controller';
|
|
33
33
|
* import type {
|
|
34
34
|
* SampleGasPricesServiceActions,
|
|
35
35
|
* SampleGasPricesServiceEvents,
|
|
36
36
|
* } from '@metamask/sample-controllers';
|
|
37
37
|
*
|
|
38
|
-
* const
|
|
39
|
-
* 'Root',
|
|
40
|
-
* SampleGasPricesServiceActions
|
|
41
|
-
* SampleGasPricesServiceEvents
|
|
42
|
-
* >({ namespace: 'Root' });
|
|
43
|
-
* const gasPricesServiceMessenger = new Messenger<
|
|
44
|
-
* 'SampleGasPricesService',
|
|
38
|
+
* const globalMessenger = new Messenger<
|
|
45
39
|
* SampleGasPricesServiceActions,
|
|
46
|
-
* SampleGasPricesServiceEvents
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
40
|
+
* SampleGasPricesServiceEvents
|
|
41
|
+
* >();
|
|
42
|
+
* const gasPricesServiceMessenger = globalMessenger.getRestricted({
|
|
43
|
+
* name: 'SampleGasPricesService',
|
|
44
|
+
* allowedActions: [],
|
|
45
|
+
* allowedEvents: [],
|
|
51
46
|
* });
|
|
52
47
|
* // Instantiate the service to register its actions on the messenger
|
|
53
48
|
* new SampleGasPricesService({
|
|
@@ -57,7 +52,7 @@ const MESSENGER_EXPOSED_METHODS = ['fetchGasPrices'];
|
|
|
57
52
|
*
|
|
58
53
|
* // Later...
|
|
59
54
|
* // Fetch gas prices for Mainnet
|
|
60
|
-
* const gasPrices = await
|
|
55
|
+
* const gasPrices = await globalMessenger.call(
|
|
61
56
|
* 'SampleGasPricesService:fetchGasPrices',
|
|
62
57
|
* '0x1',
|
|
63
58
|
* );
|