@metamask-previews/sample-controllers 0.0.0-preview-37b05a4

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 (53) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/LICENSE +20 -0
  3. package/README.md +15 -0
  4. package/dist/tsconfig.build.tsbuildinfo +1 -0
  5. package/dist/types/gas-prices-controller.cjs +133 -0
  6. package/dist/types/gas-prices-controller.cjs.map +1 -0
  7. package/dist/types/gas-prices-controller.d.cts +157 -0
  8. package/dist/types/gas-prices-controller.d.cts.map +1 -0
  9. package/dist/types/gas-prices-controller.d.mts +157 -0
  10. package/dist/types/gas-prices-controller.d.mts.map +1 -0
  11. package/dist/types/gas-prices-controller.mjs +128 -0
  12. package/dist/types/gas-prices-controller.mjs.map +1 -0
  13. package/dist/types/gas-prices-service/abstract-gas-prices-service.cjs +3 -0
  14. package/dist/types/gas-prices-service/abstract-gas-prices-service.cjs.map +1 -0
  15. package/dist/types/gas-prices-service/abstract-gas-prices-service.d.cts +7 -0
  16. package/dist/types/gas-prices-service/abstract-gas-prices-service.d.cts.map +1 -0
  17. package/dist/types/gas-prices-service/abstract-gas-prices-service.d.mts +7 -0
  18. package/dist/types/gas-prices-service/abstract-gas-prices-service.d.mts.map +1 -0
  19. package/dist/types/gas-prices-service/abstract-gas-prices-service.mjs +2 -0
  20. package/dist/types/gas-prices-service/abstract-gas-prices-service.mjs.map +1 -0
  21. package/dist/types/gas-prices-service/gas-prices-service.cjs +72 -0
  22. package/dist/types/gas-prices-service/gas-prices-service.cjs.map +1 -0
  23. package/dist/types/gas-prices-service/gas-prices-service.d.cts +55 -0
  24. package/dist/types/gas-prices-service/gas-prices-service.d.cts.map +1 -0
  25. package/dist/types/gas-prices-service/gas-prices-service.d.mts +55 -0
  26. package/dist/types/gas-prices-service/gas-prices-service.d.mts.map +1 -0
  27. package/dist/types/gas-prices-service/gas-prices-service.mjs +68 -0
  28. package/dist/types/gas-prices-service/gas-prices-service.mjs.map +1 -0
  29. package/dist/types/index.cjs +12 -0
  30. package/dist/types/index.cjs.map +1 -0
  31. package/dist/types/index.d.cts +6 -0
  32. package/dist/types/index.d.cts.map +1 -0
  33. package/dist/types/index.d.mts +6 -0
  34. package/dist/types/index.d.mts.map +1 -0
  35. package/dist/types/index.mjs +4 -0
  36. package/dist/types/index.mjs.map +1 -0
  37. package/dist/types/network-controller-types.cjs +20 -0
  38. package/dist/types/network-controller-types.cjs.map +1 -0
  39. package/dist/types/network-controller-types.d.cts +34 -0
  40. package/dist/types/network-controller-types.d.cts.map +1 -0
  41. package/dist/types/network-controller-types.d.mts +34 -0
  42. package/dist/types/network-controller-types.d.mts.map +1 -0
  43. package/dist/types/network-controller-types.mjs +16 -0
  44. package/dist/types/network-controller-types.mjs.map +1 -0
  45. package/dist/types/pet-names-controller.cjs +113 -0
  46. package/dist/types/pet-names-controller.cjs.map +1 -0
  47. package/dist/types/pet-names-controller.d.cts +124 -0
  48. package/dist/types/pet-names-controller.d.cts.map +1 -0
  49. package/dist/types/pet-names-controller.d.mts +124 -0
  50. package/dist/types/pet-names-controller.d.mts.map +1 -0
  51. package/dist/types/pet-names-controller.mjs +108 -0
  52. package/dist/types/pet-names-controller.mjs.map +1 -0
  53. package/package.json +72 -0
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PetNamesController = exports.getDefaultPetNamesControllerState = exports.controllerName = void 0;
4
+ const base_controller_1 = require("@metamask/base-controller");
5
+ const controller_utils_1 = require("@metamask/controller-utils");
6
+ // === GENERAL ===
7
+ /**
8
+ * The name of the {@link PetNamesController}, used to namespace the
9
+ * controller's actions and events and to namespace the controller's state data
10
+ * when composed with other controllers.
11
+ */
12
+ exports.controllerName = 'PetNamesController';
13
+ /**
14
+ * The metadata for each property in {@link PetNamesControllerState}.
15
+ */
16
+ const petNamesControllerMetadata = {
17
+ namesByChainIdAndAddress: {
18
+ persist: true,
19
+ anonymous: false,
20
+ },
21
+ };
22
+ /**
23
+ * Constructs the default {@link PetNamesController} state. This allows
24
+ * consumers to provide a partial state object when initializing the controller
25
+ * and also helps in constructing complete state objects for this controller in
26
+ * tests.
27
+ *
28
+ * @returns The default {@link PetNamesController} state.
29
+ */
30
+ function getDefaultPetNamesControllerState() {
31
+ return {
32
+ namesByChainIdAndAddress: {},
33
+ };
34
+ }
35
+ exports.getDefaultPetNamesControllerState = getDefaultPetNamesControllerState;
36
+ // === CONTROLLER DEFINITION ===
37
+ /**
38
+ * `PetNamesController` records user-provided nicknames for various addresses on
39
+ * various chains.
40
+ *
41
+ * @example
42
+ *
43
+ * ``` ts
44
+ * import { Messenger } from '@metamask/base-controller';
45
+ * import type {
46
+ * PetNamesControllerActions,
47
+ * PetNamesControllerEvents
48
+ * } from '@metamask/example-controllers';
49
+ *
50
+ * const rootMessenger = new Messenger<
51
+ * PetNamesControllerActions,
52
+ * PetNamesControllerEvents
53
+ * >();
54
+ * const petNamesMessenger = rootMessenger.getRestricted({
55
+ * name: 'PetNamesController',
56
+ * allowedActions: [],
57
+ * allowedEvents: [],
58
+ * });
59
+ * const petNamesController = new GasPricesController({
60
+ * messenger: petNamesMessenger,
61
+ * });
62
+ *
63
+ * petNamesController.assignPetName(
64
+ * '0x1',
65
+ * '0xF57F855e17483B1f09bFec62783C9d3b6c8b3A99',
66
+ * 'Primary Account'
67
+ * );
68
+ * petNamesController.state.namesByChainIdAndAddress
69
+ * // => { '0x1': { '0xF57F855e17483B1f09bFec62783C9d3b6c8b3A99': 'Primary Account' } }
70
+ * ```
71
+ */
72
+ class PetNamesController extends base_controller_1.BaseController {
73
+ /**
74
+ * Constructs a new {@link PetNamesController}.
75
+ *
76
+ * @param args - The arguments to the controller.
77
+ * @param args.messenger - The messenger suited for this controller.
78
+ * @param args.state - The desired state with which to initialize this
79
+ * controller. Missing properties will be filled in with defaults.
80
+ */
81
+ constructor({ messenger, state, }) {
82
+ super({
83
+ messenger,
84
+ metadata: petNamesControllerMetadata,
85
+ name: exports.controllerName,
86
+ state: {
87
+ ...getDefaultPetNamesControllerState(),
88
+ ...state,
89
+ },
90
+ });
91
+ }
92
+ /**
93
+ * Registers the given name with the given address (relative to the given
94
+ * chain).
95
+ *
96
+ * @param chainId - The chain ID that the address belongs to.
97
+ * @param address - The account address to name.
98
+ * @param name - The name to assign to the address.
99
+ */
100
+ assignPetName(chainId, address, name) {
101
+ if (!(0, controller_utils_1.isSafeDynamicKey)(chainId)) {
102
+ throw new Error('Invalid chain ID');
103
+ }
104
+ const normalizedAddress = address.toLowerCase();
105
+ this.update((state) => {
106
+ var _a;
107
+ (_a = state.namesByChainIdAndAddress)[chainId] ?? (_a[chainId] = {});
108
+ state.namesByChainIdAndAddress[chainId][normalizedAddress] = name;
109
+ });
110
+ }
111
+ }
112
+ exports.PetNamesController = PetNamesController;
113
+ //# sourceMappingURL=pet-names-controller.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pet-names-controller.cjs","sourceRoot":"","sources":["../../src/pet-names-controller.ts"],"names":[],"mappings":";;;AAMA,+DAA2D;AAC3D,iEAA8D;AAG9D,kBAAkB;AAElB;;;;GAIG;AACU,QAAA,cAAc,GAAG,oBAAoB,CAAC;AAmBnD;;GAEG;AACH,MAAM,0BAA0B,GAAG;IACjC,wBAAwB,EAAE;QACxB,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,KAAK;KACjB;CAC+C,CAAC;AAuDnD;;;;;;;GAOG;AACH,SAAgB,iCAAiC;IAC/C,OAAO;QACL,wBAAwB,EAAE,EAAE;KAC7B,CAAC;AACJ,CAAC;AAJD,8EAIC;AAED,gCAAgC;AAEhC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,MAAa,kBAAmB,SAAQ,gCAIvC;IACC;;;;;;;OAOG;IACH,YAAY,EACV,SAAS,EACT,KAAK,GAIN;QACC,KAAK,CAAC;YACJ,SAAS;YACT,QAAQ,EAAE,0BAA0B;YACpC,IAAI,EAAE,sBAAc;YACpB,KAAK,EAAE;gBACL,GAAG,iCAAiC,EAAE;gBACtC,GAAG,KAAK;aACT;SACF,CAAC,CAAC;IACL,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;AAnDD,gDAmDC","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\n// === GENERAL ===\n\n/**\n * The name of the {@link PetNamesController}, 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 = 'PetNamesController';\n\n// === STATE ===\n\n/**\n * Describes the shape of the state object for {@link PetNamesController}.\n */\nexport type PetNamesControllerState = {\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 PetNamesControllerState}.\n */\nconst petNamesControllerMetadata = {\n namesByChainIdAndAddress: {\n persist: true,\n anonymous: false,\n },\n} satisfies StateMetadata<PetNamesControllerState>;\n\n// === MESSENGER ===\n\n/**\n * The action which can be used to retrieve the state of the\n * {@link PetNamesController}.\n */\nexport type PetNamesControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n PetNamesControllerState\n>;\n\n/**\n * All actions that {@link PetNamesController} registers, to be called\n * externally.\n */\nexport type PetNamesControllerActions = PetNamesControllerGetStateAction;\n\n/**\n * All actions that {@link PetNamesController} calls internally.\n */\ntype AllowedActions = never;\n\n/**\n * The event that {@link PetNamesController} publishes when updating state.\n */\nexport type PetNamesControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n PetNamesControllerState\n>;\n\n/**\n * All events that {@link PetNamesController} publishes, to be subscribed to\n * externally.\n */\nexport type PetNamesControllerEvents = PetNamesControllerStateChangeEvent;\n\n/**\n * All events that {@link PetNamesController} subscribes to internally.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger which is restricted to actions and events accessed by\n * {@link PetNamesController}.\n */\nexport type PetNamesControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n PetNamesControllerActions | AllowedActions,\n PetNamesControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\n/**\n * Constructs the default {@link PetNamesController} 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 PetNamesController} state.\n */\nexport function getDefaultPetNamesControllerState(): PetNamesControllerState {\n return {\n namesByChainIdAndAddress: {},\n };\n}\n\n// === CONTROLLER DEFINITION ===\n\n/**\n * `PetNamesController` records user-provided nicknames for various addresses on\n * various chains.\n *\n * @example\n *\n * ``` ts\n * import { Messenger } from '@metamask/base-controller';\n * import type {\n * PetNamesControllerActions,\n * PetNamesControllerEvents\n * } from '@metamask/example-controllers';\n *\n * const rootMessenger = new Messenger<\n * PetNamesControllerActions,\n * PetNamesControllerEvents\n * >();\n * const petNamesMessenger = rootMessenger.getRestricted({\n * name: 'PetNamesController',\n * allowedActions: [],\n * allowedEvents: [],\n * });\n * const petNamesController = new GasPricesController({\n * messenger: petNamesMessenger,\n * });\n *\n * petNamesController.assignPetName(\n * '0x1',\n * '0xF57F855e17483B1f09bFec62783C9d3b6c8b3A99',\n * 'Primary Account'\n * );\n * petNamesController.state.namesByChainIdAndAddress\n * // => { '0x1': { '0xF57F855e17483B1f09bFec62783C9d3b6c8b3A99': 'Primary Account' } }\n * ```\n */\nexport class PetNamesController extends BaseController<\n typeof controllerName,\n PetNamesControllerState,\n PetNamesControllerMessenger\n> {\n /**\n * Constructs a new {@link PetNamesController}.\n *\n * @param args - The arguments to the 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: PetNamesControllerMessenger;\n state?: Partial<PetNamesControllerState>;\n }) {\n super({\n messenger,\n metadata: petNamesControllerMetadata,\n name: controllerName,\n state: {\n ...getDefaultPetNamesControllerState(),\n ...state,\n },\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"]}
@@ -0,0 +1,124 @@
1
+ import type { ControllerGetStateAction, ControllerStateChangeEvent, RestrictedMessenger } from "@metamask/base-controller";
2
+ import { BaseController } from "@metamask/base-controller";
3
+ import type { Hex } from "@metamask/utils";
4
+ /**
5
+ * The name of the {@link PetNamesController}, used to namespace the
6
+ * controller's actions and events and to namespace the controller's state data
7
+ * when composed with other controllers.
8
+ */
9
+ export declare const controllerName = "PetNamesController";
10
+ /**
11
+ * Describes the shape of the state object for {@link PetNamesController}.
12
+ */
13
+ export type PetNamesControllerState = {
14
+ /**
15
+ * The registry of pet names, categorized by chain ID first and address
16
+ * second.
17
+ */
18
+ namesByChainIdAndAddress: {
19
+ [chainId: Hex]: {
20
+ [address: Hex]: string;
21
+ };
22
+ };
23
+ };
24
+ /**
25
+ * The action which can be used to retrieve the state of the
26
+ * {@link PetNamesController}.
27
+ */
28
+ export type PetNamesControllerGetStateAction = ControllerGetStateAction<typeof controllerName, PetNamesControllerState>;
29
+ /**
30
+ * All actions that {@link PetNamesController} registers, to be called
31
+ * externally.
32
+ */
33
+ export type PetNamesControllerActions = PetNamesControllerGetStateAction;
34
+ /**
35
+ * All actions that {@link PetNamesController} calls internally.
36
+ */
37
+ type AllowedActions = never;
38
+ /**
39
+ * The event that {@link PetNamesController} publishes when updating state.
40
+ */
41
+ export type PetNamesControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, PetNamesControllerState>;
42
+ /**
43
+ * All events that {@link PetNamesController} publishes, to be subscribed to
44
+ * externally.
45
+ */
46
+ export type PetNamesControllerEvents = PetNamesControllerStateChangeEvent;
47
+ /**
48
+ * All events that {@link PetNamesController} subscribes to internally.
49
+ */
50
+ type AllowedEvents = never;
51
+ /**
52
+ * The messenger which is restricted to actions and events accessed by
53
+ * {@link PetNamesController}.
54
+ */
55
+ export type PetNamesControllerMessenger = RestrictedMessenger<typeof controllerName, PetNamesControllerActions | AllowedActions, PetNamesControllerEvents | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
56
+ /**
57
+ * Constructs the default {@link PetNamesController} state. This allows
58
+ * consumers to provide a partial state object when initializing the controller
59
+ * and also helps in constructing complete state objects for this controller in
60
+ * tests.
61
+ *
62
+ * @returns The default {@link PetNamesController} state.
63
+ */
64
+ export declare function getDefaultPetNamesControllerState(): PetNamesControllerState;
65
+ /**
66
+ * `PetNamesController` records user-provided nicknames for various addresses on
67
+ * various chains.
68
+ *
69
+ * @example
70
+ *
71
+ * ``` ts
72
+ * import { Messenger } from '@metamask/base-controller';
73
+ * import type {
74
+ * PetNamesControllerActions,
75
+ * PetNamesControllerEvents
76
+ * } from '@metamask/example-controllers';
77
+ *
78
+ * const rootMessenger = new Messenger<
79
+ * PetNamesControllerActions,
80
+ * PetNamesControllerEvents
81
+ * >();
82
+ * const petNamesMessenger = rootMessenger.getRestricted({
83
+ * name: 'PetNamesController',
84
+ * allowedActions: [],
85
+ * allowedEvents: [],
86
+ * });
87
+ * const petNamesController = new GasPricesController({
88
+ * messenger: petNamesMessenger,
89
+ * });
90
+ *
91
+ * petNamesController.assignPetName(
92
+ * '0x1',
93
+ * '0xF57F855e17483B1f09bFec62783C9d3b6c8b3A99',
94
+ * 'Primary Account'
95
+ * );
96
+ * petNamesController.state.namesByChainIdAndAddress
97
+ * // => { '0x1': { '0xF57F855e17483B1f09bFec62783C9d3b6c8b3A99': 'Primary Account' } }
98
+ * ```
99
+ */
100
+ export declare class PetNamesController extends BaseController<typeof controllerName, PetNamesControllerState, PetNamesControllerMessenger> {
101
+ /**
102
+ * Constructs a new {@link PetNamesController}.
103
+ *
104
+ * @param args - The arguments to the controller.
105
+ * @param args.messenger - The messenger suited for this controller.
106
+ * @param args.state - The desired state with which to initialize this
107
+ * controller. Missing properties will be filled in with defaults.
108
+ */
109
+ constructor({ messenger, state, }: {
110
+ messenger: PetNamesControllerMessenger;
111
+ state?: Partial<PetNamesControllerState>;
112
+ });
113
+ /**
114
+ * Registers the given name with the given address (relative to the given
115
+ * chain).
116
+ *
117
+ * @param chainId - The chain ID that the address belongs to.
118
+ * @param address - The account address to name.
119
+ * @param name - The name to assign to the address.
120
+ */
121
+ assignPetName(chainId: Hex, address: Hex, name: string): void;
122
+ }
123
+ export {};
124
+ //# sourceMappingURL=pet-names-controller.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pet-names-controller.d.cts","sourceRoot":"","sources":["../../src/pet-names-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;AAI3C;;;;GAIG;AACH,eAAO,MAAM,cAAc,uBAAuB,CAAC;AAInD;;GAEG;AACH,MAAM,MAAM,uBAAuB,GAAG;IACpC;;;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;;;GAGG;AACH,MAAM,MAAM,gCAAgC,GAAG,wBAAwB,CACrE,OAAO,cAAc,EACrB,uBAAuB,CACxB,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,yBAAyB,GAAG,gCAAgC,CAAC;AAEzE;;GAEG;AACH,KAAK,cAAc,GAAG,KAAK,CAAC;AAE5B;;GAEG;AACH,MAAM,MAAM,kCAAkC,GAAG,0BAA0B,CACzE,OAAO,cAAc,EACrB,uBAAuB,CACxB,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,wBAAwB,GAAG,kCAAkC,CAAC;AAE1E;;GAEG;AACH,KAAK,aAAa,GAAG,KAAK,CAAC;AAE3B;;;GAGG;AACH,MAAM,MAAM,2BAA2B,GAAG,mBAAmB,CAC3D,OAAO,cAAc,EACrB,yBAAyB,GAAG,cAAc,EAC1C,wBAAwB,GAAG,aAAa,EACxC,cAAc,CAAC,MAAM,CAAC,EACtB,aAAa,CAAC,MAAM,CAAC,CACtB,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAgB,iCAAiC,IAAI,uBAAuB,CAI3E;AAID;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,qBAAa,kBAAmB,SAAQ,cAAc,CACpD,OAAO,cAAc,EACrB,uBAAuB,EACvB,2BAA2B,CAC5B;IACC;;;;;;;OAOG;gBACS,EACV,SAAS,EACT,KAAK,GACN,EAAE;QACD,SAAS,EAAE,2BAA2B,CAAC;QACvC,KAAK,CAAC,EAAE,OAAO,CAAC,uBAAuB,CAAC,CAAC;KAC1C;IAYD;;;;;;;OAOG;IACH,aAAa,CAAC,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM;CAYvD"}
@@ -0,0 +1,124 @@
1
+ import type { ControllerGetStateAction, ControllerStateChangeEvent, RestrictedMessenger } from "@metamask/base-controller";
2
+ import { BaseController } from "@metamask/base-controller";
3
+ import type { Hex } from "@metamask/utils";
4
+ /**
5
+ * The name of the {@link PetNamesController}, used to namespace the
6
+ * controller's actions and events and to namespace the controller's state data
7
+ * when composed with other controllers.
8
+ */
9
+ export declare const controllerName = "PetNamesController";
10
+ /**
11
+ * Describes the shape of the state object for {@link PetNamesController}.
12
+ */
13
+ export type PetNamesControllerState = {
14
+ /**
15
+ * The registry of pet names, categorized by chain ID first and address
16
+ * second.
17
+ */
18
+ namesByChainIdAndAddress: {
19
+ [chainId: Hex]: {
20
+ [address: Hex]: string;
21
+ };
22
+ };
23
+ };
24
+ /**
25
+ * The action which can be used to retrieve the state of the
26
+ * {@link PetNamesController}.
27
+ */
28
+ export type PetNamesControllerGetStateAction = ControllerGetStateAction<typeof controllerName, PetNamesControllerState>;
29
+ /**
30
+ * All actions that {@link PetNamesController} registers, to be called
31
+ * externally.
32
+ */
33
+ export type PetNamesControllerActions = PetNamesControllerGetStateAction;
34
+ /**
35
+ * All actions that {@link PetNamesController} calls internally.
36
+ */
37
+ type AllowedActions = never;
38
+ /**
39
+ * The event that {@link PetNamesController} publishes when updating state.
40
+ */
41
+ export type PetNamesControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, PetNamesControllerState>;
42
+ /**
43
+ * All events that {@link PetNamesController} publishes, to be subscribed to
44
+ * externally.
45
+ */
46
+ export type PetNamesControllerEvents = PetNamesControllerStateChangeEvent;
47
+ /**
48
+ * All events that {@link PetNamesController} subscribes to internally.
49
+ */
50
+ type AllowedEvents = never;
51
+ /**
52
+ * The messenger which is restricted to actions and events accessed by
53
+ * {@link PetNamesController}.
54
+ */
55
+ export type PetNamesControllerMessenger = RestrictedMessenger<typeof controllerName, PetNamesControllerActions | AllowedActions, PetNamesControllerEvents | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
56
+ /**
57
+ * Constructs the default {@link PetNamesController} state. This allows
58
+ * consumers to provide a partial state object when initializing the controller
59
+ * and also helps in constructing complete state objects for this controller in
60
+ * tests.
61
+ *
62
+ * @returns The default {@link PetNamesController} state.
63
+ */
64
+ export declare function getDefaultPetNamesControllerState(): PetNamesControllerState;
65
+ /**
66
+ * `PetNamesController` records user-provided nicknames for various addresses on
67
+ * various chains.
68
+ *
69
+ * @example
70
+ *
71
+ * ``` ts
72
+ * import { Messenger } from '@metamask/base-controller';
73
+ * import type {
74
+ * PetNamesControllerActions,
75
+ * PetNamesControllerEvents
76
+ * } from '@metamask/example-controllers';
77
+ *
78
+ * const rootMessenger = new Messenger<
79
+ * PetNamesControllerActions,
80
+ * PetNamesControllerEvents
81
+ * >();
82
+ * const petNamesMessenger = rootMessenger.getRestricted({
83
+ * name: 'PetNamesController',
84
+ * allowedActions: [],
85
+ * allowedEvents: [],
86
+ * });
87
+ * const petNamesController = new GasPricesController({
88
+ * messenger: petNamesMessenger,
89
+ * });
90
+ *
91
+ * petNamesController.assignPetName(
92
+ * '0x1',
93
+ * '0xF57F855e17483B1f09bFec62783C9d3b6c8b3A99',
94
+ * 'Primary Account'
95
+ * );
96
+ * petNamesController.state.namesByChainIdAndAddress
97
+ * // => { '0x1': { '0xF57F855e17483B1f09bFec62783C9d3b6c8b3A99': 'Primary Account' } }
98
+ * ```
99
+ */
100
+ export declare class PetNamesController extends BaseController<typeof controllerName, PetNamesControllerState, PetNamesControllerMessenger> {
101
+ /**
102
+ * Constructs a new {@link PetNamesController}.
103
+ *
104
+ * @param args - The arguments to the controller.
105
+ * @param args.messenger - The messenger suited for this controller.
106
+ * @param args.state - The desired state with which to initialize this
107
+ * controller. Missing properties will be filled in with defaults.
108
+ */
109
+ constructor({ messenger, state, }: {
110
+ messenger: PetNamesControllerMessenger;
111
+ state?: Partial<PetNamesControllerState>;
112
+ });
113
+ /**
114
+ * Registers the given name with the given address (relative to the given
115
+ * chain).
116
+ *
117
+ * @param chainId - The chain ID that the address belongs to.
118
+ * @param address - The account address to name.
119
+ * @param name - The name to assign to the address.
120
+ */
121
+ assignPetName(chainId: Hex, address: Hex, name: string): void;
122
+ }
123
+ export {};
124
+ //# sourceMappingURL=pet-names-controller.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pet-names-controller.d.mts","sourceRoot":"","sources":["../../src/pet-names-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;AAI3C;;;;GAIG;AACH,eAAO,MAAM,cAAc,uBAAuB,CAAC;AAInD;;GAEG;AACH,MAAM,MAAM,uBAAuB,GAAG;IACpC;;;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;;;GAGG;AACH,MAAM,MAAM,gCAAgC,GAAG,wBAAwB,CACrE,OAAO,cAAc,EACrB,uBAAuB,CACxB,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,yBAAyB,GAAG,gCAAgC,CAAC;AAEzE;;GAEG;AACH,KAAK,cAAc,GAAG,KAAK,CAAC;AAE5B;;GAEG;AACH,MAAM,MAAM,kCAAkC,GAAG,0BAA0B,CACzE,OAAO,cAAc,EACrB,uBAAuB,CACxB,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,wBAAwB,GAAG,kCAAkC,CAAC;AAE1E;;GAEG;AACH,KAAK,aAAa,GAAG,KAAK,CAAC;AAE3B;;;GAGG;AACH,MAAM,MAAM,2BAA2B,GAAG,mBAAmB,CAC3D,OAAO,cAAc,EACrB,yBAAyB,GAAG,cAAc,EAC1C,wBAAwB,GAAG,aAAa,EACxC,cAAc,CAAC,MAAM,CAAC,EACtB,aAAa,CAAC,MAAM,CAAC,CACtB,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAgB,iCAAiC,IAAI,uBAAuB,CAI3E;AAID;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,qBAAa,kBAAmB,SAAQ,cAAc,CACpD,OAAO,cAAc,EACrB,uBAAuB,EACvB,2BAA2B,CAC5B;IACC;;;;;;;OAOG;gBACS,EACV,SAAS,EACT,KAAK,GACN,EAAE;QACD,SAAS,EAAE,2BAA2B,CAAC;QACvC,KAAK,CAAC,EAAE,OAAO,CAAC,uBAAuB,CAAC,CAAC;KAC1C;IAYD;;;;;;;OAOG;IACH,aAAa,CAAC,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM;CAYvD"}
@@ -0,0 +1,108 @@
1
+ import { BaseController } from "@metamask/base-controller";
2
+ import { isSafeDynamicKey } from "@metamask/controller-utils";
3
+ // === GENERAL ===
4
+ /**
5
+ * The name of the {@link PetNamesController}, used to namespace the
6
+ * controller's actions and events and to namespace the controller's state data
7
+ * when composed with other controllers.
8
+ */
9
+ export const controllerName = 'PetNamesController';
10
+ /**
11
+ * The metadata for each property in {@link PetNamesControllerState}.
12
+ */
13
+ const petNamesControllerMetadata = {
14
+ namesByChainIdAndAddress: {
15
+ persist: true,
16
+ anonymous: false,
17
+ },
18
+ };
19
+ /**
20
+ * Constructs the default {@link PetNamesController} state. This allows
21
+ * consumers to provide a partial state object when initializing the controller
22
+ * and also helps in constructing complete state objects for this controller in
23
+ * tests.
24
+ *
25
+ * @returns The default {@link PetNamesController} state.
26
+ */
27
+ export function getDefaultPetNamesControllerState() {
28
+ return {
29
+ namesByChainIdAndAddress: {},
30
+ };
31
+ }
32
+ // === CONTROLLER DEFINITION ===
33
+ /**
34
+ * `PetNamesController` records user-provided nicknames for various addresses on
35
+ * various chains.
36
+ *
37
+ * @example
38
+ *
39
+ * ``` ts
40
+ * import { Messenger } from '@metamask/base-controller';
41
+ * import type {
42
+ * PetNamesControllerActions,
43
+ * PetNamesControllerEvents
44
+ * } from '@metamask/example-controllers';
45
+ *
46
+ * const rootMessenger = new Messenger<
47
+ * PetNamesControllerActions,
48
+ * PetNamesControllerEvents
49
+ * >();
50
+ * const petNamesMessenger = rootMessenger.getRestricted({
51
+ * name: 'PetNamesController',
52
+ * allowedActions: [],
53
+ * allowedEvents: [],
54
+ * });
55
+ * const petNamesController = new GasPricesController({
56
+ * messenger: petNamesMessenger,
57
+ * });
58
+ *
59
+ * petNamesController.assignPetName(
60
+ * '0x1',
61
+ * '0xF57F855e17483B1f09bFec62783C9d3b6c8b3A99',
62
+ * 'Primary Account'
63
+ * );
64
+ * petNamesController.state.namesByChainIdAndAddress
65
+ * // => { '0x1': { '0xF57F855e17483B1f09bFec62783C9d3b6c8b3A99': 'Primary Account' } }
66
+ * ```
67
+ */
68
+ export class PetNamesController extends BaseController {
69
+ /**
70
+ * Constructs a new {@link PetNamesController}.
71
+ *
72
+ * @param args - The arguments to the controller.
73
+ * @param args.messenger - The messenger suited for this controller.
74
+ * @param args.state - The desired state with which to initialize this
75
+ * controller. Missing properties will be filled in with defaults.
76
+ */
77
+ constructor({ messenger, state, }) {
78
+ super({
79
+ messenger,
80
+ metadata: petNamesControllerMetadata,
81
+ name: controllerName,
82
+ state: {
83
+ ...getDefaultPetNamesControllerState(),
84
+ ...state,
85
+ },
86
+ });
87
+ }
88
+ /**
89
+ * Registers the given name with the given address (relative to the given
90
+ * chain).
91
+ *
92
+ * @param chainId - The chain ID that the address belongs to.
93
+ * @param address - The account address to name.
94
+ * @param name - The name to assign to the address.
95
+ */
96
+ assignPetName(chainId, address, name) {
97
+ if (!isSafeDynamicKey(chainId)) {
98
+ throw new Error('Invalid chain ID');
99
+ }
100
+ const normalizedAddress = address.toLowerCase();
101
+ this.update((state) => {
102
+ var _a;
103
+ (_a = state.namesByChainIdAndAddress)[chainId] ?? (_a[chainId] = {});
104
+ state.namesByChainIdAndAddress[chainId][normalizedAddress] = name;
105
+ });
106
+ }
107
+ }
108
+ //# sourceMappingURL=pet-names-controller.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pet-names-controller.mjs","sourceRoot":"","sources":["../../src/pet-names-controller.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,EAAE,gBAAgB,EAAE,mCAAmC;AAG9D,kBAAkB;AAElB;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,oBAAoB,CAAC;AAmBnD;;GAEG;AACH,MAAM,0BAA0B,GAAG;IACjC,wBAAwB,EAAE;QACxB,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,KAAK;KACjB;CAC+C,CAAC;AAuDnD;;;;;;;GAOG;AACH,MAAM,UAAU,iCAAiC;IAC/C,OAAO;QACL,wBAAwB,EAAE,EAAE;KAC7B,CAAC;AACJ,CAAC;AAED,gCAAgC;AAEhC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,MAAM,OAAO,kBAAmB,SAAQ,cAIvC;IACC;;;;;;;OAOG;IACH,YAAY,EACV,SAAS,EACT,KAAK,GAIN;QACC,KAAK,CAAC;YACJ,SAAS;YACT,QAAQ,EAAE,0BAA0B;YACpC,IAAI,EAAE,cAAc;YACpB,KAAK,EAAE;gBACL,GAAG,iCAAiC,EAAE;gBACtC,GAAG,KAAK;aACT;SACF,CAAC,CAAC;IACL,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\n// === GENERAL ===\n\n/**\n * The name of the {@link PetNamesController}, 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 = 'PetNamesController';\n\n// === STATE ===\n\n/**\n * Describes the shape of the state object for {@link PetNamesController}.\n */\nexport type PetNamesControllerState = {\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 PetNamesControllerState}.\n */\nconst petNamesControllerMetadata = {\n namesByChainIdAndAddress: {\n persist: true,\n anonymous: false,\n },\n} satisfies StateMetadata<PetNamesControllerState>;\n\n// === MESSENGER ===\n\n/**\n * The action which can be used to retrieve the state of the\n * {@link PetNamesController}.\n */\nexport type PetNamesControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n PetNamesControllerState\n>;\n\n/**\n * All actions that {@link PetNamesController} registers, to be called\n * externally.\n */\nexport type PetNamesControllerActions = PetNamesControllerGetStateAction;\n\n/**\n * All actions that {@link PetNamesController} calls internally.\n */\ntype AllowedActions = never;\n\n/**\n * The event that {@link PetNamesController} publishes when updating state.\n */\nexport type PetNamesControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n PetNamesControllerState\n>;\n\n/**\n * All events that {@link PetNamesController} publishes, to be subscribed to\n * externally.\n */\nexport type PetNamesControllerEvents = PetNamesControllerStateChangeEvent;\n\n/**\n * All events that {@link PetNamesController} subscribes to internally.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger which is restricted to actions and events accessed by\n * {@link PetNamesController}.\n */\nexport type PetNamesControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n PetNamesControllerActions | AllowedActions,\n PetNamesControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\n/**\n * Constructs the default {@link PetNamesController} 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 PetNamesController} state.\n */\nexport function getDefaultPetNamesControllerState(): PetNamesControllerState {\n return {\n namesByChainIdAndAddress: {},\n };\n}\n\n// === CONTROLLER DEFINITION ===\n\n/**\n * `PetNamesController` records user-provided nicknames for various addresses on\n * various chains.\n *\n * @example\n *\n * ``` ts\n * import { Messenger } from '@metamask/base-controller';\n * import type {\n * PetNamesControllerActions,\n * PetNamesControllerEvents\n * } from '@metamask/example-controllers';\n *\n * const rootMessenger = new Messenger<\n * PetNamesControllerActions,\n * PetNamesControllerEvents\n * >();\n * const petNamesMessenger = rootMessenger.getRestricted({\n * name: 'PetNamesController',\n * allowedActions: [],\n * allowedEvents: [],\n * });\n * const petNamesController = new GasPricesController({\n * messenger: petNamesMessenger,\n * });\n *\n * petNamesController.assignPetName(\n * '0x1',\n * '0xF57F855e17483B1f09bFec62783C9d3b6c8b3A99',\n * 'Primary Account'\n * );\n * petNamesController.state.namesByChainIdAndAddress\n * // => { '0x1': { '0xF57F855e17483B1f09bFec62783C9d3b6c8b3A99': 'Primary Account' } }\n * ```\n */\nexport class PetNamesController extends BaseController<\n typeof controllerName,\n PetNamesControllerState,\n PetNamesControllerMessenger\n> {\n /**\n * Constructs a new {@link PetNamesController}.\n *\n * @param args - The arguments to the 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: PetNamesControllerMessenger;\n state?: Partial<PetNamesControllerState>;\n }) {\n super({\n messenger,\n metadata: petNamesControllerMetadata,\n name: controllerName,\n state: {\n ...getDefaultPetNamesControllerState(),\n ...state,\n },\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 ADDED
@@ -0,0 +1,72 @@
1
+ {
2
+ "name": "@metamask-previews/sample-controllers",
3
+ "version": "0.0.0-preview-37b05a4",
4
+ "description": "Sample package to illustrate best practices for controllers",
5
+ "keywords": [
6
+ "MetaMask",
7
+ "Ethereum"
8
+ ],
9
+ "homepage": "https://github.com/MetaMask/core/tree/main/packages/sample-controllers#readme",
10
+ "bugs": {
11
+ "url": "https://github.com/MetaMask/core/issues"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "https://github.com/MetaMask/core.git"
16
+ },
17
+ "license": "MIT",
18
+ "sideEffects": false,
19
+ "exports": {
20
+ ".": {
21
+ "import": {
22
+ "types": "./dist/index.d.mts",
23
+ "default": "./dist/index.mjs"
24
+ },
25
+ "require": {
26
+ "types": "./dist/index.d.cts",
27
+ "default": "./dist/index.cjs"
28
+ }
29
+ },
30
+ "./package.json": "./package.json"
31
+ },
32
+ "main": "./dist/index.cjs",
33
+ "types": "./dist/index.d.cts",
34
+ "files": [
35
+ "dist/"
36
+ ],
37
+ "scripts": {
38
+ "build": "ts-bridge --project tsconfig.build.json --verbose --clean --no-references",
39
+ "build:docs": "typedoc",
40
+ "changelog:update": "../../scripts/update-changelog.sh @metamask/sample-controllers",
41
+ "changelog:validate": "../../scripts/validate-changelog.sh @metamask/sample-controllers",
42
+ "since-latest-release": "../../scripts/since-latest-release.sh",
43
+ "publish:preview": "yarn npm publish --tag preview",
44
+ "test": "NODE_OPTIONS=--experimental-vm-modules jest --reporters=jest-silent-reporter",
45
+ "test:clean": "NODE_OPTIONS=--experimental-vm-modules jest --clearCache",
46
+ "test:verbose": "NODE_OPTIONS=--experimental-vm-modules jest --verbose",
47
+ "test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch"
48
+ },
49
+ "dependencies": {
50
+ "@metamask/base-controller": "^8.0.0",
51
+ "@metamask/utils": "^11.2.0"
52
+ },
53
+ "devDependencies": {
54
+ "@metamask/auto-changelog": "^3.4.4",
55
+ "@metamask/controller-utils": "^11.5.0",
56
+ "@types/jest": "^27.4.1",
57
+ "deepmerge": "^4.2.2",
58
+ "jest": "^27.5.1",
59
+ "nock": "^13.3.1",
60
+ "ts-jest": "^27.1.4",
61
+ "typedoc": "^0.24.8",
62
+ "typedoc-plugin-missing-exports": "^2.0.0",
63
+ "typescript": "~5.2.2"
64
+ },
65
+ "engines": {
66
+ "node": "^18.18 || >=20"
67
+ },
68
+ "publishConfig": {
69
+ "access": "public",
70
+ "registry": "https://registry.npmjs.org/"
71
+ }
72
+ }