@metamask-previews/sample-controllers 1.0.0-preview-fa84029c → 1.0.0-preview-616c7d82

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
19
19
  - `SampleGasPricesServiceFetchGasPricesAction`
20
20
  - `SampleGasPricesServiceMessenger`
21
21
  - Export `getDefaultPetnamesControllerState` ([#6168](https://github.com/MetaMask/core/pull/6168))
22
+ - Add two new controller state metadata properties: `includeInStateLogs` and `usedInUi` ([#6471](https://github.com/MetaMask/core/pull/6471))
22
23
 
23
24
  ### Changed
24
25
 
@@ -26,8 +26,10 @@ exports.controllerName = 'SampleGasPricesController';
26
26
  */
27
27
  const gasPricesControllerMetadata = {
28
28
  gasPricesByChainId: {
29
+ includeInStateLogs: true,
29
30
  persist: true,
30
31
  anonymous: false,
32
+ usedInUi: true,
31
33
  },
32
34
  };
33
35
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"sample-gas-prices-controller.cjs","sourceRoot":"","sources":["../src/sample-gas-prices-controller.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAMA,+DAA2D;AAW3D,kBAAkB;AAElB;;;;GAIG;AACU,QAAA,cAAc,GAAG,2BAA2B,CAAC;AAsC1D;;GAEG;AACH,MAAM,2BAA2B,GAAG;IAClC,kBAAkB,EAAE;QAClB,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,KAAK;KACjB;CACsD,CAAC;AAE1D;;;;;;;GAOG;AACH,SAAgB,wCAAwC;IACtD,OAAO;QACL,kBAAkB,EAAE,EAAE;KACvB,CAAC;AACJ,CAAC;AAJD,4FAIC;AAED,oBAAoB;AAEpB,MAAM,yBAAyB,GAAG,CAAC,iBAAiB,CAAU,CAAC;AAyD/D,gCAAgC;AAEhC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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;QACrC,uBAAA,IAAI,8CAAoB,OAAO,MAAA,CAAC;QAChC,MAAM,IAAI,CAAC,eAAe,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC;KACzC;AACH,CAAC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n RestrictedMessenger,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type {\n NetworkClientId,\n NetworkControllerGetNetworkClientByIdAction,\n NetworkControllerStateChangeEvent,\n} from '@metamask/network-controller';\nimport type { Hex } from '@metamask/utils';\n\nimport type { SampleGasPricesControllerMethodActions } from './sample-gas-prices-controller-method-action-types';\nimport type { SampleGasPricesServiceFetchGasPricesAction } from './sample-gas-prices-service/sample-gas-prices-service-method-action-types';\n\n// === GENERAL ===\n\n/**\n * The name of the {@link SampleGasPricesController}, used to namespace the\n * controller's actions and events and to namespace the controller's state data\n * when composed with other controllers.\n */\nexport const controllerName = 'SampleGasPricesController';\n\n// === STATE ===\n\n/**\n * The collection of gas price data fetched periodically.\n */\ntype GasPrices = {\n /**\n * The total estimated gas in the \"low\" bucket.\n */\n low: number;\n /**\n * The total estimated gas in the \"average\" bucket.\n */\n average: number;\n /**\n * The total estimated gas in the \"high\" bucket.\n */\n high: number;\n /**\n * The date/time (in ISO-8601 format) when prices were fetched.\n */\n fetchedDate: string;\n};\n\n/**\n * Describes the shape of the state object for {@link SampleGasPricesController}.\n */\nexport type SampleGasPricesControllerState = {\n /**\n * Fetched gas prices categorized by chain ID.\n */\n gasPricesByChainId: {\n [chainId: Hex]: GasPrices;\n };\n};\n\n/**\n * The metadata for each property in {@link SampleGasPricesControllerState}.\n */\nconst gasPricesControllerMetadata = {\n gasPricesByChainId: {\n persist: true,\n anonymous: false,\n },\n} satisfies StateMetadata<SampleGasPricesControllerState>;\n\n/**\n * Constructs the default {@link SampleGasPricesController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link SampleGasPricesController} state.\n */\nexport function getDefaultSampleGasPricesControllerState(): SampleGasPricesControllerState {\n return {\n gasPricesByChainId: {},\n };\n}\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = ['updateGasPrices'] as const;\n\n/**\n * Retrieves the state of the {@link SampleGasPricesController}.\n */\nexport type SampleGasPricesControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n SampleGasPricesControllerState\n>;\n\n/**\n * Actions that {@link SampleGasPricesMessenger} exposes to other consumers.\n */\nexport type SampleGasPricesControllerActions =\n | SampleGasPricesControllerGetStateAction\n | SampleGasPricesControllerMethodActions;\n\n/**\n * Actions from other messengers that {@link SampleGasPricesMessenger} calls.\n */\ntype AllowedActions =\n | NetworkControllerGetNetworkClientByIdAction\n | SampleGasPricesServiceFetchGasPricesAction;\n\n/**\n * Published when the state of {@link SampleGasPricesController} changes.\n */\nexport type SampleGasPricesControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n SampleGasPricesControllerState\n >;\n\n/**\n * Events that {@link SampleGasPricesMessenger} exposes to other consumers.\n */\nexport type SampleGasPricesControllerEvents =\n SampleGasPricesControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link SampleGasPricesMessenger} subscribes\n * to.\n */\ntype AllowedEvents = NetworkControllerStateChangeEvent;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link SampleGasPricesController}.\n */\nexport type SampleGasPricesControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n SampleGasPricesControllerActions | AllowedActions,\n SampleGasPricesControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\n// === CONTROLLER DEFINITION ===\n\n/**\n * `SampleGasPricesController` fetches and persists gas prices for various chains.\n *\n * @example\n *\n * ``` ts\n * import { Messenger } from '@metamask/base-controller';\n * import type {\n * NetworkControllerActions,\n * NetworkControllerEvents,\n * } from '@metamask/network-controller';\n * import type {\n * SampleGasPricesControllerActions,\n * SampleGasPricesControllerEvents,\n * SampleGasPricesServiceActions,\n * SampleGasPricesServiceEvents,\n * } from '@metamask/sample-controllers';\n * import {\n * SampleGasPricesController,\n * SampleGasPricesService,\n * selectGasPrices,\n * } from '@metamask/sample-controllers';\n *\n * const globalMessenger = new Messenger<\n * SampleGasPricesServiceActions\n * | SampleGasPricesControllerActions\n * | NetworkControllerActions,\n * SampleGasPricesServiceEvents\n * | SampleGasPricesControllerEvents\n * | NetworkControllerEvents\n * >();\n * const gasPricesServiceMessenger = globalMessenger.getRestricted({\n * name: 'SampleGasPricesService',\n * allowedActions: [],\n * allowedEvents: [],\n * });\n * // Instantiate the service to register its actions on the messenger\n * new SampleGasPricesService({\n * messenger: gasPricesServiceMessenger,\n * // We assume you're using this in the browser.\n * fetch,\n * });\n * const gasPricesControllerMessenger = globalMessenger.getRestricted({\n * name: 'SampleGasPricesController',\n * allowedActions: ['NetworkController:getNetworkClientById'],\n * allowedEvents: ['NetworkController:stateChange'],\n * });\n * // Instantiate the controller to register its actions on the messenger\n * new SampleGasPricesController({\n * messenger: gasPricesControllerMessenger,\n * });\n *\n * // Later...\n * await globalMessenger.call(\n * 'SampleGasPricesController:updateGasPrices',\n * { chainId: '0x42' },\n * );\n * const gasPricesControllerState = await globalMessenger.call(\n * 'SampleGasPricesController:getState',\n * );\n * gasPricesControllerState.gasPricesByChainId\n * // => { '0x42': { low: 5, average: 10, high: 15, fetchedDate: '2024-01-02T00:00:00.000Z' } }\n * ```\n */\nexport class SampleGasPricesController extends BaseController<\n typeof controllerName,\n SampleGasPricesControllerState,\n SampleGasPricesControllerMessenger\n> {\n /**\n * The globally selected chain ID.\n */\n #selectedChainId: Hex | undefined;\n\n /**\n * Constructs a new {@link SampleGasPricesController}.\n *\n * @param args - The constructor arguments.\n * @param args.messenger - The messenger suited for this controller.\n * @param args.state - The desired state with which to initialize this\n * controller. Missing properties will be filled in with defaults.\n */\n constructor({\n messenger,\n state,\n }: {\n messenger: SampleGasPricesControllerMessenger;\n state?: Partial<SampleGasPricesControllerState>;\n }) {\n super({\n messenger,\n metadata: gasPricesControllerMetadata,\n name: controllerName,\n state: {\n ...getDefaultSampleGasPricesControllerState(),\n ...state,\n },\n });\n\n this.messagingSystem.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n\n this.messagingSystem.subscribe(\n 'NetworkController:stateChange',\n this.#onSelectedNetworkClientIdChange.bind(this),\n (networkControllerState) =>\n networkControllerState.selectedNetworkClientId,\n );\n }\n\n /**\n * Fetches the latest gas prices for the given chain and persists them to\n * state.\n *\n * @param args - The arguments to the function.\n * @param args.chainId - The chain ID for which to fetch gas prices.\n */\n async updateGasPrices({ chainId }: { chainId: Hex }) {\n const gasPricesResponse = await this.messagingSystem.call(\n 'SampleGasPricesService:fetchGasPrices',\n chainId,\n );\n\n this.update((state) => {\n state.gasPricesByChainId[chainId] = {\n ...gasPricesResponse,\n fetchedDate: new Date().toISOString(),\n };\n });\n }\n\n /**\n * Callback to call when the globally selected network client ID changes,\n * ensuring that gas prices get updated.\n *\n * @param selectedNetworkClientId - The globally selected network client ID.\n */\n async #onSelectedNetworkClientIdChange(\n selectedNetworkClientId: NetworkClientId,\n ) {\n const {\n configuration: { chainId },\n } = this.messagingSystem.call(\n 'NetworkController:getNetworkClientById',\n selectedNetworkClientId,\n );\n\n if (chainId !== this.#selectedChainId) {\n this.#selectedChainId = chainId;\n await this.updateGasPrices({ chainId });\n }\n }\n}\n"]}
1
+ {"version":3,"file":"sample-gas-prices-controller.cjs","sourceRoot":"","sources":["../src/sample-gas-prices-controller.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAMA,+DAA2D;AAW3D,kBAAkB;AAElB;;;;GAIG;AACU,QAAA,cAAc,GAAG,2BAA2B,CAAC;AAsC1D;;GAEG;AACH,MAAM,2BAA2B,GAAG;IAClC,kBAAkB,EAAE;QAClB,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,KAAK;QAChB,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EAAE;aACtC,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;CAuBF;AA1FD,8DA0FC;;AArBC;;;;;GAKG;AACH,KAAK,qEACH,uBAAwC;IAExC,MAAM,EACJ,aAAa,EAAE,EAAE,OAAO,EAAE,GAC3B,GAAG,IAAI,CAAC,eAAe,CAAC,IAAI,CAC3B,wCAAwC,EACxC,uBAAuB,CACxB,CAAC;IAEF,IAAI,OAAO,KAAK,uBAAA,IAAI,kDAAiB,EAAE;QACrC,uBAAA,IAAI,8CAAoB,OAAO,MAAA,CAAC;QAChC,MAAM,IAAI,CAAC,eAAe,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC;KACzC;AACH,CAAC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n RestrictedMessenger,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type {\n NetworkClientId,\n NetworkControllerGetNetworkClientByIdAction,\n NetworkControllerStateChangeEvent,\n} from '@metamask/network-controller';\nimport type { Hex } from '@metamask/utils';\n\nimport type { SampleGasPricesControllerMethodActions } from './sample-gas-prices-controller-method-action-types';\nimport type { SampleGasPricesServiceFetchGasPricesAction } from './sample-gas-prices-service/sample-gas-prices-service-method-action-types';\n\n// === GENERAL ===\n\n/**\n * The name of the {@link SampleGasPricesController}, used to namespace the\n * controller's actions and events and to namespace the controller's state data\n * when composed with other controllers.\n */\nexport const controllerName = 'SampleGasPricesController';\n\n// === STATE ===\n\n/**\n * The collection of gas price data fetched periodically.\n */\ntype GasPrices = {\n /**\n * The total estimated gas in the \"low\" bucket.\n */\n low: number;\n /**\n * The total estimated gas in the \"average\" bucket.\n */\n average: number;\n /**\n * The total estimated gas in the \"high\" bucket.\n */\n high: number;\n /**\n * The date/time (in ISO-8601 format) when prices were fetched.\n */\n fetchedDate: string;\n};\n\n/**\n * Describes the shape of the state object for {@link SampleGasPricesController}.\n */\nexport type SampleGasPricesControllerState = {\n /**\n * Fetched gas prices categorized by chain ID.\n */\n gasPricesByChainId: {\n [chainId: Hex]: GasPrices;\n };\n};\n\n/**\n * The metadata for each property in {@link SampleGasPricesControllerState}.\n */\nconst gasPricesControllerMetadata = {\n gasPricesByChainId: {\n includeInStateLogs: true,\n persist: true,\n anonymous: false,\n usedInUi: true,\n },\n} satisfies StateMetadata<SampleGasPricesControllerState>;\n\n/**\n * Constructs the default {@link SampleGasPricesController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link SampleGasPricesController} state.\n */\nexport function getDefaultSampleGasPricesControllerState(): SampleGasPricesControllerState {\n return {\n gasPricesByChainId: {},\n };\n}\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = ['updateGasPrices'] as const;\n\n/**\n * Retrieves the state of the {@link SampleGasPricesController}.\n */\nexport type SampleGasPricesControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n SampleGasPricesControllerState\n>;\n\n/**\n * Actions that {@link SampleGasPricesMessenger} exposes to other consumers.\n */\nexport type SampleGasPricesControllerActions =\n | SampleGasPricesControllerGetStateAction\n | SampleGasPricesControllerMethodActions;\n\n/**\n * Actions from other messengers that {@link SampleGasPricesMessenger} calls.\n */\ntype AllowedActions =\n | NetworkControllerGetNetworkClientByIdAction\n | SampleGasPricesServiceFetchGasPricesAction;\n\n/**\n * Published when the state of {@link SampleGasPricesController} changes.\n */\nexport type SampleGasPricesControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n SampleGasPricesControllerState\n >;\n\n/**\n * Events that {@link SampleGasPricesMessenger} exposes to other consumers.\n */\nexport type SampleGasPricesControllerEvents =\n SampleGasPricesControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link SampleGasPricesMessenger} subscribes\n * to.\n */\ntype AllowedEvents = NetworkControllerStateChangeEvent;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link SampleGasPricesController}.\n */\nexport type SampleGasPricesControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n SampleGasPricesControllerActions | AllowedActions,\n SampleGasPricesControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\n// === CONTROLLER DEFINITION ===\n\n/**\n * `SampleGasPricesController` fetches and persists gas prices for various chains.\n *\n * @example\n *\n * ``` ts\n * import { Messenger } from '@metamask/base-controller';\n * import type {\n * NetworkControllerActions,\n * NetworkControllerEvents,\n * } from '@metamask/network-controller';\n * import type {\n * SampleGasPricesControllerActions,\n * SampleGasPricesControllerEvents,\n * SampleGasPricesServiceActions,\n * SampleGasPricesServiceEvents,\n * } from '@metamask/sample-controllers';\n * import {\n * SampleGasPricesController,\n * SampleGasPricesService,\n * selectGasPrices,\n * } from '@metamask/sample-controllers';\n *\n * const globalMessenger = new Messenger<\n * SampleGasPricesServiceActions\n * | SampleGasPricesControllerActions\n * | NetworkControllerActions,\n * SampleGasPricesServiceEvents\n * | SampleGasPricesControllerEvents\n * | NetworkControllerEvents\n * >();\n * const gasPricesServiceMessenger = globalMessenger.getRestricted({\n * name: 'SampleGasPricesService',\n * allowedActions: [],\n * allowedEvents: [],\n * });\n * // Instantiate the service to register its actions on the messenger\n * new SampleGasPricesService({\n * messenger: gasPricesServiceMessenger,\n * // We assume you're using this in the browser.\n * fetch,\n * });\n * const gasPricesControllerMessenger = globalMessenger.getRestricted({\n * name: 'SampleGasPricesController',\n * allowedActions: ['NetworkController:getNetworkClientById'],\n * allowedEvents: ['NetworkController:stateChange'],\n * });\n * // Instantiate the controller to register its actions on the messenger\n * new SampleGasPricesController({\n * messenger: gasPricesControllerMessenger,\n * });\n *\n * // Later...\n * await globalMessenger.call(\n * 'SampleGasPricesController:updateGasPrices',\n * { chainId: '0x42' },\n * );\n * const gasPricesControllerState = await globalMessenger.call(\n * 'SampleGasPricesController:getState',\n * );\n * gasPricesControllerState.gasPricesByChainId\n * // => { '0x42': { low: 5, average: 10, high: 15, fetchedDate: '2024-01-02T00:00:00.000Z' } }\n * ```\n */\nexport class SampleGasPricesController extends BaseController<\n typeof controllerName,\n SampleGasPricesControllerState,\n SampleGasPricesControllerMessenger\n> {\n /**\n * The globally selected chain ID.\n */\n #selectedChainId: Hex | undefined;\n\n /**\n * Constructs a new {@link SampleGasPricesController}.\n *\n * @param args - The constructor arguments.\n * @param args.messenger - The messenger suited for this controller.\n * @param args.state - The desired state with which to initialize this\n * controller. Missing properties will be filled in with defaults.\n */\n constructor({\n messenger,\n state,\n }: {\n messenger: SampleGasPricesControllerMessenger;\n state?: Partial<SampleGasPricesControllerState>;\n }) {\n super({\n messenger,\n metadata: gasPricesControllerMetadata,\n name: controllerName,\n state: {\n ...getDefaultSampleGasPricesControllerState(),\n ...state,\n },\n });\n\n this.messagingSystem.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n\n this.messagingSystem.subscribe(\n 'NetworkController:stateChange',\n this.#onSelectedNetworkClientIdChange.bind(this),\n (networkControllerState) =>\n networkControllerState.selectedNetworkClientId,\n );\n }\n\n /**\n * Fetches the latest gas prices for the given chain and persists them to\n * state.\n *\n * @param args - The arguments to the function.\n * @param args.chainId - The chain ID for which to fetch gas prices.\n */\n async updateGasPrices({ chainId }: { chainId: Hex }) {\n const gasPricesResponse = await this.messagingSystem.call(\n 'SampleGasPricesService:fetchGasPrices',\n chainId,\n );\n\n this.update((state) => {\n state.gasPricesByChainId[chainId] = {\n ...gasPricesResponse,\n fetchedDate: new Date().toISOString(),\n };\n });\n }\n\n /**\n * Callback to call when the globally selected network client ID changes,\n * ensuring that gas prices get updated.\n *\n * @param selectedNetworkClientId - The globally selected network client ID.\n */\n async #onSelectedNetworkClientIdChange(\n selectedNetworkClientId: NetworkClientId,\n ) {\n const {\n configuration: { chainId },\n } = this.messagingSystem.call(\n 'NetworkController:getNetworkClientById',\n selectedNetworkClientId,\n );\n\n if (chainId !== this.#selectedChainId) {\n this.#selectedChainId = chainId;\n await this.updateGasPrices({ chainId });\n }\n }\n}\n"]}
@@ -1 +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,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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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
+ {"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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C9B,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 +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,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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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
+ {"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,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gCAAgC,GAAG,cAAc,EACjD,+BAA+B,GAAG,aAAa,EAC/C,cAAc,CAAC,MAAM,CAAC,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"}
@@ -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
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"sample-gas-prices-controller.mjs","sourceRoot":"","sources":["../src/sample-gas-prices-controller.ts"],"names":[],"mappings":";;;;;;;;;;;;AAMA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAW3D,kBAAkB;AAElB;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,2BAA2B,CAAC;AAsC1D;;GAEG;AACH,MAAM,2BAA2B,GAAG;IAClC,kBAAkB,EAAE;QAClB,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,KAAK;KACjB;CACsD,CAAC;AAE1D;;;;;;;GAOG;AACH,MAAM,UAAU,wCAAwC;IACtD,OAAO;QACL,kBAAkB,EAAE,EAAE;KACvB,CAAC;AACJ,CAAC;AAED,oBAAoB;AAEpB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AI,CAAC,eAAe,CAAC,IAAI,CAC3B,wCAAwC,EACxC,uBAAuB,CACxB,CAAC;IAEF,IAAI,OAAO,KAAK,uBAAA,IAAI,kDAAiB,EAAE;QACrC,uBAAA,IAAI,8CAAoB,OAAO,MAAA,CAAC;QAChC,MAAM,IAAI,CAAC,eAAe,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC;KACzC;AACH,CAAC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n RestrictedMessenger,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type {\n NetworkClientId,\n NetworkControllerGetNetworkClientByIdAction,\n NetworkControllerStateChangeEvent,\n} from '@metamask/network-controller';\nimport type { Hex } from '@metamask/utils';\n\nimport type { SampleGasPricesControllerMethodActions } from './sample-gas-prices-controller-method-action-types';\nimport type { SampleGasPricesServiceFetchGasPricesAction } from './sample-gas-prices-service/sample-gas-prices-service-method-action-types';\n\n// === GENERAL ===\n\n/**\n * The name of the {@link SampleGasPricesController}, used to namespace the\n * controller's actions and events and to namespace the controller's state data\n * when composed with other controllers.\n */\nexport const controllerName = 'SampleGasPricesController';\n\n// === STATE ===\n\n/**\n * The collection of gas price data fetched periodically.\n */\ntype GasPrices = {\n /**\n * The total estimated gas in the \"low\" bucket.\n */\n low: number;\n /**\n * The total estimated gas in the \"average\" bucket.\n */\n average: number;\n /**\n * The total estimated gas in the \"high\" bucket.\n */\n high: number;\n /**\n * The date/time (in ISO-8601 format) when prices were fetched.\n */\n fetchedDate: string;\n};\n\n/**\n * Describes the shape of the state object for {@link SampleGasPricesController}.\n */\nexport type SampleGasPricesControllerState = {\n /**\n * Fetched gas prices categorized by chain ID.\n */\n gasPricesByChainId: {\n [chainId: Hex]: GasPrices;\n };\n};\n\n/**\n * The metadata for each property in {@link SampleGasPricesControllerState}.\n */\nconst gasPricesControllerMetadata = {\n gasPricesByChainId: {\n persist: true,\n anonymous: false,\n },\n} satisfies StateMetadata<SampleGasPricesControllerState>;\n\n/**\n * Constructs the default {@link SampleGasPricesController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link SampleGasPricesController} state.\n */\nexport function getDefaultSampleGasPricesControllerState(): SampleGasPricesControllerState {\n return {\n gasPricesByChainId: {},\n };\n}\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = ['updateGasPrices'] as const;\n\n/**\n * Retrieves the state of the {@link SampleGasPricesController}.\n */\nexport type SampleGasPricesControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n SampleGasPricesControllerState\n>;\n\n/**\n * Actions that {@link SampleGasPricesMessenger} exposes to other consumers.\n */\nexport type SampleGasPricesControllerActions =\n | SampleGasPricesControllerGetStateAction\n | SampleGasPricesControllerMethodActions;\n\n/**\n * Actions from other messengers that {@link SampleGasPricesMessenger} calls.\n */\ntype AllowedActions =\n | NetworkControllerGetNetworkClientByIdAction\n | SampleGasPricesServiceFetchGasPricesAction;\n\n/**\n * Published when the state of {@link SampleGasPricesController} changes.\n */\nexport type SampleGasPricesControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n SampleGasPricesControllerState\n >;\n\n/**\n * Events that {@link SampleGasPricesMessenger} exposes to other consumers.\n */\nexport type SampleGasPricesControllerEvents =\n SampleGasPricesControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link SampleGasPricesMessenger} subscribes\n * to.\n */\ntype AllowedEvents = NetworkControllerStateChangeEvent;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link SampleGasPricesController}.\n */\nexport type SampleGasPricesControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n SampleGasPricesControllerActions | AllowedActions,\n SampleGasPricesControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\n// === CONTROLLER DEFINITION ===\n\n/**\n * `SampleGasPricesController` fetches and persists gas prices for various chains.\n *\n * @example\n *\n * ``` ts\n * import { Messenger } from '@metamask/base-controller';\n * import type {\n * NetworkControllerActions,\n * NetworkControllerEvents,\n * } from '@metamask/network-controller';\n * import type {\n * SampleGasPricesControllerActions,\n * SampleGasPricesControllerEvents,\n * SampleGasPricesServiceActions,\n * SampleGasPricesServiceEvents,\n * } from '@metamask/sample-controllers';\n * import {\n * SampleGasPricesController,\n * SampleGasPricesService,\n * selectGasPrices,\n * } from '@metamask/sample-controllers';\n *\n * const globalMessenger = new Messenger<\n * SampleGasPricesServiceActions\n * | SampleGasPricesControllerActions\n * | NetworkControllerActions,\n * SampleGasPricesServiceEvents\n * | SampleGasPricesControllerEvents\n * | NetworkControllerEvents\n * >();\n * const gasPricesServiceMessenger = globalMessenger.getRestricted({\n * name: 'SampleGasPricesService',\n * allowedActions: [],\n * allowedEvents: [],\n * });\n * // Instantiate the service to register its actions on the messenger\n * new SampleGasPricesService({\n * messenger: gasPricesServiceMessenger,\n * // We assume you're using this in the browser.\n * fetch,\n * });\n * const gasPricesControllerMessenger = globalMessenger.getRestricted({\n * name: 'SampleGasPricesController',\n * allowedActions: ['NetworkController:getNetworkClientById'],\n * allowedEvents: ['NetworkController:stateChange'],\n * });\n * // Instantiate the controller to register its actions on the messenger\n * new SampleGasPricesController({\n * messenger: gasPricesControllerMessenger,\n * });\n *\n * // Later...\n * await globalMessenger.call(\n * 'SampleGasPricesController:updateGasPrices',\n * { chainId: '0x42' },\n * );\n * const gasPricesControllerState = await globalMessenger.call(\n * 'SampleGasPricesController:getState',\n * );\n * gasPricesControllerState.gasPricesByChainId\n * // => { '0x42': { low: 5, average: 10, high: 15, fetchedDate: '2024-01-02T00:00:00.000Z' } }\n * ```\n */\nexport class SampleGasPricesController extends BaseController<\n typeof controllerName,\n SampleGasPricesControllerState,\n SampleGasPricesControllerMessenger\n> {\n /**\n * The globally selected chain ID.\n */\n #selectedChainId: Hex | undefined;\n\n /**\n * Constructs a new {@link SampleGasPricesController}.\n *\n * @param args - The constructor arguments.\n * @param args.messenger - The messenger suited for this controller.\n * @param args.state - The desired state with which to initialize this\n * controller. Missing properties will be filled in with defaults.\n */\n constructor({\n messenger,\n state,\n }: {\n messenger: SampleGasPricesControllerMessenger;\n state?: Partial<SampleGasPricesControllerState>;\n }) {\n super({\n messenger,\n metadata: gasPricesControllerMetadata,\n name: controllerName,\n state: {\n ...getDefaultSampleGasPricesControllerState(),\n ...state,\n },\n });\n\n this.messagingSystem.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n\n this.messagingSystem.subscribe(\n 'NetworkController:stateChange',\n this.#onSelectedNetworkClientIdChange.bind(this),\n (networkControllerState) =>\n networkControllerState.selectedNetworkClientId,\n );\n }\n\n /**\n * Fetches the latest gas prices for the given chain and persists them to\n * state.\n *\n * @param args - The arguments to the function.\n * @param args.chainId - The chain ID for which to fetch gas prices.\n */\n async updateGasPrices({ chainId }: { chainId: Hex }) {\n const gasPricesResponse = await this.messagingSystem.call(\n 'SampleGasPricesService:fetchGasPrices',\n chainId,\n );\n\n this.update((state) => {\n state.gasPricesByChainId[chainId] = {\n ...gasPricesResponse,\n fetchedDate: new Date().toISOString(),\n };\n });\n }\n\n /**\n * Callback to call when the globally selected network client ID changes,\n * ensuring that gas prices get updated.\n *\n * @param selectedNetworkClientId - The globally selected network client ID.\n */\n async #onSelectedNetworkClientIdChange(\n selectedNetworkClientId: NetworkClientId,\n ) {\n const {\n configuration: { chainId },\n } = this.messagingSystem.call(\n 'NetworkController:getNetworkClientById',\n selectedNetworkClientId,\n );\n\n if (chainId !== this.#selectedChainId) {\n this.#selectedChainId = chainId;\n await this.updateGasPrices({ chainId });\n }\n }\n}\n"]}
1
+ {"version":3,"file":"sample-gas-prices-controller.mjs","sourceRoot":"","sources":["../src/sample-gas-prices-controller.ts"],"names":[],"mappings":";;;;;;;;;;;;AAMA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAW3D,kBAAkB;AAElB;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,2B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C,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,kBAAkB,CAAC,OAAO,CAAC,GAAG;gBAClC,GAAG,iBAAiB;gBACpB,WAAW,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;aACtC,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;CAuBF;;AArBC;;;;;GAKG;AACH,KAAK,qEACH,uBAAwC;IAExC,MAAM,EACJ,aAAa,EAAE,EAAE,OAAO,EAAE,GAC3B,GAAG,IAAI,CAAC,eAAe,CAAC,IAAI,CAC3B,wCAAwC,EACxC,uBAAuB,CACxB,CAAC;IAEF,IAAI,OAAO,KAAK,uBAAA,IAAI,kDAAiB,EAAE;QACrC,uBAAA,IAAI,8CAAoB,OAAO,MAAA,CAAC;QAChC,MAAM,IAAI,CAAC,eAAe,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC;KACzC;AACH,CAAC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n RestrictedMessenger,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type {\n NetworkClientId,\n NetworkControllerGetNetworkClientByIdAction,\n NetworkControllerStateChangeEvent,\n} from '@metamask/network-controller';\nimport type { Hex } from '@metamask/utils';\n\nimport type { SampleGasPricesControllerMethodActions } from './sample-gas-prices-controller-method-action-types';\nimport type { SampleGasPricesServiceFetchGasPricesAction } from './sample-gas-prices-service/sample-gas-prices-service-method-action-types';\n\n// === GENERAL ===\n\n/**\n * The name of the {@link SampleGasPricesController}, used to namespace the\n * controller's actions and events and to namespace the controller's state data\n * when composed with other controllers.\n */\nexport const controllerName = 'SampleGasPricesController';\n\n// === STATE ===\n\n/**\n * The collection of gas price data fetched periodically.\n */\ntype GasPrices = {\n /**\n * The total estimated gas in the \"low\" bucket.\n */\n low: number;\n /**\n * The total estimated gas in the \"average\" bucket.\n */\n average: number;\n /**\n * The total estimated gas in the \"high\" bucket.\n */\n high: number;\n /**\n * The date/time (in ISO-8601 format) when prices were fetched.\n */\n fetchedDate: string;\n};\n\n/**\n * Describes the shape of the state object for {@link SampleGasPricesController}.\n */\nexport type SampleGasPricesControllerState = {\n /**\n * Fetched gas prices categorized by chain ID.\n */\n gasPricesByChainId: {\n [chainId: Hex]: GasPrices;\n };\n};\n\n/**\n * The metadata for each property in {@link SampleGasPricesControllerState}.\n */\nconst gasPricesControllerMetadata = {\n gasPricesByChainId: {\n includeInStateLogs: true,\n persist: true,\n anonymous: false,\n usedInUi: true,\n },\n} satisfies StateMetadata<SampleGasPricesControllerState>;\n\n/**\n * Constructs the default {@link SampleGasPricesController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link SampleGasPricesController} state.\n */\nexport function getDefaultSampleGasPricesControllerState(): SampleGasPricesControllerState {\n return {\n gasPricesByChainId: {},\n };\n}\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = ['updateGasPrices'] as const;\n\n/**\n * Retrieves the state of the {@link SampleGasPricesController}.\n */\nexport type SampleGasPricesControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n SampleGasPricesControllerState\n>;\n\n/**\n * Actions that {@link SampleGasPricesMessenger} exposes to other consumers.\n */\nexport type SampleGasPricesControllerActions =\n | SampleGasPricesControllerGetStateAction\n | SampleGasPricesControllerMethodActions;\n\n/**\n * Actions from other messengers that {@link SampleGasPricesMessenger} calls.\n */\ntype AllowedActions =\n | NetworkControllerGetNetworkClientByIdAction\n | SampleGasPricesServiceFetchGasPricesAction;\n\n/**\n * Published when the state of {@link SampleGasPricesController} changes.\n */\nexport type SampleGasPricesControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n SampleGasPricesControllerState\n >;\n\n/**\n * Events that {@link SampleGasPricesMessenger} exposes to other consumers.\n */\nexport type SampleGasPricesControllerEvents =\n SampleGasPricesControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link SampleGasPricesMessenger} subscribes\n * to.\n */\ntype AllowedEvents = NetworkControllerStateChangeEvent;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link SampleGasPricesController}.\n */\nexport type SampleGasPricesControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n SampleGasPricesControllerActions | AllowedActions,\n SampleGasPricesControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\n// === CONTROLLER DEFINITION ===\n\n/**\n * `SampleGasPricesController` fetches and persists gas prices for various chains.\n *\n * @example\n *\n * ``` ts\n * import { Messenger } from '@metamask/base-controller';\n * import type {\n * NetworkControllerActions,\n * NetworkControllerEvents,\n * } from '@metamask/network-controller';\n * import type {\n * SampleGasPricesControllerActions,\n * SampleGasPricesControllerEvents,\n * SampleGasPricesServiceActions,\n * SampleGasPricesServiceEvents,\n * } from '@metamask/sample-controllers';\n * import {\n * SampleGasPricesController,\n * SampleGasPricesService,\n * selectGasPrices,\n * } from '@metamask/sample-controllers';\n *\n * const globalMessenger = new Messenger<\n * SampleGasPricesServiceActions\n * | SampleGasPricesControllerActions\n * | NetworkControllerActions,\n * SampleGasPricesServiceEvents\n * | SampleGasPricesControllerEvents\n * | NetworkControllerEvents\n * >();\n * const gasPricesServiceMessenger = globalMessenger.getRestricted({\n * name: 'SampleGasPricesService',\n * allowedActions: [],\n * allowedEvents: [],\n * });\n * // Instantiate the service to register its actions on the messenger\n * new SampleGasPricesService({\n * messenger: gasPricesServiceMessenger,\n * // We assume you're using this in the browser.\n * fetch,\n * });\n * const gasPricesControllerMessenger = globalMessenger.getRestricted({\n * name: 'SampleGasPricesController',\n * allowedActions: ['NetworkController:getNetworkClientById'],\n * allowedEvents: ['NetworkController:stateChange'],\n * });\n * // Instantiate the controller to register its actions on the messenger\n * new SampleGasPricesController({\n * messenger: gasPricesControllerMessenger,\n * });\n *\n * // Later...\n * await globalMessenger.call(\n * 'SampleGasPricesController:updateGasPrices',\n * { chainId: '0x42' },\n * );\n * const gasPricesControllerState = await globalMessenger.call(\n * 'SampleGasPricesController:getState',\n * );\n * gasPricesControllerState.gasPricesByChainId\n * // => { '0x42': { low: 5, average: 10, high: 15, fetchedDate: '2024-01-02T00:00:00.000Z' } }\n * ```\n */\nexport class SampleGasPricesController extends BaseController<\n typeof controllerName,\n SampleGasPricesControllerState,\n SampleGasPricesControllerMessenger\n> {\n /**\n * The globally selected chain ID.\n */\n #selectedChainId: Hex | undefined;\n\n /**\n * Constructs a new {@link SampleGasPricesController}.\n *\n * @param args - The constructor arguments.\n * @param args.messenger - The messenger suited for this controller.\n * @param args.state - The desired state with which to initialize this\n * controller. Missing properties will be filled in with defaults.\n */\n constructor({\n messenger,\n state,\n }: {\n messenger: SampleGasPricesControllerMessenger;\n state?: Partial<SampleGasPricesControllerState>;\n }) {\n super({\n messenger,\n metadata: gasPricesControllerMetadata,\n name: controllerName,\n state: {\n ...getDefaultSampleGasPricesControllerState(),\n ...state,\n },\n });\n\n this.messagingSystem.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n\n this.messagingSystem.subscribe(\n 'NetworkController:stateChange',\n this.#onSelectedNetworkClientIdChange.bind(this),\n (networkControllerState) =>\n networkControllerState.selectedNetworkClientId,\n );\n }\n\n /**\n * Fetches the latest gas prices for the given chain and persists them to\n * state.\n *\n * @param args - The arguments to the function.\n * @param args.chainId - The chain ID for which to fetch gas prices.\n */\n async updateGasPrices({ chainId }: { chainId: Hex }) {\n const gasPricesResponse = await this.messagingSystem.call(\n 'SampleGasPricesService:fetchGasPrices',\n chainId,\n );\n\n this.update((state) => {\n state.gasPricesByChainId[chainId] = {\n ...gasPricesResponse,\n fetchedDate: new Date().toISOString(),\n };\n });\n }\n\n /**\n * Callback to call when the globally selected network client ID changes,\n * ensuring that gas prices get updated.\n *\n * @param selectedNetworkClientId - The globally selected network client ID.\n */\n async #onSelectedNetworkClientIdChange(\n selectedNetworkClientId: NetworkClientId,\n ) {\n const {\n configuration: { chainId },\n } = this.messagingSystem.call(\n 'NetworkController:getNetworkClientById',\n selectedNetworkClientId,\n );\n\n if (chainId !== this.#selectedChainId) {\n this.#selectedChainId = chainId;\n await this.updateGasPrices({ chainId });\n }\n }\n}\n"]}
@@ -15,8 +15,10 @@ exports.controllerName = 'SamplePetnamesController';
15
15
  */
16
16
  const samplePetnamesControllerMetadata = {
17
17
  namesByChainIdAndAddress: {
18
+ includeInStateLogs: true,
18
19
  persist: true,
19
20
  anonymous: false,
21
+ usedInUi: true,
20
22
  },
21
23
  };
22
24
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"sample-petnames-controller.cjs","sourceRoot":"","sources":["../src/sample-petnames-controller.ts"],"names":[],"mappings":";;;AAMA,+DAA2D;AAC3D,iEAA8D;AAK9D,kBAAkB;AAElB;;;;GAIG;AACU,QAAA,cAAc,GAAG,0BAA0B,CAAC;AAmBzD;;GAEG;AACH,MAAM,gCAAgC,GAAG;IACvC,wBAAwB,EAAE;QACxB,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,KAAK;KACjB;CACqD,CAAC;AAEzD;;;;;;;GAOG;AACH,SAAgB,iCAAiC;IAC/C,OAAO;QACL,wBAAwB,EAAE,EAAE;KAC7B,CAAC;AACJ,CAAC;AAJD,8EAIC;AAED,oBAAoB;AAEpB,MAAM,yBAAyB,GAAG,CAAC,eAAe,CAAU,CAAC;AAuD7D,gCAAgC;AAEhC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AACH,MAAa,wBAAyB,SAAQ,gCAI7C;IACC;;;;;;;OAOG;IACH,YAAY,EACV,SAAS,EACT,KAAK,GAIN;QACC,KAAK,CAAC;YACJ,SAAS;YACT,QAAQ,EAAE,gCAAgC;YAC1C,IAAI,EAAE,sBAAc;YACpB,KAAK,EAAE;gBACL,GAAG,iCAAiC,EAAE;gBACtC,GAAG,KAAK;aACT;SACF,CAAC,CAAC;QAEH,IAAI,CAAC,eAAe,CAAC,4BAA4B,CAC/C,IAAI,EACJ,yBAAyB,CAC1B,CAAC;IACJ,CAAC;IAED;;;;;;;OAOG;IACH,aAAa,CAAC,OAAY,EAAE,OAAY,EAAE,IAAY;QACpD,IAAI,CAAC,IAAA,mCAAgB,EAAC,OAAO,CAAC,EAAE;YAC9B,MAAM,IAAI,KAAK,CAAC,kBAAkB,CAAC,CAAC;SACrC;QAED,MAAM,iBAAiB,GAAG,OAAO,CAAC,WAAW,EAAS,CAAC;QAEvD,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;;YACpB,MAAA,KAAK,CAAC,wBAAwB,EAAC,OAAO,SAAP,OAAO,IAAM,EAAE,EAAC;YAC/C,KAAK,CAAC,wBAAwB,CAAC,OAAO,CAAC,CAAC,iBAAiB,CAAC,GAAG,IAAI,CAAC;QACpE,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAxDD,4DAwDC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n RestrictedMessenger,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport { isSafeDynamicKey } from '@metamask/controller-utils';\nimport type { Hex } from '@metamask/utils';\n\nimport type { SamplePetnamesControllerMethodActions } from './sample-petnames-controller-method-action-types';\n\n// === GENERAL ===\n\n/**\n * The name of the {@link SamplePetnamesController}, used to namespace the\n * controller's actions and events and to namespace the controller's state data\n * when composed with other controllers.\n */\nexport const controllerName = 'SamplePetnamesController';\n\n// === STATE ===\n\n/**\n * Describes the shape of the state object for {@link SamplePetnamesController}.\n */\nexport type SamplePetnamesControllerState = {\n /**\n * The registry of pet names, categorized by chain ID first and address\n * second.\n */\n namesByChainIdAndAddress: {\n [chainId: Hex]: {\n [address: Hex]: string;\n };\n };\n};\n\n/**\n * The metadata for each property in {@link SamplePetnamesControllerState}.\n */\nconst samplePetnamesControllerMetadata = {\n namesByChainIdAndAddress: {\n persist: true,\n anonymous: false,\n },\n} satisfies StateMetadata<SamplePetnamesControllerState>;\n\n/**\n * Constructs the default {@link SamplePetnamesController} 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 SamplePetnamesController} state.\n */\nexport function getDefaultPetnamesControllerState(): SamplePetnamesControllerState {\n return {\n namesByChainIdAndAddress: {},\n };\n}\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = ['assignPetname'] as const;\n\n/**\n * Retrieves the state of the {@link SamplePetnamesController}.\n */\nexport type SamplePetnamesControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n SamplePetnamesControllerState\n>;\n\n/**\n * Actions that {@link SampleGasPricesMessenger} exposes to other consumers.\n */\nexport type SamplePetnamesControllerActions =\n | SamplePetnamesControllerGetStateAction\n | SamplePetnamesControllerMethodActions;\n\n/**\n * Actions from other messengers that {@link SampleGasPricesMessenger} calls.\n */\ntype AllowedActions = never;\n\n/**\n * Published when the state of {@link SamplePetnamesController} changes.\n */\nexport type SamplePetnamesControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n SamplePetnamesControllerState\n >;\n\n/**\n * Events that {@link SampleGasPricesMessenger} exposes to other consumers.\n */\nexport type SamplePetnamesControllerEvents =\n SamplePetnamesControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link SampleGasPricesMessenger} subscribes\n * to.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link SamplePetnamesController}.\n */\nexport type SamplePetnamesControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n SamplePetnamesControllerActions | AllowedActions,\n SamplePetnamesControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\n// === CONTROLLER DEFINITION ===\n\n/**\n * `SamplePetnamesController` records user-provided nicknames for various\n * addresses on various chains.\n *\n * @example\n *\n * ``` ts\n * import { Messenger } from '@metamask/base-controller';\n * import type {\n * SamplePetnamesControllerActions,\n * SamplePetnamesControllerEvents,\n * } from '@metamask/sample-controllers';\n *\n * const globalMessenger = new Messenger<\n * SamplePetnamesControllerActions,\n * SamplePetnamesControllerEvents\n * >();\n * const samplePetnamesMessenger = globalMessenger.getRestricted({\n * name: 'SamplePetnamesController',\n * allowedActions: [],\n * allowedEvents: [],\n * });\n * // Instantiate the controller to register its actions on the messenger\n * new SamplePetnamesController({\n * messenger: samplePetnamesMessenger,\n * });\n *\n * globalMessenger.call(\n * 'SamplePetnamesController:assignPetname',\n * [\n * '0x1',\n * '0xF57F855e17483B1f09bFec62783C9d3b6c8b3A99',\n * 'Primary Account',\n * ],\n * );\n * const samplePetnamesControllerState = await globalMessenger.call(\n * 'SamplePetnamesController:getState',\n * );\n * samplePetnamesControllerState.namesByChainIdAndAddress\n * // => { '0x1': { '0xF57F855e17483B1f09bFec62783C9d3b6c8b3A99': 'Primary Account' } }\n * ```\n */\nexport class SamplePetnamesController extends BaseController<\n typeof controllerName,\n SamplePetnamesControllerState,\n SamplePetnamesControllerMessenger\n> {\n /**\n * Constructs a new {@link SamplePetnamesController}.\n *\n * @param args - The arguments to this controller.\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: SamplePetnamesControllerMessenger;\n state?: Partial<SamplePetnamesControllerState>;\n }) {\n super({\n messenger,\n metadata: samplePetnamesControllerMetadata,\n name: controllerName,\n state: {\n ...getDefaultPetnamesControllerState(),\n ...state,\n },\n });\n\n this.messagingSystem.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n }\n\n /**\n * Registers the given name with the given address (relative to the given\n * chain).\n *\n * @param chainId - The chain ID that the address belongs to.\n * @param address - The account address to name.\n * @param name - The name to assign to the address.\n */\n assignPetname(chainId: Hex, address: Hex, name: string) {\n if (!isSafeDynamicKey(chainId)) {\n throw new Error('Invalid chain ID');\n }\n\n const normalizedAddress = address.toLowerCase() as Hex;\n\n this.update((state) => {\n state.namesByChainIdAndAddress[chainId] ??= {};\n state.namesByChainIdAndAddress[chainId][normalizedAddress] = name;\n });\n }\n}\n"]}
1
+ {"version":3,"file":"sample-petnames-controller.cjs","sourceRoot":"","sources":["../src/sample-petnames-controller.ts"],"names":[],"mappings":";;;AAMA,+DAA2D;AAC3D,iEAA8D;AAK9D,kBAAkB;AAElB;;;;GAIG;AACU,QAAA,cAAc,GAAG,0BAA0B,CAAC;AAmBzD;;GAEG;AACH,MAAM,gCAAgC,GAAG;IACvC,wBAAwB,EAAE;QACxB,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,KAAK;QAChB,QAAQ,EAAE,IAAI;KACf;CACqD,CAAC;AAEzD;;;;;;;GAOG;AACH,SAAgB,iCAAiC;IAC/C,OAAO;QACL,wBAAwB,EAAE,EAAE;KAC7B,CAAC;AACJ,CAAC;AAJD,8EAIC;AAED,oBAAoB;AAEpB,MAAM,yBAAyB,GAAG,CAAC,eAAe,CAAU,CAAC;AAuD7D,gCAAgC;AAEhC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AACH,MAAa,wBAAyB,SAAQ,gCAI7C;IACC;;;;;;;OAOG;IACH,YAAY,EACV,SAAS,EACT,KAAK,GAIN;QACC,KAAK,CAAC;YACJ,SAAS;YACT,QAAQ,EAAE,gCAAgC;YAC1C,IAAI,EAAE,sBAAc;YACpB,KAAK,EAAE;gBACL,GAAG,iCAAiC,EAAE;gBACtC,GAAG,KAAK;aACT;SACF,CAAC,CAAC;QAEH,IAAI,CAAC,eAAe,CAAC,4BAA4B,CAC/C,IAAI,EACJ,yBAAyB,CAC1B,CAAC;IACJ,CAAC;IAED;;;;;;;OAOG;IACH,aAAa,CAAC,OAAY,EAAE,OAAY,EAAE,IAAY;QACpD,IAAI,CAAC,IAAA,mCAAgB,EAAC,OAAO,CAAC,EAAE;YAC9B,MAAM,IAAI,KAAK,CAAC,kBAAkB,CAAC,CAAC;SACrC;QAED,MAAM,iBAAiB,GAAG,OAAO,CAAC,WAAW,EAAS,CAAC;QAEvD,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;;YACpB,MAAA,KAAK,CAAC,wBAAwB,EAAC,OAAO,SAAP,OAAO,IAAM,EAAE,EAAC;YAC/C,KAAK,CAAC,wBAAwB,CAAC,OAAO,CAAC,CAAC,iBAAiB,CAAC,GAAG,IAAI,CAAC;QACpE,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAxDD,4DAwDC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n RestrictedMessenger,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport { isSafeDynamicKey } from '@metamask/controller-utils';\nimport type { Hex } from '@metamask/utils';\n\nimport type { SamplePetnamesControllerMethodActions } from './sample-petnames-controller-method-action-types';\n\n// === GENERAL ===\n\n/**\n * The name of the {@link SamplePetnamesController}, used to namespace the\n * controller's actions and events and to namespace the controller's state data\n * when composed with other controllers.\n */\nexport const controllerName = 'SamplePetnamesController';\n\n// === STATE ===\n\n/**\n * Describes the shape of the state object for {@link SamplePetnamesController}.\n */\nexport type SamplePetnamesControllerState = {\n /**\n * The registry of pet names, categorized by chain ID first and address\n * second.\n */\n namesByChainIdAndAddress: {\n [chainId: Hex]: {\n [address: Hex]: string;\n };\n };\n};\n\n/**\n * The metadata for each property in {@link SamplePetnamesControllerState}.\n */\nconst samplePetnamesControllerMetadata = {\n namesByChainIdAndAddress: {\n includeInStateLogs: true,\n persist: true,\n anonymous: false,\n usedInUi: true,\n },\n} satisfies StateMetadata<SamplePetnamesControllerState>;\n\n/**\n * Constructs the default {@link SamplePetnamesController} 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 SamplePetnamesController} state.\n */\nexport function getDefaultPetnamesControllerState(): SamplePetnamesControllerState {\n return {\n namesByChainIdAndAddress: {},\n };\n}\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = ['assignPetname'] as const;\n\n/**\n * Retrieves the state of the {@link SamplePetnamesController}.\n */\nexport type SamplePetnamesControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n SamplePetnamesControllerState\n>;\n\n/**\n * Actions that {@link SampleGasPricesMessenger} exposes to other consumers.\n */\nexport type SamplePetnamesControllerActions =\n | SamplePetnamesControllerGetStateAction\n | SamplePetnamesControllerMethodActions;\n\n/**\n * Actions from other messengers that {@link SampleGasPricesMessenger} calls.\n */\ntype AllowedActions = never;\n\n/**\n * Published when the state of {@link SamplePetnamesController} changes.\n */\nexport type SamplePetnamesControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n SamplePetnamesControllerState\n >;\n\n/**\n * Events that {@link SampleGasPricesMessenger} exposes to other consumers.\n */\nexport type SamplePetnamesControllerEvents =\n SamplePetnamesControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link SampleGasPricesMessenger} subscribes\n * to.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link SamplePetnamesController}.\n */\nexport type SamplePetnamesControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n SamplePetnamesControllerActions | AllowedActions,\n SamplePetnamesControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\n// === CONTROLLER DEFINITION ===\n\n/**\n * `SamplePetnamesController` records user-provided nicknames for various\n * addresses on various chains.\n *\n * @example\n *\n * ``` ts\n * import { Messenger } from '@metamask/base-controller';\n * import type {\n * SamplePetnamesControllerActions,\n * SamplePetnamesControllerEvents,\n * } from '@metamask/sample-controllers';\n *\n * const globalMessenger = new Messenger<\n * SamplePetnamesControllerActions,\n * SamplePetnamesControllerEvents\n * >();\n * const samplePetnamesMessenger = globalMessenger.getRestricted({\n * name: 'SamplePetnamesController',\n * allowedActions: [],\n * allowedEvents: [],\n * });\n * // Instantiate the controller to register its actions on the messenger\n * new SamplePetnamesController({\n * messenger: samplePetnamesMessenger,\n * });\n *\n * globalMessenger.call(\n * 'SamplePetnamesController:assignPetname',\n * [\n * '0x1',\n * '0xF57F855e17483B1f09bFec62783C9d3b6c8b3A99',\n * 'Primary Account',\n * ],\n * );\n * const samplePetnamesControllerState = await globalMessenger.call(\n * 'SamplePetnamesController:getState',\n * );\n * samplePetnamesControllerState.namesByChainIdAndAddress\n * // => { '0x1': { '0xF57F855e17483B1f09bFec62783C9d3b6c8b3A99': 'Primary Account' } }\n * ```\n */\nexport class SamplePetnamesController extends BaseController<\n typeof controllerName,\n SamplePetnamesControllerState,\n SamplePetnamesControllerMessenger\n> {\n /**\n * Constructs a new {@link SamplePetnamesController}.\n *\n * @param args - The arguments to this controller.\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: SamplePetnamesControllerMessenger;\n state?: Partial<SamplePetnamesControllerState>;\n }) {\n super({\n messenger,\n metadata: samplePetnamesControllerMetadata,\n name: controllerName,\n state: {\n ...getDefaultPetnamesControllerState(),\n ...state,\n },\n });\n\n this.messagingSystem.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n }\n\n /**\n * Registers the given name with the given address (relative to the given\n * chain).\n *\n * @param chainId - The chain ID that the address belongs to.\n * @param address - The account address to name.\n * @param name - The name to assign to the address.\n */\n assignPetname(chainId: Hex, address: Hex, name: string) {\n if (!isSafeDynamicKey(chainId)) {\n throw new Error('Invalid chain ID');\n }\n\n const normalizedAddress = address.toLowerCase() as Hex;\n\n this.update((state) => {\n state.namesByChainIdAndAddress[chainId] ??= {};\n state.namesByChainIdAndAddress[chainId][normalizedAddress] = name;\n });\n }\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"sample-petnames-controller.d.cts","sourceRoot":"","sources":["../src/sample-petnames-controller.ts"],"names":[],"mappings":"AAAA,OAAO,KAA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Ac,CAC1D,OAAO,cAAc,EACrB,6BAA6B,EAC7B,iCAAiC,CAClC;IACC;;;;;;;OAOG;gBACS,EACV,SAAS,EACT,KAAK,GACN,EAAE;QACD,SAAS,EAAE,iCAAiC,CAAC;QAC7C,KAAK,CAAC,EAAE,OAAO,CAAC,6BAA6B,CAAC,CAAC;KAChD;IAiBD;;;;;;;OAOG;IACH,aAAa,CAAC,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM;CAYvD"}
1
+ {"version":3,"file":"sample-petnames-controller.d.cts","sourceRoot":"","sources":["../src/sample-petnames-controller.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAC1B,mBAAmB,EAEpB,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAE3D,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAE3C,OAAO,KAAK,EAAE,qCAAqC,EAAE,6DAAyD;AAI9G;;;;GAIG;AACH,eAAO,MAAM,cAAc,6BAA6B,CAAC;AAIzD;;GAEG;AACH,MAAM,MAAM,6BAA6B,GAAG;IAC1C;;;OAGG;IACH,wBAAwB,EAAE;QACxB,CAAC,OAAO,EAAE,GAAG,GAAG;YACd,CAAC,OAAO,EAAE,GAAG,GAAG,MAAM,CAAC;SACxB,CAAC;KACH,CAAC;CACH,CAAC;AAcF;;;;;;;GAOG;AACH,wBAAgB,iCAAiC,IAAI,6BAA6B,CAIjF;AAMD;;GAEG;AACH,MAAM,MAAM,sCAAsC,GAAG,wBAAwB,CAC3E,OAAO,cAAc,EACrB,6BAA6B,CAC9B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,+BAA+B,GACvC,sCAAsC,GACtC,qCAAqC,CAAC;AAE1C;;GAEG;AACH,KAAK,cAAc,GAAG,KAAK,CAAC;AAE5B;;GAEG;AACH,MAAM,MAAM,wCAAwC,GAClD,0BAA0B,CACxB,OAAO,cAAc,EACrB,6BAA6B,CAC9B,CAAC;AAEJ;;GAEG;AACH,MAAM,MAAM,8BAA8B,GACxC,wCAAwC,CAAC;AAE3C;;;GAGG;AACH,KAAK,aAAa,GAAG,KAAK,CAAC;AAE3B;;;GAGG;AACH,MAAM,MAAM,iCAAiC,GAAG,mBAAmB,CACjE,OAAO,cAAc,EACrB,+BAA+B,GAAG,cAAc,EAChD,8BAA8B,GAAG,aAAa,EAC9C,cAAc,CAAC,MAAM,CAAC,EACtB,aAAa,CAAC,MAAM,CAAC,CACtB,CAAC;AAIF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AACH,qBAAa,wBAAyB,SAAQ,cAAc,CAC1D,OAAO,cAAc,EACrB,6BAA6B,EAC7B,iCAAiC,CAClC;IACC;;;;;;;OAOG;gBACS,EACV,SAAS,EACT,KAAK,GACN,EAAE;QACD,SAAS,EAAE,iCAAiC,CAAC;QAC7C,KAAK,CAAC,EAAE,OAAO,CAAC,6BAA6B,CAAC,CAAC;KAChD;IAiBD;;;;;;;OAOG;IACH,aAAa,CAAC,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM;CAYvD"}
@@ -1 +1 @@
1
- {"version":3,"file":"sample-petnames-controller.d.mts","sourceRoot":"","sources":["../src/sample-petnames-controller.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAC1B,mBAAmB,EAEpB,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAE3D,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAE3C,OAAO,KAAK,EAAE,qCAAqC,EAAE,6DAAyD;AAI9G;;;;GAIG;AACH,eAAO,MAAM,cAAc,6BAA6B,CAAC;AAIzD;;GAEG;AACH,MAAM,MAAM,6BAA6B,GAAG;IAC1C;;;OAGG;IACH,wBAAwB,EAAE;QACxB,CAAC,OAAO,EAAE,GAAG,GAAG;YACd,CA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;;;;;;;;;;;;GAyCG;AACH,qBAAa,wBAAyB,SAAQ,cAAc,CAC1D,OAAO,cAAc,EACrB,6BAA6B,EAC7B,iCAAiC,CAClC;IACC;;;;;;;OAOG;gBACS,EACV,SAAS,EACT,KAAK,GACN,EAAE;QACD,SAAS,EAAE,iCAAiC,CAAC;QAC7C,KAAK,CAAC,EAAE,OAAO,CAAC,6BAA6B,CAAC,CAAC;KAChD;IAiBD;;;;;;;OAOG;IACH,aAAa,CAAC,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM;CAYvD"}
@@ -12,8 +12,10 @@ export const controllerName = 'SamplePetnamesController';
12
12
  */
13
13
  const samplePetnamesControllerMetadata = {
14
14
  namesByChainIdAndAddress: {
15
+ includeInStateLogs: true,
15
16
  persist: true,
16
17
  anonymous: false,
18
+ usedInUi: true,
17
19
  },
18
20
  };
19
21
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"sample-petnames-controller.mjs","sourceRoot":"","sources":["../src/sample-petnames-controller.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,EAAE,gBAAgB,EAAE,mCAAmC;AAK9D,kBAAkB;AAElB;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,0BAA0B,CAAC;AAmBzD;;GAEG;AACH,MAAM,gCAAgC,GAAG;IACvC,wBAAwB,EAAE;QACxB,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,KAAK;KACjB;CACqD,CAAC;AAEzD;;;;;;;GAOG;AACH,MAAM,UAAU,iCAAiC;IAC/C,OAAO;QACL,wBAAwB,EAAE,EAAE;KAC7B,CAAC;AACJ,CAAC;AAED,oBAAoB;AAEpB,MAAM,yBAAyB,GAAG,CAAC,eAAe,CAAU,CAAC;AAuD7D,gCAAgC;AAEhC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AACH,MAAM,OAAO,wBAAyB,SAAQ,cAI7C;IACC;;;;;;;OAOG;IACH,YAAY,EACV,SAAS,EACT,KAAK,GAIN;QACC,KAAK,CAAC;YACJ,SAAS;YACT,QAAQ,EAAE,gCAAgC;YAC1C,IAAI,EAAE,cAAc;YACpB,KAAK,EAAE;gBACL,GAAG,iCAAiC,EAAE;gBACtC,GAAG,KAAK;aACT;SACF,CAAC,CAAC;QAEH,IAAI,CAAC,eAAe,CAAC,4BAA4B,CAC/C,IAAI,EACJ,yBAAyB,CAC1B,CAAC;IACJ,CAAC;IAED;;;;;;;OAOG;IACH,aAAa,CAAC,OAAY,EAAE,OAAY,EAAE,IAAY;QACpD,IAAI,CAAC,gBAAgB,CAAC,OAAO,CAAC,EAAE;YAC9B,MAAM,IAAI,KAAK,CAAC,kBAAkB,CAAC,CAAC;SACrC;QAED,MAAM,iBAAiB,GAAG,OAAO,CAAC,WAAW,EAAS,CAAC;QAEvD,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;;YACpB,MAAA,KAAK,CAAC,wBAAwB,EAAC,OAAO,SAAP,OAAO,IAAM,EAAE,EAAC;YAC/C,KAAK,CAAC,wBAAwB,CAAC,OAAO,CAAC,CAAC,iBAAiB,CAAC,GAAG,IAAI,CAAC;QACpE,CAAC,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n RestrictedMessenger,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport { isSafeDynamicKey } from '@metamask/controller-utils';\nimport type { Hex } from '@metamask/utils';\n\nimport type { SamplePetnamesControllerMethodActions } from './sample-petnames-controller-method-action-types';\n\n// === GENERAL ===\n\n/**\n * The name of the {@link SamplePetnamesController}, used to namespace the\n * controller's actions and events and to namespace the controller's state data\n * when composed with other controllers.\n */\nexport const controllerName = 'SamplePetnamesController';\n\n// === STATE ===\n\n/**\n * Describes the shape of the state object for {@link SamplePetnamesController}.\n */\nexport type SamplePetnamesControllerState = {\n /**\n * The registry of pet names, categorized by chain ID first and address\n * second.\n */\n namesByChainIdAndAddress: {\n [chainId: Hex]: {\n [address: Hex]: string;\n };\n };\n};\n\n/**\n * The metadata for each property in {@link SamplePetnamesControllerState}.\n */\nconst samplePetnamesControllerMetadata = {\n namesByChainIdAndAddress: {\n persist: true,\n anonymous: false,\n },\n} satisfies StateMetadata<SamplePetnamesControllerState>;\n\n/**\n * Constructs the default {@link SamplePetnamesController} 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 SamplePetnamesController} state.\n */\nexport function getDefaultPetnamesControllerState(): SamplePetnamesControllerState {\n return {\n namesByChainIdAndAddress: {},\n };\n}\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = ['assignPetname'] as const;\n\n/**\n * Retrieves the state of the {@link SamplePetnamesController}.\n */\nexport type SamplePetnamesControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n SamplePetnamesControllerState\n>;\n\n/**\n * Actions that {@link SampleGasPricesMessenger} exposes to other consumers.\n */\nexport type SamplePetnamesControllerActions =\n | SamplePetnamesControllerGetStateAction\n | SamplePetnamesControllerMethodActions;\n\n/**\n * Actions from other messengers that {@link SampleGasPricesMessenger} calls.\n */\ntype AllowedActions = never;\n\n/**\n * Published when the state of {@link SamplePetnamesController} changes.\n */\nexport type SamplePetnamesControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n SamplePetnamesControllerState\n >;\n\n/**\n * Events that {@link SampleGasPricesMessenger} exposes to other consumers.\n */\nexport type SamplePetnamesControllerEvents =\n SamplePetnamesControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link SampleGasPricesMessenger} subscribes\n * to.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link SamplePetnamesController}.\n */\nexport type SamplePetnamesControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n SamplePetnamesControllerActions | AllowedActions,\n SamplePetnamesControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\n// === CONTROLLER DEFINITION ===\n\n/**\n * `SamplePetnamesController` records user-provided nicknames for various\n * addresses on various chains.\n *\n * @example\n *\n * ``` ts\n * import { Messenger } from '@metamask/base-controller';\n * import type {\n * SamplePetnamesControllerActions,\n * SamplePetnamesControllerEvents,\n * } from '@metamask/sample-controllers';\n *\n * const globalMessenger = new Messenger<\n * SamplePetnamesControllerActions,\n * SamplePetnamesControllerEvents\n * >();\n * const samplePetnamesMessenger = globalMessenger.getRestricted({\n * name: 'SamplePetnamesController',\n * allowedActions: [],\n * allowedEvents: [],\n * });\n * // Instantiate the controller to register its actions on the messenger\n * new SamplePetnamesController({\n * messenger: samplePetnamesMessenger,\n * });\n *\n * globalMessenger.call(\n * 'SamplePetnamesController:assignPetname',\n * [\n * '0x1',\n * '0xF57F855e17483B1f09bFec62783C9d3b6c8b3A99',\n * 'Primary Account',\n * ],\n * );\n * const samplePetnamesControllerState = await globalMessenger.call(\n * 'SamplePetnamesController:getState',\n * );\n * samplePetnamesControllerState.namesByChainIdAndAddress\n * // => { '0x1': { '0xF57F855e17483B1f09bFec62783C9d3b6c8b3A99': 'Primary Account' } }\n * ```\n */\nexport class SamplePetnamesController extends BaseController<\n typeof controllerName,\n SamplePetnamesControllerState,\n SamplePetnamesControllerMessenger\n> {\n /**\n * Constructs a new {@link SamplePetnamesController}.\n *\n * @param args - The arguments to this controller.\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: SamplePetnamesControllerMessenger;\n state?: Partial<SamplePetnamesControllerState>;\n }) {\n super({\n messenger,\n metadata: samplePetnamesControllerMetadata,\n name: controllerName,\n state: {\n ...getDefaultPetnamesControllerState(),\n ...state,\n },\n });\n\n this.messagingSystem.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n }\n\n /**\n * Registers the given name with the given address (relative to the given\n * chain).\n *\n * @param chainId - The chain ID that the address belongs to.\n * @param address - The account address to name.\n * @param name - The name to assign to the address.\n */\n assignPetname(chainId: Hex, address: Hex, name: string) {\n if (!isSafeDynamicKey(chainId)) {\n throw new Error('Invalid chain ID');\n }\n\n const normalizedAddress = address.toLowerCase() as Hex;\n\n this.update((state) => {\n state.namesByChainIdAndAddress[chainId] ??= {};\n state.namesByChainIdAndAddress[chainId][normalizedAddress] = name;\n });\n }\n}\n"]}
1
+ {"version":3,"file":"sample-petnames-controller.mjs","sourceRoot":"","sources":["../src/sample-petnames-controller.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,EAAE,gBAAgB,EAAE,mCAAmC;AAK9D,kBAAkB;AAElB;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,0BAA0B,CAAC;AAmBzD;;GAEG;AACH,MAAM,gCAAgC,GAAG;IACvC,wBAAwB,EAAE;QACxB,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,KAAK;QAChB,QAAQ,EAAE,IAAI;KACf;CACqD,CAAC;AAEzD;;;;;;;GAOG;AACH,MAAM,UAAU,iCAAiC;IAC/C,OAAO;QACL,wBAAwB,EAAE,EAAE;KAC7B,CAAC;AACJ,CAAC;AAED,oBAAoB;AAEpB,MAAM,yBAAyB,GAAG,CAAC,eAAe,CAAU,CAAC;AAuD7D,gCAAgC;AAEhC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AACH,MAAM,OAAO,wBAAyB,SAAQ,cAI7C;IACC;;;;;;;OAOG;IACH,YAAY,EACV,SAAS,EACT,KAAK,GAIN;QACC,KAAK,CAAC;YACJ,SAAS;YACT,QAAQ,EAAE,gCAAgC;YAC1C,IAAI,EAAE,cAAc;YACpB,KAAK,EAAE;gBACL,GAAG,iCAAiC,EAAE;gBACtC,GAAG,KAAK;aACT;SACF,CAAC,CAAC;QAEH,IAAI,CAAC,eAAe,CAAC,4BAA4B,CAC/C,IAAI,EACJ,yBAAyB,CAC1B,CAAC;IACJ,CAAC;IAED;;;;;;;OAOG;IACH,aAAa,CAAC,OAAY,EAAE,OAAY,EAAE,IAAY;QACpD,IAAI,CAAC,gBAAgB,CAAC,OAAO,CAAC,EAAE;YAC9B,MAAM,IAAI,KAAK,CAAC,kBAAkB,CAAC,CAAC;SACrC;QAED,MAAM,iBAAiB,GAAG,OAAO,CAAC,WAAW,EAAS,CAAC;QAEvD,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;;YACpB,MAAA,KAAK,CAAC,wBAAwB,EAAC,OAAO,SAAP,OAAO,IAAM,EAAE,EAAC;YAC/C,KAAK,CAAC,wBAAwB,CAAC,OAAO,CAAC,CAAC,iBAAiB,CAAC,GAAG,IAAI,CAAC;QACpE,CAAC,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n RestrictedMessenger,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport { isSafeDynamicKey } from '@metamask/controller-utils';\nimport type { Hex } from '@metamask/utils';\n\nimport type { SamplePetnamesControllerMethodActions } from './sample-petnames-controller-method-action-types';\n\n// === GENERAL ===\n\n/**\n * The name of the {@link SamplePetnamesController}, used to namespace the\n * controller's actions and events and to namespace the controller's state data\n * when composed with other controllers.\n */\nexport const controllerName = 'SamplePetnamesController';\n\n// === STATE ===\n\n/**\n * Describes the shape of the state object for {@link SamplePetnamesController}.\n */\nexport type SamplePetnamesControllerState = {\n /**\n * The registry of pet names, categorized by chain ID first and address\n * second.\n */\n namesByChainIdAndAddress: {\n [chainId: Hex]: {\n [address: Hex]: string;\n };\n };\n};\n\n/**\n * The metadata for each property in {@link SamplePetnamesControllerState}.\n */\nconst samplePetnamesControllerMetadata = {\n namesByChainIdAndAddress: {\n includeInStateLogs: true,\n persist: true,\n anonymous: false,\n usedInUi: true,\n },\n} satisfies StateMetadata<SamplePetnamesControllerState>;\n\n/**\n * Constructs the default {@link SamplePetnamesController} 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 SamplePetnamesController} state.\n */\nexport function getDefaultPetnamesControllerState(): SamplePetnamesControllerState {\n return {\n namesByChainIdAndAddress: {},\n };\n}\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = ['assignPetname'] as const;\n\n/**\n * Retrieves the state of the {@link SamplePetnamesController}.\n */\nexport type SamplePetnamesControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n SamplePetnamesControllerState\n>;\n\n/**\n * Actions that {@link SampleGasPricesMessenger} exposes to other consumers.\n */\nexport type SamplePetnamesControllerActions =\n | SamplePetnamesControllerGetStateAction\n | SamplePetnamesControllerMethodActions;\n\n/**\n * Actions from other messengers that {@link SampleGasPricesMessenger} calls.\n */\ntype AllowedActions = never;\n\n/**\n * Published when the state of {@link SamplePetnamesController} changes.\n */\nexport type SamplePetnamesControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n SamplePetnamesControllerState\n >;\n\n/**\n * Events that {@link SampleGasPricesMessenger} exposes to other consumers.\n */\nexport type SamplePetnamesControllerEvents =\n SamplePetnamesControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link SampleGasPricesMessenger} subscribes\n * to.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link SamplePetnamesController}.\n */\nexport type SamplePetnamesControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n SamplePetnamesControllerActions | AllowedActions,\n SamplePetnamesControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\n// === CONTROLLER DEFINITION ===\n\n/**\n * `SamplePetnamesController` records user-provided nicknames for various\n * addresses on various chains.\n *\n * @example\n *\n * ``` ts\n * import { Messenger } from '@metamask/base-controller';\n * import type {\n * SamplePetnamesControllerActions,\n * SamplePetnamesControllerEvents,\n * } from '@metamask/sample-controllers';\n *\n * const globalMessenger = new Messenger<\n * SamplePetnamesControllerActions,\n * SamplePetnamesControllerEvents\n * >();\n * const samplePetnamesMessenger = globalMessenger.getRestricted({\n * name: 'SamplePetnamesController',\n * allowedActions: [],\n * allowedEvents: [],\n * });\n * // Instantiate the controller to register its actions on the messenger\n * new SamplePetnamesController({\n * messenger: samplePetnamesMessenger,\n * });\n *\n * globalMessenger.call(\n * 'SamplePetnamesController:assignPetname',\n * [\n * '0x1',\n * '0xF57F855e17483B1f09bFec62783C9d3b6c8b3A99',\n * 'Primary Account',\n * ],\n * );\n * const samplePetnamesControllerState = await globalMessenger.call(\n * 'SamplePetnamesController:getState',\n * );\n * samplePetnamesControllerState.namesByChainIdAndAddress\n * // => { '0x1': { '0xF57F855e17483B1f09bFec62783C9d3b6c8b3A99': 'Primary Account' } }\n * ```\n */\nexport class SamplePetnamesController extends BaseController<\n typeof controllerName,\n SamplePetnamesControllerState,\n SamplePetnamesControllerMessenger\n> {\n /**\n * Constructs a new {@link SamplePetnamesController}.\n *\n * @param args - The arguments to this controller.\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: SamplePetnamesControllerMessenger;\n state?: Partial<SamplePetnamesControllerState>;\n }) {\n super({\n messenger,\n metadata: samplePetnamesControllerMetadata,\n name: controllerName,\n state: {\n ...getDefaultPetnamesControllerState(),\n ...state,\n },\n });\n\n this.messagingSystem.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n }\n\n /**\n * Registers the given name with the given address (relative to the given\n * chain).\n *\n * @param chainId - The chain ID that the address belongs to.\n * @param address - The account address to name.\n * @param name - The name to assign to the address.\n */\n assignPetname(chainId: Hex, address: Hex, name: string) {\n if (!isSafeDynamicKey(chainId)) {\n throw new Error('Invalid chain ID');\n }\n\n const normalizedAddress = address.toLowerCase() as Hex;\n\n this.update((state) => {\n state.namesByChainIdAndAddress[chainId] ??= {};\n state.namesByChainIdAndAddress[chainId][normalizedAddress] = name;\n });\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask-previews/sample-controllers",
3
- "version": "1.0.0-preview-fa84029c",
3
+ "version": "1.0.0-preview-616c7d82",
4
4
  "description": "Sample package to illustrate best practices for controllers",
5
5
  "keywords": [
6
6
  "MetaMask",