@metamask-previews/sample-controllers 1.0.0-preview-aa8c4291 → 1.0.0-preview-e821953

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 (82) hide show
  1. package/CHANGELOG.md +23 -1
  2. package/dist/index.cjs +5 -4
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.cts +7 -3
  5. package/dist/index.d.cts.map +1 -1
  6. package/dist/index.d.mts +7 -3
  7. package/dist/index.d.mts.map +1 -1
  8. package/dist/index.mjs +3 -3
  9. package/dist/index.mjs.map +1 -1
  10. package/dist/sample-gas-prices-controller-method-action-types.cjs +7 -0
  11. package/dist/sample-gas-prices-controller-method-action-types.cjs.map +1 -0
  12. package/dist/sample-gas-prices-controller-method-action-types.d.cts +21 -0
  13. package/dist/sample-gas-prices-controller-method-action-types.d.cts.map +1 -0
  14. package/dist/sample-gas-prices-controller-method-action-types.d.mts +21 -0
  15. package/dist/sample-gas-prices-controller-method-action-types.d.mts.map +1 -0
  16. package/dist/sample-gas-prices-controller-method-action-types.mjs +6 -0
  17. package/dist/sample-gas-prices-controller-method-action-types.mjs.map +1 -0
  18. package/dist/sample-gas-prices-controller.cjs +77 -39
  19. package/dist/sample-gas-prices-controller.cjs.map +1 -1
  20. package/dist/sample-gas-prices-controller.d.cts +74 -59
  21. package/dist/sample-gas-prices-controller.d.cts.map +1 -1
  22. package/dist/sample-gas-prices-controller.d.mts +74 -59
  23. package/dist/sample-gas-prices-controller.d.mts.map +1 -1
  24. package/dist/sample-gas-prices-controller.mjs +77 -39
  25. package/dist/sample-gas-prices-controller.mjs.map +1 -1
  26. package/dist/sample-gas-prices-service/sample-gas-prices-service-method-action-types.cjs +7 -0
  27. package/dist/sample-gas-prices-service/sample-gas-prices-service-method-action-types.cjs.map +1 -0
  28. package/dist/sample-gas-prices-service/sample-gas-prices-service-method-action-types.d.cts +21 -0
  29. package/dist/sample-gas-prices-service/sample-gas-prices-service-method-action-types.d.cts.map +1 -0
  30. package/dist/sample-gas-prices-service/sample-gas-prices-service-method-action-types.d.mts +21 -0
  31. package/dist/sample-gas-prices-service/sample-gas-prices-service-method-action-types.d.mts.map +1 -0
  32. package/dist/sample-gas-prices-service/sample-gas-prices-service-method-action-types.mjs +6 -0
  33. package/dist/sample-gas-prices-service/sample-gas-prices-service-method-action-types.mjs.map +1 -0
  34. package/dist/sample-gas-prices-service/sample-gas-prices-service.cjs +135 -29
  35. package/dist/sample-gas-prices-service/sample-gas-prices-service.cjs.map +1 -1
  36. package/dist/sample-gas-prices-service/sample-gas-prices-service.d.cts +122 -28
  37. package/dist/sample-gas-prices-service/sample-gas-prices-service.d.cts.map +1 -1
  38. package/dist/sample-gas-prices-service/sample-gas-prices-service.d.mts +122 -28
  39. package/dist/sample-gas-prices-service/sample-gas-prices-service.d.mts.map +1 -1
  40. package/dist/sample-gas-prices-service/sample-gas-prices-service.mjs +134 -28
  41. package/dist/sample-gas-prices-service/sample-gas-prices-service.mjs.map +1 -1
  42. package/dist/sample-petnames-controller-method-action-types.cjs +7 -0
  43. package/dist/sample-petnames-controller-method-action-types.cjs.map +1 -0
  44. package/dist/sample-petnames-controller-method-action-types.d.cts +22 -0
  45. package/dist/sample-petnames-controller-method-action-types.d.cts.map +1 -0
  46. package/dist/sample-petnames-controller-method-action-types.d.mts +22 -0
  47. package/dist/sample-petnames-controller-method-action-types.d.mts.map +1 -0
  48. package/dist/sample-petnames-controller-method-action-types.mjs +6 -0
  49. package/dist/sample-petnames-controller-method-action-types.mjs.map +1 -0
  50. package/dist/sample-petnames-controller.cjs +24 -13
  51. package/dist/sample-petnames-controller.cjs.map +1 -1
  52. package/dist/sample-petnames-controller.d.cts +38 -23
  53. package/dist/sample-petnames-controller.d.cts.map +1 -1
  54. package/dist/sample-petnames-controller.d.mts +38 -23
  55. package/dist/sample-petnames-controller.d.mts.map +1 -1
  56. package/dist/sample-petnames-controller.mjs +23 -13
  57. package/dist/sample-petnames-controller.mjs.map +1 -1
  58. package/package.json +2 -1
  59. package/dist/network-controller-types.cjs +0 -20
  60. package/dist/network-controller-types.cjs.map +0 -1
  61. package/dist/network-controller-types.d.cts +0 -34
  62. package/dist/network-controller-types.d.cts.map +0 -1
  63. package/dist/network-controller-types.d.mts +0 -34
  64. package/dist/network-controller-types.d.mts.map +0 -1
  65. package/dist/network-controller-types.mjs +0 -16
  66. package/dist/network-controller-types.mjs.map +0 -1
  67. package/dist/sample-gas-prices-service/index.cjs +0 -6
  68. package/dist/sample-gas-prices-service/index.cjs.map +0 -1
  69. package/dist/sample-gas-prices-service/index.d.cts +0 -3
  70. package/dist/sample-gas-prices-service/index.d.cts.map +0 -1
  71. package/dist/sample-gas-prices-service/index.d.mts +0 -3
  72. package/dist/sample-gas-prices-service/index.d.mts.map +0 -1
  73. package/dist/sample-gas-prices-service/index.mjs +0 -2
  74. package/dist/sample-gas-prices-service/index.mjs.map +0 -1
  75. package/dist/sample-gas-prices-service/sample-abstract-gas-prices-service.cjs +0 -3
  76. package/dist/sample-gas-prices-service/sample-abstract-gas-prices-service.cjs.map +0 -1
  77. package/dist/sample-gas-prices-service/sample-abstract-gas-prices-service.d.cts +0 -7
  78. package/dist/sample-gas-prices-service/sample-abstract-gas-prices-service.d.cts.map +0 -1
  79. package/dist/sample-gas-prices-service/sample-abstract-gas-prices-service.d.mts +0 -7
  80. package/dist/sample-gas-prices-service/sample-abstract-gas-prices-service.d.mts.map +0 -1
  81. package/dist/sample-gas-prices-service/sample-abstract-gas-prices-service.mjs +0 -2
  82. package/dist/sample-gas-prices-service/sample-abstract-gas-prices-service.mjs.map +0 -1
