@metamask-previews/sample-controllers 1.0.0-preview-aa8c4291 → 1.0.0-preview-e821953
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +23 -1
- package/dist/index.cjs +5 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +7 -3
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +7 -3
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +3 -3
- package/dist/index.mjs.map +1 -1
- package/dist/sample-gas-prices-controller-method-action-types.cjs +7 -0
- package/dist/sample-gas-prices-controller-method-action-types.cjs.map +1 -0
- package/dist/sample-gas-prices-controller-method-action-types.d.cts +21 -0
- package/dist/sample-gas-prices-controller-method-action-types.d.cts.map +1 -0
- package/dist/sample-gas-prices-controller-method-action-types.d.mts +21 -0
- package/dist/sample-gas-prices-controller-method-action-types.d.mts.map +1 -0
- package/dist/sample-gas-prices-controller-method-action-types.mjs +6 -0
- package/dist/sample-gas-prices-controller-method-action-types.mjs.map +1 -0
- package/dist/sample-gas-prices-controller.cjs +77 -39
- package/dist/sample-gas-prices-controller.cjs.map +1 -1
- package/dist/sample-gas-prices-controller.d.cts +74 -59
- package/dist/sample-gas-prices-controller.d.cts.map +1 -1
- package/dist/sample-gas-prices-controller.d.mts +74 -59
- package/dist/sample-gas-prices-controller.d.mts.map +1 -1
- package/dist/sample-gas-prices-controller.mjs +77 -39
- package/dist/sample-gas-prices-controller.mjs.map +1 -1
- package/dist/sample-gas-prices-service/sample-gas-prices-service-method-action-types.cjs +7 -0
- package/dist/sample-gas-prices-service/sample-gas-prices-service-method-action-types.cjs.map +1 -0
- package/dist/sample-gas-prices-service/sample-gas-prices-service-method-action-types.d.cts +21 -0
- package/dist/sample-gas-prices-service/sample-gas-prices-service-method-action-types.d.cts.map +1 -0
- package/dist/sample-gas-prices-service/sample-gas-prices-service-method-action-types.d.mts +21 -0
- package/dist/sample-gas-prices-service/sample-gas-prices-service-method-action-types.d.mts.map +1 -0
- package/dist/sample-gas-prices-service/sample-gas-prices-service-method-action-types.mjs +6 -0
- package/dist/sample-gas-prices-service/sample-gas-prices-service-method-action-types.mjs.map +1 -0
- package/dist/sample-gas-prices-service/sample-gas-prices-service.cjs +135 -29
- package/dist/sample-gas-prices-service/sample-gas-prices-service.cjs.map +1 -1
- package/dist/sample-gas-prices-service/sample-gas-prices-service.d.cts +122 -28
- package/dist/sample-gas-prices-service/sample-gas-prices-service.d.cts.map +1 -1
- package/dist/sample-gas-prices-service/sample-gas-prices-service.d.mts +122 -28
- package/dist/sample-gas-prices-service/sample-gas-prices-service.d.mts.map +1 -1
- package/dist/sample-gas-prices-service/sample-gas-prices-service.mjs +134 -28
- package/dist/sample-gas-prices-service/sample-gas-prices-service.mjs.map +1 -1
- package/dist/sample-petnames-controller-method-action-types.cjs +7 -0
- package/dist/sample-petnames-controller-method-action-types.cjs.map +1 -0
- package/dist/sample-petnames-controller-method-action-types.d.cts +22 -0
- package/dist/sample-petnames-controller-method-action-types.d.cts.map +1 -0
- package/dist/sample-petnames-controller-method-action-types.d.mts +22 -0
- package/dist/sample-petnames-controller-method-action-types.d.mts.map +1 -0
- package/dist/sample-petnames-controller-method-action-types.mjs +6 -0
- package/dist/sample-petnames-controller-method-action-types.mjs.map +1 -0
- package/dist/sample-petnames-controller.cjs +24 -13
- package/dist/sample-petnames-controller.cjs.map +1 -1
- package/dist/sample-petnames-controller.d.cts +38 -23
- package/dist/sample-petnames-controller.d.cts.map +1 -1
- package/dist/sample-petnames-controller.d.mts +38 -23
- package/dist/sample-petnames-controller.d.mts.map +1 -1
- package/dist/sample-petnames-controller.mjs +23 -13
- package/dist/sample-petnames-controller.mjs.map +1 -1
- package/package.json +2 -1
- package/dist/network-controller-types.cjs +0 -20
- package/dist/network-controller-types.cjs.map +0 -1
- package/dist/network-controller-types.d.cts +0 -34
- package/dist/network-controller-types.d.cts.map +0 -1
- package/dist/network-controller-types.d.mts +0 -34
- package/dist/network-controller-types.d.mts.map +0 -1
- package/dist/network-controller-types.mjs +0 -16
- package/dist/network-controller-types.mjs.map +0 -1
- package/dist/sample-gas-prices-service/index.cjs +0 -6
- package/dist/sample-gas-prices-service/index.cjs.map +0 -1
- package/dist/sample-gas-prices-service/index.d.cts +0 -3
- package/dist/sample-gas-prices-service/index.d.cts.map +0 -1
- package/dist/sample-gas-prices-service/index.d.mts +0 -3
- package/dist/sample-gas-prices-service/index.d.mts.map +0 -1
- package/dist/sample-gas-prices-service/index.mjs +0 -2
- package/dist/sample-gas-prices-service/index.mjs.map +0 -1
- package/dist/sample-gas-prices-service/sample-abstract-gas-prices-service.cjs +0 -3
- package/dist/sample-gas-prices-service/sample-abstract-gas-prices-service.cjs.map +0 -1
- package/dist/sample-gas-prices-service/sample-abstract-gas-prices-service.d.cts +0 -7
- package/dist/sample-gas-prices-service/sample-abstract-gas-prices-service.d.cts.map +0 -1
- package/dist/sample-gas-prices-service/sample-abstract-gas-prices-service.d.mts +0 -7
- package/dist/sample-gas-prices-service/sample-abstract-gas-prices-service.d.mts.map +0 -1
- package/dist/sample-gas-prices-service/sample-abstract-gas-prices-service.mjs +0 -2
- package/dist/sample-gas-prices-service/sample-abstract-gas-prices-service.mjs.map +0 -1
|
@@ -1,44 +1,141 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { RestrictedMessenger } from "@metamask/base-controller";
|
|
2
|
+
import type { CreateServicePolicyOptions, ServicePolicy } from "@metamask/controller-utils";
|
|
3
|
+
import { type Hex } from "@metamask/utils";
|
|
4
|
+
import type { SampleGasPricesServiceMethodActions } from "./sample-gas-prices-service-method-action-types.mjs";
|
|
5
|
+
/**
|
|
6
|
+
* The name of the {@link SampleGasPricesService}, used to namespace the
|
|
7
|
+
* service's actions and events.
|
|
8
|
+
*/
|
|
9
|
+
export declare const serviceName = "SampleGasPricesService";
|
|
10
|
+
/**
|
|
11
|
+
* Actions that {@link SampleGasPricesService} exposes to other consumers.
|
|
12
|
+
*/
|
|
13
|
+
export type SampleGasPricesServiceActions = SampleGasPricesServiceMethodActions;
|
|
14
|
+
/**
|
|
15
|
+
* Actions from other messengers that {@link SampleGasPricesMessenger} calls.
|
|
16
|
+
*/
|
|
17
|
+
type AllowedActions = never;
|
|
18
|
+
/**
|
|
19
|
+
* Events that {@link SampleGasPricesService} exposes to other consumers.
|
|
20
|
+
*/
|
|
21
|
+
export type SampleGasPricesServiceEvents = never;
|
|
22
|
+
/**
|
|
23
|
+
* Events from other messengers that {@link SampleGasPricesService} subscribes
|
|
24
|
+
* to.
|
|
25
|
+
*/
|
|
26
|
+
type AllowedEvents = never;
|
|
27
|
+
/**
|
|
28
|
+
* The messenger which is restricted to actions and events accessed by
|
|
29
|
+
* {@link SampleGasPricesService}.
|
|
30
|
+
*/
|
|
31
|
+
export type SampleGasPricesServiceMessenger = RestrictedMessenger<typeof serviceName, SampleGasPricesServiceActions | AllowedActions, SampleGasPricesServiceEvents | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
|
|
32
|
+
/**
|
|
33
|
+
* What the API endpoint returns.
|
|
34
|
+
*/
|
|
35
|
+
type GasPricesResponse = {
|
|
36
|
+
data: {
|
|
37
|
+
low: number;
|
|
38
|
+
average: number;
|
|
39
|
+
high: number;
|
|
40
|
+
};
|
|
41
|
+
};
|
|
2
42
|
/**
|
|
3
43
|
* This service object is responsible for fetching gas prices via an API.
|
|
4
44
|
*
|
|
5
45
|
* @example
|
|
6
46
|
*
|
|
7
|
-
* On its own:
|
|
8
|
-
*
|
|
9
47
|
* ``` ts
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* ```
|
|
48
|
+
* import type {
|
|
49
|
+
* SampleGasPricesServiceActions,
|
|
50
|
+
* SampleGasPricesServiceEvents
|
|
51
|
+
* } from '@metamask/sample-controllers';
|
|
15
52
|
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
* const
|
|
21
|
-
*
|
|
22
|
-
*
|
|
53
|
+
* const globalMessenger = new Messenger<
|
|
54
|
+
* SampleGasPricesServiceActions
|
|
55
|
+
* SampleGasPricesServiceEvents
|
|
56
|
+
* >();
|
|
57
|
+
* const gasPricesServiceMessenger = globalMessenger.getRestricted({
|
|
58
|
+
* name: 'SampleGasPricesService',
|
|
59
|
+
* allowedActions: [],
|
|
60
|
+
* allowedEvents: [],
|
|
61
|
+
* });
|
|
62
|
+
* // Instantiate the service to register its actions on the messenger
|
|
63
|
+
* new SampleGasPricesService({
|
|
64
|
+
* messenger: gasPricesServiceMessenger,
|
|
65
|
+
* fetch,
|
|
23
66
|
* });
|
|
24
|
-
*
|
|
25
|
-
*
|
|
67
|
+
*
|
|
68
|
+
* // Later...
|
|
69
|
+
* // Fetch gas prices for Mainnet
|
|
70
|
+
* const gasPrices = await globalMessenger.call(
|
|
71
|
+
* 'SampleGasPricesService:fetchGasPrices',
|
|
72
|
+
* '0x1',
|
|
73
|
+
* );
|
|
74
|
+
* // ... Do something with the gas prices ...
|
|
26
75
|
* ```
|
|
27
76
|
*/
|
|
28
77
|
export declare class SampleGasPricesService {
|
|
29
78
|
#private;
|
|
79
|
+
/**
|
|
80
|
+
* The name of the service.
|
|
81
|
+
*/
|
|
82
|
+
readonly name: typeof serviceName;
|
|
30
83
|
/**
|
|
31
84
|
* Constructs a new SampleGasPricesService object.
|
|
32
85
|
*
|
|
33
|
-
* @param args - The arguments.
|
|
34
|
-
* @param args.
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
86
|
+
* @param args - The constructor arguments.
|
|
87
|
+
* @param args.messenger - The messenger suited for this service.
|
|
88
|
+
* @param args.fetch - A function that can be used to make an HTTP request. If
|
|
89
|
+
* your JavaScript environment supports `fetch` natively, you'll probably want
|
|
90
|
+
* to pass that; otherwise you can pass an equivalent (such as `fetch` via
|
|
91
|
+
* `node-fetch`).
|
|
92
|
+
* @param args.policyOptions - Options to pass to `createServicePolicy`, which
|
|
93
|
+
* is used to wrap each request. See {@link CreateServicePolicyOptions}.
|
|
38
94
|
*/
|
|
39
|
-
constructor({ fetch: fetchFunction }: {
|
|
95
|
+
constructor({ messenger, fetch: fetchFunction, policyOptions, }: {
|
|
96
|
+
messenger: SampleGasPricesServiceMessenger;
|
|
40
97
|
fetch: typeof fetch;
|
|
98
|
+
policyOptions?: CreateServicePolicyOptions;
|
|
41
99
|
});
|
|
100
|
+
/**
|
|
101
|
+
* Registers a handler that will be called after a request returns a non-500
|
|
102
|
+
* response, causing a retry. Primarily useful in tests where timers are being
|
|
103
|
+
* mocked.
|
|
104
|
+
*
|
|
105
|
+
* @param listener - The handler to be called.
|
|
106
|
+
* @returns An object that can be used to unregister the handler. See
|
|
107
|
+
* {@link CockatielEvent}.
|
|
108
|
+
* @see {@link createServicePolicy}
|
|
109
|
+
*/
|
|
110
|
+
onRetry(listener: Parameters<ServicePolicy['onRetry']>[0]): import("cockatiel").IDisposable;
|
|
111
|
+
/**
|
|
112
|
+
* Registers a handler that will be called after a set number of retry rounds
|
|
113
|
+
* prove that requests to the API endpoint consistently return a 5xx response.
|
|
114
|
+
*
|
|
115
|
+
* @param listener - The handler to be called.
|
|
116
|
+
* @returns An object that can be used to unregister the handler. See
|
|
117
|
+
* {@link CockatielEvent}.
|
|
118
|
+
* @see {@link createServicePolicy}
|
|
119
|
+
*/
|
|
120
|
+
onBreak(listener: Parameters<ServicePolicy['onBreak']>[0]): import("cockatiel").IDisposable;
|
|
121
|
+
/**
|
|
122
|
+
* Registers a handler that will be called under one of two circumstances:
|
|
123
|
+
*
|
|
124
|
+
* 1. After a set number of retries prove that requests to the API
|
|
125
|
+
* consistently result in one of the following failures:
|
|
126
|
+
* 1. A connection initiation error
|
|
127
|
+
* 2. A connection reset error
|
|
128
|
+
* 3. A timeout error
|
|
129
|
+
* 4. A non-JSON response
|
|
130
|
+
* 5. A 502, 503, or 504 response
|
|
131
|
+
* 2. After a successful request is made to the API, but the response takes
|
|
132
|
+
* longer than a set duration to return.
|
|
133
|
+
*
|
|
134
|
+
* @param listener - The handler to be called.
|
|
135
|
+
* @returns An object that can be used to unregister the handler. See
|
|
136
|
+
* {@link CockatielEvent}.
|
|
137
|
+
*/
|
|
138
|
+
onDegraded(listener: Parameters<ServicePolicy['onDegraded']>[0]): import("cockatiel").IDisposable;
|
|
42
139
|
/**
|
|
43
140
|
* Makes a request to the API in order to retrieve gas prices for a particular
|
|
44
141
|
* chain.
|
|
@@ -46,10 +143,7 @@ export declare class SampleGasPricesService {
|
|
|
46
143
|
* @param chainId - The chain ID for which you want to fetch gas prices.
|
|
47
144
|
* @returns The gas prices for the given chain.
|
|
48
145
|
*/
|
|
49
|
-
fetchGasPrices(chainId: Hex): Promise<
|
|
50
|
-
low: number;
|
|
51
|
-
average: number;
|
|
52
|
-
high: number;
|
|
53
|
-
}>;
|
|
146
|
+
fetchGasPrices(chainId: Hex): Promise<GasPricesResponse['data']>;
|
|
54
147
|
}
|
|
148
|
+
export {};
|
|
55
149
|
//# sourceMappingURL=sample-gas-prices-service.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sample-gas-prices-service.d.mts","sourceRoot":"","sources":["../../src/sample-gas-prices-service/sample-gas-prices-service.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;
|
|
1
|
+
{"version":3,"file":"sample-gas-prices-service.d.mts","sourceRoot":"","sources":["../../src/sample-gas-prices-service/sample-gas-prices-service.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,kCAAkC;AACrE,OAAO,KAAK,EACV,0BAA0B,EAC1B,aAAa,EACd,mCAAmC;AAMpC,OAAO,EAA8B,KAAK,GAAG,EAAE,wBAAwB;AAEvE,OAAO,KAAK,EAAE,mCAAmC,EAAE,4DAAwD;AAI3G;;;GAGG;AACH,eAAO,MAAM,WAAW,2BAA2B,CAAC;AAMpD;;GAEG;AACH,MAAM,MAAM,6BAA6B,GAAG,mCAAmC,CAAC;AAEhF;;GAEG;AACH,KAAK,cAAc,GAAG,KAAK,CAAC;AAE5B;;GAEG;AACH,MAAM,MAAM,4BAA4B,GAAG,KAAK,CAAC;AAEjD;;;GAGG;AACH,KAAK,aAAa,GAAG,KAAK,CAAC;AAE3B;;;GAGG;AACH,MAAM,MAAM,+BAA+B,GAAG,mBAAmB,CAC/D,OAAO,WAAW,EAClB,6BAA6B,GAAG,cAAc,EAC9C,4BAA4B,GAAG,aAAa,EAC5C,cAAc,CAAC,MAAM,CAAC,EACtB,aAAa,CAAC,MAAM,CAAC,CACtB,CAAC;AAIF;;GAEG;AACH,KAAK,iBAAiB,GAAG;IACvB,IAAI,EAAE;QACJ,GAAG,EAAE,MAAM,CAAC;QACZ,OAAO,EAAE,MAAM,CAAC;QAChB,IAAI,EAAE,MAAM,CAAC;KACd,CAAC;CACH,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,qBAAa,sBAAsB;;IACjC;;OAEG;IACH,QAAQ,CAAC,IAAI,EAAE,OAAO,WAAW,CAAC;IAuBlC;;;;;;;;;;;OAWG;gBACS,EACV,SAAS,EACT,KAAK,EAAE,aAAa,EACpB,aAAkB,GACnB,EAAE;QACD,SAAS,EAAE,+BAA+B,CAAC;QAC3C,KAAK,EAAE,OAAO,KAAK,CAAC;QACpB,aAAa,CAAC,EAAE,0BAA0B,CAAC;KAC5C;IAYD;;;;;;;;;OASG;IACH,OAAO,CAAC,QAAQ,EAAE,UAAU,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC;IAIzD;;;;;;;;OAQG;IACH,OAAO,CAAC,QAAQ,EAAE,UAAU,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC;IAKzD;;;;;;;;;;;;;;;;OAgBG;IAEH,UAAU,CAAC,QAAQ,EAAE,UAAU,CAAC,aAAa,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC;IAI/D;;;;;;OAMG;IACG,cAAc,CAAC,OAAO,EAAE,GAAG,GAAG,OAAO,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC;CAqCvE"}
|
|
@@ -9,46 +9,132 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
9
9
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
|
-
var _SampleGasPricesService_fetch;
|
|
12
|
+
var _a, _SampleGasPricesService_messenger, _SampleGasPricesService_fetch, _SampleGasPricesService_policy;
|
|
13
|
+
import { createServicePolicy, fromHex, HttpError } from "@metamask/controller-utils";
|
|
14
|
+
import { hasProperty, isPlainObject } from "@metamask/utils";
|
|
15
|
+
// === GENERAL ===
|
|
16
|
+
/**
|
|
17
|
+
* The name of the {@link SampleGasPricesService}, used to namespace the
|
|
18
|
+
* service's actions and events.
|
|
19
|
+
*/
|
|
20
|
+
export const serviceName = 'SampleGasPricesService';
|
|
21
|
+
// === MESSENGER ===
|
|
22
|
+
const MESSENGER_EXPOSED_METHODS = ['fetchGasPrices'];
|
|
13
23
|
/**
|
|
14
24
|
* This service object is responsible for fetching gas prices via an API.
|
|
15
25
|
*
|
|
16
26
|
* @example
|
|
17
27
|
*
|
|
18
|
-
* On its own:
|
|
19
|
-
*
|
|
20
28
|
* ``` ts
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
* ```
|
|
26
|
-
*
|
|
27
|
-
* In conjunction with `SampleGasPricesController`:
|
|
29
|
+
* import type {
|
|
30
|
+
* SampleGasPricesServiceActions,
|
|
31
|
+
* SampleGasPricesServiceEvents
|
|
32
|
+
* } from '@metamask/sample-controllers';
|
|
28
33
|
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
+
* const globalMessenger = new Messenger<
|
|
35
|
+
* SampleGasPricesServiceActions
|
|
36
|
+
* SampleGasPricesServiceEvents
|
|
37
|
+
* >();
|
|
38
|
+
* const gasPricesServiceMessenger = globalMessenger.getRestricted({
|
|
39
|
+
* name: 'SampleGasPricesService',
|
|
40
|
+
* allowedActions: [],
|
|
41
|
+
* allowedEvents: [],
|
|
42
|
+
* });
|
|
43
|
+
* // Instantiate the service to register its actions on the messenger
|
|
44
|
+
* new SampleGasPricesService({
|
|
45
|
+
* messenger: gasPricesServiceMessenger,
|
|
46
|
+
* fetch,
|
|
34
47
|
* });
|
|
35
|
-
*
|
|
36
|
-
*
|
|
48
|
+
*
|
|
49
|
+
* // Later...
|
|
50
|
+
* // Fetch gas prices for Mainnet
|
|
51
|
+
* const gasPrices = await globalMessenger.call(
|
|
52
|
+
* 'SampleGasPricesService:fetchGasPrices',
|
|
53
|
+
* '0x1',
|
|
54
|
+
* );
|
|
55
|
+
* // ... Do something with the gas prices ...
|
|
37
56
|
* ```
|
|
38
57
|
*/
|
|
39
58
|
export class SampleGasPricesService {
|
|
40
59
|
/**
|
|
41
60
|
* Constructs a new SampleGasPricesService object.
|
|
42
61
|
*
|
|
43
|
-
* @param args - The arguments.
|
|
44
|
-
* @param args.
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
62
|
+
* @param args - The constructor arguments.
|
|
63
|
+
* @param args.messenger - The messenger suited for this service.
|
|
64
|
+
* @param args.fetch - A function that can be used to make an HTTP request. If
|
|
65
|
+
* your JavaScript environment supports `fetch` natively, you'll probably want
|
|
66
|
+
* to pass that; otherwise you can pass an equivalent (such as `fetch` via
|
|
67
|
+
* `node-fetch`).
|
|
68
|
+
* @param args.policyOptions - Options to pass to `createServicePolicy`, which
|
|
69
|
+
* is used to wrap each request. See {@link CreateServicePolicyOptions}.
|
|
48
70
|
*/
|
|
49
|
-
constructor({ fetch: fetchFunction }) {
|
|
71
|
+
constructor({ messenger, fetch: fetchFunction, policyOptions = {}, }) {
|
|
72
|
+
/**
|
|
73
|
+
* The messenger suited for this service.
|
|
74
|
+
*/
|
|
75
|
+
_SampleGasPricesService_messenger.set(this, void 0);
|
|
76
|
+
/**
|
|
77
|
+
* A function that can be used to make an HTTP request.
|
|
78
|
+
*/
|
|
50
79
|
_SampleGasPricesService_fetch.set(this, void 0);
|
|
80
|
+
/**
|
|
81
|
+
* The policy that wraps the request.
|
|
82
|
+
*
|
|
83
|
+
* @see {@link createServicePolicy}
|
|
84
|
+
*/
|
|
85
|
+
_SampleGasPricesService_policy.set(this, void 0);
|
|
86
|
+
this.name = serviceName;
|
|
87
|
+
__classPrivateFieldSet(this, _SampleGasPricesService_messenger, messenger, "f");
|
|
51
88
|
__classPrivateFieldSet(this, _SampleGasPricesService_fetch, fetchFunction, "f");
|
|
89
|
+
__classPrivateFieldSet(this, _SampleGasPricesService_policy, createServicePolicy(policyOptions), "f");
|
|
90
|
+
__classPrivateFieldGet(this, _SampleGasPricesService_messenger, "f").registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Registers a handler that will be called after a request returns a non-500
|
|
94
|
+
* response, causing a retry. Primarily useful in tests where timers are being
|
|
95
|
+
* mocked.
|
|
96
|
+
*
|
|
97
|
+
* @param listener - The handler to be called.
|
|
98
|
+
* @returns An object that can be used to unregister the handler. See
|
|
99
|
+
* {@link CockatielEvent}.
|
|
100
|
+
* @see {@link createServicePolicy}
|
|
101
|
+
*/
|
|
102
|
+
onRetry(listener) {
|
|
103
|
+
return __classPrivateFieldGet(this, _SampleGasPricesService_policy, "f").onRetry(listener);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Registers a handler that will be called after a set number of retry rounds
|
|
107
|
+
* prove that requests to the API endpoint consistently return a 5xx response.
|
|
108
|
+
*
|
|
109
|
+
* @param listener - The handler to be called.
|
|
110
|
+
* @returns An object that can be used to unregister the handler. See
|
|
111
|
+
* {@link CockatielEvent}.
|
|
112
|
+
* @see {@link createServicePolicy}
|
|
113
|
+
*/
|
|
114
|
+
onBreak(listener) {
|
|
115
|
+
return __classPrivateFieldGet(this, _SampleGasPricesService_policy, "f").onBreak(listener);
|
|
116
|
+
}
|
|
117
|
+
/* eslint-disable jsdoc/check-indentation */
|
|
118
|
+
/**
|
|
119
|
+
* Registers a handler that will be called under one of two circumstances:
|
|
120
|
+
*
|
|
121
|
+
* 1. After a set number of retries prove that requests to the API
|
|
122
|
+
* consistently result in one of the following failures:
|
|
123
|
+
* 1. A connection initiation error
|
|
124
|
+
* 2. A connection reset error
|
|
125
|
+
* 3. A timeout error
|
|
126
|
+
* 4. A non-JSON response
|
|
127
|
+
* 5. A 502, 503, or 504 response
|
|
128
|
+
* 2. After a successful request is made to the API, but the response takes
|
|
129
|
+
* longer than a set duration to return.
|
|
130
|
+
*
|
|
131
|
+
* @param listener - The handler to be called.
|
|
132
|
+
* @returns An object that can be used to unregister the handler. See
|
|
133
|
+
* {@link CockatielEvent}.
|
|
134
|
+
*/
|
|
135
|
+
/* eslint-enable jsdoc/check-indentation */
|
|
136
|
+
onDegraded(listener) {
|
|
137
|
+
return __classPrivateFieldGet(this, _SampleGasPricesService_policy, "f").onDegraded(listener);
|
|
52
138
|
}
|
|
53
139
|
/**
|
|
54
140
|
* Makes a request to the API in order to retrieve gas prices for a particular
|
|
@@ -58,11 +144,31 @@ export class SampleGasPricesService {
|
|
|
58
144
|
* @returns The gas prices for the given chain.
|
|
59
145
|
*/
|
|
60
146
|
async fetchGasPrices(chainId) {
|
|
61
|
-
const response = await __classPrivateFieldGet(this,
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
147
|
+
const response = await __classPrivateFieldGet(this, _SampleGasPricesService_policy, "f").execute(async () => {
|
|
148
|
+
const url = new URL('https://api.example.com/gas-prices');
|
|
149
|
+
url.searchParams.append('chainId', `eip155:${fromHex(chainId)}`);
|
|
150
|
+
const localResponse = await __classPrivateFieldGet(this, _SampleGasPricesService_fetch, "f").call(this, url);
|
|
151
|
+
if (!localResponse.ok) {
|
|
152
|
+
throw new HttpError(localResponse.status, `Fetching '${url.toString()}' failed with status '${localResponse.status}'`);
|
|
153
|
+
}
|
|
154
|
+
return localResponse;
|
|
155
|
+
});
|
|
156
|
+
const jsonResponse = await response.json();
|
|
157
|
+
if (isPlainObject(jsonResponse) &&
|
|
158
|
+
hasProperty(jsonResponse, 'data') &&
|
|
159
|
+
isPlainObject(jsonResponse.data) &&
|
|
160
|
+
hasProperty(jsonResponse.data, 'low') &&
|
|
161
|
+
hasProperty(jsonResponse.data, 'average') &&
|
|
162
|
+
hasProperty(jsonResponse.data, 'high')) {
|
|
163
|
+
const { data: { low, average, high }, } = jsonResponse;
|
|
164
|
+
if (typeof low === 'number' &&
|
|
165
|
+
typeof average === 'number' &&
|
|
166
|
+
typeof high === 'number') {
|
|
167
|
+
return { low, average, high };
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
throw new Error('Malformed response received from gas prices API');
|
|
65
171
|
}
|
|
66
172
|
}
|
|
67
|
-
_SampleGasPricesService_fetch = new WeakMap();
|
|
173
|
+
_a = SampleGasPricesService, _SampleGasPricesService_messenger = new WeakMap(), _SampleGasPricesService_fetch = new WeakMap(), _SampleGasPricesService_policy = new WeakMap();
|
|
68
174
|
//# sourceMappingURL=sample-gas-prices-service.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sample-gas-prices-service.mjs","sourceRoot":"","sources":["../../src/sample-gas-prices-service/sample-gas-prices-service.ts"],"names":[],"mappings":";;;;;;;;;;;;AAaA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,OAAO,sBAAsB;IAGjC;;;;;;;;OAQG;IACH,YAAY,EAAE,KAAK,EAAE,aAAa,EAA2B;QAXpD,gDAAqB;QAY5B,uBAAA,IAAI,iCAAU,aAAa,MAAA,CAAC;IAC9B,CAAC;IAED;;;;;;OAMG;IACH,KAAK,CAAC,cAAc,CAAC,OAAY;QAC/B,MAAM,QAAQ,GAAG,MAAM,uBAAA,IAAI,qCAAO,MAAX,IAAI,EACzB,kCAAkC,OAAO,OAAO,CACjD,CAAC;QACF,oEAAoE;QACpE,MAAM,iBAAiB,GACrB,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAiC,CAAC;QAC1D,OAAO,iBAAiB,CAAC,IAAI,CAAC;IAChC,CAAC;CACF","sourcesContent":["import type { Hex } from '@metamask/utils';\n\n/**\n * What the API endpoint returns.\n */\ntype GasPricesResponse = {\n data: {\n low: number;\n average: number;\n high: number;\n };\n};\n\n/**\n * This service object is responsible for fetching gas prices via an API.\n *\n * @example\n *\n * On its own:\n *\n * ``` ts\n * const gasPricesService = new SampleGasPricesService({ fetch });\n * // Fetch gas prices for Mainnet\n * const gasPricesResponse = await gasPricesService.fetchGasPrices('0x1');\n * // ... Do something with the response ...\n * ```\n *\n * In conjunction with `SampleGasPricesController`:\n *\n * ``` ts\n * const gasPricesService = new SampleGasPricesService({ fetch });\n * const gasPricesController = new SampleGasPricesController({\n * // ... state, messenger, etc. ...\n * gasPricesService,\n * });\n * // This will use the service object internally\n * gasPricesController.updateGasPrices();\n * ```\n */\nexport class SampleGasPricesService {\n readonly #fetch: typeof fetch;\n\n /**\n * Constructs a new SampleGasPricesService object.\n *\n * @param args - The arguments.\n * @param args.fetch - A function that can be used to make an HTTP request.\n * If your JavaScript environment supports `fetch` natively, you'll probably\n * want to pass that; otherwise you can pass an equivalent (such as `fetch`\n * via `node-fetch`).\n */\n constructor({ fetch: fetchFunction }: { fetch: typeof fetch }) {\n this.#fetch = fetchFunction;\n }\n\n /**\n * Makes a request to the API in order to retrieve gas prices for a particular\n * chain.\n *\n * @param chainId - The chain ID for which you want to fetch gas prices.\n * @returns The gas prices for the given chain.\n */\n async fetchGasPrices(chainId: Hex) {\n const response = await this.#fetch(\n `https://example.com/gas-prices/${chainId}.json`,\n );\n // Type assertion: We have to assume the shape of the response data.\n const gasPricesResponse =\n (await response.json()) as unknown as GasPricesResponse;\n return gasPricesResponse.data;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"sample-gas-prices-service.mjs","sourceRoot":"","sources":["../../src/sample-gas-prices-service/sample-gas-prices-service.ts"],"names":[],"mappings":"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","sourcesContent":["import type { RestrictedMessenger } from '@metamask/base-controller';\nimport type {\n CreateServicePolicyOptions,\n ServicePolicy,\n} from '@metamask/controller-utils';\nimport {\n createServicePolicy,\n fromHex,\n HttpError,\n} from '@metamask/controller-utils';\nimport { hasProperty, isPlainObject, type Hex } from '@metamask/utils';\n\nimport type { SampleGasPricesServiceMethodActions } from './sample-gas-prices-service-method-action-types';\n\n// === GENERAL ===\n\n/**\n * The name of the {@link SampleGasPricesService}, used to namespace the\n * service's actions and events.\n */\nexport const serviceName = 'SampleGasPricesService';\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = ['fetchGasPrices'] as const;\n\n/**\n * Actions that {@link SampleGasPricesService} exposes to other consumers.\n */\nexport type SampleGasPricesServiceActions = SampleGasPricesServiceMethodActions;\n\n/**\n * Actions from other messengers that {@link SampleGasPricesMessenger} calls.\n */\ntype AllowedActions = never;\n\n/**\n * Events that {@link SampleGasPricesService} exposes to other consumers.\n */\nexport type SampleGasPricesServiceEvents = never;\n\n/**\n * Events from other messengers that {@link SampleGasPricesService} subscribes\n * to.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger which is restricted to actions and events accessed by\n * {@link SampleGasPricesService}.\n */\nexport type SampleGasPricesServiceMessenger = RestrictedMessenger<\n typeof serviceName,\n SampleGasPricesServiceActions | AllowedActions,\n SampleGasPricesServiceEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\n// === SERVICE DEFINITION ===\n\n/**\n * What the API endpoint returns.\n */\ntype GasPricesResponse = {\n data: {\n low: number;\n average: number;\n high: number;\n };\n};\n\n/**\n * This service object is responsible for fetching gas prices via an API.\n *\n * @example\n *\n * ``` ts\n * import type {\n * SampleGasPricesServiceActions,\n * SampleGasPricesServiceEvents\n * } from '@metamask/sample-controllers';\n *\n * const globalMessenger = new Messenger<\n * SampleGasPricesServiceActions\n * SampleGasPricesServiceEvents\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 * fetch,\n * });\n *\n * // Later...\n * // Fetch gas prices for Mainnet\n * const gasPrices = await globalMessenger.call(\n * 'SampleGasPricesService:fetchGasPrices',\n * '0x1',\n * );\n * // ... Do something with the gas prices ...\n * ```\n */\nexport class SampleGasPricesService {\n /**\n * The name of the service.\n */\n readonly name: typeof serviceName;\n\n /**\n * The messenger suited for this service.\n */\n readonly #messenger: ConstructorParameters<\n typeof SampleGasPricesService\n >[0]['messenger'];\n\n /**\n * A function that can be used to make an HTTP request.\n */\n readonly #fetch: ConstructorParameters<\n typeof SampleGasPricesService\n >[0]['fetch'];\n\n /**\n * The policy that wraps the request.\n *\n * @see {@link createServicePolicy}\n */\n readonly #policy: ServicePolicy;\n\n /**\n * Constructs a new SampleGasPricesService object.\n *\n * @param args - The constructor arguments.\n * @param args.messenger - The messenger suited for this service.\n * @param args.fetch - A function that can be used to make an HTTP request. If\n * your JavaScript environment supports `fetch` natively, you'll probably want\n * to pass that; otherwise you can pass an equivalent (such as `fetch` via\n * `node-fetch`).\n * @param args.policyOptions - Options to pass to `createServicePolicy`, which\n * is used to wrap each request. See {@link CreateServicePolicyOptions}.\n */\n constructor({\n messenger,\n fetch: fetchFunction,\n policyOptions = {},\n }: {\n messenger: SampleGasPricesServiceMessenger;\n fetch: typeof fetch;\n policyOptions?: CreateServicePolicyOptions;\n }) {\n this.name = serviceName;\n this.#messenger = messenger;\n this.#fetch = fetchFunction;\n this.#policy = createServicePolicy(policyOptions);\n\n this.#messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n }\n\n /**\n * Registers a handler that will be called after a request returns a non-500\n * response, causing a retry. Primarily useful in tests where timers are being\n * mocked.\n *\n * @param listener - The handler to be called.\n * @returns An object that can be used to unregister the handler. See\n * {@link CockatielEvent}.\n * @see {@link createServicePolicy}\n */\n onRetry(listener: Parameters<ServicePolicy['onRetry']>[0]) {\n return this.#policy.onRetry(listener);\n }\n\n /**\n * Registers a handler that will be called after a set number of retry rounds\n * prove that requests to the API endpoint consistently return a 5xx response.\n *\n * @param listener - The handler to be called.\n * @returns An object that can be used to unregister the handler. See\n * {@link CockatielEvent}.\n * @see {@link createServicePolicy}\n */\n onBreak(listener: Parameters<ServicePolicy['onBreak']>[0]) {\n return this.#policy.onBreak(listener);\n }\n\n /* eslint-disable jsdoc/check-indentation */\n /**\n * Registers a handler that will be called under one of two circumstances:\n *\n * 1. After a set number of retries prove that requests to the API\n * consistently result in one of the following failures:\n * 1. A connection initiation error\n * 2. A connection reset error\n * 3. A timeout error\n * 4. A non-JSON response\n * 5. A 502, 503, or 504 response\n * 2. After a successful request is made to the API, but the response takes\n * longer than a set duration to return.\n *\n * @param listener - The handler to be called.\n * @returns An object that can be used to unregister the handler. See\n * {@link CockatielEvent}.\n */\n /* eslint-enable jsdoc/check-indentation */\n onDegraded(listener: Parameters<ServicePolicy['onDegraded']>[0]) {\n return this.#policy.onDegraded(listener);\n }\n\n /**\n * Makes a request to the API in order to retrieve gas prices for a particular\n * chain.\n *\n * @param chainId - The chain ID for which you want to fetch gas prices.\n * @returns The gas prices for the given chain.\n */\n async fetchGasPrices(chainId: Hex): Promise<GasPricesResponse['data']> {\n const response = await this.#policy.execute(async () => {\n const url = new URL('https://api.example.com/gas-prices');\n url.searchParams.append('chainId', `eip155:${fromHex(chainId)}`);\n const localResponse = await this.#fetch(url);\n if (!localResponse.ok) {\n throw new HttpError(\n localResponse.status,\n `Fetching '${url.toString()}' failed with status '${localResponse.status}'`,\n );\n }\n return localResponse;\n });\n const jsonResponse = await response.json();\n\n if (\n isPlainObject(jsonResponse) &&\n hasProperty(jsonResponse, 'data') &&\n isPlainObject(jsonResponse.data) &&\n hasProperty(jsonResponse.data, 'low') &&\n hasProperty(jsonResponse.data, 'average') &&\n hasProperty(jsonResponse.data, 'high')\n ) {\n const {\n data: { low, average, high },\n } = jsonResponse;\n if (\n typeof low === 'number' &&\n typeof average === 'number' &&\n typeof high === 'number'\n ) {\n return { low, average, high };\n }\n }\n\n throw new Error('Malformed response received from gas prices API');\n }\n}\n"]}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* This file is auto generated by `scripts/generate-method-action-types.ts`.
|
|
4
|
+
* Do not edit manually.
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
//# sourceMappingURL=sample-petnames-controller-method-action-types.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sample-petnames-controller-method-action-types.cjs","sourceRoot":"","sources":["../src/sample-petnames-controller-method-action-types.ts"],"names":[],"mappings":";AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated by `scripts/generate-method-action-types.ts`.\n * Do not edit manually.\n */\n\nimport type { SamplePetnamesController } from './sample-petnames-controller';\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 */\nexport type SamplePetnamesControllerAssignPetnameAction = {\n type: `SamplePetnamesController:assignPetname`;\n handler: SamplePetnamesController['assignPetname'];\n};\n\n/**\n * Union of all SamplePetnamesController action types.\n */\nexport type SamplePetnamesControllerMethodActions =\n SamplePetnamesControllerAssignPetnameAction;\n"]}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is auto generated by `scripts/generate-method-action-types.ts`.
|
|
3
|
+
* Do not edit manually.
|
|
4
|
+
*/
|
|
5
|
+
import type { SamplePetnamesController } from "./sample-petnames-controller.cjs";
|
|
6
|
+
/**
|
|
7
|
+
* Registers the given name with the given address (relative to the given
|
|
8
|
+
* chain).
|
|
9
|
+
*
|
|
10
|
+
* @param chainId - The chain ID that the address belongs to.
|
|
11
|
+
* @param address - The account address to name.
|
|
12
|
+
* @param name - The name to assign to the address.
|
|
13
|
+
*/
|
|
14
|
+
export type SamplePetnamesControllerAssignPetnameAction = {
|
|
15
|
+
type: `SamplePetnamesController:assignPetname`;
|
|
16
|
+
handler: SamplePetnamesController['assignPetname'];
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Union of all SamplePetnamesController action types.
|
|
20
|
+
*/
|
|
21
|
+
export type SamplePetnamesControllerMethodActions = SamplePetnamesControllerAssignPetnameAction;
|
|
22
|
+
//# sourceMappingURL=sample-petnames-controller-method-action-types.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sample-petnames-controller-method-action-types.d.cts","sourceRoot":"","sources":["../src/sample-petnames-controller-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,wBAAwB,EAAE,yCAAqC;AAE7E;;;;;;;GAOG;AACH,MAAM,MAAM,2CAA2C,GAAG;IACxD,IAAI,EAAE,wCAAwC,CAAC;IAC/C,OAAO,EAAE,wBAAwB,CAAC,eAAe,CAAC,CAAC;CACpD,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,qCAAqC,GAC/C,2CAA2C,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is auto generated by `scripts/generate-method-action-types.ts`.
|
|
3
|
+
* Do not edit manually.
|
|
4
|
+
*/
|
|
5
|
+
import type { SamplePetnamesController } from "./sample-petnames-controller.mjs";
|
|
6
|
+
/**
|
|
7
|
+
* Registers the given name with the given address (relative to the given
|
|
8
|
+
* chain).
|
|
9
|
+
*
|
|
10
|
+
* @param chainId - The chain ID that the address belongs to.
|
|
11
|
+
* @param address - The account address to name.
|
|
12
|
+
* @param name - The name to assign to the address.
|
|
13
|
+
*/
|
|
14
|
+
export type SamplePetnamesControllerAssignPetnameAction = {
|
|
15
|
+
type: `SamplePetnamesController:assignPetname`;
|
|
16
|
+
handler: SamplePetnamesController['assignPetname'];
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Union of all SamplePetnamesController action types.
|
|
20
|
+
*/
|
|
21
|
+
export type SamplePetnamesControllerMethodActions = SamplePetnamesControllerAssignPetnameAction;
|
|
22
|
+
//# sourceMappingURL=sample-petnames-controller-method-action-types.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sample-petnames-controller-method-action-types.d.mts","sourceRoot":"","sources":["../src/sample-petnames-controller-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,wBAAwB,EAAE,yCAAqC;AAE7E;;;;;;;GAOG;AACH,MAAM,MAAM,2CAA2C,GAAG;IACxD,IAAI,EAAE,wCAAwC,CAAC;IAC/C,OAAO,EAAE,wBAAwB,CAAC,eAAe,CAAC,CAAC;CACpD,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,qCAAqC,GAC/C,2CAA2C,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sample-petnames-controller-method-action-types.mjs","sourceRoot":"","sources":["../src/sample-petnames-controller-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated by `scripts/generate-method-action-types.ts`.\n * Do not edit manually.\n */\n\nimport type { SamplePetnamesController } from './sample-petnames-controller';\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 */\nexport type SamplePetnamesControllerAssignPetnameAction = {\n type: `SamplePetnamesController:assignPetname`;\n handler: SamplePetnamesController['assignPetname'];\n};\n\n/**\n * Union of all SamplePetnamesController action types.\n */\nexport type SamplePetnamesControllerMethodActions =\n SamplePetnamesControllerAssignPetnameAction;\n"]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SamplePetnamesController = exports.controllerName = void 0;
|
|
3
|
+
exports.SamplePetnamesController = exports.getDefaultPetnamesControllerState = exports.controllerName = void 0;
|
|
4
4
|
const base_controller_1 = require("@metamask/base-controller");
|
|
5
5
|
const controller_utils_1 = require("@metamask/controller-utils");
|
|
6
6
|
// === GENERAL ===
|
|
@@ -32,10 +32,13 @@ function getDefaultPetnamesControllerState() {
|
|
|
32
32
|
namesByChainIdAndAddress: {},
|
|
33
33
|
};
|
|
34
34
|
}
|
|
35
|
+
exports.getDefaultPetnamesControllerState = getDefaultPetnamesControllerState;
|
|
36
|
+
// === MESSENGER ===
|
|
37
|
+
const MESSENGER_EXPOSED_METHODS = ['assignPetname'];
|
|
35
38
|
// === CONTROLLER DEFINITION ===
|
|
36
39
|
/**
|
|
37
|
-
* `SamplePetnamesController` records user-provided nicknames for various
|
|
38
|
-
* various chains.
|
|
40
|
+
* `SamplePetnamesController` records user-provided nicknames for various
|
|
41
|
+
* addresses on various chains.
|
|
39
42
|
*
|
|
40
43
|
* @example
|
|
41
44
|
*
|
|
@@ -44,27 +47,34 @@ function getDefaultPetnamesControllerState() {
|
|
|
44
47
|
* import type {
|
|
45
48
|
* SamplePetnamesControllerActions,
|
|
46
49
|
* SamplePetnamesControllerEvents
|
|
47
|
-
* } from '@metamask/
|
|
50
|
+
* } from '@metamask/sample-controllers';
|
|
48
51
|
*
|
|
49
|
-
* const
|
|
52
|
+
* const globalMessenger = new Messenger<
|
|
50
53
|
* SamplePetnamesControllerActions,
|
|
51
54
|
* SamplePetnamesControllerEvents
|
|
52
55
|
* >();
|
|
53
|
-
* const samplePetnamesMessenger =
|
|
56
|
+
* const samplePetnamesMessenger = globalMessenger.getRestricted({
|
|
54
57
|
* name: 'SamplePetnamesController',
|
|
55
58
|
* allowedActions: [],
|
|
56
59
|
* allowedEvents: [],
|
|
57
60
|
* });
|
|
58
|
-
*
|
|
61
|
+
* // Instantiate the controller to register its actions on the messenger
|
|
62
|
+
* new SamplePetnamesController({
|
|
59
63
|
* messenger: samplePetnamesMessenger,
|
|
60
64
|
* });
|
|
61
65
|
*
|
|
62
|
-
*
|
|
63
|
-
* '
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
+
* globalMessenger.call(
|
|
67
|
+
* 'SamplePetnamesController:assignPetname',
|
|
68
|
+
* [
|
|
69
|
+
* '0x1',
|
|
70
|
+
* '0xF57F855e17483B1f09bFec62783C9d3b6c8b3A99',
|
|
71
|
+
* 'Primary Account',
|
|
72
|
+
* ],
|
|
66
73
|
* );
|
|
67
|
-
*
|
|
74
|
+
* const samplePetnamesControllerState = await globalMessenger.call(
|
|
75
|
+
* 'SamplePetnamesController:getState',
|
|
76
|
+
* );
|
|
77
|
+
* samplePetnamesControllerState.namesByChainIdAndAddress
|
|
68
78
|
* // => { '0x1': { '0xF57F855e17483B1f09bFec62783C9d3b6c8b3A99': 'Primary Account' } }
|
|
69
79
|
* ```
|
|
70
80
|
*/
|
|
@@ -72,7 +82,7 @@ class SamplePetnamesController extends base_controller_1.BaseController {
|
|
|
72
82
|
/**
|
|
73
83
|
* Constructs a new {@link SamplePetnamesController}.
|
|
74
84
|
*
|
|
75
|
-
* @param args - The arguments to
|
|
85
|
+
* @param args - The arguments to this controller.
|
|
76
86
|
* @param args.messenger - The messenger suited for this controller.
|
|
77
87
|
* @param args.state - The desired state with which to initialize this
|
|
78
88
|
* controller. Missing properties will be filled in with defaults.
|
|
@@ -87,6 +97,7 @@ class SamplePetnamesController extends base_controller_1.BaseController {
|
|
|
87
97
|
...state,
|
|
88
98
|
},
|
|
89
99
|
});
|
|
100
|
+
this.messagingSystem.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
|
|
90
101
|
}
|
|
91
102
|
/**
|
|
92
103
|
* Registers the given name with the given address (relative to the given
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sample-petnames-controller.cjs","sourceRoot":"","sources":["../src/sample-petnames-controller.ts"],"names":[],"mappings":";;;AAMA,+DAA2D;AAC3D,iEAA8D;
|
|
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"]}
|