@metamask-previews/sample-controllers 1.0.0-preview-f3f2e8c8 → 1.0.0-preview-1f079244

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 +25 -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 +83 -41
  19. package/dist/sample-gas-prices-controller.cjs.map +1 -1
  20. package/dist/sample-gas-prices-controller.d.cts +78 -61
  21. package/dist/sample-gas-prices-controller.d.cts.map +1 -1
  22. package/dist/sample-gas-prices-controller.d.mts +78 -61
  23. package/dist/sample-gas-prices-controller.d.mts.map +1 -1
  24. package/dist/sample-gas-prices-controller.mjs +83 -41
  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 +136 -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 +123 -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 +123 -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 +135 -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 +27 -14
  51. package/dist/sample-petnames-controller.cjs.map +1 -1
  52. package/dist/sample-petnames-controller.d.cts +39 -24
  53. package/dist/sample-petnames-controller.d.cts.map +1 -1
  54. package/dist/sample-petnames-controller.d.mts +39 -24
  55. package/dist/sample-petnames-controller.d.mts.map +1 -1
  56. package/dist/sample-petnames-controller.mjs +26 -14
  57. package/dist/sample-petnames-controller.mjs.map +1 -1
  58. package/package.json +4 -3
  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,60 @@ 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
- * SampleGasPricesControllerEvents
107
- * } from '@metamask/example-controllers';
108
- * import type { NetworkControllerGetStateAction } from '@metamask/network-controller';
97
+ * SampleGasPricesControllerEvents,
98
+ * SampleGasPricesServiceActions,
99
+ * SampleGasPricesServiceEvents,
100
+ * } from '@metamask/sample-controllers';
101
+ * import {
102
+ * SampleGasPricesController,
103
+ * SampleGasPricesService,
104
+ * selectGasPrices,
105
+ * } from '@metamask/sample-controllers';
109
106
  *
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
107
+ * const globalMessenger = new Messenger<
108
+ * SampleGasPricesServiceActions
109
+ * | SampleGasPricesControllerActions
110
+ * | NetworkControllerActions,
111
+ * SampleGasPricesServiceEvents
112
+ * | SampleGasPricesControllerEvents
113
+ * | NetworkControllerEvents
115
114
  * >();