@@ -1,8 +1,9 @@
1
1
  import type { ControllerGetStateAction, ControllerStateChangeEvent, RestrictedMessenger } from "@metamask/base-controller";
2
2
  import { BaseController } from "@metamask/base-controller";
3
+ import type { NetworkControllerGetNetworkClientByIdAction, NetworkControllerStateChangeEvent } from "@metamask/network-controller";
3
4
  import type { Hex } from "@metamask/utils";
4
- import type { NetworkControllerGetStateAction } from "./network-controller-types.cjs";
5
- import type { SampleAbstractGasPricesService } from "./sample-gas-prices-service/index.cjs";
5
+ import type { SampleGasPricesControllerMethodActions } from "./sample-gas-prices-controller-method-action-types.cjs";
6
+ import type { SampleGasPricesServiceFetchGasPricesAction } from "./sample-gas-prices-service/sample-gas-prices-service-method-action-types.cjs";
6
7
  /**
7
8
  * The name of the {@link SampleGasPricesController}, used to namespace the
8
9
  * controller's actions and events and to namespace the controller's state data
@@ -35,61 +36,51 @@ type GasPrices = {
35
36
  */
36
37
  export type SampleGasPricesControllerState = {
37
38
  /**
38
- * The registry of pet names, categorized by chain ID first and address
39
- * second.
39
+ * Fetched gas prices categorized by chain ID.
40
40
  */
41
41
  gasPricesByChainId: {
42
42
  [chainId: Hex]: GasPrices;
43
43
  };
44
44
  };
45
45
  /**
46
- * The action which can be used to retrieve the state of the
47
- * {@link SampleGasPricesController}.
46
+ * Constructs the default {@link SampleGasPricesController} state. This allows
47
+ * consumers to provide a partial state object when initializing the controller
48
+ * and also helps in constructing complete state objects for this controller in
49
+ * tests.
50
+ *
51
+ * @returns The default {@link SampleGasPricesController} state.
48
52
  */
49
- export type SampleGasPricesControllerGetStateAction = ControllerGetStateAction<typeof controllerName, SampleGasPricesControllerState>;
53
+ export declare function getDefaultSampleGasPricesControllerState(): SampleGasPricesControllerState;
50
54
  /**
51
- * The action which can be used to update gas prices.
55
+ * Retrieves the state of the {@link SampleGasPricesController}.
52
56
  */
53
- export type SampleGasPricesControllerUpdateGasPricesAction = {
54
- type: `${typeof controllerName}:updateGasPrices`;
55
- handler: SampleGasPricesController['updateGasPrices'];
56
- };
57
+ export type SampleGasPricesControllerGetStateAction = ControllerGetStateAction<typeof controllerName, SampleGasPricesControllerState>;
57
58
  /**
58
- * All actions that {@link SampleGasPricesController} registers, to be called
59
- * externally.
59
+ * Actions that {@link SampleGasPricesMessenger} exposes to other consumers.
60
60
  */
61
- export type SampleGasPricesControllerActions = SampleGasPricesControllerGetStateAction | SampleGasPricesControllerUpdateGasPricesAction;
61
+ export type SampleGasPricesControllerActions = SampleGasPricesControllerGetStateAction | SampleGasPricesControllerMethodActions;
62
62
  /**
63
- * All actions that {@link SampleGasPricesController} calls internally.
63
+ * Actions from other messengers that {@link SampleGasPricesMessenger} calls.
64
64
  */
65
- type AllowedActions = NetworkControllerGetStateAction;
65
+ type AllowedActions = NetworkControllerGetNetworkClientByIdAction | SampleGasPricesServiceFetchGasPricesAction;
66
66
  /**
67
- * The event that {@link SampleGasPricesController} publishes when updating state.
67
+ * Published when the state of {@link SampleGasPricesController} changes.
68
68
  */
69
69
  export type SampleGasPricesControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, SampleGasPricesControllerState>;
70
70
  /**
71
- * All events that {@link SampleGasPricesController} publishes, to be subscribed to
72
- * externally.
71
+ * Events that {@link SampleGasPricesMessenger} exposes to other consumers.
73
72
  */
74
73
  export type SampleGasPricesControllerEvents = SampleGasPricesControllerStateChangeEvent;
