@metamask-previews/sample-controllers 5.0.6-preview-b13d916 → 5.0.6-preview-4f4c98e

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.
Files changed (72) hide show
  1. package/CHANGELOG.md +1 -0
  2. package/dist/{index.d.mts → index.d.ts} +10 -10
  3. package/dist/index.d.ts.map +1 -0
  4. package/dist/index.js +4 -0
  5. package/dist/{index.cjs.map → index.js.map} +1 -1
  6. package/dist/{sample-gas-prices-controller-method-action-types.d.cts → sample-gas-prices-controller-method-action-types.d.ts} +2 -2
  7. package/dist/sample-gas-prices-controller-method-action-types.d.ts.map +1 -0
  8. package/dist/{sample-gas-prices-controller-method-action-types.mjs → sample-gas-prices-controller-method-action-types.js} +1 -1
  9. package/dist/sample-gas-prices-controller-method-action-types.js.map +1 -0
  10. package/dist/{sample-gas-prices-controller.d.cts → sample-gas-prices-controller.d.ts} +8 -8
  11. package/dist/sample-gas-prices-controller.d.ts.map +1 -0
  12. package/dist/{sample-gas-prices-controller.mjs → sample-gas-prices-controller.js} +2 -2
  13. package/dist/sample-gas-prices-controller.js.map +1 -0
  14. package/dist/sample-gas-prices-service/{sample-gas-prices-service-method-action-types.d.cts → sample-gas-prices-service-method-action-types.d.ts} +2 -2
  15. package/dist/sample-gas-prices-service/sample-gas-prices-service-method-action-types.d.ts.map +1 -0
  16. package/dist/sample-gas-prices-service/{sample-gas-prices-service-method-action-types.mjs → sample-gas-prices-service-method-action-types.js} +1 -1
  17. package/dist/sample-gas-prices-service/sample-gas-prices-service-method-action-types.js.map +1 -0
  18. package/dist/sample-gas-prices-service/{sample-gas-prices-service.d.mts → sample-gas-prices-service.d.ts} +9 -9
  19. package/dist/sample-gas-prices-service/sample-gas-prices-service.d.ts.map +1 -0
  20. package/dist/sample-gas-prices-service/{sample-gas-prices-service.mjs → sample-gas-prices-service.js} +4 -4
  21. package/dist/sample-gas-prices-service/sample-gas-prices-service.js.map +1 -0
  22. package/dist/{sample-petnames-controller-method-action-types.d.cts → sample-petnames-controller-method-action-types.d.ts} +2 -2
  23. package/dist/sample-petnames-controller-method-action-types.d.ts.map +1 -0
  24. package/dist/{sample-petnames-controller-method-action-types.mjs → sample-petnames-controller-method-action-types.js} +1 -1
  25. package/dist/sample-petnames-controller-method-action-types.js.map +1 -0
  26. package/dist/{sample-petnames-controller.d.mts → sample-petnames-controller.d.ts} +6 -6
  27. package/dist/sample-petnames-controller.d.ts.map +1 -0
  28. package/dist/{sample-petnames-controller.mjs → sample-petnames-controller.js} +3 -3
  29. package/dist/sample-petnames-controller.js.map +1 -0
  30. package/package.json +12 -16
  31. package/dist/index.cjs +0 -12
  32. package/dist/index.d.cts +0 -10
  33. package/dist/index.d.cts.map +0 -1
  34. package/dist/index.d.mts.map +0 -1
  35. package/dist/index.mjs +0 -4
  36. package/dist/index.mjs.map +0 -1
  37. package/dist/sample-gas-prices-controller-method-action-types.cjs +0 -7
  38. package/dist/sample-gas-prices-controller-method-action-types.cjs.map +0 -1
  39. package/dist/sample-gas-prices-controller-method-action-types.d.cts.map +0 -1
  40. package/dist/sample-gas-prices-controller-method-action-types.d.mts +0 -21
  41. package/dist/sample-gas-prices-controller-method-action-types.d.mts.map +0 -1
  42. package/dist/sample-gas-prices-controller-method-action-types.mjs.map +0 -1
  43. package/dist/sample-gas-prices-controller.cjs +0 -179
  44. package/dist/sample-gas-prices-controller.cjs.map +0 -1
  45. package/dist/sample-gas-prices-controller.d.cts.map +0 -1
  46. package/dist/sample-gas-prices-controller.d.mts +0 -184
  47. package/dist/sample-gas-prices-controller.d.mts.map +0 -1
  48. package/dist/sample-gas-prices-controller.mjs.map +0 -1
  49. package/dist/sample-gas-prices-service/sample-gas-prices-service-method-action-types.cjs +0 -7
  50. package/dist/sample-gas-prices-service/sample-gas-prices-service-method-action-types.cjs.map +0 -1
  51. package/dist/sample-gas-prices-service/sample-gas-prices-service-method-action-types.d.cts.map +0 -1
  52. package/dist/sample-gas-prices-service/sample-gas-prices-service-method-action-types.d.mts +0 -21
  53. package/dist/sample-gas-prices-service/sample-gas-prices-service-method-action-types.d.mts.map +0 -1
  54. package/dist/sample-gas-prices-service/sample-gas-prices-service-method-action-types.mjs.map +0 -1
  55. package/dist/sample-gas-prices-service/sample-gas-prices-service.cjs +0 -121
  56. package/dist/sample-gas-prices-service/sample-gas-prices-service.cjs.map +0 -1
  57. package/dist/sample-gas-prices-service/sample-gas-prices-service.d.cts +0 -139
  58. package/dist/sample-gas-prices-service/sample-gas-prices-service.d.cts.map +0 -1
  59. package/dist/sample-gas-prices-service/sample-gas-prices-service.d.mts.map +0 -1
  60. package/dist/sample-gas-prices-service/sample-gas-prices-service.mjs.map +0 -1
  61. package/dist/sample-petnames-controller-method-action-types.cjs +0 -7
  62. package/dist/sample-petnames-controller-method-action-types.cjs.map +0 -1
  63. package/dist/sample-petnames-controller-method-action-types.d.cts.map +0 -1
  64. package/dist/sample-petnames-controller-method-action-types.d.mts +0 -22
  65. package/dist/sample-petnames-controller-method-action-types.d.mts.map +0 -1
  66. package/dist/sample-petnames-controller-method-action-types.mjs.map +0 -1
  67. package/dist/sample-petnames-controller.cjs +0 -130
  68. package/dist/sample-petnames-controller.cjs.map +0 -1
  69. package/dist/sample-petnames-controller.d.cts +0 -136
  70. package/dist/sample-petnames-controller.d.cts.map +0 -1
  71. package/dist/sample-petnames-controller.d.mts.map +0 -1
  72. package/dist/sample-petnames-controller.mjs.map +0 -1