116
- * const gasPricesMessenger = rootMessenger.getRestricted({
117
- * name: 'SampleGasPricesController',
118
- * allowedActions: ['NetworkController:getState'],
115
+ * const gasPricesServiceMessenger = globalMessenger.getRestricted({
116
+ * name: 'SampleGasPricesService',
117
+ * allowedActions: [],
119
118
  * allowedEvents: [],
120
119
  * });
121
- * const gasPricesController = new SampleGasPricesController({
122
- * messenger: gasPricesMessenger,
123
- * gasPricesService,
120
+ * // Instantiate the service to register its actions on the messenger
121
+ * new SampleGasPricesService({
122
+ * messenger: gasPricesServiceMessenger,
123
+ * // We assume you're using this in the browser.
124
+ * fetch,
125
+ * });
126
+ * const gasPricesControllerMessenger = globalMessenger.getRestricted({
127
+ * name: 'SampleGasPricesController',
128
+ * allowedActions: ['NetworkController:getNetworkClientById'],
129
+ * allowedEvents: ['NetworkController:stateChange'],
130
+ * });
131
+ * // Instantiate the controller to register its actions on the messenger
132
+ * new SampleGasPricesController({
133
+ * messenger: gasPricesControllerMessenger,
124
134
  * });
125
135
  *
126
- * // Assuming that `NetworkController:getState` returns an object with a
127
- * // `chainId` of `0x42`...
128
- * await gasPricesController.updateGasPrices();
129
- * gasPricesController.state.gasPricesByChainId
136
+ * // Later...
137
+ * await globalMessenger.call(
138
+ * 'SampleGasPricesController:updateGasPrices',
139
+ * { chainId: '0x42' },
140
+ * );
141
+ * const gasPricesControllerState = await globalMessenger.call(
142
+ * 'SampleGasPricesController:getState',
143
+ * );
144
+ * gasPricesControllerState.gasPricesByChainId
130
145
  * // => { '0x42': { low: 5, average: 10, high: 15, fetchedDate: '2024-01-02T00:00:00.000Z' } }
131
146
  * ```
132
147
  */
@@ -135,23 +150,25 @@ export declare class SampleGasPricesController extends BaseController<typeof con
135
150
  /**
136
151
  * Constructs a new {@link SampleGasPricesController}.
137
152
  *
138
- * @param args - The arguments to the controller.
153
+ * @param args - The constructor arguments.
139
154
  * @param args.messenger - The messenger suited for this controller.
140
155
  * @param args.state - The desired state with which to initialize this
141
156
  * 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
157
  */
145
- constructor({ messenger, state, gasPricesService, }: {
158
+ constructor({ messenger, state, }: {
146
159
  messenger: SampleGasPricesControllerMessenger;
147
160
  state?: Partial<SampleGasPricesControllerState>;
148
- gasPricesService: SampleAbstractGasPricesService;
149
161
  });
150
162
  /**
151
- * Fetches the latest gas prices for the current chain, persisting them to
163
+ * Fetches the latest gas prices for the given chain and persists them to
152
164
  * state.
165
+ *
166
+ * @param args - The arguments to the function.
167
+ * @param args.chainId - The chain ID for which to fetch gas prices.
153
168
  */
154
- updateGasPrices(): Promise<void>;
169
+ updateGasPrices({ chainId }: {
170
+ chainId: Hex;
171
+ }): Promise<void>;
155
172
  }
156
173
  export {};
157
174
  //# 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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,EACtB,aAAa,CAAC,MAAM,CAAC,CACtB,CAAC;AAIF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+DG;AACH,qBAAa,yBAA0B,SAAQ,cAAc,CAC3D,OAAO,cAAc,EACrB,8BAA8B,EAC9B,kCAAkC,CACnC;;IAMC;;;;;;;OAOG;gBACS,EACV,SAAS,EACT,KAAK,GACN,EAAE;QACD,SAAS,EAAE,kCAAkC,CAAC;QAC9C,KAAK,CAAC,EAAE,OAAO,CAAC,8BAA8B,CAAC,CAAC;KACjD;IAwBD;;;;;;OAMG;IACG,eAAe,CAAC,EAAE,OAAO,EAAE,EAAE;QAAE,OAAO,EAAE,GAAG,CAAA;KAAE;CAmCpD"}
@@ -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,60 @@ 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
- * SampleGasPricesControllerEvents
107
- * } from '@metamask/example-controllers';
108
- * import type { NetworkControllerGetStateAction } from '@metamask/network-controller';
97
+ * SampleGasPricesControllerEvents,
98
+ * SampleGasPricesServiceActions,
99
+ * SampleGasPricesServiceEvents,
100
+ * } from '@metamask/sample-controllers';
101
+ * import {
102
+ * SampleGasPricesController,
103
+ * SampleGasPricesService,
104
+ * selectGasPrices,
105
+ * } from '@metamask/sample-controllers';
109
106
  *
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
107
+ * const globalMessenger = new Messenger<
108
+ * SampleGasPricesServiceActions
109
+ * | SampleGasPricesControllerActions
110
+ * | NetworkControllerActions,
111
+ * SampleGasPricesServiceEvents
112
+ * | SampleGasPricesControllerEvents
113
+ * | NetworkControllerEvents
115
114
  * >();
116
- * const gasPricesMessenger = rootMessenger.getRestricted({
117
- * name: 'SampleGasPricesController',
118
- * allowedActions: ['NetworkController:getState'],
115
+ * const gasPricesServiceMessenger = globalMessenger.getRestricted({
116
+ * name: 'SampleGasPricesService',
117
+ * allowedActions: [],
119
118
  * allowedEvents: [],
120
119
  * });
121
- * const gasPricesController = new SampleGasPricesController({
122
- * messenger: gasPricesMessenger,
123
- * gasPricesService,
120
+ * // Instantiate the service to register its actions on the messenger
121
+ * new SampleGasPricesService({
122
+ * messenger: gasPricesServiceMessenger,
123
+ * // We assume you're using this in the browser.
124
+ * fetch,
125
+ * });
126
+ * const gasPricesControllerMessenger = globalMessenger.getRestricted({
127
+ * name: 'SampleGasPricesController',
128
+ * allowedActions: ['NetworkController:getNetworkClientById'],
129
+ * allowedEvents: ['NetworkController:stateChange'],
130
+ * });
131
+ * // Instantiate the controller to register its actions on the messenger
132
+ * new SampleGasPricesController({
133
+ * messenger: gasPricesControllerMessenger,
124
134
  * });
125
135
  *
126
- * // Assuming that `NetworkController:getState` returns an object with a
127
- * // `chainId` of `0x42`...
128
- * await gasPricesController.updateGasPrices();
129
- * gasPricesController.state.gasPricesByChainId
136
+ * // Later...
137
+ * await globalMessenger.call(
138
+ * 'SampleGasPricesController:updateGasPrices',
139
+ * { chainId: '0x42' },
140
+ * );
141
+ * const gasPricesControllerState = await globalMessenger.call(
142
+ * 'SampleGasPricesController:getState',
143
+ * );
144
+ * gasPricesControllerState.gasPricesByChainId
130
145
  * // => { '0x42': { low: 5, average: 10, high: 15, fetchedDate: '2024-01-02T00:00:00.000Z' } }
131
146
  * ```
132
147
  */
@@ -135,23 +150,25 @@ export declare class SampleGasPricesController extends BaseController<typeof con
135
150
  /**
136
151
  * Constructs a new {@link SampleGasPricesController}.
137
152
  *
138
- * @param args - The arguments to the controller.
153
+ * @param args - The constructor arguments.
139
154
  * @param args.messenger - The messenger suited for this controller.
140
155
  * @param args.state - The desired state with which to initialize this
141
156
  * 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
157
  */
145
- constructor({ messenger, state, gasPricesService, }: {
158
+ constructor({ messenger, state, }: {
146
159
  messenger: SampleGasPricesControllerMessenger;
147
160
  state?: Partial<SampleGasPricesControllerState>;
148
- gasPricesService: SampleAbstractGasPricesService;
149
161
  });
150
162
  /**
151
- * Fetches the latest gas prices for the current chain, persisting them to
163
+ * Fetches the latest gas prices for the given chain and persists them to
152
164
  * state.
165
+ *
166
+ * @param args - The arguments to the function.
167
+ * @param args.chainId - The chain ID for which to fetch gas prices.
153
168
  */
154
- updateGasPrices(): Promise<void>;
169
+ updateGasPrices({ chainId }: {
170
+ chainId: Hex;
171
+ }): Promise<void>;
155
172
  }
156
173
  export {};
157
174
  //# 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,E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;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,G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FieldGet = (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
  /**
@@ -23,8 +23,10 @@ export const controllerName = 'SampleGasPricesController';
23
23
  */
24
24
  const gasPricesControllerMetadata = {
25
25
  gasPricesByChainId: {
26
+ includeInStateLogs: true,
26
27
  persist: true,
27
28
  anonymous: false,
29
+ usedInUi: true,
28
30
  },
29
31
  };
30
32
  /**
@@ -40,6 +42,8 @@ export function getDefaultSampleGasPricesControllerState() {
40
42
  gasPricesByChainId: {},
41
43
  };
42
44
  }
45
+ // === MESSENGER ===
46
+ const MESSENGER_EXPOSED_METHODS = ['updateGasPrices'];
43
47
  // === CONTROLLER DEFINITION ===
44
48
  /**
45
49
  * `SampleGasPricesController` fetches and persists gas prices for various chains.
@@ -48,36 +52,60 @@ export function getDefaultSampleGasPricesControllerState() {
48
52
  *
49
53
  * ``` ts
50
54
  * import { Messenger } from '@metamask/base-controller';
51
- * import {
52
- * SampleGasPricesController,
53
- * SampleGasPricesService
54
- * } from '@metamask/example-controllers';
55
+ * import type {
56
+ * NetworkControllerActions,
57
+ * NetworkControllerEvents,
58
+ * } from '@metamask/network-controller';
55
59
  * import type {
56
60
  * SampleGasPricesControllerActions,
57
- * SampleGasPricesControllerEvents
58
- * } from '@metamask/example-controllers';
59
- * import type { NetworkControllerGetStateAction } from '@metamask/network-controller';
61
+ * SampleGasPricesControllerEvents,
62
+ * SampleGasPricesServiceActions,
63
+ * SampleGasPricesServiceEvents,
64
+ * } from '@metamask/sample-controllers';
65
+ * import {
66
+ * SampleGasPricesController,
67
+ * SampleGasPricesService,
68
+ * selectGasPrices,
69
+ * } from '@metamask/sample-controllers';
60
70
  *
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
71
+ * const globalMessenger = new Messenger<
72
+ * SampleGasPricesServiceActions
73
+ * | SampleGasPricesControllerActions
74
+ * | NetworkControllerActions,
75
+ * SampleGasPricesServiceEvents
76
+ * | SampleGasPricesControllerEvents
77
+ * | NetworkControllerEvents
66
78
  * >();
67
- * const gasPricesMessenger = rootMessenger.getRestricted({
68
- * name: 'SampleGasPricesController',
69
- * allowedActions: ['NetworkController:getState'],
79
+ * const gasPricesServiceMessenger = globalMessenger.getRestricted({
80
+ * name: 'SampleGasPricesService',
81
+ * allowedActions: [],
70
82
  * allowedEvents: [],
71
83
  * });
72
- * const gasPricesController = new SampleGasPricesController({
73
- * messenger: gasPricesMessenger,
74
- * gasPricesService,
84
+ * // Instantiate the service to register its actions on the messenger
85
+ * new SampleGasPricesService({
86
+ * messenger: gasPricesServiceMessenger,
87
+ * // We assume you're using this in the browser.
88
+ * fetch,
89
+ * });
90
+ * const gasPricesControllerMessenger = globalMessenger.getRestricted({
91
+ * name: 'SampleGasPricesController',
92
+ * allowedActions: ['NetworkController:getNetworkClientById'],
93
+ * allowedEvents: ['NetworkController:stateChange'],
94
+ * });
95
+ * // Instantiate the controller to register its actions on the messenger
96
+ * new SampleGasPricesController({
97
+ * messenger: gasPricesControllerMessenger,
75
98
  * });
76
99
  *
77
- * // Assuming that `NetworkController:getState` returns an object with a
78
- * // `chainId` of `0x42`...
79
- * await gasPricesController.updateGasPrices();
80
- * gasPricesController.state.gasPricesByChainId
100
+ * // Later...
101
+ * await globalMessenger.call(
102
+ * 'SampleGasPricesController:updateGasPrices',
103
+ * { chainId: '0x42' },
104
+ * );
105
+ * const gasPricesControllerState = await globalMessenger.call(
106
+ * 'SampleGasPricesController:getState',
107
+ * );
108
+ * gasPricesControllerState.gasPricesByChainId
81
109
  * // => { '0x42': { low: 5, average: 10, high: 15, fetchedDate: '2024-01-02T00:00:00.000Z' } }
82
110
  * ```
83
111
  */
@@ -85,14 +113,12 @@ export class SampleGasPricesController extends BaseController {
85
113
  /**
86
114
  * Constructs a new {@link SampleGasPricesController}.
87
115
  *
88
- * @param args - The arguments to the controller.
116
+ * @param args - The constructor arguments.
89
117
  * @param args.messenger - The messenger suited for this controller.
90
118
  * @param args.state - The desired state with which to initialize this
91
119
  * 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
120
  */
95
- constructor({ messenger, state, gasPricesService, }) {
121
+ constructor({ messenger, state, }) {
96
122
  super({
97
123
  messenger,
98
124
  metadata: gasPricesControllerMetadata,
@@ -102,20 +128,23 @@ export class SampleGasPricesController extends BaseController {
102
128
  ...state,
103
129
  },
104
130
  });
131
+ _SampleGasPricesController_instances.add(this);
105
132
  /**
106
- * The service object that is used to obtain gas prices.
133
+ * The globally selected chain ID.
107
134
  */
108
- _SampleGasPricesController_gasPricesService.set(this, void 0);
109
- __classPrivateFieldSet(this, _SampleGasPricesController_gasPricesService, gasPricesService, "f");
110
- this.messagingSystem.registerActionHandler(`${controllerName}:updateGasPrices`, this.updateGasPrices.bind(this));
135
+ _SampleGasPricesController_selectedChainId.set(this, void 0);
136
+ this.messagingSystem.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
137
+ this.messagingSystem.subscribe('NetworkController:stateChange', __classPrivateFieldGet(this, _SampleGasPricesController_instances, "m", _SampleGasPricesController_onSelectedNetworkClientIdChange).bind(this), (networkControllerState) => networkControllerState.selectedNetworkClientId);
111
138
  }
112
139
  /**
113
- * Fetches the latest gas prices for the current chain, persisting them to
140
+ * Fetches the latest gas prices for the given chain and persists them to
114
141
  * state.
142
+ *
143
+ * @param args - The arguments to the function.
144
+ * @param args.chainId - The chain ID for which to fetch gas prices.
115
145
  */
116
- async updateGasPrices() {
117
- const { chainId } = this.messagingSystem.call('NetworkController:getState');
118
- const gasPricesResponse = await __classPrivateFieldGet(this, _SampleGasPricesController_gasPricesService, "f").fetchGasPrices(chainId);
146
+ async updateGasPrices({ chainId }) {
147
+ const gasPricesResponse = await this.messagingSystem.call('SampleGasPricesService:fetchGasPrices', chainId);
119
148
  this.update((state) => {
120
149
  state.gasPricesByChainId[chainId] = {
121
150
  ...gasPricesResponse,
@@ -124,5 +153,18 @@ export class SampleGasPricesController extends BaseController {
124
153
  });
125
154
  }
126
155
  }
127
- _SampleGasPricesController_gasPricesService = new WeakMap();
156
+ _SampleGasPricesController_selectedChainId = new WeakMap(), _SampleGasPricesController_instances = new WeakSet(), _SampleGasPricesController_onSelectedNetworkClientIdChange =
157
+ /**
158
+ * Callback to call when the globally selected network client ID changes,
159
+ * ensuring that gas prices get updated.
160
+ *
161
+ * @param selectedNetworkClientId - The globally selected network client ID.
162
+ */
163
+ async function _SampleGasPricesController_onSelectedNetworkClientIdChange(selectedNetworkClientId) {
164
+ const { configuration: { chainId }, } = this.messagingSystem.call('NetworkController:getNetworkClientById', selectedNetworkClientId);
165
+ if (chainId !== __classPrivateFieldGet(this, _SampleGasPricesController_selectedChainId, "f")) {
166
+ __classPrivateFieldSet(this, _SampleGasPricesController_selectedChainId, chainId, "f");
167
+ await this.updateGasPrices({ chainId });
168
+ }
169
+ };
128
170
  //# sourceMappingURL=sample-gas-prices-controller.mjs.map