75
74
  /**
76
- * All events that {@link SampleGasPricesController} subscribes to internally.
75
+ * Events from other messengers that {@link SampleGasPricesMessenger} subscribes
76
+ * to.
77
77
  */
78
- type AllowedEvents = never;
78
+ type AllowedEvents = NetworkControllerStateChangeEvent;
79
79
  /**
80
- * The messenger which is restricted to actions and events accessed by
80
+ * The messenger restricted to actions and events accessed by
81
81
  * {@link SampleGasPricesController}.
82
82
  */
83
83
  export type SampleGasPricesControllerMessenger = RestrictedMessenger<typeof controllerName, SampleGasPricesControllerActions | AllowedActions, SampleGasPricesControllerEvents | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
84
- /**
85
- * Constructs the default {@link SampleGasPricesController} state. This allows
86
- * consumers to provide a partial state object when initializing the controller
87
- * and also helps in constructing complete state objects for this controller in
88
- * tests.
89
- *
90
- * @returns The default {@link SampleGasPricesController} state.
91
- */
92
- export declare function getDefaultSampleGasPricesControllerState(): SampleGasPricesControllerState;
93
84
  /**
94
85
  * `SampleGasPricesController` fetches and persists gas prices for various chains.
95
86
  *
@@ -97,36 +88,58 @@ export declare function getDefaultSampleGasPricesControllerState(): SampleGasPri
97
88
  *
98
89
  * ``` ts
99
90
  * import { Messenger } from '@metamask/base-controller';
100
- * import {
101
- * SampleGasPricesController,
102
- * SampleGasPricesService
103
- * } from '@metamask/example-controllers';
91
+ * import type {
92
+ * NetworkControllerActions,
93
+ * NetworkControllerEvents
94
+ * } from '@metamask/network-controller';
104
95
  * import type {
105
96
  * SampleGasPricesControllerActions,
106
97
  * SampleGasPricesControllerEvents
107
98
  * } from '@metamask/example-controllers';
108
- * import type { NetworkControllerGetStateAction } from '@metamask/network-controller';
99
+ * import {
100
+ * SampleGasPricesController,
101
+ * SampleGasPricesService,
102
+ * selectGasPrices,
103
+ * } from '@metamask/example-controllers';
109
104
  *
110
- * // Assuming that you're using this in the browser
111
- * const gasPricesService = new SampleGasPricesService({ fetch });
112
- * const rootMessenger = new Messenger<
113
- * SampleGasPricesControllerActions | NetworkControllerGetStateAction,
114
- * SampleGasPricesControllerEvents
105
+ * const globalMessenger = new Messenger<
106
+ * SampleGasPricesServiceActions
107
+ * | SampleGasPricesControllerActions
108
+ * | NetworkControllerActions
109
+ * SampleGasPricesServiceEvents
110
+ * | SampleGasPricesControllerEvents
111
+ * | NetworkControllerEvents
115
112
  * >();
116
- * const gasPricesMessenger = rootMessenger.getRestricted({
117
- * name: 'SampleGasPricesController',
118
- * allowedActions: ['NetworkController:getState'],
113
+ * const gasPricesServiceMessenger = globalMessenger.getRestricted({
114
+ * name: 'SampleGasPricesService',
115
+ * allowedActions: [],
119
116
  * allowedEvents: [],
120
117
  * });
121
- * const gasPricesController = new SampleGasPricesController({
122
- * messenger: gasPricesMessenger,
123
- * gasPricesService,
118
+ * // Instantiate the service to register its actions on the messenger
119
+ * new SampleGasPricesService({
120
+ * messenger: gasPricesServiceMessenger,
121
+ * // We assume you're using this in the browser.
122
+ * fetch,
123
+ * });
124
+ * const gasPricesControllerMessenger = globalMessenger.getRestricted({
125
+ * name: 'SampleGasPricesController',
126
+ * allowedActions: ['NetworkController:getNetworkClientById'],
127
+ * allowedEvents: ['NetworkController:stateChange'],
128
+ * });
129
+ * // Instantiate the controller to register its actions on the messenger
130
+ * new SampleGasPricesController({
131
+ * messenger: gasPricesControllerMessenger,
124
132
  * });
125
133
  *
126
- * // Assuming that `NetworkController:getState` returns an object with a
127
- * // `chainId` of `0x42`...
128
- * await gasPricesController.updateGasPrices();
129
- * gasPricesController.state.gasPricesByChainId
134
+ * // Later...
135
+ * await globalMessenger.call(
136
+ * 'SampleGasPricesController:updateGasPrices',
137
+ * { chainId: '0x42' },
138
+ * );
139
+ * const gasPricesControllerState = await globalMessenger.call(
140
+ * 'SampleGasPricesController:getState',
141
+ * );
142
+ * gasPricesControllerState.gasPricesByChainId
130
143
  * // => { '0x42': { low: 5, average: 10, high: 15, fetchedDate: '2024-01-02T00:00:00.000Z' } }
131
144
  * ```
132
145
  */
@@ -135,23 +148,25 @@ export declare class SampleGasPricesController extends BaseController<typeof con
135
148
  /**
136
149
  * Constructs a new {@link SampleGasPricesController}.
137
150
  *
138
- * @param args - The arguments to the controller.
151
+ * @param args - The constructor arguments.
139
152
  * @param args.messenger - The messenger suited for this controller.
140
153
  * @param args.state - The desired state with which to initialize this
141
154
  * controller. Missing properties will be filled in with defaults.
142
- * @param args.gasPricesService - The service object that will be used to
143
- * obtain gas prices.
144
155
  */