@@ -1,179 +0,0 @@
1
- "use strict";
2
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
3
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
4
- 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");
5
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
6
- };
7
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
8
- if (kind === "m") throw new TypeError("Private method is not writable");
9
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
10
- 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");
11
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
12
- };
13
- var _SampleGasPricesController_instances, _SampleGasPricesController_selectedChainId, _SampleGasPricesController_onSelectedNetworkClientIdChange;
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.SampleGasPricesController = exports.getDefaultSampleGasPricesControllerState = void 0;
16
- const base_controller_1 = require("@metamask/base-controller");
17
- // === GENERAL ===
18
- /**
19
- * The name of the {@link SampleGasPricesController}, used to namespace the
20
- * controller's actions and events and to namespace the controller's state data
21
- * when composed with other controllers.
22
- */
23
- const CONTROLLER_NAME = 'SampleGasPricesController';
24
- /**
25
- * The metadata for each property in {@link SampleGasPricesControllerState}.
26
- */
27
- const gasPricesControllerMetadata = {
28
- gasPricesByChainId: {
29
- includeInDebugSnapshot: false,
30
- includeInStateLogs: true,
31
- persist: true,
32
- usedInUi: true,
33
- },
34
- };
35
- /**
36
- * Constructs the default {@link SampleGasPricesController} state. This allows
37
- * consumers to provide a partial state object when initializing the controller
38
- * and also helps in constructing complete state objects for this controller in
39
- * tests.
40
- *
41
- * @returns The default {@link SampleGasPricesController} state.
42
- */
43
- function getDefaultSampleGasPricesControllerState() {
44
- return {
45
- gasPricesByChainId: {},
46
- };
47
- }
48
- exports.getDefaultSampleGasPricesControllerState = getDefaultSampleGasPricesControllerState;
49
- // === MESSENGER ===
50
- const MESSENGER_EXPOSED_METHODS = ['updateGasPrices'];
51
- // === CONTROLLER DEFINITION ===
52
- /**
53
- * `SampleGasPricesController` fetches and persists gas prices for various chains.
54
- *
55
- * @example
56
- *
57
- * ``` ts
58
- * import { Messenger } from '@metamask/messenger';
59
- * import type {
60
- * NetworkControllerActions,
61
- * NetworkControllerEvents,
62
- * } from '@metamask/network-controller';
63
- * import type {
64
- * SampleGasPricesControllerActions,
65
- * SampleGasPricesControllerEvents,
66
- * SampleGasPricesServiceActions,
67
- * SampleGasPricesServiceEvents,
68
- * } from '@metamask/sample-controllers';
69
- * import {
70
- * SampleGasPricesController,
71
- * SampleGasPricesService,
72
- * selectGasPrices,
73
- * } from '@metamask/sample-controllers';
74
- *
75
- * const rootMessenger = new Messenger<
76
- * 'Root',
77
- * SampleGasPricesServiceActions
78
- * | SampleGasPricesControllerActions
79
- * | NetworkControllerActions,
80
- * SampleGasPricesServiceEvents
81
- * | SampleGasPricesControllerEvents
82
- * | NetworkControllerEvents
83
- * >({ namespace: 'Root' });
84
- * const gasPricesServiceMessenger = new Messenger<
85
- * 'SampleGasPricesService',
86
- * SampleGasPricesServiceActions,
87
- * SampleGasPricesServiceEvents,
88
- * typeof rootMessenger,
89
- * >({
90
- * namespace: 'SampleGasPricesService',
91
- * parent: rootMessenger,
92
- * });
93
- * // Instantiate the service to register its actions on the messenger
94
- * new SampleGasPricesService({
95
- * messenger: gasPricesServiceMessenger,
96
- * // We assume you're using this in the browser.
97
- * fetch,
98
- * });
99
- * const gasPricesControllerMessenger = new Messenger<
100
- * 'SampleGasPricesController',
101
- * SampleGasPricesControllerActions | NetworkControllerGetNetworkClientByIdAction,
102
- * SampleGasPricesControllerEvents | NetworkControllerStateChangeEvent,
103
- * typeof rootMessenger,
104
- * >({
105
- * namespace: 'SampleGasPricesController',
106
- * parent: rootMessenger,
107
- * });
108
- * // Instantiate the controller to register its actions on the messenger
109
- * new SampleGasPricesController({
110
- * messenger: gasPricesControllerMessenger,
111
- * });
112
- *
113
- * // Later...
114
- * await rootMessenger.call(
115
- * 'SampleGasPricesController:updateGasPrices',
116
- * { chainId: '0x42' },
117
- * );
118
- * const gasPricesControllerState = await rootMessenger.call(
119
- * 'SampleGasPricesController:getState',
120
- * );
121
- * gasPricesControllerState.gasPricesByChainId
122
- * // => { '0x42': { low: 5, average: 10, high: 15, fetchedDate: '2024-01-02T00:00:00.000Z' } }
123
- * ```
124
- */
125
- class SampleGasPricesController extends base_controller_1.BaseController {
126
- /**
127
- * Constructs a new {@link SampleGasPricesController}.
128
- *
129
- * @param args - The constructor arguments.
130
- * @param args.messenger - The messenger suited for this controller.
131
- * @param args.state - The desired state with which to initialize this
132
- * controller. Missing properties will be filled in with defaults.
133
- */
134
- constructor({ messenger, state, }) {
135
- super({
136
- messenger,
137
- metadata: gasPricesControllerMetadata,
138
- name: CONTROLLER_NAME,
139
- state: {
140
- ...getDefaultSampleGasPricesControllerState(),
141
- ...state,
142
- },
143
- });
144
- _SampleGasPricesController_instances.add(this);
145
- /**
146
- * The globally selected chain ID.
147
- */
148
- _SampleGasPricesController_selectedChainId.set(this, void 0);
149
- this.messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
150
- this.messenger.subscribe('NetworkController:stateChange', __classPrivateFieldGet(this, _SampleGasPricesController_instances, "m", _SampleGasPricesController_onSelectedNetworkClientIdChange).bind(this), (networkControllerState) => networkControllerState.selectedNetworkClientId);
151
- }
152
- /**
153
- * Fetches the latest gas prices for the given chain and persists them to
154
- * state.
155
- *
156
- * @param args - The arguments to the function.
157
- * @param args.chainId - The chain ID for which to fetch gas prices.
158
- */
159
- async updateGasPrices({ chainId }) {
160
- const gasPricesResponse = await this.messenger.call('SampleGasPricesService:fetchGasPrices', chainId);
161
- this.update((state) => {
162
- state.gasPricesByChainId[chainId] = {
163
- ...gasPricesResponse,
164
- fetchedDate: new Date().toISOString(),
165
- };
166
- });
167
- }
168
- }
169
- exports.SampleGasPricesController = SampleGasPricesController;
170
- _SampleGasPricesController_selectedChainId = new WeakMap(), _SampleGasPricesController_instances = new WeakSet(), _SampleGasPricesController_onSelectedNetworkClientIdChange = function _SampleGasPricesController_onSelectedNetworkClientIdChange(selectedNetworkClientId) {
171
- const { configuration: { chainId }, } = this.messenger.call('NetworkController:getNetworkClientById', selectedNetworkClientId);
172
- if (chainId !== __classPrivateFieldGet(this, _SampleGasPricesController_selectedChainId, "f")) {
173
- __classPrivateFieldSet(this, _SampleGasPricesController_selectedChainId, chainId, "f");
174
- this.updateGasPrices({ chainId }).catch((error) => {
175
- this.messenger.captureException?.(error);
176
- });
177
- }
178
- };
179
- //# sourceMappingURL=sample-gas-prices-controller.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"sample-gas-prices-controller.cjs","sourceRoot":"","sources":["../src/sample-gas-prices-controller.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAKA,+DAA2D;AAY3D,kBAAkB;AAElB;;;;GAIG;AACH,MAAM,eAAe,GAAG,2BAA2B,CAAC;AAsCpD;;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,4FAIC;AAED,oBAAoB;AAEpB,MAAM,yBAAyB,GAAG,CAAC,iBAAiB,CAAU,CAAC;AAuD/D,gCAAgC;AAEhC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwEG;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,eAAe;YACrB,KAAK,EAAE;gBACL,GAAG,wCAAwC,EAAE;gBAC7C,GAAG,KAAK;aACT;SACF,CAAC,CAAC;;QA5BL;;WAEG;QACH,6DAAkC;QA2BhC,IAAI,CAAC,SAAS,CAAC,4BAA4B,CACzC,IAAI,EACJ,yBAAyB,CAC1B,CAAC;QAEF,IAAI,CAAC,SAAS,CAAC,SAAS,CACtB,+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,SAAS,CAAC,IAAI,CACjD,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;CAyBF;AA5FD,8DA4FC;mPAhBG,uBAAwC;IAExC,MAAM,EACJ,aAAa,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,CAAC;QACtC,uBAAA,IAAI,8CAAoB,OAAO,MAAA,CAAC;QAChC,IAAI,CAAC,eAAe,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;YAChD,IAAI,CAAC,SAAS,CAAC,gBAAgB,EAAE,CAAC,KAAK,CAAC,CAAC;QAC3C,CAAC,CAAC,CAAC;IACL,CAAC;AACH,CAAC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\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.js';\nimport type { SampleGasPricesServiceFetchGasPricesAction } from './sample-gas-prices-service/sample-gas-prices-service-method-action-types.js';\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 */\nconst CONTROLLER_NAME = '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 CONTROLLER_NAME,\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 CONTROLLER_NAME,\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 CONTROLLER_NAME,\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 CONTROLLER_NAME,\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: CONTROLLER_NAME,\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 }): Promise<void> {\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 #onSelectedNetworkClientIdChange(\n selectedNetworkClientId: NetworkClientId,\n ): void {\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 this.updateGasPrices({ chainId }).catch((error) => {\n this.messenger.captureException?.(error);\n });\n }\n }\n}\n"]}
@@ -1 +0,0 @@
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,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,OAAO,KAAK,EAEV,2CAA2C,EAC3C,iCAAiC,EAClC,qCAAqC;AACtC,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAE3C,OAAO,KAAK,EAAE,sCAAsC,EAAE,+DAA8D;AACpH,OAAO,KAAK,EAAE,0CAA0C,EAAE,sFAAqF;AAI/I;;;;GAIG;AACH,QAAA,MAAM,eAAe,8BAA8B,CAAC;AAIpD;;GAEG;AACH,KAAK,SAAS,GAAG;IACf;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;IACZ;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;IAChB;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IACb;;OAEG;IACH,WAAW,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,8BAA8B,GAAG;IAC3C;;OAEG;IACH,kBAAkB,EAAE;QAClB,CAAC,OAAO,EAAE,GAAG,GAAG,SAAS,CAAC;KAC3B,CAAC;CACH,CAAC;AAcF;;;;;;;GAOG;AACH,wBAAgB,wCAAwC,IAAI,8BAA8B,CAIzF;AAMD;;GAEG;AACH,MAAM,MAAM,uCAAuC,GAAG,wBAAwB,CAC5E,OAAO,eAAe,EACtB,8BAA8B,CAC/B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,gCAAgC,GACxC,uCAAuC,GACvC,sCAAsC,CAAC;AAE3C;;GAEG;AACH,KAAK,cAAc,GACf,2CAA2C,GAC3C,0CAA0C,CAAC;AAE/C;;GAEG;AACH,MAAM,MAAM,yCAAyC,GACnD,0BAA0B,CACxB,OAAO,eAAe,EACtB,8BAA8B,CAC/B,CAAC;AAEJ;;GAEG;AACH,MAAM,MAAM,+BAA+B,GACzC,yCAAyC,CAAC;AAE5C;;;GAGG;AACH,KAAK,aAAa,GAAG,iCAAiC,CAAC;AAEvD;;;GAGG;AACH,MAAM,MAAM,kCAAkC,GAAG,SAAS,CACxD,OAAO,eAAe,EACtB,gCAAgC,GAAG,cAAc,EACjD,+BAA+B,GAAG,aAAa,CAChD,CAAC;AAIF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwEG;AACH,qBAAa,yBAA0B,SAAQ,cAAc,CAC3D,OAAO,eAAe,EACtB,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,GAAG,OAAO,CAAC,IAAI,CAAC;CAqCpE"}
@@ -1,184 +0,0 @@
1
- import type { ControllerGetStateAction, ControllerStateChangeEvent } from "@metamask/base-controller";
2
- import { BaseController } from "@metamask/base-controller";
3
- import type { Messenger } from "@metamask/messenger";
4
- import type { NetworkControllerGetNetworkClientByIdAction, NetworkControllerStateChangeEvent } from "@metamask/network-controller";
5
- import type { Hex } from "@metamask/utils";
6
- import type { SampleGasPricesControllerMethodActions } from "./sample-gas-prices-controller-method-action-types.mjs";
7
- import type { SampleGasPricesServiceFetchGasPricesAction } from "./sample-gas-prices-service/sample-gas-prices-service-method-action-types.mjs";
8
- /**
9
- * The name of the {@link SampleGasPricesController}, used to namespace the
10
- * controller's actions and events and to namespace the controller's state data
11
- * when composed with other controllers.
12
- */
13
- declare const CONTROLLER_NAME = "SampleGasPricesController";
14
- /**
15
- * The collection of gas price data fetched periodically.
16
- */
17
- type GasPrices = {
18
- /**
19
- * The total estimated gas in the "low" bucket.
20
- */
21
- low: number;
22
- /**
23
- * The total estimated gas in the "average" bucket.
24
- */
25
- average: number;
26
- /**
27
- * The total estimated gas in the "high" bucket.
28
- */
29
- high: number;
30
- /**
31
- * The date/time (in ISO-8601 format) when prices were fetched.
32
- */
33
- fetchedDate: string;
34
- };
35
- /**
36
- * Describes the shape of the state object for {@link SampleGasPricesController}.
37
- */
38
- export type SampleGasPricesControllerState = {
39
- /**
40
- * Fetched gas prices categorized by chain ID.
41
- */
42
- gasPricesByChainId: {
43
- [chainId: Hex]: GasPrices;
44
- };
45
- };
46
- /**
47
- * Constructs the default {@link SampleGasPricesController} state. This allows
48
- * consumers to provide a partial state object when initializing the controller
49
- * and also helps in constructing complete state objects for this controller in
50
- * tests.
51
- *
52
- * @returns The default {@link SampleGasPricesController} state.
53
- */
54
- export declare function getDefaultSampleGasPricesControllerState(): SampleGasPricesControllerState;
55
- /**
56
- * Retrieves the state of the {@link SampleGasPricesController}.
57
- */
58
- export type SampleGasPricesControllerGetStateAction = ControllerGetStateAction<typeof CONTROLLER_NAME, SampleGasPricesControllerState>;
59
- /**
60
- * Actions that {@link SampleGasPricesMessenger} exposes to other consumers.
61
- */
62
- export type SampleGasPricesControllerActions = SampleGasPricesControllerGetStateAction | SampleGasPricesControllerMethodActions;
63
- /**
64
- * Actions from other messengers that {@link SampleGasPricesMessenger} calls.
65
- */
66
- type AllowedActions = NetworkControllerGetNetworkClientByIdAction | SampleGasPricesServiceFetchGasPricesAction;
67
- /**
68
- * Published when the state of {@link SampleGasPricesController} changes.
69
- */
70
- export type SampleGasPricesControllerStateChangeEvent = ControllerStateChangeEvent<typeof CONTROLLER_NAME, SampleGasPricesControllerState>;
71
- /**
72
- * Events that {@link SampleGasPricesMessenger} exposes to other consumers.
73
- */
74
- export type SampleGasPricesControllerEvents = SampleGasPricesControllerStateChangeEvent;
75
- /**
76
- * Events from other messengers that {@link SampleGasPricesMessenger} subscribes
77
- * to.
78
- */
79
- type AllowedEvents = NetworkControllerStateChangeEvent;
80
- /**
81
- * The messenger restricted to actions and events accessed by
82
- * {@link SampleGasPricesController}.
83
- */
84
- export type SampleGasPricesControllerMessenger = Messenger<typeof CONTROLLER_NAME, SampleGasPricesControllerActions | AllowedActions, SampleGasPricesControllerEvents | AllowedEvents>;
85
- /**
86
- * `SampleGasPricesController` fetches and persists gas prices for various chains.
87
- *
88
- * @example
89
- *
90
- * ``` ts
91
- * import { Messenger } from '@metamask/messenger';
92
- * import type {
93
- * NetworkControllerActions,
94
- * NetworkControllerEvents,
95
- * } from '@metamask/network-controller';
96
- * import type {
97
- * SampleGasPricesControllerActions,
98
- * SampleGasPricesControllerEvents,
99
- * SampleGasPricesServiceActions,
100
- * SampleGasPricesServiceEvents,
101
- * } from '@metamask/sample-controllers';
102
- * import {
103
- * SampleGasPricesController,
104
- * SampleGasPricesService,
105
- * selectGasPrices,
106
- * } from '@metamask/sample-controllers';
107
- *
108
- * const rootMessenger = new Messenger<
109
- * 'Root',
110
- * SampleGasPricesServiceActions
111
- * | SampleGasPricesControllerActions
112
- * | NetworkControllerActions,
113
- * SampleGasPricesServiceEvents
114
- * | SampleGasPricesControllerEvents
115
- * | NetworkControllerEvents
116
- * >({ namespace: 'Root' });
117
- * const gasPricesServiceMessenger = new Messenger<
118
- * 'SampleGasPricesService',
119
- * SampleGasPricesServiceActions,
120
- * SampleGasPricesServiceEvents,
121
- * typeof rootMessenger,
122
- * >({
123
- * namespace: 'SampleGasPricesService',
124
- * parent: rootMessenger,
125
- * });
126
- * // Instantiate the service to register its actions on the messenger
127
- * new SampleGasPricesService({
128
- * messenger: gasPricesServiceMessenger,
129
- * // We assume you're using this in the browser.
130
- * fetch,
131
- * });
132
- * const gasPricesControllerMessenger = new Messenger<
133
- * 'SampleGasPricesController',
134
- * SampleGasPricesControllerActions | NetworkControllerGetNetworkClientByIdAction,
135
- * SampleGasPricesControllerEvents | NetworkControllerStateChangeEvent,
136
- * typeof rootMessenger,
137
- * >({
138
- * namespace: 'SampleGasPricesController',
139
- * parent: rootMessenger,
140
- * });
141
- * // Instantiate the controller to register its actions on the messenger
142
- * new SampleGasPricesController({
143
- * messenger: gasPricesControllerMessenger,
144
- * });
145
- *
146
- * // Later...
147
- * await rootMessenger.call(
148
- * 'SampleGasPricesController:updateGasPrices',
149
- * { chainId: '0x42' },
150
- * );
151
- * const gasPricesControllerState = await rootMessenger.call(
152
- * 'SampleGasPricesController:getState',
153
- * );
154
- * gasPricesControllerState.gasPricesByChainId
155
- * // => { '0x42': { low: 5, average: 10, high: 15, fetchedDate: '2024-01-02T00:00:00.000Z' } }
156
- * ```
157
- */
158
- export declare class SampleGasPricesController extends BaseController<typeof CONTROLLER_NAME, SampleGasPricesControllerState, SampleGasPricesControllerMessenger> {
159
- #private;
160
- /**
161
- * Constructs a new {@link SampleGasPricesController}.
162
- *
163
- * @param args - The constructor arguments.
164
- * @param args.messenger - The messenger suited for this controller.
165
- * @param args.state - The desired state with which to initialize this
166
- * controller. Missing properties will be filled in with defaults.
167
- */
168
- constructor({ messenger, state, }: {
169
- messenger: SampleGasPricesControllerMessenger;
170
- state?: Partial<SampleGasPricesControllerState>;
171
- });
172
- /**
173
- * Fetches the latest gas prices for the given chain and persists them to
174
- * state.
175
- *
176
- * @param args - The arguments to the function.
177
- * @param args.chainId - The chain ID for which to fetch gas prices.
178
- */
179
- updateGasPrices({ chainId }: {
180
- chainId: Hex;
181
- }): Promise<void>;
182
- }
183
- export {};
184
- //# sourceMappingURL=sample-gas-prices-controller.d.mts.map
@@ -1 +0,0 @@
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,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,OAAO,KAAK,EAEV,2CAA2C,EAC3C,iCAAiC,EAClC,qCAAqC;AACtC,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAE3C,OAAO,KAAK,EAAE,sCAAsC,EAAE,+DAA8D;AACpH,OAAO,KAAK,EAAE,0CAA0C,EAAE,sFAAqF;AAI/I;;;;GAIG;AACH,QAAA,MAAM,eAAe,8BAA8B,CAAC;AAIpD;;GAEG;AACH,KAAK,SAAS,GAAG;IACf;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;IACZ;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;IAChB;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IACb;;OAEG;IACH,WAAW,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,8BAA8B,GAAG;IAC3C;;OAEG;IACH,kBAAkB,EAAE;QAClB,CAAC,OAAO,EAAE,GAAG,GAAG,SAAS,CAAC;KAC3B,CAAC;CACH,CAAC;AAcF;;;;;;;GAOG;AACH,wBAAgB,wCAAwC,IAAI,8BAA8B,CAIzF;AAMD;;GAEG;AACH,MAAM,MAAM,uCAAuC,GAAG,wBAAwB,CAC5E,OAAO,eAAe,EACtB,8BAA8B,CAC/B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,gCAAgC,GACxC,uCAAuC,GACvC,sCAAsC,CAAC;AAE3C;;GAEG;AACH,KAAK,cAAc,GACf,2CAA2C,GAC3C,0CAA0C,CAAC;AAE/C;;GAEG;AACH,MAAM,MAAM,yCAAyC,GACnD,0BAA0B,CACxB,OAAO,eAAe,EACtB,8BAA8B,CAC/B,CAAC;AAEJ;;GAEG;AACH,MAAM,MAAM,+BAA+B,GACzC,yCAAyC,CAAC;AAE5C;;;GAGG;AACH,KAAK,aAAa,GAAG,iCAAiC,CAAC;AAEvD;;;GAGG;AACH,MAAM,MAAM,kCAAkC,GAAG,SAAS,CACxD,OAAO,eAAe,EACtB,gCAAgC,GAAG,cAAc,EACjD,+BAA+B,GAAG,aAAa,CAChD,CAAC;AAIF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwEG;AACH,qBAAa,yBAA0B,SAAQ,cAAc,CAC3D,OAAO,eAAe,EACtB,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,GAAG,OAAO,CAAC,IAAI,CAAC;CAqCpE"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"sample-gas-prices-controller.mjs","sourceRoot":"","sources":["../src/sample-gas-prices-controller.ts"],"names":[],"mappings":";;;;;;;;;;;;AAKA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAY3D,kBAAkB;AAElB;;;;GAIG;AACH,MAAM,eAAe,GAAG,2BAA2B,CAAC;AAsCpD;;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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwEG;AACH,MAAM,OAAO,yBAA0B,SAAQ,cAI9C;IAMC;;;;;;;OAOG;IACH,YAAY,EACV,SAAS,EACT,KAAK,GAIN;QACC,KAAK,CAAC;YACJ,SAAS;YACT,QAAQ,EAAE,2BAA2B;YACrC,IAAI,EAAE,eAAe;YACrB,KAAK,EAAE;gBACL,GAAG,wCAAwC,EAAE;gBAC7C,GAAG,KAAK;aACT;SACF,CAAC,CAAC;;QA5BL;;WAEG;QACH,6DAAkC;QA2BhC,IAAI,CAAC,SAAS,CAAC,4BAA4B,CACzC,IAAI,EACJ,yBAAyB,CAC1B,CAAC;QAEF,IAAI,CAAC,SAAS,CAAC,SAAS,CACtB,+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,SAAS,CAAC,IAAI,CACjD,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;CAyBF;mPAhBG,uBAAwC;IAExC,MAAM,EACJ,aAAa,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,CAAC;QACtC,uBAAA,IAAI,8CAAoB,OAAO,MAAA,CAAC;QAChC,IAAI,CAAC,eAAe,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;YAChD,IAAI,CAAC,SAAS,CAAC,gBAAgB,EAAE,CAAC,KAAK,CAAC,CAAC;QAC3C,CAAC,CAAC,CAAC;IACL,CAAC;AACH,CAAC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\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.js';\nimport type { SampleGasPricesServiceFetchGasPricesAction } from './sample-gas-prices-service/sample-gas-prices-service-method-action-types.js';\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 */\nconst CONTROLLER_NAME = '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 CONTROLLER_NAME,\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 CONTROLLER_NAME,\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 CONTROLLER_NAME,\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 CONTROLLER_NAME,\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: CONTROLLER_NAME,\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 }): Promise<void> {\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 #onSelectedNetworkClientIdChange(\n selectedNetworkClientId: NetworkClientId,\n ): void {\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 this.updateGasPrices({ chainId }).catch((error) => {\n this.messenger.captureException?.(error);\n });\n }\n }\n}\n"]}
@@ -1,7 +0,0 @@
1
- "use strict";
2
- /**
3
- * This file is auto generated.
4
- * Do not edit manually.
5
- */
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- //# sourceMappingURL=sample-gas-prices-service-method-action-types.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"sample-gas-prices-service-method-action-types.cjs","sourceRoot":"","sources":["../../src/sample-gas-prices-service/sample-gas-prices-service-method-action-types.ts"],"names":[],"mappings":";AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated.\n * Do not edit manually.\n */\n\nimport type { SampleGasPricesService } from './sample-gas-prices-service.js';\n\n/**\n * Makes a request to the API in order to retrieve gas prices for a particular\n * chain.\n *\n * @param chainId - The chain ID for which you want to fetch gas prices.\n * @returns The gas prices for the given chain.\n */\nexport type SampleGasPricesServiceFetchGasPricesAction = {\n type: `SampleGasPricesService:fetchGasPrices`;\n handler: SampleGasPricesService['fetchGasPrices'];\n};\n\n/**\n * Union of all SampleGasPricesService action types.\n */\nexport type SampleGasPricesServiceMethodActions =\n SampleGasPricesServiceFetchGasPricesAction;\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"file":"sample-gas-prices-service-method-action-types.d.cts","sourceRoot":"","sources":["../../src/sample-gas-prices-service/sample-gas-prices-service-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,sBAAsB,EAAE,wCAAuC;AAE7E;;;;;;GAMG;AACH,MAAM,MAAM,0CAA0C,GAAG;IACvD,IAAI,EAAE,uCAAuC,CAAC;IAC9C,OAAO,EAAE,sBAAsB,CAAC,gBAAgB,CAAC,CAAC;CACnD,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,mCAAmC,GAC7C,0CAA0C,CAAC"}
@@ -1,21 +0,0 @@
1
- /**
2
- * This file is auto generated.
3
- * Do not edit manually.
4
- */
5
- import type { SampleGasPricesService } from "./sample-gas-prices-service.mjs";
6
- /**
7
- * Makes a request to the API in order to retrieve gas prices for a particular
8
- * chain.
9
- *
10
- * @param chainId - The chain ID for which you want to fetch gas prices.
11
- * @returns The gas prices for the given chain.
12
- */
13
- export type SampleGasPricesServiceFetchGasPricesAction = {
14
- type: `SampleGasPricesService:fetchGasPrices`;
15
- handler: SampleGasPricesService['fetchGasPrices'];
16
- };
17
- /**
18
- * Union of all SampleGasPricesService action types.
19
- */
20
- export type SampleGasPricesServiceMethodActions = SampleGasPricesServiceFetchGasPricesAction;
21
- //# sourceMappingURL=sample-gas-prices-service-method-action-types.d.mts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"sample-gas-prices-service-method-action-types.d.mts","sourceRoot":"","sources":["../../src/sample-gas-prices-service/sample-gas-prices-service-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,sBAAsB,EAAE,wCAAuC;AAE7E;;;;;;GAMG;AACH,MAAM,MAAM,0CAA0C,GAAG;IACvD,IAAI,EAAE,uCAAuC,CAAC;IAC9C,OAAO,EAAE,sBAAsB,CAAC,gBAAgB,CAAC,CAAC;CACnD,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,mCAAmC,GAC7C,0CAA0C,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"sample-gas-prices-service-method-action-types.mjs","sourceRoot":"","sources":["../../src/sample-gas-prices-service/sample-gas-prices-service-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated.\n * Do not edit manually.\n */\n\nimport type { SampleGasPricesService } from './sample-gas-prices-service.js';\n\n/**\n * Makes a request to the API in order to retrieve gas prices for a particular\n * chain.\n *\n * @param chainId - The chain ID for which you want to fetch gas prices.\n * @returns The gas prices for the given chain.\n */\nexport type SampleGasPricesServiceFetchGasPricesAction = {\n type: `SampleGasPricesService:fetchGasPrices`;\n handler: SampleGasPricesService['fetchGasPrices'];\n};\n\n/**\n * Union of all SampleGasPricesService action types.\n */\nexport type SampleGasPricesServiceMethodActions =\n SampleGasPricesServiceFetchGasPricesAction;\n"]}
@@ -1,121 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SampleGasPricesService = exports.serviceName = void 0;
4
- const base_data_service_1 = require("@metamask/base-data-service");
5
- const controller_utils_1 = require("@metamask/controller-utils");
6
- const superstruct_1 = require("@metamask/superstruct");
7
- // === GENERAL ===
8
- /**
9
- * The name of the {@link SampleGasPricesService}, used to namespace the
10
- * service's actions and events.
11
- */
12
- exports.serviceName = 'SampleGasPricesService';
13
- // === MESSENGER ===
14
- /**
15
- * All of the methods within {@link SampleGasPricesService} that are exposed via
16
- * the messenger.
17
- */
18
- const MESSENGER_EXPOSED_METHODS = ['fetchGasPrices'];
19
- // === SERVICE DEFINITION ===
20
- /**
21
- * Struct to validate what the API endpoint returns.
22
- */
23
- const GasPricesResponseStruct = (0, superstruct_1.type)({
24
- data: (0, superstruct_1.type)({
25
- low: (0, superstruct_1.number)(),
26
- average: (0, superstruct_1.number)(),
27
- high: (0, superstruct_1.number)(),
28
- }),
29
- });
30
- /**
31
- * The base URL of the API that the service represents.
32
- */
33
- const BASE_URL = 'https://api.example.com';
34
- /**
35
- * This service object is responsible for fetching gas prices via an API.
36
- *
37
- * @example
38
- *
39
- * ``` ts
40
- * import type { MessengerActions, MessengerEvents } from '@metamask/messenger';
41
- * import { Messenger } from '@metamask/messenger';
42
- * import type {
43
- * SampleGasPricesServiceMessenger,
44
- * } from '@metamask/sample-controllers';
45
- *
46
- * const rootMessenger = new Messenger<
47
- * 'Root',
48
- * SampleGasPricesServiceActions
49
- * SampleGasPricesServiceEvents
50
- * >({ namespace: 'Root' });
51
- * const gasPricesServiceMessenger = new Messenger<
52
- * 'SampleGasPricesService',
53
- * MessengerActions<SampleGasPricesServiceMessenger>,
54
- * MessengerEvents<SampleGasPricesServiceMessenger>,
55
- * typeof rootMessenger,
56
- * >({
57
- * namespace: 'SampleGasPricesService',
58
- * parent: rootMessenger,
59
- * });
60
- * // Instantiate the service to register its actions on the messenger
61
- * new SampleGasPricesService({
62
- * messenger: gasPricesServiceMessenger,
63
- * });
64
- *
65
- * // Later...
66
- * // Fetch gas prices for Mainnet
67
- * const gasPrices = await rootMessenger.call(
68
- * 'SampleGasPricesService:fetchGasPrices',
69
- * '0x1',
70
- * );
71
- * // ... Do something with the gas prices ...
72
- * ```
73
- */
74
- class SampleGasPricesService extends base_data_service_1.BaseDataService {
75
- /**
76
- * Constructs a new SampleGasPricesService object.
77
- *
78
- * @param args - The constructor arguments.
79
- * @param args.messenger - The messenger suited for this service.
80
- * @param args.queryClientConfig - Configuration for the underlying TanStack
81
- * Query client.
82
- * @param args.policyOptions - Options to pass to `createServicePolicy`, which
83
- * is used to wrap each request. See {@link CreateServicePolicyOptions}.
84
- */
85
- constructor({ messenger, queryClientConfig = {}, policyOptions = {}, }) {
86
- super({
87
- name: exports.serviceName,
88
- messenger,
89
- queryClientConfig,
90
- policyOptions,
91
- });
92
- this.messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
93
- }
94
- /**
95
- * Makes a request to the API in order to retrieve gas prices for a particular
96
- * chain.
97
- *
98
- * @param chainId - The chain ID for which you want to fetch gas prices.
99
- * @returns The gas prices for the given chain.
100
- */
101
- async fetchGasPrices(chainId) {
102
- const url = new URL('/gas-prices', BASE_URL);
103
- url.searchParams.append('chainId', `eip155:${(0, controller_utils_1.fromHex)(chainId).toString()}`);
104
- const jsonResponse = await this.fetchQuery({
105
- queryKey: [`${this.name}:fetchGasPrices`, chainId],
106
- queryFn: async () => {
107
- const response = await fetch(url);
108
- if (!response.ok) {
109
- throw new controller_utils_1.HttpError(response.status, `Gas prices API failed with status '${response.status}'`);
110
- }
111
- return response.json();
112
- },
113
- });
114
- if (!(0, superstruct_1.is)(jsonResponse, GasPricesResponseStruct)) {
115
- throw new Error('Malformed response received from gas prices API');
116
- }
117
- return jsonResponse.data;
118
- }
119
- }
120
- exports.SampleGasPricesService = SampleGasPricesService;
121
- //# sourceMappingURL=sample-gas-prices-service.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"sample-gas-prices-service.cjs","sourceRoot":"","sources":["../../src/sample-gas-prices-service/sample-gas-prices-service.ts"],"names":[],"mappings":";;;AAAA,mEAA8D;AAO9D,iEAAgE;AAGhE,uDAAyD;AAMzD,kBAAkB;AAElB;;;GAGG;AACU,QAAA,WAAW,GAAG,wBAAwB,CAAC;AAEpD,oBAAoB;AAEpB;;;GAGG;AACH,MAAM,yBAAyB,GAAG,CAAC,gBAAgB,CAAU,CAAC;AAwD9D,6BAA6B;AAE7B;;GAEG;AACH,MAAM,uBAAuB,GAAG,IAAA,kBAAI,EAAC;IACnC,IAAI,EAAE,IAAA,kBAAI,EAAC;QACT,GAAG,EAAE,IAAA,oBAAM,GAAE;QACb,OAAO,EAAE,IAAA,oBAAM,GAAE;QACjB,IAAI,EAAE,IAAA,oBAAM,GAAE;KACf,CAAC;CACH,CAAC,CAAC;AAOH;;GAEG;AACH,MAAM,QAAQ,GAAG,yBAAyB,CAAC;AAE3C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,MAAa,sBAAuB,SAAQ,mCAG3C;IACC;;;;;;;;;OASG;IACH,YAAY,EACV,SAAS,EACT,iBAAiB,GAAG,EAAE,EACtB,aAAa,GAAG,EAAE,GAKnB;QACC,KAAK,CAAC;YACJ,IAAI,EAAE,mBAAW;YACjB,SAAS;YACT,iBAAiB;YACjB,aAAa;SACd,CAAC,CAAC;QAEH,IAAI,CAAC,SAAS,CAAC,4BAA4B,CACzC,IAAI,EACJ,yBAAyB,CAC1B,CAAC;IACJ,CAAC;IAED;;;;;;OAMG;IACH,KAAK,CAAC,cAAc,CAAC,OAAY;QAC/B,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,aAAa,EAAE,QAAQ,CAAC,CAAC;QAC7C,GAAG,CAAC,YAAY,CAAC,MAAM,CAAC,SAAS,EAAE,UAAU,IAAA,0BAAO,EAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;QAE5E,MAAM,YAAY,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC;YACzC,QAAQ,EAAE,CAAC,GAAG,IAAI,CAAC,IAAI,iBAAiB,EAAE,OAAO,CAAC;YAClD,OAAO,EAAE,KAAK,IAAI,EAAE;gBAClB,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,CAAC,CAAC;gBAElC,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;oBACjB,MAAM,IAAI,4BAAS,CACjB,QAAQ,CAAC,MAAM,EACf,sCAAsC,QAAQ,CAAC,MAAM,GAAG,CACzD,CAAC;gBACJ,CAAC;gBAED,OAAO,QAAQ,CAAC,IAAI,EAAE,CAAC;YACzB,CAAC;SACF,CAAC,CAAC;QAEH,IAAI,CAAC,IAAA,gBAAE,EAAC,YAAY,EAAE,uBAAuB,CAAC,EAAE,CAAC;YAC/C,MAAM,IAAI,KAAK,CAAC,iDAAiD,CAAC,CAAC;QACrE,CAAC;QAED,OAAO,YAAY,CAAC,IAAI,CAAC;IAC3B,CAAC;CACF;AArED,wDAqEC","sourcesContent":["import { BaseDataService } from '@metamask/base-data-service';\nimport type {\n DataServiceCacheUpdatedEvent,\n DataServiceGranularCacheUpdatedEvent,\n DataServiceInvalidateQueriesAction,\n} from '@metamask/base-data-service';\nimport type { CreateServicePolicyOptions } from '@metamask/controller-utils';\nimport { HttpError, fromHex } from '@metamask/controller-utils';\nimport type { Messenger } from '@metamask/messenger';\nimport type { Infer } from '@metamask/superstruct';\nimport { is, number, type } from '@metamask/superstruct';\nimport type { Hex } from '@metamask/utils';\nimport type { QueryClientConfig } from '@tanstack/query-core';\n\nimport type { SampleGasPricesServiceMethodActions } from './sample-gas-prices-service-method-action-types.js';\n\n// === GENERAL ===\n\n/**\n * The name of the {@link SampleGasPricesService}, used to namespace the\n * service's actions and events.\n */\nexport const serviceName = 'SampleGasPricesService';\n\n// === MESSENGER ===\n\n/**\n * All of the methods within {@link SampleGasPricesService} that are exposed via\n * the messenger.\n */\nconst MESSENGER_EXPOSED_METHODS = ['fetchGasPrices'] as const;\n\n/**\n * Invalidates cached queries for {@link SampleGasPricesService}.\n */\nexport type SampleGasPricesServiceInvalidateQueriesAction =\n DataServiceInvalidateQueriesAction<typeof serviceName>;\n\n/**\n * Actions that {@link SampleGasPricesService} exposes to other consumers.\n */\nexport type SampleGasPricesServiceActions =\n | SampleGasPricesServiceMethodActions\n | SampleGasPricesServiceInvalidateQueriesAction;\n\n/**\n * Actions from other messengers that {@link SampleGasPricesService} calls.\n */\ntype AllowedActions = never;\n\n/**\n * Published when {@link SampleGasPricesService}'s cache is updated.\n */\nexport type SampleGasPricesServiceCacheUpdatedEvent =\n DataServiceCacheUpdatedEvent<typeof serviceName>;\n\n/**\n * Published when a key within {@link SampleGasPricesService}'s cache is\n * updated.\n */\nexport type SampleGasPricesServiceGranularCacheUpdatedEvent =\n DataServiceGranularCacheUpdatedEvent<typeof serviceName>;\n\n/**\n * Events that {@link SampleGasPricesService} exposes to other consumers.\n */\nexport type SampleGasPricesServiceEvents =\n | SampleGasPricesServiceCacheUpdatedEvent\n | SampleGasPricesServiceGranularCacheUpdatedEvent;\n\n/**\n * Events from other messengers that {@link SampleGasPricesService} subscribes\n * to.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger which is restricted to actions and events accessed by\n * {@link SampleGasPricesService}.\n */\nexport type SampleGasPricesServiceMessenger = Messenger<\n typeof serviceName,\n SampleGasPricesServiceActions | AllowedActions,\n SampleGasPricesServiceEvents | AllowedEvents\n>;\n\n// === SERVICE DEFINITION ===\n\n/**\n * Struct to validate what the API endpoint returns.\n */\nconst GasPricesResponseStruct = type({\n data: type({\n low: number(),\n average: number(),\n high: number(),\n }),\n});\n\n/**\n * What the API endpoint returns.\n */\ntype GasPricesResponse = Infer<typeof GasPricesResponseStruct>;\n\n/**\n * The base URL of the API that the service represents.\n */\nconst BASE_URL = 'https://api.example.com';\n\n/**\n * This service object is responsible for fetching gas prices via an API.\n *\n * @example\n *\n * ``` ts\n * import type { MessengerActions, MessengerEvents } from '@metamask/messenger';\n * import { Messenger } from '@metamask/messenger';\n * import type {\n * SampleGasPricesServiceMessenger,\n * } from '@metamask/sample-controllers';\n *\n * const rootMessenger = new Messenger<\n * 'Root',\n * SampleGasPricesServiceActions\n * SampleGasPricesServiceEvents\n * >({ namespace: 'Root' });\n * const gasPricesServiceMessenger = new Messenger<\n * 'SampleGasPricesService',\n * MessengerActions<SampleGasPricesServiceMessenger>,\n * MessengerEvents<SampleGasPricesServiceMessenger>,\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 * });\n *\n * // Later...\n * // Fetch gas prices for Mainnet\n * const gasPrices = await rootMessenger.call(\n * 'SampleGasPricesService:fetchGasPrices',\n * '0x1',\n * );\n * // ... Do something with the gas prices ...\n * ```\n */\nexport class SampleGasPricesService extends BaseDataService<\n typeof serviceName,\n SampleGasPricesServiceMessenger\n> {\n /**\n * Constructs a new SampleGasPricesService object.\n *\n * @param args - The constructor arguments.\n * @param args.messenger - The messenger suited for this service.\n * @param args.queryClientConfig - Configuration for the underlying TanStack\n * Query client.\n * @param args.policyOptions - Options to pass to `createServicePolicy`, which\n * is used to wrap each request. See {@link CreateServicePolicyOptions}.\n */\n constructor({\n messenger,\n queryClientConfig = {},\n policyOptions = {},\n }: {\n messenger: SampleGasPricesServiceMessenger;\n queryClientConfig?: QueryClientConfig;\n policyOptions?: CreateServicePolicyOptions;\n }) {\n super({\n name: serviceName,\n messenger,\n queryClientConfig,\n policyOptions,\n });\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n }\n\n /**\n * Makes a request to the API in order to retrieve gas prices for a particular\n * chain.\n *\n * @param chainId - The chain ID for which you want to fetch gas prices.\n * @returns The gas prices for the given chain.\n */\n async fetchGasPrices(chainId: Hex): Promise<GasPricesResponse['data']> {\n const url = new URL('/gas-prices', BASE_URL);\n url.searchParams.append('chainId', `eip155:${fromHex(chainId).toString()}`);\n\n const jsonResponse = await this.fetchQuery({\n queryKey: [`${this.name}:fetchGasPrices`, chainId],\n queryFn: async () => {\n const response = await fetch(url);\n\n if (!response.ok) {\n throw new HttpError(\n response.status,\n `Gas prices API failed with status '${response.status}'`,\n );\n }\n\n return response.json();\n },\n });\n\n if (!is(jsonResponse, GasPricesResponseStruct)) {\n throw new Error('Malformed response received from gas prices API');\n }\n\n return jsonResponse.data;\n }\n}\n"]}