@metamask-previews/sample-controllers 3.0.0-preview-d4351f75 → 3.0.0-preview-bf3ab7d

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.
@@ -1 +1 @@
1
- {"version":3,"file":"sample-gas-prices-service.cjs","sourceRoot":"","sources":["../../src/sample-gas-prices-service/sample-gas-prices-service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAIA,iEAIoC;AAEpC,2CAAuE;AAIvE,kBAAkB;AAElB;;;GAGG;AACU,QAAA,WAAW,GAAG,wBAAwB,CAAC;AAEpD,oBAAoB;AAEpB,MAAM,yBAAyB,GAAG,CAAC,gBAAgB,CAAU,CAAC;AAgD9D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,MAAa,sBAAsB;IA2BjC;;;;;;;;;;;OAWG;IACH,YAAY,EACV,SAAS,EACT,KAAK,EAAE,aAAa,EACpB,aAAa,GAAG,EAAE,GAKnB;QAzCD;;WAEG;QACM,oDAES;QAElB;;WAEG;QACM,gDAEK;QAEd;;;;WAIG;QACM,iDAAuB;QAuB9B,IAAI,CAAC,IAAI,GAAG,mBAAW,CAAC;QACxB,uBAAA,IAAI,qCAAc,SAAS,MAAA,CAAC;QAC5B,uBAAA,IAAI,iCAAU,aAAa,MAAA,CAAC;QAC5B,uBAAA,IAAI,kCAAW,IAAA,sCAAmB,EAAC,aAAa,CAAC,MAAA,CAAC;QAElD,uBAAA,IAAI,yCAAW,CAAC,4BAA4B,CAC1C,IAAI,EACJ,yBAAyB,CAC1B,CAAC;IACJ,CAAC;IAED;;;;;;;;;OASG;IACH,OAAO,CAAC,QAAiD;QACvD,OAAO,uBAAA,IAAI,sCAAQ,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IACxC,CAAC;IAED;;;;;;;;OAQG;IACH,OAAO,CAAC,QAAiD;QACvD,OAAO,uBAAA,IAAI,sCAAQ,CAAC,OAAO,CAAC,QAAQ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,MAAM,CAAC,EACtC,CAAC;YACD,MAAM,EACJ,IAAI,EAAE,EAAE,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,GAC7B,GAAG,YAAY,CAAC;YACjB,IACE,OAAO,GAAG,KAAK,QAAQ;gBACvB,OAAO,OAAO,KAAK,QAAQ;gBAC3B,OAAO,IAAI,KAAK,QAAQ,EACxB,CAAC;gBACD,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;YAChC,CAAC;QACH,CAAC;QAED,MAAM,IAAI,KAAK,CAAC,iDAAiD,CAAC,CAAC;IACrE,CAAC;CACF;AA5JD,wDA4JC","sourcesContent":["import type {\n CreateServicePolicyOptions,\n ServicePolicy,\n} from '@metamask/controller-utils';\nimport {\n createServicePolicy,\n fromHex,\n HttpError,\n} from '@metamask/controller-utils';\nimport type { Messenger } from '@metamask/messenger';\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 = Messenger<\n typeof serviceName,\n SampleGasPricesServiceActions | AllowedActions,\n // TODO: Disable this lint rule\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-arguments\n SampleGasPricesServiceEvents | AllowedEvents\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 { Messenger } from '@metamask/messenger';\n * import type {\n * SampleGasPricesServiceActions,\n * SampleGasPricesServiceEvents,\n * } from '@metamask/sample-controllers';\n *\n * const rootMessenger = new Messenger<\n * 'Root',\n * SampleGasPricesServiceActions\n * SampleGasPricesServiceEvents\n * >({ namespace: 'Root' });\n * const gasPricesServiceMessenger = new Messenger<\n * 'SampleGasPricesService',\n * SampleGasPricesServiceActions,\n * SampleGasPricesServiceEvents,\n * typeof rootMessenger,\n * >({\n * namespace: 'SampleGasPricesService',\n * parent: rootMessenger,\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 rootMessenger.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(\n 'chainId',\n `eip155:${fromHex(chainId).toString()}`,\n );\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"]}
1
+ {"version":3,"file":"sample-gas-prices-service.cjs","sourceRoot":"","sources":["../../src/sample-gas-prices-service/sample-gas-prices-service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAIA,iEAIoC;AAEpC,2CAAuE;AAIvE,kBAAkB;AAElB;;;GAGG;AACU,QAAA,WAAW,GAAG,wBAAwB,CAAC;AAEpD,oBAAoB;AAEpB,MAAM,yBAAyB,GAAG,CAAC,gBAAgB,CAAU,CAAC;AA8C9D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,MAAa,sBAAsB;IA2BjC;;;;;;;;;;;OAWG;IACH,YAAY,EACV,SAAS,EACT,KAAK,EAAE,aAAa,EACpB,aAAa,GAAG,EAAE,GAKnB;QAzCD;;WAEG;QACM,oDAES;QAElB;;WAEG;QACM,gDAEK;QAEd;;;;WAIG;QACM,i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B,aAAa,CAAC,MAAM,EACpB,aAAa,GAAG,CAAC,QAAQ,EAAE,yBAAyB,aAAa,CAAC,MAAM,GAAG,CAC5E,CAAC;YACJ,CAAC;YACD,OAAO,aAAa,CAAC;QACvB,CAAC,CAAC,CAAC;QACH,MAAM,YAAY,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;QAE3C,IACE,IAAA,qBAAa,EAAC,YAAY,CAAC;YAC3B,IAAA,mBAAW,EAAC,YAAY,EAAE,MAAM,CAAC;YACjC,IAAA,qBAAa,EAAC,YAAY,CAAC,IAAI,CAAC;YAChC,IAAA,mBAAW,EAAC,YAAY,CAAC,IAAI,EAAE,KAAK,CAAC;YACrC,IAAA,mBAAW,EAAC,YAAY,CAAC,IAAI,EAAE,SAAS,CAAC;YACzC,IAAA,mBAAW,EAAC,YAAY,CAAC,IAAI,EAAE,MAAM,CAAC,EACtC,CAAC;YACD,MAAM,EACJ,IAAI,EAAE,EAAE,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,GAC7B,GAAG,YAAY,CAAC;YACjB,IACE,OAAO,GAAG,KAAK,QAAQ;gBACvB,OAAO,OAAO,KAAK,QAAQ;gBAC3B,OAAO,IAAI,KAAK,QAAQ,EACxB,CAAC;gBACD,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;YAChC,CAAC;QACH,CAAC;QAED,MAAM,IAAI,KAAK,CAAC,iDAAiD,CAAC,CAAC;IACrE,CAAC;CACF;AA5JD,wDA4JC","sourcesContent":["import type {\n CreateServicePolicyOptions,\n ServicePolicy,\n} from '@metamask/controller-utils';\nimport {\n createServicePolicy,\n fromHex,\n HttpError,\n} from '@metamask/controller-utils';\nimport type { Messenger } from '@metamask/messenger';\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 = Messenger<\n typeof serviceName,\n SampleGasPricesServiceActions | AllowedActions,\n SampleGasPricesServiceEvents | AllowedEvents\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 { Messenger } from '@metamask/messenger';\n * import type {\n * SampleGasPricesServiceActions,\n * SampleGasPricesServiceEvents,\n * } from '@metamask/sample-controllers';\n *\n * const rootMessenger = new Messenger<\n * 'Root',\n * SampleGasPricesServiceActions\n * SampleGasPricesServiceEvents\n * >({ namespace: 'Root' });\n * const gasPricesServiceMessenger = new Messenger<\n * 'SampleGasPricesService',\n * SampleGasPricesServiceActions,\n * SampleGasPricesServiceEvents,\n * typeof rootMessenger,\n * >({\n * namespace: 'SampleGasPricesService',\n * parent: rootMessenger,\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 rootMessenger.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(\n 'chainId',\n `eip155:${fromHex(chainId).toString()}`,\n );\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"]}
@@ -1 +1 @@
1
- {"version":3,"file":"sample-gas-prices-service.d.cts","sourceRoot":"","sources":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AAC,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;CAwCvE"}
1
+ {"version":3,"file":"sample-gas-prices-service.d.cts","sourceRoot":"","sources":["../../src/sample-gas-prices-service/sample-gas-prices-service.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,0BAA0B,EAC1B,aAAa,EACd,mCAAmC;AAMpC,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,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,SAAS,CACrD,OAAO,WAAW,EAClB,6BAA6B,GAAG,cAAc,EAC9C,4BAA4B,GAAG,aAAa,CAC7C,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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;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;CAwCvE"}
@@ -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,EACV,0BAA0B,EAC1B,aAAa,EACd,mCAAmC;AAMpC,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,OAAO,EAA8B,KAAK,GAAG,EAAE,wBAAwB;AAEvE,OAAO,KAAK,EAAE,mCAAmC,EAAE,4DAAwD;AAI3G;;;GAGG;AACH,eAAO,MAAM,WAAW,2BAA2B,CAAC;AAMpD;;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ACG,cAAc,CAAC,OAAO,EAAE,GAAG,GAAG,OAAO,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC;CAwCvE"}
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,EACV,0BAA0B,EAC1B,aAAa,EACd,mCAAmC;AAMpC,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,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,SAAS,CACrD,OAAO,WAAW,EAClB,6BAA6B,GAAG,cAAc,EAC9C,4BAA4B,GAAG,aAAa,CAC7C,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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;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;CAwCvE"}
@@ -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":";;;;;;;;;;;;AAIA,OAAO,EACL,mBAAmB,EACnB,OAAO,EACP,SAAS,EACV,mCAAmC;AAEpC,OAAO,EAAE,WAAW,EAAE,aAAa,EAAY,wBAAwB;AAIvE,kBAAkB;AAElB;;;GAGG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,wBAAwB,CAAC;AAEpD,oBAAoB;AAEpB,MAAM,yBAAyB,GAAG,CAAC,gBAAgB,CAAU,CAAC;AAgD9D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,MAAM,OAAO,sBAAsB;IA2BjC;;;;;;;;;;;OAWG;IACH,YAAY,EACV,SAAS,EACT,KAAK,EAAE,aAAa,EACpB,aAAa,GAAG,EAAE,GAKnB;QAzCD;;WAEG;QACM,oDAES;QAElB;;WAEG;QACM,gDAEK;QAEd;;;;WAIG;QACM,iDAAuB;QAuB9B,IAAI,CAAC,IAA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AQ,EAAE,yBAAyB,aAAa,CAAC,MAAM,GAAG,CAC5E,CAAC;YACJ,CAAC;YACD,OAAO,aAAa,CAAC;QACvB,CAAC,CAAC,CAAC;QACH,MAAM,YAAY,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;QAE3C,IACE,aAAa,CAAC,YAAY,CAAC;YAC3B,WAAW,CAAC,YAAY,EAAE,MAAM,CAAC;YACjC,aAAa,CAAC,YAAY,CAAC,IAAI,CAAC;YAChC,WAAW,CAAC,YAAY,CAAC,IAAI,EAAE,KAAK,CAAC;YACrC,WAAW,CAAC,YAAY,CAAC,IAAI,EAAE,SAAS,CAAC;YACzC,WAAW,CAAC,YAAY,CAAC,IAAI,EAAE,MAAM,CAAC,EACtC,CAAC;YACD,MAAM,EACJ,IAAI,EAAE,EAAE,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,GAC7B,GAAG,YAAY,CAAC;YACjB,IACE,OAAO,GAAG,KAAK,QAAQ;gBACvB,OAAO,OAAO,KAAK,QAAQ;gBAC3B,OAAO,IAAI,KAAK,QAAQ,EACxB,CAAC;gBACD,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;YAChC,CAAC;QACH,CAAC;QAED,MAAM,IAAI,KAAK,CAAC,iDAAiD,CAAC,CAAC;IACrE,CAAC;CACF","sourcesContent":["import type {\n CreateServicePolicyOptions,\n ServicePolicy,\n} from '@metamask/controller-utils';\nimport {\n createServicePolicy,\n fromHex,\n HttpError,\n} from '@metamask/controller-utils';\nimport type { Messenger } from '@metamask/messenger';\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 = Messenger<\n typeof serviceName,\n SampleGasPricesServiceActions | AllowedActions,\n // TODO: Disable this lint rule\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-arguments\n SampleGasPricesServiceEvents | AllowedEvents\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 { Messenger } from '@metamask/messenger';\n * import type {\n * SampleGasPricesServiceActions,\n * SampleGasPricesServiceEvents,\n * } from '@metamask/sample-controllers';\n *\n * const rootMessenger = new Messenger<\n * 'Root',\n * SampleGasPricesServiceActions\n * SampleGasPricesServiceEvents\n * >({ namespace: 'Root' });\n * const gasPricesServiceMessenger = new Messenger<\n * 'SampleGasPricesService',\n * SampleGasPricesServiceActions,\n * SampleGasPricesServiceEvents,\n * typeof rootMessenger,\n * >({\n * namespace: 'SampleGasPricesService',\n * parent: rootMessenger,\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 rootMessenger.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(\n 'chainId',\n `eip155:${fromHex(chainId).toString()}`,\n );\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"]}
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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xB,CAAC;gBACD,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;YAChC,CAAC;QACH,CAAC;QAED,MAAM,IAAI,KAAK,CAAC,iDAAiD,CAAC,CAAC;IACrE,CAAC;CACF","sourcesContent":["import type {\n CreateServicePolicyOptions,\n ServicePolicy,\n} from '@metamask/controller-utils';\nimport {\n createServicePolicy,\n fromHex,\n HttpError,\n} from '@metamask/controller-utils';\nimport type { Messenger } from '@metamask/messenger';\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 = Messenger<\n typeof serviceName,\n SampleGasPricesServiceActions | AllowedActions,\n SampleGasPricesServiceEvents | AllowedEvents\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 { Messenger } from '@metamask/messenger';\n * import type {\n * SampleGasPricesServiceActions,\n * SampleGasPricesServiceEvents,\n * } from '@metamask/sample-controllers';\n *\n * const rootMessenger = new Messenger<\n * 'Root',\n * SampleGasPricesServiceActions\n * SampleGasPricesServiceEvents\n * >({ namespace: 'Root' });\n * const gasPricesServiceMessenger = new Messenger<\n * 'SampleGasPricesService',\n * SampleGasPricesServiceActions,\n * SampleGasPricesServiceEvents,\n * typeof rootMessenger,\n * >({\n * namespace: 'SampleGasPricesService',\n * parent: rootMessenger,\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 rootMessenger.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(\n 'chainId',\n `eip155:${fromHex(chainId).toString()}`,\n );\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"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask-previews/sample-controllers",
3
- "version": "3.0.0-preview-d4351f75",
3
+ "version": "3.0.0-preview-bf3ab7d",
4
4
  "description": "Sample package to illustrate best practices for controllers",
5
5
  "keywords": [
6
6
  "MetaMask",