145
- constructor({ messenger, state, gasPricesService, }: {
156
+ constructor({ messenger, state, }: {
146
157
  messenger: SampleGasPricesControllerMessenger;
147
158
  state?: Partial<SampleGasPricesControllerState>;
148
- gasPricesService: SampleAbstractGasPricesService;
149
159
  });
150
160
  /**
151
- * Fetches the latest gas prices for the current chain, persisting them to
161
+ * Fetches the latest gas prices for the given chain and persists them to
152
162
  * state.
163
+ *
164
+ * @param args - The arguments to the function.
165
+ * @param args.chainId - The chain ID for which to fetch gas prices.
153
166
  */
154
- updateGasPrices(): Promise<void>;
167
+ updateGasPrices({ chainId }: {
168
+ chainId: Hex;
169
+ }): Promise<void>;
155
170
  }
156
171
  export {};
157
172
  //# sourceMappingURL=sample-gas-prices-controller.d.cts.map
@@ -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,EAC1B,mBAAmB,EAEpB,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAE3C,OAAO,KAAK,EAAE,+BAA+B,EAAE,uCAAmC;AAClF,OAAO,KAAK,EAAE,8BAA8B,EAAE,8CAAoC;AAIlF;;;;GAIG;AACH,eAAO,MAAM,cAAc,8BAA8B,CAAC;AAI1D;;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;;;OAGG;IACH,kBAAkB,EAAE;QAClB,CAAC,OAAO,EAAE,GAAG,GAAG,SAAS,CAAC;KAC3B,CAAC;CACH,CAAC;AAcF;;;GAGG;AACH,MAAM,MAAM,uCAAuC,GAAG,wBAAwB,CAC5E,OAAO,cAAc,EACrB,8BAA8B,CAC/B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,8CAA8C,GAAG;IAC3D,IAAI,EAAE,GAAG,OAAO,cAAc,kBAAkB,CAAC;IACjD,OAAO,EAAE,yBAAyB,CAAC,iBAAiB,CAAC,CAAC;CACvD,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,gCAAgC,GACxC,uCAAuC,GACvC,8CAA8C,CAAC;AAEnD;;GAEG;AACH,KAAK,cAAc,GAAG,+BAA+B,CAAC;AAEtD;;GAEG;AACH,MAAM,MAAM,yCAAyC,GACnD,0BAA0B,CACxB,OAAO,cAAc,EACrB,8BAA8B,CAC/B,CAAC;AAEJ;;;GAGG;AACH,MAAM,MAAM,+BAA+B,GACzC,yCAAyC,CAAC;AAE5C;;GAEG;AACH,KAAK,aAAa,GAAG,KAAK,CAAC;AAE3B;;;GAGG;AACH,MAAM,MAAM,kCAAkC,GAAG,mBAAmB,CAClE,OAAO,cAAc,EACrB,gCAAgC,GAAG,cAAc,EACjD,+BAA+B,GAAG,aAAa,EAC/C,cAAc,CAAC,MAAM,CAAC,EACtB,aAAa,CAAC,MAAM,CAAC,CACtB,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAgB,wCAAwC,IAAI,8BAA8B,CAIzF;AAID;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,qBAAa,yBAA0B,SAAQ,cAAc,CAC3D,OAAO,cAAc,EACrB,8BAA8B,EAC9B,kCAAkC,CACnC;;IAMC;;;;;;;;;OASG;gBACS,EACV,SAAS,EACT,KAAK,EACL,gBAAgB,GACjB,EAAE;QACD,SAAS,EAAE,kCAAkC,CAAC;QAC9C,KAAK,CAAC,EAAE,OAAO,CAAC,8BAA8B,CAAC,CAAC;QAChD,gBAAgB,EAAE,8BAA8B,CAAC;KAClD;IAmBD;;;OAGG;IACG,eAAe;CAWtB"}
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,sFAAkF;AAI5I;;;;GAIG;AACH,eAAO,MAAM,cAAc,8BAA8B,CAAC;AAI1D;;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;AAYF;;;;;;;GAOG;AACH,wBAAgB,wCAAwC,IAAI,8BAA8B,CAIzF;AAMD;;GAEG;AACH,MAAM,MAAM,uCAAuC,GAAG,wBAAwB,CAC5E,OAAO,cAAc,EACrB,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,cAAc,EACrB,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,mBAAmB,CAClE,OAAO,cAAc,EACrB,gCAAgC,GAAG,cAAc,EACjD,+BAA+B,GAAG,aAAa,EAC/C,cAAc,CAAC,MAAM,CAAC,EACtB,aAAa,CAAC,MAAM,CAAC,CACtB,CAAC;AAIF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6DG;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"}
@@ -1,8 +1,9 @@
1
1
  import type { ControllerGetStateAction, ControllerStateChangeEvent, RestrictedMessenger } from "@metamask/base-controller";
2
2
  import { BaseController } from "@metamask/base-controller";
3
+ import type { NetworkControllerGetNetworkClientByIdAction, NetworkControllerStateChangeEvent } from "@metamask/network-controller";
3
4
  import type { Hex } from "@metamask/utils";
4
- import type { NetworkControllerGetStateAction } from "./network-controller-types.mjs";
5
- import type { SampleAbstractGasPricesService } from "./sample-gas-prices-service/index.mjs";
5
+ import type { SampleGasPricesControllerMethodActions } from "./sample-gas-prices-controller-method-action-types.mjs";
6
+ import type { SampleGasPricesServiceFetchGasPricesAction } from "./sample-gas-prices-service/sample-gas-prices-service-method-action-types.mjs";
6
7
  /**
7
8
  * The name of the {@link SampleGasPricesController}, used to namespace the
8
9
  * controller's actions and events and to namespace the controller's state data
@@ -35,61 +36,51 @@ type GasPrices = {
35
36
  */
36
37
  export type SampleGasPricesControllerState = {
37
38
  /**
38
- * The registry of pet names, categorized by chain ID first and address
39
- * second.
39
+ * Fetched gas prices categorized by chain ID.
40
40
  */
41
41
  gasPricesByChainId: {
42
42
  [chainId: Hex]: GasPrices;
43
43
  };
44
44
  };
45
45
  /**
46
- * The action which can be used to retrieve the state of the
47
- * {@link SampleGasPricesController}.
46
+ * Constructs the default {@link SampleGasPricesController} state. This allows
47
+ * consumers to provide a partial state object when initializing the controller
48
+ * and also helps in constructing complete state objects for this controller in
49
+ * tests.
50
+ *
51
+ * @returns The default {@link SampleGasPricesController} state.
48
52
  */
49
- export type SampleGasPricesControllerGetStateAction = ControllerGetStateAction<typeof controllerName, SampleGasPricesControllerState>;
53
+ export declare function getDefaultSampleGasPricesControllerState(): SampleGasPricesControllerState;
50
54
  /**
51
- * The action which can be used to update gas prices.
55
+ * Retrieves the state of the {@link SampleGasPricesController}.
52
56
  */
53
- export type SampleGasPricesControllerUpdateGasPricesAction = {
54
- type: `${typeof controllerName}:updateGasPrices`;
55
- handler: SampleGasPricesController['updateGasPrices'];
56
- };
57
+ export type SampleGasPricesControllerGetStateAction = ControllerGetStateAction<typeof controllerName, SampleGasPricesControllerState>;
57
58
  /**
58
- * All actions that {@link SampleGasPricesController} registers, to be called
59
- * externally.
59
+ * Actions that {@link SampleGasPricesMessenger} exposes to other consumers.
60
60
  */
61
- export type SampleGasPricesControllerActions = SampleGasPricesControllerGetStateAction | SampleGasPricesControllerUpdateGasPricesAction;
61
+ export type SampleGasPricesControllerActions = SampleGasPricesControllerGetStateAction | SampleGasPricesControllerMethodActions;
62
62
  /**
63
- * All actions that {@link SampleGasPricesController} calls internally.
63
+ * Actions from other messengers that {@link SampleGasPricesMessenger} calls.
64
64
  */
65
- type AllowedActions = NetworkControllerGetStateAction;
65
+ type AllowedActions = NetworkControllerGetNetworkClientByIdAction | SampleGasPricesServiceFetchGasPricesAction;
66
66
  /**
67
- * The event that {@link SampleGasPricesController} publishes when updating state.
67
+ * Published when the state of {@link SampleGasPricesController} changes.
68
68
  */
69
69
  export type SampleGasPricesControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, SampleGasPricesControllerState>;
70
70
  /**
71
- * All events that {@link SampleGasPricesController} publishes, to be subscribed to
72
- * externally.
71
+ * Events that {@link SampleGasPricesMessenger} exposes to other consumers.
73
72
  */
74
73
  export type SampleGasPricesControllerEvents = SampleGasPricesControllerStateChangeEvent;
75
74
  /**
76
- * All events that {@link SampleGasPricesController} subscribes to internally.
75
+ * Events from other messengers that {@link SampleGasPricesMessenger} subscribes
76
+ * to.
77
77
  */
78
- type AllowedEvents = never;
78
+ type AllowedEvents = NetworkControllerStateChangeEvent;
79
79
  /**
80
- * The messenger which is restricted to actions and events accessed by
80
+ * The messenger restricted to actions and events accessed by
81
81
  * {@link SampleGasPricesController}.
82
82
  */
83
83
  export type SampleGasPricesControllerMessenger = RestrictedMessenger<typeof controllerName, SampleGasPricesControllerActions | AllowedActions, SampleGasPricesControllerEvents | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
84
- /**
85
- * Constructs the default {@link SampleGasPricesController} state. This allows
86
- * consumers to provide a partial state object when initializing the controller
87
- * and also helps in constructing complete state objects for this controller in
88
- * tests.
89
- *
90
- * @returns The default {@link SampleGasPricesController} state.
91
- */
92
- export declare function getDefaultSampleGasPricesControllerState(): SampleGasPricesControllerState;
93
84
  /**
94
85
  * `SampleGasPricesController` fetches and persists gas prices for various chains.
95
86
  *
@@ -97,36 +88,58 @@ export declare function getDefaultSampleGasPricesControllerState(): SampleGasPri
97
88
  *
98
89
  * ``` ts
99
90
  * import { Messenger } from '@metamask/base-controller';
100
- * import {
101
- * SampleGasPricesController,
102
- * SampleGasPricesService
103
- * } from '@metamask/example-controllers';
91
+ * import type {
92
+ * NetworkControllerActions,
93
+ * NetworkControllerEvents
94
+ * } from '@metamask/network-controller';
104
95
  * import type {
105
96
  * SampleGasPricesControllerActions,
106
97
  * SampleGasPricesControllerEvents
107
98
  * } from '@metamask/example-controllers';
108
- * import type { NetworkControllerGetStateAction } from '@metamask/network-controller';
99
+ * import {
100
+ * SampleGasPricesController,
101
+ * SampleGasPricesService,
102
+ * selectGasPrices,
103
+ * } from '@metamask/example-controllers';
109
104
  *
110
- * // Assuming that you're using this in the browser
111
- * const gasPricesService = new SampleGasPricesService({ fetch });
112
- * const rootMessenger = new Messenger<
113
- * SampleGasPricesControllerActions | NetworkControllerGetStateAction,
114
- * SampleGasPricesControllerEvents
105
+ * const globalMessenger = new Messenger<
106
+ * SampleGasPricesServiceActions
107
+ * | SampleGasPricesControllerActions
108
+ * | NetworkControllerActions
109
+ * SampleGasPricesServiceEvents
110
+ * | SampleGasPricesControllerEvents
111
+ * | NetworkControllerEvents
115
112
  * >();
116
- * const gasPricesMessenger = rootMessenger.getRestricted({
117
- * name: 'SampleGasPricesController',
118
- * allowedActions: ['NetworkController:getState'],
113
+ * const gasPricesServiceMessenger = globalMessenger.getRestricted({
114
+ * name: 'SampleGasPricesService',
115
+ * allowedActions: [],
119
116
  * allowedEvents: [],
120
117
  * });
121
- * const gasPricesController = new SampleGasPricesController({
122
- * messenger: gasPricesMessenger,
123
- * gasPricesService,
118
+ * // Instantiate the service to register its actions on the messenger
119
+ * new SampleGasPricesService({
120
+ * messenger: gasPricesServiceMessenger,
121
+ * // We assume you're using this in the browser.
122
+ * fetch,
123
+ * });
124
+ * const gasPricesControllerMessenger = globalMessenger.getRestricted({
125
+ * name: 'SampleGasPricesController',
126
+ * allowedActions: ['NetworkController:getNetworkClientById'],
127
+ * allowedEvents: ['NetworkController:stateChange'],
128
+ * });
129
+ * // Instantiate the controller to register its actions on the messenger
130
+ * new SampleGasPricesController({
131
+ * messenger: gasPricesControllerMessenger,
124
132
  * });
125
133
  *
126
- * // Assuming that `NetworkController:getState` returns an object with a
127
- * // `chainId` of `0x42`...
128
- * await gasPricesController.updateGasPrices();
129
- * gasPricesController.state.gasPricesByChainId
134
+ * // Later...
135
+ * await globalMessenger.call(
136
+ * 'SampleGasPricesController:updateGasPrices',
137
+ * { chainId: '0x42' },
138
+ * );
139
+ * const gasPricesControllerState = await globalMessenger.call(
140
+ * 'SampleGasPricesController:getState',
141
+ * );
142
+ * gasPricesControllerState.gasPricesByChainId
130
143
  * // => { '0x42': { low: 5, average: 10, high: 15, fetchedDate: '2024-01-02T00:00:00.000Z' } }
131
144
  * ```
132
145
  */
@@ -135,23 +148,25 @@ export declare class SampleGasPricesController extends BaseController<typeof con
135
148
  /**
136
149
  * Constructs a new {@link SampleGasPricesController}.
137
150
  *
138
- * @param args - The arguments to the controller.
151
+ * @param args - The constructor arguments.
139
152
  * @param args.messenger - The messenger suited for this controller.
140
153
  * @param args.state - The desired state with which to initialize this
141
154
  * controller. Missing properties will be filled in with defaults.
142
- * @param args.gasPricesService - The service object that will be used to
143
- * obtain gas prices.
144
155
  */
145
- constructor({ messenger, state, gasPricesService, }: {
156
+ constructor({ messenger, state, }: {
146
157
  messenger: SampleGasPricesControllerMessenger;
147
158
  state?: Partial<SampleGasPricesControllerState>;
148
- gasPricesService: SampleAbstractGasPricesService;
149
159
  });
150
160
  /**
151
- * Fetches the latest gas prices for the current chain, persisting them to
161
+ * Fetches the latest gas prices for the given chain and persists them to
152
162
  * state.
163
+ *
164
+ * @param args - The arguments to the function.
165
+ * @param args.chainId - The chain ID for which to fetch gas prices.
153
166
  */
154
- updateGasPrices(): Promise<void>;
167
+ updateGasPrices({ chainId }: {
168
+ chainId: Hex;
169
+ }): Promise<void>;
155
170
  }
156
171
  export {};
157
172
  //# sourceMappingURL=sample-gas-prices-controller.d.mts.map
@@ -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,EAC1B,mBAAmB,EAEpB,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAE3C,OAAO,KAAK,EAAE,+BAA+B,EAAE,uCAAmC;AAClF,OAAO,KAAK,EAAE,8BAA8B,EAAE,8CAAoC;AAIlF;;;;GAIG;AACH,eAAO,MAAM,cAAc,8BAA8B,CAAC;AAI1D;;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;;;OAGG;IACH,kBAAkB,EAAE;QAClB,CAAC,OAAO,EAAE,GAAG,GAAG,SAAS,CAAC;KAC3B,CAAC;CACH,CAAC;AAcF;;;GAGG;AACH,MAAM,MAAM,uCAAuC,GAAG,wBAAwB,CAC5E,OAAO,cAAc,EACrB,8BAA8B,CAC/B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,8CAA8C,GAAG;IAC3D,IAAI,EAAE,GAAG,OAAO,cAAc,kBAAkB,CAAC;IACjD,OAAO,EAAE,yBAAyB,CAAC,iBAAiB,CAAC,CAAC;CACvD,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,gCAAgC,GACxC,uCAAuC,GACvC,8CAA8C,CAAC;AAEnD;;GAEG;AACH,KAAK,cAAc,GAAG,+BAA+B,CAAC;AAEtD;;GAEG;AACH,MAAM,MAAM,yCAAyC,GACnD,0BAA0B,CACxB,OAAO,cAAc,EACrB,8BAA8B,CAC/B,CAAC;AAEJ;;;GAGG;AACH,MAAM,MAAM,+BAA+B,GACzC,yCAAyC,CAAC;AAE5C;;GAEG;AACH,KAAK,aAAa,GAAG,KAAK,CAAC;AAE3B;;;GAGG;AACH,MAAM,MAAM,kCAAkC,GAAG,mBAAmB,CAClE,OAAO,cAAc,EACrB,gCAAgC,GAAG,cAAc,EACjD,+BAA+B,GAAG,aAAa,EAC/C,cAAc,CAAC,MAAM,CAAC,EACtB,aAAa,CAAC,MAAM,CAAC,CACtB,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAgB,wCAAwC,IAAI,8BAA8B,CAIzF;AAID;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,qBAAa,yBAA0B,SAAQ,cAAc,CAC3D,OAAO,cAAc,EACrB,8BAA8B,EAC9B,kCAAkC,CACnC;;IAMC;;;;;;;;;OASG;gBACS,EACV,SAAS,EACT,KAAK,EACL,gBAAgB,GACjB,EAAE;QACD,SAAS,EAAE,kCAAkC,CAAC;QAC9C,KAAK,CAAC,EAAE,OAAO,CAAC,8BAA8B,CAAC,CAAC;QAChD,gBAAgB,EAAE,8BAA8B,CAAC;KAClD;IAmBD;;;OAGG;IACG,eAAe;CAWtB"}
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,0CAA0C,EAAE,sFAAkF;AAI5I;;;;GAIG;AACH,eAAO,MAAM,cAAc,8BAA8B,CAAC;AAI1D;;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;AAYF;;;;;;;GAOG;AACH,wBAAgB,wCAAwC,IAAI,8BAA8B,CAIzF;AAMD;;GAEG;AACH,MAAM,MAAM,uCAAuC,GAAG,wBAAwB,CAC5E,OAAO,cAAc,EACrB,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,cAAc,EACrB,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,mBAAmB,CAClE,OAAO,cAAc,EACrB,gCAAgC,GAAG,cAAc,EACjD,+BAA+B,GAAG,aAAa,EAC/C,cAAc,CAAC,MAAM,CAAC,EACtB,aAAa,CAAC,MAAM,CAAC,CACtB,CAAC;AAIF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6DG;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"}
@@ -1,15 +1,15 @@
1
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
2
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
3
+ 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");
4
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
5
+ };
1
6
  var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
7
  if (kind === "m") throw new TypeError("Private method is not writable");
3
8
  if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
9
  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");
5
10
  return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
11
  };
