@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.
- package/CHANGELOG.md +10 -0
- package/LICENSE +20 -0
- package/README.md +15 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/dist/types/gas-prices-controller.cjs +133 -0
- package/dist/types/gas-prices-controller.cjs.map +1 -0
- package/dist/types/gas-prices-controller.d.cts +157 -0
- package/dist/types/gas-prices-controller.d.cts.map +1 -0
- package/dist/types/gas-prices-controller.d.mts +157 -0
- package/dist/types/gas-prices-controller.d.mts.map +1 -0
- package/dist/types/gas-prices-controller.mjs +128 -0
- package/dist/types/gas-prices-controller.mjs.map +1 -0
- package/dist/types/gas-prices-service/abstract-gas-prices-service.cjs +3 -0
- package/dist/types/gas-prices-service/abstract-gas-prices-service.cjs.map +1 -0
- package/dist/types/gas-prices-service/abstract-gas-prices-service.d.cts +7 -0
- package/dist/types/gas-prices-service/abstract-gas-prices-service.d.cts.map +1 -0
- package/dist/types/gas-prices-service/abstract-gas-prices-service.d.mts +7 -0
- package/dist/types/gas-prices-service/abstract-gas-prices-service.d.mts.map +1 -0
- package/dist/types/gas-prices-service/abstract-gas-prices-service.mjs +2 -0
- package/dist/types/gas-prices-service/abstract-gas-prices-service.mjs.map +1 -0
- package/dist/types/gas-prices-service/gas-prices-service.cjs +72 -0
- package/dist/types/gas-prices-service/gas-prices-service.cjs.map +1 -0
- package/dist/types/gas-prices-service/gas-prices-service.d.cts +55 -0
- package/dist/types/gas-prices-service/gas-prices-service.d.cts.map +1 -0
- package/dist/types/gas-prices-service/gas-prices-service.d.mts +55 -0
- package/dist/types/gas-prices-service/gas-prices-service.d.mts.map +1 -0
- package/dist/types/gas-prices-service/gas-prices-service.mjs +68 -0
- package/dist/types/gas-prices-service/gas-prices-service.mjs.map +1 -0
- package/dist/types/index.cjs +12 -0
- package/dist/types/index.cjs.map +1 -0
- package/dist/types/index.d.cts +6 -0
- package/dist/types/index.d.cts.map +1 -0
- package/dist/types/index.d.mts +6 -0
- package/dist/types/index.d.mts.map +1 -0
- package/dist/types/index.mjs +4 -0
- package/dist/types/index.mjs.map +1 -0
- package/dist/types/network-controller-types.cjs +20 -0
- package/dist/types/network-controller-types.cjs.map +1 -0
- package/dist/types/network-controller-types.d.cts +34 -0
- package/dist/types/network-controller-types.d.cts.map +1 -0
- package/dist/types/network-controller-types.d.mts +34 -0
- package/dist/types/network-controller-types.d.mts.map +1 -0
- package/dist/types/network-controller-types.mjs +16 -0
- package/dist/types/network-controller-types.mjs.map +1 -0
- package/dist/types/pet-names-controller.cjs +113 -0
- package/dist/types/pet-names-controller.cjs.map +1 -0
- package/dist/types/pet-names-controller.d.cts +124 -0
- package/dist/types/pet-names-controller.d.cts.map +1 -0
- package/dist/types/pet-names-controller.d.mts +124 -0
- package/dist/types/pet-names-controller.d.mts.map +1 -0
- package/dist/types/pet-names-controller.mjs +108 -0
- package/dist/types/pet-names-controller.mjs.map +1 -0
- package/package.json +72 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
[Unreleased]: https://github.com/MetaMask/core/
|
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 MetaMask
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
package/README.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# `@metamask/sample-controllers`
|
|
2
|
+
|
|
3
|
+
This package is designed to illustrate best practices for controller packages and controller files, including tests.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
`yarn add @metamask/sample-controllers`
|
|
8
|
+
|
|
9
|
+
or
|
|
10
|
+
|
|
11
|
+
`npm install @metamask/sample-controllers`
|
|
12
|
+
|
|
13
|
+
## Contributing
|
|
14
|
+
|
|
15
|
+
This package is part of a monorepo. Instructions for contributing can be found in the [monorepo README](https://github.com/MetaMask/core#readme).
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":"5.2.2"}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
5
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
6
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
7
|
+
};
|
|
8
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
10
|
+
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");
|
|
11
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
|
+
};
|
|
13
|
+
var _GasPricesController_gasPricesService;
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.GasPricesController = exports.getDefaultGasPricesControllerState = exports.controllerName = void 0;
|
|
16
|
+
const base_controller_1 = require("@metamask/base-controller");
|
|
17
|
+
// === GENERAL ===
|
|
18
|
+
/**
|
|
19
|
+
* The name of the {@link GasPricesController}, used to namespace the
|
|
20
|
+
* controller's actions and events and to namespace the controller's state data
|
|
21
|
+
* when composed with other controllers.
|
|
22
|
+
*/
|
|
23
|
+
exports.controllerName = 'GasPricesController';
|
|
24
|
+
/**
|
|
25
|
+
* The metadata for each property in {@link GasPricesControllerState}.
|
|
26
|
+
*/
|
|
27
|
+
const gasPricesControllerMetadata = {
|
|
28
|
+
gasPricesByChainId: {
|
|
29
|
+
persist: true,
|
|
30
|
+
anonymous: false,
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Constructs the default {@link GasPricesController} state. This allows
|
|
35
|
+
* consumers to provide a partial state object when initializing the controller
|
|
36
|
+
* and also helps in constructing complete state objects for this controller in
|
|
37
|
+
* tests.
|
|
38
|
+
*
|
|
39
|
+
* @returns The default {@link GasPricesController} state.
|
|
40
|
+
*/
|
|
41
|
+
function getDefaultGasPricesControllerState() {
|
|
42
|
+
return {
|
|
43
|
+
gasPricesByChainId: {},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
exports.getDefaultGasPricesControllerState = getDefaultGasPricesControllerState;
|
|
47
|
+
// === CONTROLLER DEFINITION ===
|
|
48
|
+
/**
|
|
49
|
+
* `GasPricesController` fetches and persists gas prices for various chains.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
*
|
|
53
|
+
* ``` ts
|
|
54
|
+
* import { Messenger } from '@metamask/base-controller';
|
|
55
|
+
* import {
|
|
56
|
+
* GasPricesController,
|
|
57
|
+
* GasPricesService
|
|
58
|
+
* } from '@metamask/example-controllers';
|
|
59
|
+
* import type {
|
|
60
|
+
* GasPricesControllerActions,
|
|
61
|
+
* GasPricesControllerEvents
|
|
62
|
+
* } from '@metamask/example-controllers';
|
|
63
|
+
* import type { NetworkControllerGetStateAction } from '@metamask/network-controller';
|
|
64
|
+
*
|
|
65
|
+
* // Assuming that you're using this in the browser
|
|
66
|
+
* const gasPricesService = new GasPricesService({ fetch });
|
|
67
|
+
* const rootMessenger = new Messenger<
|
|
68
|
+
* GasPricesControllerActions | NetworkControllerGetStateAction,
|
|
69
|
+
* GasPricesControllerEvents
|
|
70
|
+
* >();
|
|
71
|
+
* const gasPricesMessenger = rootMessenger.getRestricted({
|
|
72
|
+
* name: 'GasPricesController',
|
|
73
|
+
* allowedActions: ['NetworkController:getState'],
|
|
74
|
+
* allowedEvents: [],
|
|
75
|
+
* });
|
|
76
|
+
* const gasPricesController = new GasPricesController({
|
|
77
|
+
* messenger: gasPricesMessenger,
|
|
78
|
+
* gasPricesService,
|
|
79
|
+
* });
|
|
80
|
+
*
|
|
81
|
+
* // Assuming that `NetworkController:getState` returns an object with a
|
|
82
|
+
* // `chainId` of `0x42`...
|
|
83
|
+
* await gasPricesController.updateGasPrices();
|
|
84
|
+
* gasPricesController.state.gasPricesByChainId
|
|
85
|
+
* // => { '0x42': { low: 5, average: 10, high: 15, fetchedDate: '2024-01-02T00:00:00.000Z' } }
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
class GasPricesController extends base_controller_1.BaseController {
|
|
89
|
+
/**
|
|
90
|
+
* Constructs a new {@link GasPricesController}.
|
|
91
|
+
*
|
|
92
|
+
* @param args - The arguments to the controller.
|
|
93
|
+
* @param args.messenger - The messenger suited for this controller.
|
|
94
|
+
* @param args.state - The desired state with which to initialize this
|
|
95
|
+
* controller. Missing properties will be filled in with defaults.
|
|
96
|
+
* @param args.gasPricesService - The service object that will be used to
|
|
97
|
+
* obtain gas prices.
|
|
98
|
+
*/
|
|
99
|
+
constructor({ messenger, state, gasPricesService, }) {
|
|
100
|
+
super({
|
|
101
|
+
messenger,
|
|
102
|
+
metadata: gasPricesControllerMetadata,
|
|
103
|
+
name: exports.controllerName,
|
|
104
|
+
state: {
|
|
105
|
+
...getDefaultGasPricesControllerState(),
|
|
106
|
+
...state,
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
/**
|
|
110
|
+
* The service object that is used to obtain gas prices.
|
|
111
|
+
*/
|
|
112
|
+
_GasPricesController_gasPricesService.set(this, void 0);
|
|
113
|
+
__classPrivateFieldSet(this, _GasPricesController_gasPricesService, gasPricesService, "f");
|
|
114
|
+
this.messagingSystem.registerActionHandler(`${exports.controllerName}:updateGasPrices`, this.updateGasPrices.bind(this));
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Fetches the latest gas prices for the current chain, persisting them to
|
|
118
|
+
* state.
|
|
119
|
+
*/
|
|
120
|
+
async updateGasPrices() {
|
|
121
|
+
const { chainId } = this.messagingSystem.call('NetworkController:getState');
|
|
122
|
+
const gasPricesResponse = await __classPrivateFieldGet(this, _GasPricesController_gasPricesService, "f").fetchGasPrices(chainId);
|
|
123
|
+
this.update((state) => {
|
|
124
|
+
state.gasPricesByChainId[chainId] = {
|
|
125
|
+
...gasPricesResponse,
|
|
126
|
+
fetchedDate: new Date().toISOString(),
|
|
127
|
+
};
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
exports.GasPricesController = GasPricesController;
|
|
132
|
+
_GasPricesController_gasPricesService = new WeakMap();
|
|
133
|
+
//# sourceMappingURL=gas-prices-controller.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gas-prices-controller.cjs","sourceRoot":"","sources":["../../src/gas-prices-controller.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAMA,+DAA2D;AAM3D,kBAAkB;AAElB;;;;GAIG;AACU,QAAA,cAAc,GAAG,qBAAqB,CAAC;AAuCpD;;GAEG;AACH,MAAM,2BAA2B,GAAG;IAClC,kBAAkB,EAAE;QAClB,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,KAAK;KACjB;CACgD,CAAC;AAiEpD;;;;;;;GAOG;AACH,SAAgB,kCAAkC;IAChD,OAAO;QACL,kBAAkB,EAAE,EAAE;KACvB,CAAC;AACJ,CAAC;AAJD,gFAIC;AAED,gCAAgC;AAEhC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,MAAa,mBAAoB,SAAQ,gCAIxC;IAMC;;;;;;;;;OASG;IACH,YAAY,EACV,SAAS,EACT,KAAK,EACL,gBAAgB,GAKjB;QACC,KAAK,CAAC;YACJ,SAAS;YACT,QAAQ,EAAE,2BAA2B;YACrC,IAAI,EAAE,sBAAc;YACpB,KAAK,EAAE;gBACL,GAAG,kCAAkC,EAAE;gBACvC,GAAG,KAAK;aACT;SACF,CAAC,CAAC;QAhCL;;WAEG;QACM,wDAA4C;QA+BnD,uBAAA,IAAI,yCAAqB,gBAAgB,MAAA,CAAC;QAE1C,IAAI,CAAC,eAAe,CAAC,qBAAqB,CACxC,GAAG,sBAAc,kBAAkB,EACnC,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,IAAI,CAAC,CAChC,CAAC;IACJ,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,eAAe;QACnB,MAAM,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,4BAA4B,CAAC,CAAC;QAC5E,MAAM,iBAAiB,GACrB,MAAM,uBAAA,IAAI,6CAAkB,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;QACvD,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,kBAAkB,CAAC,OAAO,CAAC,GAAG;gBAClC,GAAG,iBAAiB;gBACpB,WAAW,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;aACtC,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AA9DD,kDA8DC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n RestrictedMessenger,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { Hex } from '@metamask/utils';\n\nimport type { AbstractGasPricesService } from './gas-prices-service/abstract-gas-prices-service';\nimport type { NetworkControllerGetStateAction } from './network-controller-types';\n\n// === GENERAL ===\n\n/**\n * The name of the {@link GasPricesController}, 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 = 'GasPricesController';\n\n// === STATE ===\n\n/**\n * The collection of gas price data fetched periodically.\n */\ntype GasPrices = {\n /**\n * The total estimated gas in the \"low\" bucket.\n */\n low: number;\n /**\n * The total estimated gas in the \"average\" bucket.\n */\n average: number;\n /**\n * The total estimated gas in the \"high\" bucket.\n */\n high: number;\n /**\n * The date/time (in ISO-8601 format) when prices were fetched.\n */\n fetchedDate: string;\n};\n\n/**\n * Describes the shape of the state object for {@link GasPricesController}.\n */\nexport type GasPricesControllerState = {\n /**\n * The registry of pet names, categorized by chain ID first and address\n * second.\n */\n gasPricesByChainId: {\n [chainId: Hex]: GasPrices;\n };\n};\n\n/**\n * The metadata for each property in {@link GasPricesControllerState}.\n */\nconst gasPricesControllerMetadata = {\n gasPricesByChainId: {\n persist: true,\n anonymous: false,\n },\n} satisfies StateMetadata<GasPricesControllerState>;\n\n// === MESSENGER ===\n\n/**\n * The action which can be used to retrieve the state of the\n * {@link GasPricesController}.\n */\nexport type GasPricesControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n GasPricesControllerState\n>;\n\n/**\n * The action which can be used to update gas prices.\n */\nexport type GasPricesControllerUpdateGasPricesAction = {\n type: `${typeof controllerName}:updateGasPrices`;\n handler: GasPricesController['updateGasPrices'];\n};\n\n/**\n * All actions that {@link GasPricesController} registers, to be called\n * externally.\n */\nexport type GasPricesControllerActions =\n | GasPricesControllerGetStateAction\n | GasPricesControllerUpdateGasPricesAction;\n\n/**\n * All actions that {@link GasPricesController} calls internally.\n */\ntype AllowedActions = NetworkControllerGetStateAction;\n\n/**\n * The event that {@link GasPricesController} publishes when updating state.\n */\nexport type GasPricesControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n GasPricesControllerState\n>;\n\n/**\n * All events that {@link GasPricesController} publishes, to be subscribed to\n * externally.\n */\nexport type GasPricesControllerEvents = GasPricesControllerStateChangeEvent;\n\n/**\n * All events that {@link GasPricesController} subscribes to internally.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger which is restricted to actions and events accessed by\n * {@link GasPricesController}.\n */\nexport type GasPricesControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n GasPricesControllerActions | AllowedActions,\n GasPricesControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\n/**\n * Constructs the default {@link GasPricesController} 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 GasPricesController} state.\n */\nexport function getDefaultGasPricesControllerState(): GasPricesControllerState {\n return {\n gasPricesByChainId: {},\n };\n}\n\n// === CONTROLLER DEFINITION ===\n\n/**\n * `GasPricesController` fetches and persists gas prices for various chains.\n *\n * @example\n *\n * ``` ts\n * import { Messenger } from '@metamask/base-controller';\n * import {\n * GasPricesController,\n * GasPricesService\n * } from '@metamask/example-controllers';\n * import type {\n * GasPricesControllerActions,\n * GasPricesControllerEvents\n * } from '@metamask/example-controllers';\n * import type { NetworkControllerGetStateAction } from '@metamask/network-controller';\n *\n * // Assuming that you're using this in the browser\n * const gasPricesService = new GasPricesService({ fetch });\n * const rootMessenger = new Messenger<\n * GasPricesControllerActions | NetworkControllerGetStateAction,\n * GasPricesControllerEvents\n * >();\n * const gasPricesMessenger = rootMessenger.getRestricted({\n * name: 'GasPricesController',\n * allowedActions: ['NetworkController:getState'],\n * allowedEvents: [],\n * });\n * const gasPricesController = new GasPricesController({\n * messenger: gasPricesMessenger,\n * gasPricesService,\n * });\n *\n * // Assuming that `NetworkController:getState` returns an object with a\n * // `chainId` of `0x42`...\n * await gasPricesController.updateGasPrices();\n * gasPricesController.state.gasPricesByChainId\n * // => { '0x42': { low: 5, average: 10, high: 15, fetchedDate: '2024-01-02T00:00:00.000Z' } }\n * ```\n */\nexport class GasPricesController extends BaseController<\n typeof controllerName,\n GasPricesControllerState,\n GasPricesControllerMessenger\n> {\n /**\n * The service object that is used to obtain gas prices.\n */\n readonly #gasPricesService: AbstractGasPricesService;\n\n /**\n * Constructs a new {@link GasPricesController}.\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 * @param args.gasPricesService - The service object that will be used to\n * obtain gas prices.\n */\n constructor({\n messenger,\n state,\n gasPricesService,\n }: {\n messenger: GasPricesControllerMessenger;\n state?: Partial<GasPricesControllerState>;\n gasPricesService: AbstractGasPricesService;\n }) {\n super({\n messenger,\n metadata: gasPricesControllerMetadata,\n name: controllerName,\n state: {\n ...getDefaultGasPricesControllerState(),\n ...state,\n },\n });\n\n this.#gasPricesService = gasPricesService;\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:updateGasPrices`,\n this.updateGasPrices.bind(this),\n );\n }\n\n /**\n * Fetches the latest gas prices for the current chain, persisting them to\n * state.\n */\n async updateGasPrices() {\n const { chainId } = this.messagingSystem.call('NetworkController:getState');\n const gasPricesResponse =\n await this.#gasPricesService.fetchGasPrices(chainId);\n this.update((state) => {\n state.gasPricesByChainId[chainId] = {\n ...gasPricesResponse,\n fetchedDate: new Date().toISOString(),\n };\n });\n }\n}\n"]}
|
|
@@ -0,0 +1,157 @@
|
|
|
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
|
+
import type { AbstractGasPricesService } from "./gas-prices-service/abstract-gas-prices-service.cjs";
|
|
5
|
+
import type { NetworkControllerGetStateAction } from "./network-controller-types.cjs";
|
|
6
|
+
/**
|
|
7
|
+
* The name of the {@link GasPricesController}, used to namespace the
|
|
8
|
+
* controller's actions and events and to namespace the controller's state data
|
|
9
|
+
* when composed with other controllers.
|
|
10
|
+
*/
|
|
11
|
+
export declare const controllerName = "GasPricesController";
|
|
12
|
+
/**
|
|
13
|
+
* The collection of gas price data fetched periodically.
|
|
14
|
+
*/
|
|
15
|
+
type GasPrices = {
|
|
16
|
+
/**
|
|
17
|
+
* The total estimated gas in the "low" bucket.
|
|
18
|
+
*/
|
|
19
|
+
low: number;
|
|
20
|
+
/**
|
|
21
|
+
* The total estimated gas in the "average" bucket.
|
|
22
|
+
*/
|
|
23
|
+
average: number;
|
|
24
|
+
/**
|
|
25
|
+
* The total estimated gas in the "high" bucket.
|
|
26
|
+
*/
|
|
27
|
+
high: number;
|
|
28
|
+
/**
|
|
29
|
+
* The date/time (in ISO-8601 format) when prices were fetched.
|
|
30
|
+
*/
|
|
31
|
+
fetchedDate: string;
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Describes the shape of the state object for {@link GasPricesController}.
|
|
35
|
+
*/
|
|
36
|
+
export type GasPricesControllerState = {
|
|
37
|
+
/**
|
|
38
|
+
* The registry of pet names, categorized by chain ID first and address
|
|
39
|
+
* second.
|
|
40
|
+
*/
|
|
41
|
+
gasPricesByChainId: {
|
|
42
|
+
[chainId: Hex]: GasPrices;
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* The action which can be used to retrieve the state of the
|
|
47
|
+
* {@link GasPricesController}.
|
|
48
|
+
*/
|
|
49
|
+
export type GasPricesControllerGetStateAction = ControllerGetStateAction<typeof controllerName, GasPricesControllerState>;
|
|
50
|
+
/**
|
|
51
|
+
* The action which can be used to update gas prices.
|
|
52
|
+
*/
|
|
53
|
+
export type GasPricesControllerUpdateGasPricesAction = {
|
|
54
|
+
type: `${typeof controllerName}:updateGasPrices`;
|
|
55
|
+
handler: GasPricesController['updateGasPrices'];
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* All actions that {@link GasPricesController} registers, to be called
|
|
59
|
+
* externally.
|
|
60
|
+
*/
|
|
61
|
+
export type GasPricesControllerActions = GasPricesControllerGetStateAction | GasPricesControllerUpdateGasPricesAction;
|
|
62
|
+
/**
|
|
63
|
+
* All actions that {@link GasPricesController} calls internally.
|
|
64
|
+
*/
|
|
65
|
+
type AllowedActions = NetworkControllerGetStateAction;
|
|
66
|
+
/**
|
|
67
|
+
* The event that {@link GasPricesController} publishes when updating state.
|
|
68
|
+
*/
|
|
69
|
+
export type GasPricesControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, GasPricesControllerState>;
|
|
70
|
+
/**
|
|
71
|
+
* All events that {@link GasPricesController} publishes, to be subscribed to
|
|
72
|
+
* externally.
|
|
73
|
+
*/
|
|
74
|
+
export type GasPricesControllerEvents = GasPricesControllerStateChangeEvent;
|
|
75
|
+
/**
|
|
76
|
+
* All events that {@link GasPricesController} subscribes to internally.
|
|
77
|
+
*/
|
|
78
|
+
type AllowedEvents = never;
|
|
79
|
+
/**
|
|
80
|
+
* The messenger which is restricted to actions and events accessed by
|
|
81
|
+
* {@link GasPricesController}.
|
|
82
|
+
*/
|
|
83
|
+
export type GasPricesControllerMessenger = RestrictedMessenger<typeof controllerName, GasPricesControllerActions | AllowedActions, GasPricesControllerEvents | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
|
|
84
|
+
/**
|
|
85
|
+
* Constructs the default {@link GasPricesController} state. This allows
|
|
86
|
+
* consumers to provide a partial state object when initializing the controller
|
|
87
|
+
* and also helps in constructing complete state objects for this controller in
|
|
88
|
+
* tests.
|
|
89
|
+
*
|
|
90
|
+
* @returns The default {@link GasPricesController} state.
|
|
91
|
+
*/
|
|
92
|
+
export declare function getDefaultGasPricesControllerState(): GasPricesControllerState;
|
|
93
|
+
/**
|
|
94
|
+
* `GasPricesController` fetches and persists gas prices for various chains.
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
*
|
|
98
|
+
* ``` ts
|
|
99
|
+
* import { Messenger } from '@metamask/base-controller';
|
|
100
|
+
* import {
|
|
101
|
+
* GasPricesController,
|
|
102
|
+
* GasPricesService
|
|
103
|
+
* } from '@metamask/example-controllers';
|
|
104
|
+
* import type {
|
|
105
|
+
* GasPricesControllerActions,
|
|
106
|
+
* GasPricesControllerEvents
|
|
107
|
+
* } from '@metamask/example-controllers';
|
|
108
|
+
* import type { NetworkControllerGetStateAction } from '@metamask/network-controller';
|
|
109
|
+
*
|
|
110
|
+
* // Assuming that you're using this in the browser
|
|
111
|
+
* const gasPricesService = new GasPricesService({ fetch });
|
|
112
|
+
* const rootMessenger = new Messenger<
|
|
113
|
+
* GasPricesControllerActions | NetworkControllerGetStateAction,
|
|
114
|
+
* GasPricesControllerEvents
|
|
115
|
+
* >();
|
|
116
|
+
* const gasPricesMessenger = rootMessenger.getRestricted({
|
|
117
|
+
* name: 'GasPricesController',
|
|
118
|
+
* allowedActions: ['NetworkController:getState'],
|
|
119
|
+
* allowedEvents: [],
|
|
120
|
+
* });
|
|
121
|
+
* const gasPricesController = new GasPricesController({
|
|
122
|
+
* messenger: gasPricesMessenger,
|
|
123
|
+
* gasPricesService,
|
|
124
|
+
* });
|
|
125
|
+
*
|
|
126
|
+
* // Assuming that `NetworkController:getState` returns an object with a
|
|
127
|
+
* // `chainId` of `0x42`...
|
|
128
|
+
* await gasPricesController.updateGasPrices();
|
|
129
|
+
* gasPricesController.state.gasPricesByChainId
|
|
130
|
+
* // => { '0x42': { low: 5, average: 10, high: 15, fetchedDate: '2024-01-02T00:00:00.000Z' } }
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
133
|
+
export declare class GasPricesController extends BaseController<typeof controllerName, GasPricesControllerState, GasPricesControllerMessenger> {
|
|
134
|
+
#private;
|
|
135
|
+
/**
|
|
136
|
+
* Constructs a new {@link GasPricesController}.
|
|
137
|
+
*
|
|
138
|
+
* @param args - The arguments to the controller.
|
|
139
|
+
* @param args.messenger - The messenger suited for this controller.
|
|
140
|
+
* @param args.state - The desired state with which to initialize this
|
|
141
|
+
* controller. Missing properties will be filled in with defaults.
|
|
142
|
+
* @param args.gasPricesService - The service object that will be used to
|
|
143
|
+
* obtain gas prices.
|
|
144
|
+
*/
|
|
145
|
+
constructor({ messenger, state, gasPricesService, }: {
|
|
146
|
+
messenger: GasPricesControllerMessenger;
|
|
147
|
+
state?: Partial<GasPricesControllerState>;
|
|
148
|
+
gasPricesService: AbstractGasPricesService;
|
|
149
|
+
});
|
|
150
|
+
/**
|
|
151
|
+
* Fetches the latest gas prices for the current chain, persisting them to
|
|
152
|
+
* state.
|
|
153
|
+
*/
|
|
154
|
+
updateGasPrices(): Promise<void>;
|
|
155
|
+
}
|
|
156
|
+
export {};
|
|
157
|
+
//# sourceMappingURL=gas-prices-controller.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gas-prices-controller.d.cts","sourceRoot":"","sources":["../../src/gas-prices-controller.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAC1B,mBAAmB,EAEpB,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAE3C,OAAO,KAAK,EAAE,wBAAwB,EAAE,6DAAyD;AACjG,OAAO,KAAK,EAAE,+BAA+B,EAAE,uCAAmC;AAIlF;;;;GAIG;AACH,eAAO,MAAM,cAAc,wBAAwB,CAAC;AAIpD;;GAEG;AACH,KAAK,SAAS,GAAG;IACf;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;IACZ;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;IAChB;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IACb;;OAEG;IACH,WAAW,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,wBAAwB,GAAG;IACrC;;;OAGG;IACH,kBAAkB,EAAE;QAClB,CAAC,OAAO,EAAE,GAAG,GAAG,SAAS,CAAC;KAC3B,CAAC;CACH,CAAC;AAcF;;;GAGG;AACH,MAAM,MAAM,iCAAiC,GAAG,wBAAwB,CACtE,OAAO,cAAc,EACrB,wBAAwB,CACzB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,wCAAwC,GAAG;IACrD,IAAI,EAAE,GAAG,OAAO,cAAc,kBAAkB,CAAC;IACjD,OAAO,EAAE,mBAAmB,CAAC,iBAAiB,CAAC,CAAC;CACjD,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,0BAA0B,GAClC,iCAAiC,GACjC,wCAAwC,CAAC;AAE7C;;GAEG;AACH,KAAK,cAAc,GAAG,+BAA+B,CAAC;AAEtD;;GAEG;AACH,MAAM,MAAM,mCAAmC,GAAG,0BAA0B,CAC1E,OAAO,cAAc,EACrB,wBAAwB,CACzB,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,yBAAyB,GAAG,mCAAmC,CAAC;AAE5E;;GAEG;AACH,KAAK,aAAa,GAAG,KAAK,CAAC;AAE3B;;;GAGG;AACH,MAAM,MAAM,4BAA4B,GAAG,mBAAmB,CAC5D,OAAO,cAAc,EACrB,0BAA0B,GAAG,cAAc,EAC3C,yBAAyB,GAAG,aAAa,EACzC,cAAc,CAAC,MAAM,CAAC,EACtB,aAAa,CAAC,MAAM,CAAC,CACtB,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAgB,kCAAkC,IAAI,wBAAwB,CAI7E;AAID;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,qBAAa,mBAAoB,SAAQ,cAAc,CACrD,OAAO,cAAc,EACrB,wBAAwB,EACxB,4BAA4B,CAC7B;;IAMC;;;;;;;;;OASG;gBACS,EACV,SAAS,EACT,KAAK,EACL,gBAAgB,GACjB,EAAE;QACD,SAAS,EAAE,4BAA4B,CAAC;QACxC,KAAK,CAAC,EAAE,OAAO,CAAC,wBAAwB,CAAC,CAAC;QAC1C,gBAAgB,EAAE,wBAAwB,CAAC;KAC5C;IAmBD;;;OAGG;IACG,eAAe;CAWtB"}
|
|
@@ -0,0 +1,157 @@
|
|
|
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
|
+
import type { AbstractGasPricesService } from "./gas-prices-service/abstract-gas-prices-service.mjs";
|
|
5
|
+
import type { NetworkControllerGetStateAction } from "./network-controller-types.mjs";
|
|
6
|
+
/**
|
|
7
|
+
* The name of the {@link GasPricesController}, used to namespace the
|
|
8
|
+
* controller's actions and events and to namespace the controller's state data
|
|
9
|
+
* when composed with other controllers.
|
|
10
|
+
*/
|
|
11
|
+
export declare const controllerName = "GasPricesController";
|
|
12
|
+
/**
|
|
13
|
+
* The collection of gas price data fetched periodically.
|
|
14
|
+
*/
|
|
15
|
+
type GasPrices = {
|
|
16
|
+
/**
|
|
17
|
+
* The total estimated gas in the "low" bucket.
|
|
18
|
+
*/
|
|
19
|
+
low: number;
|
|
20
|
+
/**
|
|
21
|
+
* The total estimated gas in the "average" bucket.
|
|
22
|
+
*/
|
|
23
|
+
average: number;
|
|
24
|
+
/**
|
|
25
|
+
* The total estimated gas in the "high" bucket.
|
|
26
|
+
*/
|
|
27
|
+
high: number;
|
|
28
|
+
/**
|
|
29
|
+
* The date/time (in ISO-8601 format) when prices were fetched.
|
|
30
|
+
*/
|
|
31
|
+
fetchedDate: string;
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Describes the shape of the state object for {@link GasPricesController}.
|
|
35
|
+
*/
|
|
36
|
+
export type GasPricesControllerState = {
|
|
37
|
+
/**
|
|
38
|
+
* The registry of pet names, categorized by chain ID first and address
|
|
39
|
+
* second.
|
|
40
|
+
*/
|
|
41
|
+
gasPricesByChainId: {
|
|
42
|
+
[chainId: Hex]: GasPrices;
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* The action which can be used to retrieve the state of the
|
|
47
|
+
* {@link GasPricesController}.
|
|
48
|
+
*/
|
|
49
|
+
export type GasPricesControllerGetStateAction = ControllerGetStateAction<typeof controllerName, GasPricesControllerState>;
|
|
50
|
+
/**
|
|
51
|
+
* The action which can be used to update gas prices.
|
|
52
|
+
*/
|
|
53
|
+
export type GasPricesControllerUpdateGasPricesAction = {
|
|
54
|
+
type: `${typeof controllerName}:updateGasPrices`;
|
|
55
|
+
handler: GasPricesController['updateGasPrices'];
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* All actions that {@link GasPricesController} registers, to be called
|
|
59
|
+
* externally.
|
|
60
|
+
*/
|
|
61
|
+
export type GasPricesControllerActions = GasPricesControllerGetStateAction | GasPricesControllerUpdateGasPricesAction;
|
|
62
|
+
/**
|
|
63
|
+
* All actions that {@link GasPricesController} calls internally.
|
|
64
|
+
*/
|
|
65
|
+
type AllowedActions = NetworkControllerGetStateAction;
|
|
66
|
+
/**
|
|
67
|
+
* The event that {@link GasPricesController} publishes when updating state.
|
|
68
|
+
*/
|
|
69
|
+
export type GasPricesControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, GasPricesControllerState>;
|
|
70
|
+
/**
|
|
71
|
+
* All events that {@link GasPricesController} publishes, to be subscribed to
|
|
72
|
+
* externally.
|
|
73
|
+
*/
|
|
74
|
+
export type GasPricesControllerEvents = GasPricesControllerStateChangeEvent;
|
|
75
|
+
/**
|
|
76
|
+
* All events that {@link GasPricesController} subscribes to internally.
|
|
77
|
+
*/
|
|
78
|
+
type AllowedEvents = never;
|
|
79
|
+
/**
|
|
80
|
+
* The messenger which is restricted to actions and events accessed by
|
|
81
|
+
* {@link GasPricesController}.
|
|
82
|
+
*/
|
|
83
|
+
export type GasPricesControllerMessenger = RestrictedMessenger<typeof controllerName, GasPricesControllerActions | AllowedActions, GasPricesControllerEvents | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
|
|
84
|
+
/**
|
|
85
|
+
* Constructs the default {@link GasPricesController} state. This allows
|
|
86
|
+
* consumers to provide a partial state object when initializing the controller
|
|
87
|
+
* and also helps in constructing complete state objects for this controller in
|
|
88
|
+
* tests.
|
|
89
|
+
*
|
|
90
|
+
* @returns The default {@link GasPricesController} state.
|
|
91
|
+
*/
|
|
92
|
+
export declare function getDefaultGasPricesControllerState(): GasPricesControllerState;
|
|
93
|
+
/**
|
|
94
|
+
* `GasPricesController` fetches and persists gas prices for various chains.
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
*
|
|
98
|
+
* ``` ts
|
|
99
|
+
* import { Messenger } from '@metamask/base-controller';
|
|
100
|
+
* import {
|
|
101
|
+
* GasPricesController,
|
|
102
|
+
* GasPricesService
|
|
103
|
+
* } from '@metamask/example-controllers';
|
|
104
|
+
* import type {
|
|
105
|
+
* GasPricesControllerActions,
|
|
106
|
+
* GasPricesControllerEvents
|
|
107
|
+
* } from '@metamask/example-controllers';
|
|
108
|
+
* import type { NetworkControllerGetStateAction } from '@metamask/network-controller';
|
|
109
|
+
*
|
|
110
|
+
* // Assuming that you're using this in the browser
|
|
111
|
+
* const gasPricesService = new GasPricesService({ fetch });
|
|
112
|
+
* const rootMessenger = new Messenger<
|
|
113
|
+
* GasPricesControllerActions | NetworkControllerGetStateAction,
|
|
114
|
+
* GasPricesControllerEvents
|
|
115
|
+
* >();
|
|
116
|
+
* const gasPricesMessenger = rootMessenger.getRestricted({
|
|
117
|
+
* name: 'GasPricesController',
|
|
118
|
+
* allowedActions: ['NetworkController:getState'],
|
|
119
|
+
* allowedEvents: [],
|
|
120
|
+
* });
|
|
121
|
+
* const gasPricesController = new GasPricesController({
|
|
122
|
+
* messenger: gasPricesMessenger,
|
|
123
|
+
* gasPricesService,
|
|
124
|
+
* });
|
|
125
|
+
*
|
|
126
|
+
* // Assuming that `NetworkController:getState` returns an object with a
|
|
127
|
+
* // `chainId` of `0x42`...
|
|
128
|
+
* await gasPricesController.updateGasPrices();
|
|
129
|
+
* gasPricesController.state.gasPricesByChainId
|
|
130
|
+
* // => { '0x42': { low: 5, average: 10, high: 15, fetchedDate: '2024-01-02T00:00:00.000Z' } }
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
133
|
+
export declare class GasPricesController extends BaseController<typeof controllerName, GasPricesControllerState, GasPricesControllerMessenger> {
|
|
134
|
+
#private;
|
|
135
|
+
/**
|
|
136
|
+
* Constructs a new {@link GasPricesController}.
|
|
137
|
+
*
|
|
138
|
+
* @param args - The arguments to the controller.
|
|
139
|
+
* @param args.messenger - The messenger suited for this controller.
|
|
140
|
+
* @param args.state - The desired state with which to initialize this
|
|
141
|
+
* controller. Missing properties will be filled in with defaults.
|
|
142
|
+
* @param args.gasPricesService - The service object that will be used to
|
|
143
|
+
* obtain gas prices.
|
|
144
|
+
*/
|
|
145
|
+
constructor({ messenger, state, gasPricesService, }: {
|
|
146
|
+
messenger: GasPricesControllerMessenger;
|
|
147
|
+
state?: Partial<GasPricesControllerState>;
|
|
148
|
+
gasPricesService: AbstractGasPricesService;
|
|
149
|
+
});
|
|
150
|
+
/**
|
|
151
|
+
* Fetches the latest gas prices for the current chain, persisting them to
|
|
152
|
+
* state.
|
|
153
|
+
*/
|
|
154
|
+
updateGasPrices(): Promise<void>;
|
|
155
|
+
}
|
|
156
|
+
export {};
|
|
157
|
+
//# sourceMappingURL=gas-prices-controller.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gas-prices-controller.d.mts","sourceRoot":"","sources":["../../src/gas-prices-controller.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAC1B,mBAAmB,EAEpB,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAE3C,OAAO,KAAK,EAAE,wBAAwB,EAAE,6DAAyD;AACjG,OAAO,KAAK,EAAE,+BAA+B,EAAE,uCAAmC;AAIlF;;;;GAIG;AACH,eAAO,MAAM,cAAc,wBAAwB,CAAC;AAIpD;;GAEG;AACH,KAAK,SAAS,GAAG;IACf;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;IACZ;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;IAChB;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IACb;;OAEG;IACH,WAAW,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,wBAAwB,GAAG;IACrC;;;OAGG;IACH,kBAAkB,EAAE;QAClB,CAAC,OAAO,EAAE,GAAG,GAAG,SAAS,CAAC;KAC3B,CAAC;CACH,CAAC;AAcF;;;GAGG;AACH,MAAM,MAAM,iCAAiC,GAAG,wBAAwB,CACtE,OAAO,cAAc,EACrB,wBAAwB,CACzB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,wCAAwC,GAAG;IACrD,IAAI,EAAE,GAAG,OAAO,cAAc,kBAAkB,CAAC;IACjD,OAAO,EAAE,mBAAmB,CAAC,iBAAiB,CAAC,CAAC;CACjD,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,0BAA0B,GAClC,iCAAiC,GACjC,wCAAwC,CAAC;AAE7C;;GAEG;AACH,KAAK,cAAc,GAAG,+BAA+B,CAAC;AAEtD;;GAEG;AACH,MAAM,MAAM,mCAAmC,GAAG,0BAA0B,CAC1E,OAAO,cAAc,EACrB,wBAAwB,CACzB,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,yBAAyB,GAAG,mCAAmC,CAAC;AAE5E;;GAEG;AACH,KAAK,aAAa,GAAG,KAAK,CAAC;AAE3B;;;GAGG;AACH,MAAM,MAAM,4BAA4B,GAAG,mBAAmB,CAC5D,OAAO,cAAc,EACrB,0BAA0B,GAAG,cAAc,EAC3C,yBAAyB,GAAG,aAAa,EACzC,cAAc,CAAC,MAAM,CAAC,EACtB,aAAa,CAAC,MAAM,CAAC,CACtB,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAgB,kCAAkC,IAAI,wBAAwB,CAI7E;AAID;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,qBAAa,mBAAoB,SAAQ,cAAc,CACrD,OAAO,cAAc,EACrB,wBAAwB,EACxB,4BAA4B,CAC7B;;IAMC;;;;;;;;;OASG;gBACS,EACV,SAAS,EACT,KAAK,EACL,gBAAgB,GACjB,EAAE;QACD,SAAS,EAAE,4BAA4B,CAAC;QACxC,KAAK,CAAC,EAAE,OAAO,CAAC,wBAAwB,CAAC,CAAC;QAC1C,gBAAgB,EAAE,wBAAwB,CAAC;KAC5C;IAmBD;;;OAGG;IACG,eAAe;CAWtB"}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _GasPricesController_gasPricesService;
|
|
13
|
+
import { BaseController } from "@metamask/base-controller";
|
|
14
|
+
// === GENERAL ===
|
|
15
|
+
/**
|
|
16
|
+
* The name of the {@link GasPricesController}, used to namespace the
|
|
17
|
+
* controller's actions and events and to namespace the controller's state data
|
|
18
|
+
* when composed with other controllers.
|
|
19
|
+
*/
|
|
20
|
+
export const controllerName = 'GasPricesController';
|
|
21
|
+
/**
|
|
22
|
+
* The metadata for each property in {@link GasPricesControllerState}.
|
|
23
|
+
*/
|
|
24
|
+
const gasPricesControllerMetadata = {
|
|
25
|
+
gasPricesByChainId: {
|
|
26
|
+
persist: true,
|
|
27
|
+
anonymous: false,
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Constructs the default {@link GasPricesController} state. This allows
|
|
32
|
+
* consumers to provide a partial state object when initializing the controller
|
|
33
|
+
* and also helps in constructing complete state objects for this controller in
|
|
34
|
+
* tests.
|
|
35
|
+
*
|
|
36
|
+
* @returns The default {@link GasPricesController} state.
|
|
37
|
+
*/
|
|
38
|
+
export function getDefaultGasPricesControllerState() {
|
|
39
|
+
return {
|
|
40
|
+
gasPricesByChainId: {},
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
// === CONTROLLER DEFINITION ===
|
|
44
|
+
/**
|
|
45
|
+
* `GasPricesController` fetches and persists gas prices for various chains.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
*
|
|
49
|
+
* ``` ts
|
|
50
|
+
* import { Messenger } from '@metamask/base-controller';
|
|
51
|
+
* import {
|
|
52
|
+
* GasPricesController,
|
|
53
|
+
* GasPricesService
|
|
54
|
+
* } from '@metamask/example-controllers';
|
|
55
|
+
* import type {
|
|
56
|
+
* GasPricesControllerActions,
|
|
57
|
+
* GasPricesControllerEvents
|
|
58
|
+
* } from '@metamask/example-controllers';
|
|
59
|
+
* import type { NetworkControllerGetStateAction } from '@metamask/network-controller';
|
|
60
|
+
*
|
|
61
|
+
* // Assuming that you're using this in the browser
|
|
62
|
+
* const gasPricesService = new GasPricesService({ fetch });
|
|
63
|
+
* const rootMessenger = new Messenger<
|
|
64
|
+
* GasPricesControllerActions | NetworkControllerGetStateAction,
|
|
65
|
+
* GasPricesControllerEvents
|
|
66
|
+
* >();
|
|
67
|
+
* const gasPricesMessenger = rootMessenger.getRestricted({
|
|
68
|
+
* name: 'GasPricesController',
|
|
69
|
+
* allowedActions: ['NetworkController:getState'],
|
|
70
|
+
* allowedEvents: [],
|
|
71
|
+
* });
|
|
72
|
+
* const gasPricesController = new GasPricesController({
|
|
73
|
+
* messenger: gasPricesMessenger,
|
|
74
|
+
* gasPricesService,
|
|
75
|
+
* });
|
|
76
|
+
*
|
|
77
|
+
* // Assuming that `NetworkController:getState` returns an object with a
|
|
78
|
+
* // `chainId` of `0x42`...
|
|
79
|
+
* await gasPricesController.updateGasPrices();
|
|
80
|
+
* gasPricesController.state.gasPricesByChainId
|
|
81
|
+
* // => { '0x42': { low: 5, average: 10, high: 15, fetchedDate: '2024-01-02T00:00:00.000Z' } }
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
export class GasPricesController extends BaseController {
|
|
85
|
+
/**
|
|
86
|
+
* Constructs a new {@link GasPricesController}.
|
|
87
|
+
*
|
|
88
|
+
* @param args - The arguments to the controller.
|
|
89
|
+
* @param args.messenger - The messenger suited for this controller.
|
|
90
|
+
* @param args.state - The desired state with which to initialize this
|
|
91
|
+
* controller. Missing properties will be filled in with defaults.
|
|
92
|
+
* @param args.gasPricesService - The service object that will be used to
|
|
93
|
+
* obtain gas prices.
|
|
94
|
+
*/
|
|
95
|
+
constructor({ messenger, state, gasPricesService, }) {
|
|
96
|
+
super({
|
|
97
|
+
messenger,
|
|
98
|
+
metadata: gasPricesControllerMetadata,
|
|
99
|
+
name: controllerName,
|
|
100
|
+
state: {
|
|
101
|
+
...getDefaultGasPricesControllerState(),
|
|
102
|
+
...state,
|
|
103
|
+
},
|
|
104
|
+
});
|
|
105
|
+
/**
|
|
106
|
+
* The service object that is used to obtain gas prices.
|
|
107
|
+
*/
|
|
108
|
+
_GasPricesController_gasPricesService.set(this, void 0);
|
|
109
|
+
__classPrivateFieldSet(this, _GasPricesController_gasPricesService, gasPricesService, "f");
|
|
110
|
+
this.messagingSystem.registerActionHandler(`${controllerName}:updateGasPrices`, this.updateGasPrices.bind(this));
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Fetches the latest gas prices for the current chain, persisting them to
|
|
114
|
+
* state.
|
|
115
|
+
*/
|
|
116
|
+
async updateGasPrices() {
|
|
117
|
+
const { chainId } = this.messagingSystem.call('NetworkController:getState');
|
|
118
|
+
const gasPricesResponse = await __classPrivateFieldGet(this, _GasPricesController_gasPricesService, "f").fetchGasPrices(chainId);
|
|
119
|
+
this.update((state) => {
|
|
120
|
+
state.gasPricesByChainId[chainId] = {
|
|
121
|
+
...gasPricesResponse,
|
|
122
|
+
fetchedDate: new Date().toISOString(),
|
|
123
|
+
};
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
_GasPricesController_gasPricesService = new WeakMap();
|
|
128
|
+
//# sourceMappingURL=gas-prices-controller.mjs.map
|