7
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
- 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");
10
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
- };
12
- var _SampleGasPricesController_gasPricesService;
12
+ var _SampleGasPricesController_instances, _SampleGasPricesController_selectedChainId, _SampleGasPricesController_onSelectedNetworkClientIdChange;
13
13
  import { BaseController } from "@metamask/base-controller";
14
14
  // === GENERAL ===
15
15
  /**
@@ -40,6 +40,8 @@ export function getDefaultSampleGasPricesControllerState() {
40
40
  gasPricesByChainId: {},
41
41
  };
42
42
  }
43
+ // === MESSENGER ===
44
+ const MESSENGER_EXPOSED_METHODS = ['updateGasPrices'];
43
45
  // === CONTROLLER DEFINITION ===
44
46
  /**
45
47
  * `SampleGasPricesController` fetches and persists gas prices for various chains.
@@ -48,36 +50,58 @@ export function getDefaultSampleGasPricesControllerState() {
48
50
  *
49
51
  * ``` ts
50
52
  * import { Messenger } from '@metamask/base-controller';
51
- * import {
52
- * SampleGasPricesController,
53
- * SampleGasPricesService
54
- * } from '@metamask/example-controllers';
53
+ * import type {
54
+ * NetworkControllerActions,
55
+ * NetworkControllerEvents
56
+ * } from '@metamask/network-controller';
55
57
  * import type {
56
58
  * SampleGasPricesControllerActions,
57
59
  * SampleGasPricesControllerEvents
58
60
  * } from '@metamask/example-controllers';
59
- * import type { NetworkControllerGetStateAction } from '@metamask/network-controller';
61
+ * import {
62
+ * SampleGasPricesController,
63
+ * SampleGasPricesService,
64
+ * selectGasPrices,
65
+ * } from '@metamask/example-controllers';
60
66
  *
61
- * // Assuming that you're using this in the browser
62
- * const gasPricesService = new SampleGasPricesService({ fetch });
63
- * const rootMessenger = new Messenger<
64
- * SampleGasPricesControllerActions | NetworkControllerGetStateAction,
65
- * SampleGasPricesControllerEvents
67
+ * const globalMessenger = new Messenger<
68
+ * SampleGasPricesServiceActions
69
+ * | SampleGasPricesControllerActions
70
+ * | NetworkControllerActions
71
+ * SampleGasPricesServiceEvents
72
+ * | SampleGasPricesControllerEvents
73
+ * | NetworkControllerEvents
66
74
  * >();
67
- * const gasPricesMessenger = rootMessenger.getRestricted({
68
- * name: 'SampleGasPricesController',
69
- * allowedActions: ['NetworkController:getState'],
75
+ * const gasPricesServiceMessenger = globalMessenger.getRestricted({
76
+ * name: 'SampleGasPricesService',
77
+ * allowedActions: [],
70
78
  * allowedEvents: [],
71
79
  * });
72
- * const gasPricesController = new SampleGasPricesController({
73
- * messenger: gasPricesMessenger,
74
- * gasPricesService,
80
+ * // Instantiate the service to register its actions on the messenger
81
+ * new SampleGasPricesService({
82
+ * messenger: gasPricesServiceMessenger,
83
+ * // We assume you're using this in the browser.
84
+ * fetch,
85
+ * });
86
+ * const gasPricesControllerMessenger = globalMessenger.getRestricted({
87
+ * name: 'SampleGasPricesController',
88
+ * allowedActions: ['NetworkController:getNetworkClientById'],
89
+ * allowedEvents: ['NetworkController:stateChange'],
90
+ * });
91
+ * // Instantiate the controller to register its actions on the messenger
92
+ * new SampleGasPricesController({
93
+ * messenger: gasPricesControllerMessenger,
75
94
  * });
76
95
  *
77
- * // Assuming that `NetworkController:getState` returns an object with a
78
- * // `chainId` of `0x42`...
79
- * await gasPricesController.updateGasPrices();
80
- * gasPricesController.state.gasPricesByChainId
96
+ * // Later...
97
+ * await globalMessenger.call(
98
+ * 'SampleGasPricesController:updateGasPrices',
99
+ * { chainId: '0x42' },
100
+ * );
101
+ * const gasPricesControllerState = await globalMessenger.call(
102
+ * 'SampleGasPricesController:getState',
103
+ * );
104
+ * gasPricesControllerState.gasPricesByChainId
81
105
  * // => { '0x42': { low: 5, average: 10, high: 15, fetchedDate: '2024-01-02T00:00:00.000Z' } }
82
106
  * ```
83
107
  */
@@ -85,14 +109,12 @@ export class SampleGasPricesController extends BaseController {
85
109
  /**
86
110
  * Constructs a new {@link SampleGasPricesController}.
87
111
  *
88
- * @param args - The arguments to the controller.
112
+ * @param args - The constructor arguments.
89
113
  * @param args.messenger - The messenger suited for this controller.
90
114
  * @param args.state - The desired state with which to initialize this
91
115
  * controller. Missing properties will be filled in with defaults.
92
- * @param args.gasPricesService - The service object that will be used to
93
- * obtain gas prices.
94
116
  */
95
- constructor({ messenger, state, gasPricesService, }) {
117
+ constructor({ messenger, state, }) {
96
118
  super({
97
119
  messenger,
98
120
  metadata: gasPricesControllerMetadata,
@@ -102,20 +124,23 @@ export class SampleGasPricesController extends BaseController {
102
124
  ...state,
103
125
  },
104
126
  });
127
+ _SampleGasPricesController_instances.add(this);
105
128
  /**
106
- * The service object that is used to obtain gas prices.
129
+ * The globally selected chain ID.
107
130
  */
108
- _SampleGasPricesController_gasPricesService.set(this, void 0);
109
- __classPrivateFieldSet(this, _SampleGasPricesController_gasPricesService, gasPricesService, "f");
110
- this.messagingSystem.registerActionHandler(`${controllerName}:updateGasPrices`, this.updateGasPrices.bind(this));
131
+ _SampleGasPricesController_selectedChainId.set(this, void 0);
132
+ this.messagingSystem.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
133
+ this.messagingSystem.subscribe('NetworkController:stateChange', __classPrivateFieldGet(this, _SampleGasPricesController_instances, "m", _SampleGasPricesController_onSelectedNetworkClientIdChange).bind(this), (networkControllerState) => networkControllerState.selectedNetworkClientId);
111
134
  }
112
135
  /**
113
- * Fetches the latest gas prices for the current chain, persisting them to
136
+ * Fetches the latest gas prices for the given chain and persists them to
114
137
  * state.
138
+ *
139
+ * @param args - The arguments to the function.
140
+ * @param args.chainId - The chain ID for which to fetch gas prices.
115
141
  */
116
- async updateGasPrices() {
117
- const { chainId } = this.messagingSystem.call('NetworkController:getState');
118
- const gasPricesResponse = await __classPrivateFieldGet(this, _SampleGasPricesController_gasPricesService, "f").fetchGasPrices(chainId);
142
+ async updateGasPrices({ chainId }) {
143
+ const gasPricesResponse = await this.messagingSystem.call('SampleGasPricesService:fetchGasPrices', chainId);
119
144
  this.update((state) => {
120
145
  state.gasPricesByChainId[chainId] = {
121
146
  ...gasPricesResponse,
@@ -124,5 +149,18 @@ export class SampleGasPricesController extends BaseController {
124
149
  });
125
150
  }
126
151
  }
127
- _SampleGasPricesController_gasPricesService = new WeakMap();
152
+ _SampleGasPricesController_selectedChainId = new WeakMap(), _SampleGasPricesController_instances = new WeakSet(), _SampleGasPricesController_onSelectedNetworkClientIdChange =
153
+ /**
154
+ * Callback to call when the globally selected network client ID changes,
155
+ * ensuring that gas prices get updated.
156
+ *
157
+ * @param selectedNetworkClientId - The globally selected network client ID.
158
+ */
159
+ async function _SampleGasPricesController_onSelectedNetworkClientIdChange(selectedNetworkClientId) {
160
+ const { configuration: { chainId }, } = this.messagingSystem.call('NetworkController:getNetworkClientById', selectedNetworkClientId);
161
+ if (chainId !== __classPrivateFieldGet(this, _SampleGasPricesController_selectedChainId, "f")) {
162
+ __classPrivateFieldSet(this, _SampleGasPricesController_selectedChainId, chainId, "f");
163
+ await this.updateGasPrices({ chainId });
164
+ }
165
+ };
128
166
  //# sourceMappingURL=sample-gas-prices-controller.mjs.map