@metamask/assets-controllers 104.2.0 → 105.0.0
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 +35 -1
- package/dist/CurrencyRateController-method-action-types.cjs +7 -0
- package/dist/CurrencyRateController-method-action-types.cjs.map +1 -0
- package/dist/CurrencyRateController-method-action-types.d.cts +28 -0
- package/dist/CurrencyRateController-method-action-types.d.cts.map +1 -0
- package/dist/CurrencyRateController-method-action-types.d.mts +28 -0
- package/dist/CurrencyRateController-method-action-types.d.mts.map +1 -0
- package/dist/CurrencyRateController-method-action-types.mjs +6 -0
- package/dist/CurrencyRateController-method-action-types.mjs.map +1 -0
- package/dist/CurrencyRateController.cjs +5 -0
- package/dist/CurrencyRateController.cjs.map +1 -1
- package/dist/CurrencyRateController.d.cts +3 -2
- package/dist/CurrencyRateController.d.cts.map +1 -1
- package/dist/CurrencyRateController.d.mts +3 -2
- package/dist/CurrencyRateController.d.mts.map +1 -1
- package/dist/CurrencyRateController.mjs +5 -0
- package/dist/CurrencyRateController.mjs.map +1 -1
- package/dist/MultichainAssetsController/MultichainAssetsController.cjs +7 -15
- package/dist/MultichainAssetsController/MultichainAssetsController.cjs.map +1 -1
- package/dist/MultichainAssetsController/MultichainAssetsController.d.cts.map +1 -1
- package/dist/MultichainAssetsController/MultichainAssetsController.d.mts.map +1 -1
- package/dist/MultichainAssetsController/MultichainAssetsController.mjs +7 -15
- package/dist/MultichainAssetsController/MultichainAssetsController.mjs.map +1 -1
- package/dist/MultichainAssetsRatesController/MultichainAssetsRatesController.cjs.map +1 -1
- package/dist/MultichainAssetsRatesController/MultichainAssetsRatesController.d.cts +2 -2
- package/dist/MultichainAssetsRatesController/MultichainAssetsRatesController.d.cts.map +1 -1
- package/dist/MultichainAssetsRatesController/MultichainAssetsRatesController.d.mts +2 -2
- package/dist/MultichainAssetsRatesController/MultichainAssetsRatesController.d.mts.map +1 -1
- package/dist/MultichainAssetsRatesController/MultichainAssetsRatesController.mjs.map +1 -1
- package/dist/TokenSearchDiscoveryDataController/TokenSearchDiscoveryDataController.cjs.map +1 -1
- package/dist/TokenSearchDiscoveryDataController/TokenSearchDiscoveryDataController.d.cts +2 -2
- package/dist/TokenSearchDiscoveryDataController/TokenSearchDiscoveryDataController.d.cts.map +1 -1
- package/dist/TokenSearchDiscoveryDataController/TokenSearchDiscoveryDataController.d.mts +2 -2
- package/dist/TokenSearchDiscoveryDataController/TokenSearchDiscoveryDataController.d.mts.map +1 -1
- package/dist/TokenSearchDiscoveryDataController/TokenSearchDiscoveryDataController.mjs.map +1 -1
- package/dist/index.cjs +2 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -1
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +2 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/selectors/token-selectors.cjs +3 -0
- package/dist/selectors/token-selectors.cjs.map +1 -1
- package/dist/selectors/token-selectors.d.cts.map +1 -1
- package/dist/selectors/token-selectors.d.mts.map +1 -1
- package/dist/selectors/token-selectors.mjs +3 -0
- package/dist/selectors/token-selectors.mjs.map +1 -1
- package/dist/token-prices-service/index.cjs +2 -1
- package/dist/token-prices-service/index.cjs.map +1 -1
- package/dist/token-prices-service/index.d.cts +1 -1
- package/dist/token-prices-service/index.d.cts.map +1 -1
- package/dist/token-prices-service/index.d.mts +1 -1
- package/dist/token-prices-service/index.d.mts.map +1 -1
- package/dist/token-prices-service/index.mjs +1 -1
- package/dist/token-prices-service/index.mjs.map +1 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,38 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [105.0.0]
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- Expose missing public `CurrencyRateController` methods through its messenger ([#8561](https://github.com/MetaMask/core/pull/8561))
|
|
15
|
+
- The following actions are now available:
|
|
16
|
+
- `CurrencyRateController:setCurrentCurrency`
|
|
17
|
+
- `CurrencyRateController:updateExchangeRate`
|
|
18
|
+
- Corresponding action types (e.g. `CurrencyRateControllerSetCurrentCurrencyAction`) are available as well.
|
|
19
|
+
|
|
20
|
+
### Changed
|
|
21
|
+
|
|
22
|
+
- **BREAKING:** Standardize names of `CurrencyRateController` messenger action types ([#8561](https://github.com/MetaMask/core/pull/8561))
|
|
23
|
+
- The `GetCurrencyRateState` messenger action has been renamed to `CurrencyRateControllerGetStateAction` to follow the convention. You will need to update imports appropriately.
|
|
24
|
+
- These changes only affect the types. The action type strings themselves have not changed, so you do not need to update the list of actions you pass when initializing `CurrencyRateController` messenger.
|
|
25
|
+
- `MultichainAssetsController`: Restore fail-open behavior for Blockaid spam token filter ([#8580](https://github.com/MetaMask/core/pull/8580))
|
|
26
|
+
- Uses blacklist approach: only rejects tokens explicitly marked as malicious by Blockaid
|
|
27
|
+
- When Blockaid bulk token scan API calls fail or return no results, tokens are allowed through
|
|
28
|
+
- This prevents legitimate tokens from being blocked due to API outages, network issues, or missing token data
|
|
29
|
+
- Malicious tokens that slip through are caught by the periodic rescan (runs daily by default)
|
|
30
|
+
- Bump `@metamask/transaction-controller` from `^64.3.0` to `^65.0.0` ([#8585](https://github.com/MetaMask/core/pull/8585), [#8613](https://github.com/MetaMask/core/pull/8613))
|
|
31
|
+
|
|
32
|
+
### Fixed
|
|
33
|
+
|
|
34
|
+
- Fix `selectAssetsBySelectedAccountGroup` crashing when an account referenced in the account tree is missing from internal accounts ([#8604](https://github.com/MetaMask/core/pull/8604))
|
|
35
|
+
|
|
36
|
+
## [104.3.0]
|
|
37
|
+
|
|
38
|
+
### Added
|
|
39
|
+
|
|
40
|
+
- Export `SPOT_PRICES_SUPPORT_INFO` from the token prices service ([#8483](https://github.com/MetaMask/core/pull/8483))
|
|
41
|
+
|
|
10
42
|
## [104.2.0]
|
|
11
43
|
|
|
12
44
|
### Added
|
|
@@ -2941,7 +2973,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
2941
2973
|
|
|
2942
2974
|
- Use Ethers for AssetsContractController ([#845](https://github.com/MetaMask/core/pull/845))
|
|
2943
2975
|
|
|
2944
|
-
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@
|
|
2976
|
+
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@105.0.0...HEAD
|
|
2977
|
+
[105.0.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@104.3.0...@metamask/assets-controllers@105.0.0
|
|
2978
|
+
[104.3.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@104.2.0...@metamask/assets-controllers@104.3.0
|
|
2945
2979
|
[104.2.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@104.1.0...@metamask/assets-controllers@104.2.0
|
|
2946
2980
|
[104.1.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@104.0.0...@metamask/assets-controllers@104.1.0
|
|
2947
2981
|
[104.0.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@103.1.1...@metamask/assets-controllers@104.0.0
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CurrencyRateController-method-action-types.cjs","sourceRoot":"","sources":["../src/CurrencyRateController-method-action-types.ts"],"names":[],"mappings":";AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated.\n * Do not edit manually.\n */\n\nimport type { CurrencyRateController } from './CurrencyRateController';\n\n/**\n * Sets a currency to track.\n *\n * @param currentCurrency - ISO 4217 currency code.\n */\nexport type CurrencyRateControllerSetCurrentCurrencyAction = {\n type: `CurrencyRateController:setCurrentCurrency`;\n handler: CurrencyRateController['setCurrentCurrency'];\n};\n\n/**\n * Updates the exchange rate for the current currency and native currency pairs.\n *\n * @param nativeCurrencies - The native currency symbols to fetch exchange rates for.\n */\nexport type CurrencyRateControllerUpdateExchangeRateAction = {\n type: `CurrencyRateController:updateExchangeRate`;\n handler: CurrencyRateController['updateExchangeRate'];\n};\n\n/**\n * Union of all CurrencyRateController action types.\n */\nexport type CurrencyRateControllerMethodActions =\n | CurrencyRateControllerSetCurrentCurrencyAction\n | CurrencyRateControllerUpdateExchangeRateAction;\n"]}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is auto generated.
|
|
3
|
+
* Do not edit manually.
|
|
4
|
+
*/
|
|
5
|
+
import type { CurrencyRateController } from "./CurrencyRateController.cjs";
|
|
6
|
+
/**
|
|
7
|
+
* Sets a currency to track.
|
|
8
|
+
*
|
|
9
|
+
* @param currentCurrency - ISO 4217 currency code.
|
|
10
|
+
*/
|
|
11
|
+
export type CurrencyRateControllerSetCurrentCurrencyAction = {
|
|
12
|
+
type: `CurrencyRateController:setCurrentCurrency`;
|
|
13
|
+
handler: CurrencyRateController['setCurrentCurrency'];
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Updates the exchange rate for the current currency and native currency pairs.
|
|
17
|
+
*
|
|
18
|
+
* @param nativeCurrencies - The native currency symbols to fetch exchange rates for.
|
|
19
|
+
*/
|
|
20
|
+
export type CurrencyRateControllerUpdateExchangeRateAction = {
|
|
21
|
+
type: `CurrencyRateController:updateExchangeRate`;
|
|
22
|
+
handler: CurrencyRateController['updateExchangeRate'];
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Union of all CurrencyRateController action types.
|
|
26
|
+
*/
|
|
27
|
+
export type CurrencyRateControllerMethodActions = CurrencyRateControllerSetCurrentCurrencyAction | CurrencyRateControllerUpdateExchangeRateAction;
|
|
28
|
+
//# sourceMappingURL=CurrencyRateController-method-action-types.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CurrencyRateController-method-action-types.d.cts","sourceRoot":"","sources":["../src/CurrencyRateController-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,sBAAsB,EAAE,qCAAiC;AAEvE;;;;GAIG;AACH,MAAM,MAAM,8CAA8C,GAAG;IAC3D,IAAI,EAAE,2CAA2C,CAAC;IAClD,OAAO,EAAE,sBAAsB,CAAC,oBAAoB,CAAC,CAAC;CACvD,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,8CAA8C,GAAG;IAC3D,IAAI,EAAE,2CAA2C,CAAC;IAClD,OAAO,EAAE,sBAAsB,CAAC,oBAAoB,CAAC,CAAC;CACvD,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,mCAAmC,GAC3C,8CAA8C,GAC9C,8CAA8C,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is auto generated.
|
|
3
|
+
* Do not edit manually.
|
|
4
|
+
*/
|
|
5
|
+
import type { CurrencyRateController } from "./CurrencyRateController.mjs";
|
|
6
|
+
/**
|
|
7
|
+
* Sets a currency to track.
|
|
8
|
+
*
|
|
9
|
+
* @param currentCurrency - ISO 4217 currency code.
|
|
10
|
+
*/
|
|
11
|
+
export type CurrencyRateControllerSetCurrentCurrencyAction = {
|
|
12
|
+
type: `CurrencyRateController:setCurrentCurrency`;
|
|
13
|
+
handler: CurrencyRateController['setCurrentCurrency'];
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Updates the exchange rate for the current currency and native currency pairs.
|
|
17
|
+
*
|
|
18
|
+
* @param nativeCurrencies - The native currency symbols to fetch exchange rates for.
|
|
19
|
+
*/
|
|
20
|
+
export type CurrencyRateControllerUpdateExchangeRateAction = {
|
|
21
|
+
type: `CurrencyRateController:updateExchangeRate`;
|
|
22
|
+
handler: CurrencyRateController['updateExchangeRate'];
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Union of all CurrencyRateController action types.
|
|
26
|
+
*/
|
|
27
|
+
export type CurrencyRateControllerMethodActions = CurrencyRateControllerSetCurrentCurrencyAction | CurrencyRateControllerUpdateExchangeRateAction;
|
|
28
|
+
//# sourceMappingURL=CurrencyRateController-method-action-types.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CurrencyRateController-method-action-types.d.mts","sourceRoot":"","sources":["../src/CurrencyRateController-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,sBAAsB,EAAE,qCAAiC;AAEvE;;;;GAIG;AACH,MAAM,MAAM,8CAA8C,GAAG;IAC3D,IAAI,EAAE,2CAA2C,CAAC;IAClD,OAAO,EAAE,sBAAsB,CAAC,oBAAoB,CAAC,CAAC;CACvD,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,8CAA8C,GAAG;IAC3D,IAAI,EAAE,2CAA2C,CAAC;IAClD,OAAO,EAAE,sBAAsB,CAAC,oBAAoB,CAAC,CAAC;CACvD,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,mCAAmC,GAC3C,8CAA8C,GAC9C,8CAA8C,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CurrencyRateController-method-action-types.mjs","sourceRoot":"","sources":["../src/CurrencyRateController-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated.\n * Do not edit manually.\n */\n\nimport type { CurrencyRateController } from './CurrencyRateController';\n\n/**\n * Sets a currency to track.\n *\n * @param currentCurrency - ISO 4217 currency code.\n */\nexport type CurrencyRateControllerSetCurrentCurrencyAction = {\n type: `CurrencyRateController:setCurrentCurrency`;\n handler: CurrencyRateController['setCurrentCurrency'];\n};\n\n/**\n * Updates the exchange rate for the current currency and native currency pairs.\n *\n * @param nativeCurrencies - The native currency symbols to fetch exchange rates for.\n */\nexport type CurrencyRateControllerUpdateExchangeRateAction = {\n type: `CurrencyRateController:updateExchangeRate`;\n handler: CurrencyRateController['updateExchangeRate'];\n};\n\n/**\n * Union of all CurrencyRateController action types.\n */\nexport type CurrencyRateControllerMethodActions =\n | CurrencyRateControllerSetCurrentCurrencyAction\n | CurrencyRateControllerUpdateExchangeRateAction;\n"]}
|
|
@@ -18,6 +18,10 @@ const polling_controller_1 = require("@metamask/polling-controller");
|
|
|
18
18
|
const async_mutex_1 = require("async-mutex");
|
|
19
19
|
const codefi_v2_1 = require("./token-prices-service/codefi-v2.cjs");
|
|
20
20
|
const name = 'CurrencyRateController';
|
|
21
|
+
const MESSENGER_EXPOSED_METHODS = [
|
|
22
|
+
'setCurrentCurrency',
|
|
23
|
+
'updateExchangeRate',
|
|
24
|
+
];
|
|
21
25
|
const metadata = {
|
|
22
26
|
currentCurrency: {
|
|
23
27
|
includeInStateLogs: true,
|
|
@@ -75,6 +79,7 @@ class CurrencyRateController extends (0, polling_controller_1.StaticIntervalPoll
|
|
|
75
79
|
__classPrivateFieldSet(this, _CurrencyRateController_useExternalServices, useExternalServices, "f");
|
|
76
80
|
this.setIntervalLength(interval);
|
|
77
81
|
__classPrivateFieldSet(this, _CurrencyRateController_tokenPricesService, tokenPricesService, "f");
|
|
82
|
+
this.messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
|
|
78
83
|
}
|
|
79
84
|
/**
|
|
80
85
|
* Sets a currency to track.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CurrencyRateController.cjs","sourceRoot":"","sources":["../src/CurrencyRateController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAKA,iEAGoC;AAOpC,qEAA+E;AAE/E,6CAAoC;AAGpC,oEAAyE;AAyBzE,MAAM,IAAI,GAAG,wBAAwB,CAAC;AA0BtC,MAAM,QAAQ,GAAqC;IACjD,eAAe,EAAE;QACf,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,QAAQ,EAAE,IAAI;KACf;IACD,aAAa,EAAE;QACb,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QAC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IAAI;SACxB,CAAC;QACF,OAAO,GAAG,CAAC;IACb,CAAC,EACD,EAAE,CACH,CAAC;AACJ,CAAC;AAED;;;;;;GAMG;AACH,KAAK,iEACH,uBAA+C;IAE/C,MAAM,EAAE,eAAe,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC;IAEvC,iDAAiD;IACjD,MAAM,EACJ,KAAK,EAAE,aAAa,EACpB,gBAAgB,EAAE,4BAA4B,GAC/C,GAAG,MAAM,uBAAA,IAAI,yFAAwB,MAA5B,IAAI,EACZ,uBAAuB,EACvB,eAAe,CAChB,CAAC;IAEF,oDAAoD;IACpD,IAAI,MAAM,CAAC,IAAI,CAAC,4BAA4B,CAAC,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC3D,OAAO,aAAa,CAAC;IACvB,CAAC;IAED,oEAAoE;IACpE,MAAM,EACJ,KAAK,EAAE,iBAAiB,EACxB,gBAAgB,EAAE,4BAA4B,GAC/C,GAAG,MAAM,uBAAA,IAAI,mGAAkC,MAAtC,IAAI,EACZ,4BAA4B,EAC5B,eAAe,CAChB,CAAC;IAEF,uEAAuE;IACvE,MAAM,SAAS,GAAG,uBAAA,IAAI,+FAA8B,MAAlC,IAAI,EACpB,MAAM,CAAC,IAAI,CAAC,4BAA4B,CAAC,CAC1C,CAAC;IAEF,4FAA4F;IAC5F,OAAO;QACL,GAAG,SAAS;QACZ,GAAG,iBAAiB;QACpB,GAAG,aAAa;KACjB,CAAC;AACJ,CAAC;AAyEH,kBAAe,sBAAsB,CAAC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport {\n TESTNET_TICKER_SYMBOLS,\n FALL_BACK_VS_CURRENCY,\n} from '@metamask/controller-utils';\nimport type { Messenger } from '@metamask/messenger';\nimport type {\n NetworkControllerGetNetworkClientByIdAction,\n NetworkControllerGetStateAction,\n NetworkConfiguration,\n} from '@metamask/network-controller';\nimport { StaticIntervalPollingController } from '@metamask/polling-controller';\nimport type { Hex } from '@metamask/utils';\nimport { Mutex } from 'async-mutex';\n\nimport type { AbstractTokenPricesService } from './token-prices-service/abstract-token-prices-service';\nimport { getNativeTokenAddress } from './token-prices-service/codefi-v2';\n\n/**\n * currencyRates - Object keyed by native currency\n *\n * currencyRates.conversionDate - Timestamp of conversion rate expressed in ms since UNIX epoch\n *\n * currencyRates.conversionRate - Conversion rate from current base asset to the current currency\n *\n * currentCurrency - Currently-active ISO 4217 currency code\n *\n * usdConversionRate - Conversion rate from usd to the current currency\n */\nexport type CurrencyRateState = {\n currentCurrency: string;\n currencyRates: Record<\n string,\n {\n conversionDate: number | null;\n conversionRate: number | null;\n usdConversionRate: number | null;\n }\n >;\n};\n\nconst name = 'CurrencyRateController';\n\nexport type CurrencyRateStateChange = ControllerStateChangeEvent<\n typeof name,\n CurrencyRateState\n>;\n\nexport type CurrencyRateControllerEvents = CurrencyRateStateChange;\n\nexport type GetCurrencyRateState = ControllerGetStateAction<\n typeof name,\n CurrencyRateState\n>;\n\nexport type CurrencyRateControllerActions = GetCurrencyRateState;\n\ntype AllowedActions =\n | NetworkControllerGetNetworkClientByIdAction\n | NetworkControllerGetStateAction;\n\nexport type CurrencyRateMessenger = Messenger<\n typeof name,\n CurrencyRateControllerActions | AllowedActions,\n CurrencyRateControllerEvents\n>;\n\nconst metadata: StateMetadata<CurrencyRateState> = {\n currentCurrency: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n currencyRates: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n};\n\nconst defaultState = {\n currentCurrency: 'usd',\n currencyRates: {\n ETH: {\n conversionDate: 0,\n conversionRate: 0,\n usdConversionRate: null,\n },\n },\n};\n\n/** The input to start polling for the {@link CurrencyRateController} */\ntype CurrencyRatePollingInput = {\n nativeCurrencies: string[];\n};\n\nconst boundedPrecisionNumber = (value: number, precision = 9): number =>\n Number(value.toFixed(precision));\n\n/**\n * Controller that passively polls on a set interval for an exchange rate from the current network\n * asset to the user's preferred currency.\n */\n/** Result from attempting to fetch rates from an API */\ntype FetchRatesResult = {\n /** Successfully fetched rates */\n rates: CurrencyRateState['currencyRates'];\n /** Currencies that failed and need fallback or null state */\n failedCurrencies: Record<string, string>;\n};\n\n/**\n * Controller that passively polls on a set interval for an exchange rate from the current network\n * asset to the user's preferred currency.\n */\nexport class CurrencyRateController extends StaticIntervalPollingController<CurrencyRatePollingInput>()<\n typeof name,\n CurrencyRateState,\n CurrencyRateMessenger\n> {\n readonly #mutex = new Mutex();\n\n readonly #includeUsdRate: boolean;\n\n readonly #useExternalServices: () => boolean;\n\n readonly #tokenPricesService: AbstractTokenPricesService;\n\n /**\n * Creates a CurrencyRateController instance.\n *\n * @param options - Constructor options.\n * @param options.includeUsdRate - Keep track of the USD rate in addition to the current currency rate.\n * @param options.interval - The polling interval, in milliseconds.\n * @param options.messenger - A reference to the messenger.\n * @param options.state - Initial state to set on this controller.\n * @param options.useExternalServices - Feature Switch for using external services (default: true)\n * @param options.tokenPricesService - An object in charge of retrieving token prices\n */\n constructor({\n includeUsdRate = false,\n interval = 180000,\n useExternalServices = () => true,\n messenger,\n state,\n tokenPricesService,\n }: {\n includeUsdRate?: boolean;\n interval?: number;\n messenger: CurrencyRateMessenger;\n state?: Partial<CurrencyRateState>;\n useExternalServices?: () => boolean;\n tokenPricesService: AbstractTokenPricesService;\n }) {\n super({\n name,\n metadata,\n messenger,\n state: { ...defaultState, ...state },\n });\n this.#includeUsdRate = includeUsdRate;\n this.#useExternalServices = useExternalServices;\n this.setIntervalLength(interval);\n this.#tokenPricesService = tokenPricesService;\n }\n\n /**\n * Sets a currency to track.\n *\n * @param currentCurrency - ISO 4217 currency code.\n */\n async setCurrentCurrency(currentCurrency: string): Promise<void> {\n const releaseLock = await this.#mutex.acquire();\n const nativeCurrencies = Object.keys(this.state.currencyRates);\n try {\n this.update(() => {\n return {\n ...defaultState,\n currentCurrency,\n };\n });\n } finally {\n releaseLock();\n }\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this.updateExchangeRate(nativeCurrencies);\n }\n\n /**\n * Attempts to fetch exchange rates from the primary Price API.\n *\n * @param nativeCurrenciesToFetch - Map of native currency to the currency symbol to fetch.\n * @param currentCurrency - The current fiat currency to get rates for.\n * @returns Object containing successful rates and currencies that failed.\n */\n async #fetchRatesFromPriceApi(\n nativeCurrenciesToFetch: Record<string, string>,\n currentCurrency: string,\n ): Promise<FetchRatesResult> {\n const rates: CurrencyRateState['currencyRates'] = {};\n let failedCurrencies: Record<string, string> = {};\n\n try {\n const response = await this.#tokenPricesService.fetchExchangeRates({\n baseCurrency: currentCurrency,\n includeUsdRate: this.#includeUsdRate,\n cryptocurrencies: [...new Set(Object.values(nativeCurrenciesToFetch))],\n });\n\n Object.entries(nativeCurrenciesToFetch).forEach(\n ([nativeCurrency, fetchedCurrency]) => {\n const rate = response[fetchedCurrency.toLowerCase()];\n\n if (rate?.value) {\n rates[nativeCurrency] = {\n conversionDate: Date.now() / 1000,\n conversionRate: boundedPrecisionNumber(1 / rate.value),\n usdConversionRate: rate?.usd\n ? boundedPrecisionNumber(1 / rate.usd)\n : null,\n };\n } else {\n failedCurrencies[nativeCurrency] = fetchedCurrency;\n }\n },\n );\n } catch (error) {\n console.error('Failed to fetch exchange rates.', error);\n failedCurrencies = { ...nativeCurrenciesToFetch };\n }\n\n return { rates, failedCurrencies };\n }\n\n /**\n * Fetches exchange rates from the token prices service as a fallback.\n * This method is designed to never throw - all errors are handled internally\n * and result in currencies being marked as failed.\n *\n * @param currenciesToFetch - Map of native currencies that need fallback fetching.\n * @param currentCurrency - The current fiat currency to get rates for.\n * @returns Object containing successful rates and currencies that failed.\n */\n async #fetchRatesFromTokenPricesService(\n currenciesToFetch: Record<string, string>,\n currentCurrency: string,\n ): Promise<FetchRatesResult> {\n try {\n const rates: CurrencyRateState['currencyRates'] = {};\n const failedCurrencies: Record<string, string> = {};\n\n const networkControllerState = this.messenger.call(\n 'NetworkController:getState',\n );\n const networkConfigurations =\n networkControllerState.networkConfigurationsByChainId;\n\n // Build a map of nativeCurrency -> chainId for currencies to fetch\n const currencyToChainIds = Object.entries(currenciesToFetch).reduce<\n Record<string, { fetchedCurrency: string; chainId: Hex }>\n >((acc, [nativeCurrency, fetchedCurrency]) => {\n const matchingEntry = (\n Object.entries(networkConfigurations) as [Hex, NetworkConfiguration][]\n ).find(\n ([, config]) =>\n config.nativeCurrency.toUpperCase() ===\n fetchedCurrency.toUpperCase(),\n );\n\n if (matchingEntry) {\n acc[nativeCurrency] = { fetchedCurrency, chainId: matchingEntry[0] };\n } else {\n // No matching network configuration - mark as failed\n failedCurrencies[nativeCurrency] = fetchedCurrency;\n }\n return acc;\n }, {});\n\n const currencyToChainIdsEntries = Object.entries(currencyToChainIds);\n const ratesResults = await Promise.allSettled(\n currencyToChainIdsEntries.map(async ([nativeCurrency, { chainId }]) => {\n const nativeTokenAddress = getNativeTokenAddress(chainId);\n const tokenPrices = await this.#tokenPricesService.fetchTokenPrices({\n assets: [{ chainId, tokenAddress: nativeTokenAddress }],\n currency: currentCurrency,\n });\n\n const tokenPrice = tokenPrices.find(\n (item) =>\n item.tokenAddress.toLowerCase() ===\n nativeTokenAddress.toLowerCase(),\n );\n\n return {\n nativeCurrency,\n conversionDate: tokenPrice ? Date.now() / 1000 : null,\n conversionRate: tokenPrice?.price\n ? boundedPrecisionNumber(tokenPrice.price)\n : null,\n usdConversionRate: null,\n };\n }),\n );\n\n ratesResults.forEach((result, index) => {\n const [nativeCurrency, { fetchedCurrency, chainId }] =\n currencyToChainIdsEntries[index];\n\n if (result.status === 'fulfilled' && result.value.conversionRate) {\n rates[nativeCurrency] = {\n conversionDate: result.value.conversionDate,\n conversionRate: result.value.conversionRate,\n usdConversionRate: result.value.usdConversionRate,\n };\n } else {\n if (result.status === 'rejected') {\n console.error(\n `Failed to fetch token price for ${nativeCurrency} on chain ${chainId}`,\n result.reason,\n );\n }\n failedCurrencies[nativeCurrency] = fetchedCurrency;\n }\n });\n\n return { rates, failedCurrencies };\n } catch (error) {\n console.error(\n 'Failed to fetch exchange rates from token prices service.',\n error,\n );\n // Return all currencies as failed\n return { rates: {}, failedCurrencies: { ...currenciesToFetch } };\n }\n }\n\n /**\n * Creates null rate entries for currencies that couldn't be fetched.\n *\n * @param currencies - Array of currency symbols to create null entries for.\n * @returns Null rate entries for all provided currencies.\n */\n #createNullRatesForCurrencies(\n currencies: string[],\n ): CurrencyRateState['currencyRates'] {\n return currencies.reduce<CurrencyRateState['currencyRates']>(\n (acc, nativeCurrency) => {\n acc[nativeCurrency] = {\n conversionDate: null,\n conversionRate: null,\n usdConversionRate: null,\n };\n return acc;\n },\n {},\n );\n }\n\n /**\n * Fetches exchange rates with fallback logic.\n * First tries the Price API, then falls back to token prices service for any failed currencies.\n *\n * @param nativeCurrenciesToFetch - Map of native currency to the currency symbol to fetch.\n * @returns Exchange rates for all requested currencies.\n */\n async #fetchExchangeRatesWithFallback(\n nativeCurrenciesToFetch: Record<string, string>,\n ): Promise<CurrencyRateState['currencyRates']> {\n const { currentCurrency } = this.state;\n\n // Step 1: Try the Price API exchange rates first\n const {\n rates: ratesPriceApi,\n failedCurrencies: failedCurrenciesFromPriceApi,\n } = await this.#fetchRatesFromPriceApi(\n nativeCurrenciesToFetch,\n currentCurrency,\n );\n\n // Step 2: If all currencies succeeded, return early\n if (Object.keys(failedCurrenciesFromPriceApi).length === 0) {\n return ratesPriceApi;\n }\n\n // Step 3: Fallback using token prices service for failed currencies\n const {\n rates: ratesFromFallback,\n failedCurrencies: failedCurrenciesFromFallback,\n } = await this.#fetchRatesFromTokenPricesService(\n failedCurrenciesFromPriceApi,\n currentCurrency,\n );\n\n // Step 4: Create null rates for currencies that failed both approaches\n const nullRates = this.#createNullRatesForCurrencies(\n Object.keys(failedCurrenciesFromFallback),\n );\n\n // Step 5: Merge all results - Price API rates take priority, then fallback, then null rates\n return {\n ...nullRates,\n ...ratesFromFallback,\n ...ratesPriceApi,\n };\n }\n\n /**\n * Updates the exchange rate for the current currency and native currency pairs.\n *\n * @param nativeCurrencies - The native currency symbols to fetch exchange rates for.\n */\n async updateExchangeRate(\n nativeCurrencies: (string | undefined)[],\n ): Promise<void> {\n if (!this.#useExternalServices()) {\n return;\n }\n\n const releaseLock = await this.#mutex.acquire();\n try {\n // For preloaded testnets (Goerli, Sepolia) we want to fetch exchange rate for real ETH.\n // Map each native currency to the symbol we want to fetch for it.\n const testnetSymbols = Object.values(TESTNET_TICKER_SYMBOLS);\n const nativeCurrenciesToFetch = nativeCurrencies.reduce<\n Record<string, string>\n >((acc, nativeCurrency) => {\n if (!nativeCurrency) {\n return acc;\n }\n\n acc[nativeCurrency] = testnetSymbols.includes(nativeCurrency)\n ? FALL_BACK_VS_CURRENCY\n : nativeCurrency;\n return acc;\n }, {});\n\n const rates = await this.#fetchExchangeRatesWithFallback(\n nativeCurrenciesToFetch,\n );\n\n this.update((state) => {\n state.currencyRates = {\n ...state.currencyRates,\n ...rates,\n };\n });\n } catch (error) {\n console.error('Failed to fetch exchange rates.', error);\n throw error;\n } finally {\n releaseLock();\n }\n }\n\n /**\n * Prepare to discard this controller.\n *\n * This stops any active polling.\n */\n override destroy(): void {\n super.destroy();\n this.stopAllPolling();\n }\n\n /**\n * Updates exchange rate for the current currency.\n *\n * @param input - The input for the poll.\n * @param input.nativeCurrencies - The native currency symbols to poll prices for.\n */\n async _executePoll({\n nativeCurrencies,\n }: CurrencyRatePollingInput): Promise<void> {\n await this.updateExchangeRate(nativeCurrencies);\n }\n}\n\nexport default CurrencyRateController;\n"]}
|
|
1
|
+
{"version":3,"file":"CurrencyRateController.cjs","sourceRoot":"","sources":["../src/CurrencyRateController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAKA,iEAGoC;AAOpC,qEAA+E;AAE/E,6CAAoC;AAIpC,oEAAyE;AAyBzE,MAAM,IAAI,GAAG,wBAAwB,CAAC;A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AAiB;QACpB,GAAG,aAAa;KACjB,CAAC;AACJ,CAAC;AAyEH,kBAAe,sBAAsB,CAAC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport {\n TESTNET_TICKER_SYMBOLS,\n FALL_BACK_VS_CURRENCY,\n} from '@metamask/controller-utils';\nimport type { Messenger } from '@metamask/messenger';\nimport type {\n NetworkControllerGetNetworkClientByIdAction,\n NetworkControllerGetStateAction,\n NetworkConfiguration,\n} from '@metamask/network-controller';\nimport { StaticIntervalPollingController } from '@metamask/polling-controller';\nimport type { Hex } from '@metamask/utils';\nimport { Mutex } from 'async-mutex';\n\nimport type { CurrencyRateControllerMethodActions } from './CurrencyRateController-method-action-types';\nimport type { AbstractTokenPricesService } from './token-prices-service/abstract-token-prices-service';\nimport { getNativeTokenAddress } from './token-prices-service/codefi-v2';\n\n/**\n * currencyRates - Object keyed by native currency\n *\n * currencyRates.conversionDate - Timestamp of conversion rate expressed in ms since UNIX epoch\n *\n * currencyRates.conversionRate - Conversion rate from current base asset to the current currency\n *\n * currentCurrency - Currently-active ISO 4217 currency code\n *\n * usdConversionRate - Conversion rate from usd to the current currency\n */\nexport type CurrencyRateState = {\n currentCurrency: string;\n currencyRates: Record<\n string,\n {\n conversionDate: number | null;\n conversionRate: number | null;\n usdConversionRate: number | null;\n }\n >;\n};\n\nconst name = 'CurrencyRateController';\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'setCurrentCurrency',\n 'updateExchangeRate',\n] as const;\n\nexport type CurrencyRateStateChange = ControllerStateChangeEvent<\n typeof name,\n CurrencyRateState\n>;\n\nexport type CurrencyRateControllerEvents = CurrencyRateStateChange;\n\nexport type CurrencyRateControllerGetStateAction = ControllerGetStateAction<\n typeof name,\n CurrencyRateState\n>;\n\nexport type CurrencyRateControllerActions =\n | CurrencyRateControllerGetStateAction\n | CurrencyRateControllerMethodActions;\n\ntype AllowedActions =\n | NetworkControllerGetNetworkClientByIdAction\n | NetworkControllerGetStateAction;\n\nexport type CurrencyRateMessenger = Messenger<\n typeof name,\n CurrencyRateControllerActions | AllowedActions,\n CurrencyRateControllerEvents\n>;\n\nconst metadata: StateMetadata<CurrencyRateState> = {\n currentCurrency: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n currencyRates: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n};\n\nconst defaultState = {\n currentCurrency: 'usd',\n currencyRates: {\n ETH: {\n conversionDate: 0,\n conversionRate: 0,\n usdConversionRate: null,\n },\n },\n};\n\n/** The input to start polling for the {@link CurrencyRateController} */\ntype CurrencyRatePollingInput = {\n nativeCurrencies: string[];\n};\n\nconst boundedPrecisionNumber = (value: number, precision = 9): number =>\n Number(value.toFixed(precision));\n\n/**\n * Controller that passively polls on a set interval for an exchange rate from the current network\n * asset to the user's preferred currency.\n */\n/** Result from attempting to fetch rates from an API */\ntype FetchRatesResult = {\n /** Successfully fetched rates */\n rates: CurrencyRateState['currencyRates'];\n /** Currencies that failed and need fallback or null state */\n failedCurrencies: Record<string, string>;\n};\n\n/**\n * Controller that passively polls on a set interval for an exchange rate from the current network\n * asset to the user's preferred currency.\n */\nexport class CurrencyRateController extends StaticIntervalPollingController<CurrencyRatePollingInput>()<\n typeof name,\n CurrencyRateState,\n CurrencyRateMessenger\n> {\n readonly #mutex = new Mutex();\n\n readonly #includeUsdRate: boolean;\n\n readonly #useExternalServices: () => boolean;\n\n readonly #tokenPricesService: AbstractTokenPricesService;\n\n /**\n * Creates a CurrencyRateController instance.\n *\n * @param options - Constructor options.\n * @param options.includeUsdRate - Keep track of the USD rate in addition to the current currency rate.\n * @param options.interval - The polling interval, in milliseconds.\n * @param options.messenger - A reference to the messenger.\n * @param options.state - Initial state to set on this controller.\n * @param options.useExternalServices - Feature Switch for using external services (default: true)\n * @param options.tokenPricesService - An object in charge of retrieving token prices\n */\n constructor({\n includeUsdRate = false,\n interval = 180000,\n useExternalServices = () => true,\n messenger,\n state,\n tokenPricesService,\n }: {\n includeUsdRate?: boolean;\n interval?: number;\n messenger: CurrencyRateMessenger;\n state?: Partial<CurrencyRateState>;\n useExternalServices?: () => boolean;\n tokenPricesService: AbstractTokenPricesService;\n }) {\n super({\n name,\n metadata,\n messenger,\n state: { ...defaultState, ...state },\n });\n this.#includeUsdRate = includeUsdRate;\n this.#useExternalServices = useExternalServices;\n this.setIntervalLength(interval);\n this.#tokenPricesService = tokenPricesService;\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n }\n\n /**\n * Sets a currency to track.\n *\n * @param currentCurrency - ISO 4217 currency code.\n */\n async setCurrentCurrency(currentCurrency: string): Promise<void> {\n const releaseLock = await this.#mutex.acquire();\n const nativeCurrencies = Object.keys(this.state.currencyRates);\n try {\n this.update(() => {\n return {\n ...defaultState,\n currentCurrency,\n };\n });\n } finally {\n releaseLock();\n }\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this.updateExchangeRate(nativeCurrencies);\n }\n\n /**\n * Attempts to fetch exchange rates from the primary Price API.\n *\n * @param nativeCurrenciesToFetch - Map of native currency to the currency symbol to fetch.\n * @param currentCurrency - The current fiat currency to get rates for.\n * @returns Object containing successful rates and currencies that failed.\n */\n async #fetchRatesFromPriceApi(\n nativeCurrenciesToFetch: Record<string, string>,\n currentCurrency: string,\n ): Promise<FetchRatesResult> {\n const rates: CurrencyRateState['currencyRates'] = {};\n let failedCurrencies: Record<string, string> = {};\n\n try {\n const response = await this.#tokenPricesService.fetchExchangeRates({\n baseCurrency: currentCurrency,\n includeUsdRate: this.#includeUsdRate,\n cryptocurrencies: [...new Set(Object.values(nativeCurrenciesToFetch))],\n });\n\n Object.entries(nativeCurrenciesToFetch).forEach(\n ([nativeCurrency, fetchedCurrency]) => {\n const rate = response[fetchedCurrency.toLowerCase()];\n\n if (rate?.value) {\n rates[nativeCurrency] = {\n conversionDate: Date.now() / 1000,\n conversionRate: boundedPrecisionNumber(1 / rate.value),\n usdConversionRate: rate?.usd\n ? boundedPrecisionNumber(1 / rate.usd)\n : null,\n };\n } else {\n failedCurrencies[nativeCurrency] = fetchedCurrency;\n }\n },\n );\n } catch (error) {\n console.error('Failed to fetch exchange rates.', error);\n failedCurrencies = { ...nativeCurrenciesToFetch };\n }\n\n return { rates, failedCurrencies };\n }\n\n /**\n * Fetches exchange rates from the token prices service as a fallback.\n * This method is designed to never throw - all errors are handled internally\n * and result in currencies being marked as failed.\n *\n * @param currenciesToFetch - Map of native currencies that need fallback fetching.\n * @param currentCurrency - The current fiat currency to get rates for.\n * @returns Object containing successful rates and currencies that failed.\n */\n async #fetchRatesFromTokenPricesService(\n currenciesToFetch: Record<string, string>,\n currentCurrency: string,\n ): Promise<FetchRatesResult> {\n try {\n const rates: CurrencyRateState['currencyRates'] = {};\n const failedCurrencies: Record<string, string> = {};\n\n const networkControllerState = this.messenger.call(\n 'NetworkController:getState',\n );\n const networkConfigurations =\n networkControllerState.networkConfigurationsByChainId;\n\n // Build a map of nativeCurrency -> chainId for currencies to fetch\n const currencyToChainIds = Object.entries(currenciesToFetch).reduce<\n Record<string, { fetchedCurrency: string; chainId: Hex }>\n >((acc, [nativeCurrency, fetchedCurrency]) => {\n const matchingEntry = (\n Object.entries(networkConfigurations) as [Hex, NetworkConfiguration][]\n ).find(\n ([, config]) =>\n config.nativeCurrency.toUpperCase() ===\n fetchedCurrency.toUpperCase(),\n );\n\n if (matchingEntry) {\n acc[nativeCurrency] = { fetchedCurrency, chainId: matchingEntry[0] };\n } else {\n // No matching network configuration - mark as failed\n failedCurrencies[nativeCurrency] = fetchedCurrency;\n }\n return acc;\n }, {});\n\n const currencyToChainIdsEntries = Object.entries(currencyToChainIds);\n const ratesResults = await Promise.allSettled(\n currencyToChainIdsEntries.map(async ([nativeCurrency, { chainId }]) => {\n const nativeTokenAddress = getNativeTokenAddress(chainId);\n const tokenPrices = await this.#tokenPricesService.fetchTokenPrices({\n assets: [{ chainId, tokenAddress: nativeTokenAddress }],\n currency: currentCurrency,\n });\n\n const tokenPrice = tokenPrices.find(\n (item) =>\n item.tokenAddress.toLowerCase() ===\n nativeTokenAddress.toLowerCase(),\n );\n\n return {\n nativeCurrency,\n conversionDate: tokenPrice ? Date.now() / 1000 : null,\n conversionRate: tokenPrice?.price\n ? boundedPrecisionNumber(tokenPrice.price)\n : null,\n usdConversionRate: null,\n };\n }),\n );\n\n ratesResults.forEach((result, index) => {\n const [nativeCurrency, { fetchedCurrency, chainId }] =\n currencyToChainIdsEntries[index];\n\n if (result.status === 'fulfilled' && result.value.conversionRate) {\n rates[nativeCurrency] = {\n conversionDate: result.value.conversionDate,\n conversionRate: result.value.conversionRate,\n usdConversionRate: result.value.usdConversionRate,\n };\n } else {\n if (result.status === 'rejected') {\n console.error(\n `Failed to fetch token price for ${nativeCurrency} on chain ${chainId}`,\n result.reason,\n );\n }\n failedCurrencies[nativeCurrency] = fetchedCurrency;\n }\n });\n\n return { rates, failedCurrencies };\n } catch (error) {\n console.error(\n 'Failed to fetch exchange rates from token prices service.',\n error,\n );\n // Return all currencies as failed\n return { rates: {}, failedCurrencies: { ...currenciesToFetch } };\n }\n }\n\n /**\n * Creates null rate entries for currencies that couldn't be fetched.\n *\n * @param currencies - Array of currency symbols to create null entries for.\n * @returns Null rate entries for all provided currencies.\n */\n #createNullRatesForCurrencies(\n currencies: string[],\n ): CurrencyRateState['currencyRates'] {\n return currencies.reduce<CurrencyRateState['currencyRates']>(\n (acc, nativeCurrency) => {\n acc[nativeCurrency] = {\n conversionDate: null,\n conversionRate: null,\n usdConversionRate: null,\n };\n return acc;\n },\n {},\n );\n }\n\n /**\n * Fetches exchange rates with fallback logic.\n * First tries the Price API, then falls back to token prices service for any failed currencies.\n *\n * @param nativeCurrenciesToFetch - Map of native currency to the currency symbol to fetch.\n * @returns Exchange rates for all requested currencies.\n */\n async #fetchExchangeRatesWithFallback(\n nativeCurrenciesToFetch: Record<string, string>,\n ): Promise<CurrencyRateState['currencyRates']> {\n const { currentCurrency } = this.state;\n\n // Step 1: Try the Price API exchange rates first\n const {\n rates: ratesPriceApi,\n failedCurrencies: failedCurrenciesFromPriceApi,\n } = await this.#fetchRatesFromPriceApi(\n nativeCurrenciesToFetch,\n currentCurrency,\n );\n\n // Step 2: If all currencies succeeded, return early\n if (Object.keys(failedCurrenciesFromPriceApi).length === 0) {\n return ratesPriceApi;\n }\n\n // Step 3: Fallback using token prices service for failed currencies\n const {\n rates: ratesFromFallback,\n failedCurrencies: failedCurrenciesFromFallback,\n } = await this.#fetchRatesFromTokenPricesService(\n failedCurrenciesFromPriceApi,\n currentCurrency,\n );\n\n // Step 4: Create null rates for currencies that failed both approaches\n const nullRates = this.#createNullRatesForCurrencies(\n Object.keys(failedCurrenciesFromFallback),\n );\n\n // Step 5: Merge all results - Price API rates take priority, then fallback, then null rates\n return {\n ...nullRates,\n ...ratesFromFallback,\n ...ratesPriceApi,\n };\n }\n\n /**\n * Updates the exchange rate for the current currency and native currency pairs.\n *\n * @param nativeCurrencies - The native currency symbols to fetch exchange rates for.\n */\n async updateExchangeRate(\n nativeCurrencies: (string | undefined)[],\n ): Promise<void> {\n if (!this.#useExternalServices()) {\n return;\n }\n\n const releaseLock = await this.#mutex.acquire();\n try {\n // For preloaded testnets (Goerli, Sepolia) we want to fetch exchange rate for real ETH.\n // Map each native currency to the symbol we want to fetch for it.\n const testnetSymbols = Object.values(TESTNET_TICKER_SYMBOLS);\n const nativeCurrenciesToFetch = nativeCurrencies.reduce<\n Record<string, string>\n >((acc, nativeCurrency) => {\n if (!nativeCurrency) {\n return acc;\n }\n\n acc[nativeCurrency] = testnetSymbols.includes(nativeCurrency)\n ? FALL_BACK_VS_CURRENCY\n : nativeCurrency;\n return acc;\n }, {});\n\n const rates = await this.#fetchExchangeRatesWithFallback(\n nativeCurrenciesToFetch,\n );\n\n this.update((state) => {\n state.currencyRates = {\n ...state.currencyRates,\n ...rates,\n };\n });\n } catch (error) {\n console.error('Failed to fetch exchange rates.', error);\n throw error;\n } finally {\n releaseLock();\n }\n }\n\n /**\n * Prepare to discard this controller.\n *\n * This stops any active polling.\n */\n override destroy(): void {\n super.destroy();\n this.stopAllPolling();\n }\n\n /**\n * Updates exchange rate for the current currency.\n *\n * @param input - The input for the poll.\n * @param input.nativeCurrencies - The native currency symbols to poll prices for.\n */\n async _executePoll({\n nativeCurrencies,\n }: CurrencyRatePollingInput): Promise<void> {\n await this.updateExchangeRate(nativeCurrencies);\n }\n}\n\nexport default CurrencyRateController;\n"]}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { ControllerGetStateAction, ControllerStateChangeEvent } from "@metamask/base-controller";
|
|
2
2
|
import type { Messenger } from "@metamask/messenger";
|
|
3
3
|
import type { NetworkControllerGetNetworkClientByIdAction, NetworkControllerGetStateAction } from "@metamask/network-controller";
|
|
4
|
+
import type { CurrencyRateControllerMethodActions } from "./CurrencyRateController-method-action-types.cjs";
|
|
4
5
|
import type { AbstractTokenPricesService } from "./token-prices-service/abstract-token-prices-service.cjs";
|
|
5
6
|
/**
|
|
6
7
|
* currencyRates - Object keyed by native currency
|
|
@@ -24,8 +25,8 @@ export type CurrencyRateState = {
|
|
|
24
25
|
declare const name = "CurrencyRateController";
|
|
25
26
|
export type CurrencyRateStateChange = ControllerStateChangeEvent<typeof name, CurrencyRateState>;
|
|
26
27
|
export type CurrencyRateControllerEvents = CurrencyRateStateChange;
|
|
27
|
-
export type
|
|
28
|
-
export type CurrencyRateControllerActions =
|
|
28
|
+
export type CurrencyRateControllerGetStateAction = ControllerGetStateAction<typeof name, CurrencyRateState>;
|
|
29
|
+
export type CurrencyRateControllerActions = CurrencyRateControllerGetStateAction | CurrencyRateControllerMethodActions;
|
|
29
30
|
type AllowedActions = NetworkControllerGetNetworkClientByIdAction | NetworkControllerGetStateAction;
|
|
30
31
|
export type CurrencyRateMessenger = Messenger<typeof name, CurrencyRateControllerActions | AllowedActions, CurrencyRateControllerEvents>;
|
|
31
32
|
/** The input to start polling for the {@link CurrencyRateController} */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CurrencyRateController.d.cts","sourceRoot":"","sources":["../src/CurrencyRateController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AAKnC,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,OAAO,KAAK,EACV,2CAA2C,EAC3C,+BAA+B,EAEhC,qCAAqC;AAKtC,OAAO,KAAK,EAAE,0BAA0B,EAAE,iEAA6D;AAGvG;;;;;;;;;;GAUG;AACH,MAAM,MAAM,iBAAiB,GAAG;IAC9B,eAAe,EAAE,MAAM,CAAC;IACxB,aAAa,EAAE,MAAM,CACnB,MAAM,EACN;QACE,cAAc,EAAE,MAAM,GAAG,IAAI,CAAC;QAC9B,cAAc,EAAE,MAAM,GAAG,IAAI,CAAC;QAC9B,iBAAiB,EAAE,MAAM,GAAG,IAAI,CAAC;KAClC,CACF,CAAC;CACH,CAAC;AAEF,QAAA,MAAM,IAAI,2BAA2B,CAAC;
|
|
1
|
+
{"version":3,"file":"CurrencyRateController.d.cts","sourceRoot":"","sources":["../src/CurrencyRateController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AAKnC,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,OAAO,KAAK,EACV,2CAA2C,EAC3C,+BAA+B,EAEhC,qCAAqC;AAKtC,OAAO,KAAK,EAAE,mCAAmC,EAAE,yDAAqD;AACxG,OAAO,KAAK,EAAE,0BAA0B,EAAE,iEAA6D;AAGvG;;;;;;;;;;GAUG;AACH,MAAM,MAAM,iBAAiB,GAAG;IAC9B,eAAe,EAAE,MAAM,CAAC;IACxB,aAAa,EAAE,MAAM,CACnB,MAAM,EACN;QACE,cAAc,EAAE,MAAM,GA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,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IA4OhE;;;;OAIG;IACG,kBAAkB,CACtB,gBAAgB,EAAE,CAAC,MAAM,GAAG,SAAS,CAAC,EAAE,GACvC,OAAO,CAAC,IAAI,CAAC;IAyChB;;;;OAIG;IACM,OAAO,IAAI,IAAI;IAKxB;;;;;OAKG;IACG,YAAY,CAAC,EACjB,gBAAgB,GACjB,EAAE,wBAAwB,GAAG,OAAO,CAAC,IAAI,CAAC;CAG5C;AAED,eAAe,sBAAsB,CAAC"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { ControllerGetStateAction, ControllerStateChangeEvent } from "@metamask/base-controller";
|
|
2
2
|
import type { Messenger } from "@metamask/messenger";
|
|
3
3
|
import type { NetworkControllerGetNetworkClientByIdAction, NetworkControllerGetStateAction } from "@metamask/network-controller";
|
|
4
|
+
import type { CurrencyRateControllerMethodActions } from "./CurrencyRateController-method-action-types.mjs";
|
|
4
5
|
import type { AbstractTokenPricesService } from "./token-prices-service/abstract-token-prices-service.mjs";
|
|
5
6
|
/**
|
|
6
7
|
* currencyRates - Object keyed by native currency
|
|
@@ -24,8 +25,8 @@ export type CurrencyRateState = {
|
|
|
24
25
|
declare const name = "CurrencyRateController";
|
|
25
26
|
export type CurrencyRateStateChange = ControllerStateChangeEvent<typeof name, CurrencyRateState>;
|
|
26
27
|
export type CurrencyRateControllerEvents = CurrencyRateStateChange;
|
|
27
|
-
export type
|
|
28
|
-
export type CurrencyRateControllerActions =
|
|
28
|
+
export type CurrencyRateControllerGetStateAction = ControllerGetStateAction<typeof name, CurrencyRateState>;
|
|
29
|
+
export type CurrencyRateControllerActions = CurrencyRateControllerGetStateAction | CurrencyRateControllerMethodActions;
|
|
29
30
|
type AllowedActions = NetworkControllerGetNetworkClientByIdAction | NetworkControllerGetStateAction;
|
|
30
31
|
export type CurrencyRateMessenger = Messenger<typeof name, CurrencyRateControllerActions | AllowedActions, CurrencyRateControllerEvents>;
|
|
31
32
|
/** The input to start polling for the {@link CurrencyRateController} */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CurrencyRateController.d.mts","sourceRoot":"","sources":["../src/CurrencyRateController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AAKnC,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,OAAO,KAAK,EACV,2CAA2C,EAC3C,+BAA+B,EAEhC,qCAAqC;AAKtC,OAAO,KAAK,EAAE,0BAA0B,EAAE,iEAA6D;AAGvG;;;;;;;;;;GAUG;AACH,MAAM,MAAM,iBAAiB,GAAG;IAC9B,eAAe,EAAE,MAAM,CAAC;IACxB,aAAa,EAAE,MAAM,CACnB,MAAM,EACN;QACE,cAAc,EAAE,MAAM,GAAG,IAAI,CAAC;QAC9B,cAAc,EAAE,MAAM,GAAG,IAAI,CAAC;QAC9B,iBAAiB,EAAE,MAAM,GAAG,IAAI,CAAC;KAClC,CACF,CAAC;CACH,CAAC;AAEF,QAAA,MAAM,IAAI,2BAA2B,CAAC;
|
|
1
|
+
{"version":3,"file":"CurrencyRateController.d.mts","sourceRoot":"","sources":["../src/CurrencyRateController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AAKnC,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,OAAO,KAAK,EACV,2CAA2C,EAC3C,+BAA+B,EAEhC,qCAAqC;AAKtC,OAAO,KAAK,EAAE,mCAAmC,EAAE,yDAAqD;AACxG,OAAO,KAAK,EAAE,0BAA0B,EAAE,iEAA6D;AAGvG;;;;;;;;;;GAUG;AACH,MAAM,MAAM,iBAAiB,GAAG;IAC9B,eAAe,EAAE,MAAM,CAAC;IACxB,aAAa,EAAE,MAAM,CACnB,MAAM,EACN;QACE,cAAc,EAAE,MAAM,GAAG,IAAI,CAAC;QAC9B,cAAc,EAAE,MAAM,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C;IA4OhE;;;;OAIG;IACG,kBAAkB,CACtB,gBAAgB,EAAE,CAAC,MAAM,GAAG,SAAS,CAAC,EAAE,GACvC,OAAO,CAAC,IAAI,CAAC;IAyChB;;;;OAIG;IACM,OAAO,IAAI,IAAI;IAKxB;;;;;OAKG;IACG,YAAY,CAAC,EACjB,gBAAgB,GACjB,EAAE,wBAAwB,GAAG,OAAO,CAAC,IAAI,CAAC;CAG5C;AAED,eAAe,sBAAsB,CAAC"}
|
|
@@ -15,6 +15,10 @@ import { StaticIntervalPollingController } from "@metamask/polling-controller";
|
|
|
15
15
|
import { Mutex } from "async-mutex";
|
|
16
16
|
import { getNativeTokenAddress } from "./token-prices-service/codefi-v2.mjs";
|
|
17
17
|
const name = 'CurrencyRateController';
|
|
18
|
+
const MESSENGER_EXPOSED_METHODS = [
|
|
19
|
+
'setCurrentCurrency',
|
|
20
|
+
'updateExchangeRate',
|
|
21
|
+
];
|
|
18
22
|
const metadata = {
|
|
19
23
|
currentCurrency: {
|
|
20
24
|
includeInStateLogs: true,
|
|
@@ -72,6 +76,7 @@ export class CurrencyRateController extends StaticIntervalPollingController() {
|
|
|
72
76
|
__classPrivateFieldSet(this, _CurrencyRateController_useExternalServices, useExternalServices, "f");
|
|
73
77
|
this.setIntervalLength(interval);
|
|
74
78
|
__classPrivateFieldSet(this, _CurrencyRateController_tokenPricesService, tokenPricesService, "f");
|
|
79
|
+
this.messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
|
|
75
80
|
}
|
|
76
81
|
/**
|
|
77
82
|
* Sets a currency to track.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CurrencyRateController.mjs","sourceRoot":"","sources":["../src/CurrencyRateController.ts"],"names":[],"mappings":";;;;;;;;;;;;AAKA,OAAO,EACL,sBAAsB,EACtB,qBAAqB,EACtB,mCAAmC;AAOpC,OAAO,EAAE,+BAA+B,EAAE,qCAAqC;AAE/E,OAAO,EAAE,KAAK,EAAE,oBAAoB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["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport {\n TESTNET_TICKER_SYMBOLS,\n FALL_BACK_VS_CURRENCY,\n} from '@metamask/controller-utils';\nimport type { Messenger } from '@metamask/messenger';\nimport type {\n NetworkControllerGetNetworkClientByIdAction,\n NetworkControllerGetStateAction,\n NetworkConfiguration,\n} from '@metamask/network-controller';\nimport { StaticIntervalPollingController } from '@metamask/polling-controller';\nimport type { Hex } from '@metamask/utils';\nimport { Mutex } from 'async-mutex';\n\nimport type { AbstractTokenPricesService } from './token-prices-service/abstract-token-prices-service';\nimport { getNativeTokenAddress } from './token-prices-service/codefi-v2';\n\n/**\n * currencyRates - Object keyed by native currency\n *\n * currencyRates.conversionDate - Timestamp of conversion rate expressed in ms since UNIX epoch\n *\n * currencyRates.conversionRate - Conversion rate from current base asset to the current currency\n *\n * currentCurrency - Currently-active ISO 4217 currency code\n *\n * usdConversionRate - Conversion rate from usd to the current currency\n */\nexport type CurrencyRateState = {\n currentCurrency: string;\n currencyRates: Record<\n string,\n {\n conversionDate: number | null;\n conversionRate: number | null;\n usdConversionRate: number | null;\n }\n >;\n};\n\nconst name = 'CurrencyRateController';\n\nexport type CurrencyRateStateChange = ControllerStateChangeEvent<\n typeof name,\n CurrencyRateState\n>;\n\nexport type CurrencyRateControllerEvents = CurrencyRateStateChange;\n\nexport type GetCurrencyRateState = ControllerGetStateAction<\n typeof name,\n CurrencyRateState\n>;\n\nexport type CurrencyRateControllerActions = GetCurrencyRateState;\n\ntype AllowedActions =\n | NetworkControllerGetNetworkClientByIdAction\n | NetworkControllerGetStateAction;\n\nexport type CurrencyRateMessenger = Messenger<\n typeof name,\n CurrencyRateControllerActions | AllowedActions,\n CurrencyRateControllerEvents\n>;\n\nconst metadata: StateMetadata<CurrencyRateState> = {\n currentCurrency: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n currencyRates: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n};\n\nconst defaultState = {\n currentCurrency: 'usd',\n currencyRates: {\n ETH: {\n conversionDate: 0,\n conversionRate: 0,\n usdConversionRate: null,\n },\n },\n};\n\n/** The input to start polling for the {@link CurrencyRateController} */\ntype CurrencyRatePollingInput = {\n nativeCurrencies: string[];\n};\n\nconst boundedPrecisionNumber = (value: number, precision = 9): number =>\n Number(value.toFixed(precision));\n\n/**\n * Controller that passively polls on a set interval for an exchange rate from the current network\n * asset to the user's preferred currency.\n */\n/** Result from attempting to fetch rates from an API */\ntype FetchRatesResult = {\n /** Successfully fetched rates */\n rates: CurrencyRateState['currencyRates'];\n /** Currencies that failed and need fallback or null state */\n failedCurrencies: Record<string, string>;\n};\n\n/**\n * Controller that passively polls on a set interval for an exchange rate from the current network\n * asset to the user's preferred currency.\n */\nexport class CurrencyRateController extends StaticIntervalPollingController<CurrencyRatePollingInput>()<\n typeof name,\n CurrencyRateState,\n CurrencyRateMessenger\n> {\n readonly #mutex = new Mutex();\n\n readonly #includeUsdRate: boolean;\n\n readonly #useExternalServices: () => boolean;\n\n readonly #tokenPricesService: AbstractTokenPricesService;\n\n /**\n * Creates a CurrencyRateController instance.\n *\n * @param options - Constructor options.\n * @param options.includeUsdRate - Keep track of the USD rate in addition to the current currency rate.\n * @param options.interval - The polling interval, in milliseconds.\n * @param options.messenger - A reference to the messenger.\n * @param options.state - Initial state to set on this controller.\n * @param options.useExternalServices - Feature Switch for using external services (default: true)\n * @param options.tokenPricesService - An object in charge of retrieving token prices\n */\n constructor({\n includeUsdRate = false,\n interval = 180000,\n useExternalServices = () => true,\n messenger,\n state,\n tokenPricesService,\n }: {\n includeUsdRate?: boolean;\n interval?: number;\n messenger: CurrencyRateMessenger;\n state?: Partial<CurrencyRateState>;\n useExternalServices?: () => boolean;\n tokenPricesService: AbstractTokenPricesService;\n }) {\n super({\n name,\n metadata,\n messenger,\n state: { ...defaultState, ...state },\n });\n this.#includeUsdRate = includeUsdRate;\n this.#useExternalServices = useExternalServices;\n this.setIntervalLength(interval);\n this.#tokenPricesService = tokenPricesService;\n }\n\n /**\n * Sets a currency to track.\n *\n * @param currentCurrency - ISO 4217 currency code.\n */\n async setCurrentCurrency(currentCurrency: string): Promise<void> {\n const releaseLock = await this.#mutex.acquire();\n const nativeCurrencies = Object.keys(this.state.currencyRates);\n try {\n this.update(() => {\n return {\n ...defaultState,\n currentCurrency,\n };\n });\n } finally {\n releaseLock();\n }\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this.updateExchangeRate(nativeCurrencies);\n }\n\n /**\n * Attempts to fetch exchange rates from the primary Price API.\n *\n * @param nativeCurrenciesToFetch - Map of native currency to the currency symbol to fetch.\n * @param currentCurrency - The current fiat currency to get rates for.\n * @returns Object containing successful rates and currencies that failed.\n */\n async #fetchRatesFromPriceApi(\n nativeCurrenciesToFetch: Record<string, string>,\n currentCurrency: string,\n ): Promise<FetchRatesResult> {\n const rates: CurrencyRateState['currencyRates'] = {};\n let failedCurrencies: Record<string, string> = {};\n\n try {\n const response = await this.#tokenPricesService.fetchExchangeRates({\n baseCurrency: currentCurrency,\n includeUsdRate: this.#includeUsdRate,\n cryptocurrencies: [...new Set(Object.values(nativeCurrenciesToFetch))],\n });\n\n Object.entries(nativeCurrenciesToFetch).forEach(\n ([nativeCurrency, fetchedCurrency]) => {\n const rate = response[fetchedCurrency.toLowerCase()];\n\n if (rate?.value) {\n rates[nativeCurrency] = {\n conversionDate: Date.now() / 1000,\n conversionRate: boundedPrecisionNumber(1 / rate.value),\n usdConversionRate: rate?.usd\n ? boundedPrecisionNumber(1 / rate.usd)\n : null,\n };\n } else {\n failedCurrencies[nativeCurrency] = fetchedCurrency;\n }\n },\n );\n } catch (error) {\n console.error('Failed to fetch exchange rates.', error);\n failedCurrencies = { ...nativeCurrenciesToFetch };\n }\n\n return { rates, failedCurrencies };\n }\n\n /**\n * Fetches exchange rates from the token prices service as a fallback.\n * This method is designed to never throw - all errors are handled internally\n * and result in currencies being marked as failed.\n *\n * @param currenciesToFetch - Map of native currencies that need fallback fetching.\n * @param currentCurrency - The current fiat currency to get rates for.\n * @returns Object containing successful rates and currencies that failed.\n */\n async #fetchRatesFromTokenPricesService(\n currenciesToFetch: Record<string, string>,\n currentCurrency: string,\n ): Promise<FetchRatesResult> {\n try {\n const rates: CurrencyRateState['currencyRates'] = {};\n const failedCurrencies: Record<string, string> = {};\n\n const networkControllerState = this.messenger.call(\n 'NetworkController:getState',\n );\n const networkConfigurations =\n networkControllerState.networkConfigurationsByChainId;\n\n // Build a map of nativeCurrency -> chainId for currencies to fetch\n const currencyToChainIds = Object.entries(currenciesToFetch).reduce<\n Record<string, { fetchedCurrency: string; chainId: Hex }>\n >((acc, [nativeCurrency, fetchedCurrency]) => {\n const matchingEntry = (\n Object.entries(networkConfigurations) as [Hex, NetworkConfiguration][]\n ).find(\n ([, config]) =>\n config.nativeCurrency.toUpperCase() ===\n fetchedCurrency.toUpperCase(),\n );\n\n if (matchingEntry) {\n acc[nativeCurrency] = { fetchedCurrency, chainId: matchingEntry[0] };\n } else {\n // No matching network configuration - mark as failed\n failedCurrencies[nativeCurrency] = fetchedCurrency;\n }\n return acc;\n }, {});\n\n const currencyToChainIdsEntries = Object.entries(currencyToChainIds);\n const ratesResults = await Promise.allSettled(\n currencyToChainIdsEntries.map(async ([nativeCurrency, { chainId }]) => {\n const nativeTokenAddress = getNativeTokenAddress(chainId);\n const tokenPrices = await this.#tokenPricesService.fetchTokenPrices({\n assets: [{ chainId, tokenAddress: nativeTokenAddress }],\n currency: currentCurrency,\n });\n\n const tokenPrice = tokenPrices.find(\n (item) =>\n item.tokenAddress.toLowerCase() ===\n nativeTokenAddress.toLowerCase(),\n );\n\n return {\n nativeCurrency,\n conversionDate: tokenPrice ? Date.now() / 1000 : null,\n conversionRate: tokenPrice?.price\n ? boundedPrecisionNumber(tokenPrice.price)\n : null,\n usdConversionRate: null,\n };\n }),\n );\n\n ratesResults.forEach((result, index) => {\n const [nativeCurrency, { fetchedCurrency, chainId }] =\n currencyToChainIdsEntries[index];\n\n if (result.status === 'fulfilled' && result.value.conversionRate) {\n rates[nativeCurrency] = {\n conversionDate: result.value.conversionDate,\n conversionRate: result.value.conversionRate,\n usdConversionRate: result.value.usdConversionRate,\n };\n } else {\n if (result.status === 'rejected') {\n console.error(\n `Failed to fetch token price for ${nativeCurrency} on chain ${chainId}`,\n result.reason,\n );\n }\n failedCurrencies[nativeCurrency] = fetchedCurrency;\n }\n });\n\n return { rates, failedCurrencies };\n } catch (error) {\n console.error(\n 'Failed to fetch exchange rates from token prices service.',\n error,\n );\n // Return all currencies as failed\n return { rates: {}, failedCurrencies: { ...currenciesToFetch } };\n }\n }\n\n /**\n * Creates null rate entries for currencies that couldn't be fetched.\n *\n * @param currencies - Array of currency symbols to create null entries for.\n * @returns Null rate entries for all provided currencies.\n */\n #createNullRatesForCurrencies(\n currencies: string[],\n ): CurrencyRateState['currencyRates'] {\n return currencies.reduce<CurrencyRateState['currencyRates']>(\n (acc, nativeCurrency) => {\n acc[nativeCurrency] = {\n conversionDate: null,\n conversionRate: null,\n usdConversionRate: null,\n };\n return acc;\n },\n {},\n );\n }\n\n /**\n * Fetches exchange rates with fallback logic.\n * First tries the Price API, then falls back to token prices service for any failed currencies.\n *\n * @param nativeCurrenciesToFetch - Map of native currency to the currency symbol to fetch.\n * @returns Exchange rates for all requested currencies.\n */\n async #fetchExchangeRatesWithFallback(\n nativeCurrenciesToFetch: Record<string, string>,\n ): Promise<CurrencyRateState['currencyRates']> {\n const { currentCurrency } = this.state;\n\n // Step 1: Try the Price API exchange rates first\n const {\n rates: ratesPriceApi,\n failedCurrencies: failedCurrenciesFromPriceApi,\n } = await this.#fetchRatesFromPriceApi(\n nativeCurrenciesToFetch,\n currentCurrency,\n );\n\n // Step 2: If all currencies succeeded, return early\n if (Object.keys(failedCurrenciesFromPriceApi).length === 0) {\n return ratesPriceApi;\n }\n\n // Step 3: Fallback using token prices service for failed currencies\n const {\n rates: ratesFromFallback,\n failedCurrencies: failedCurrenciesFromFallback,\n } = await this.#fetchRatesFromTokenPricesService(\n failedCurrenciesFromPriceApi,\n currentCurrency,\n );\n\n // Step 4: Create null rates for currencies that failed both approaches\n const nullRates = this.#createNullRatesForCurrencies(\n Object.keys(failedCurrenciesFromFallback),\n );\n\n // Step 5: Merge all results - Price API rates take priority, then fallback, then null rates\n return {\n ...nullRates,\n ...ratesFromFallback,\n ...ratesPriceApi,\n };\n }\n\n /**\n * Updates the exchange rate for the current currency and native currency pairs.\n *\n * @param nativeCurrencies - The native currency symbols to fetch exchange rates for.\n */\n async updateExchangeRate(\n nativeCurrencies: (string | undefined)[],\n ): Promise<void> {\n if (!this.#useExternalServices()) {\n return;\n }\n\n const releaseLock = await this.#mutex.acquire();\n try {\n // For preloaded testnets (Goerli, Sepolia) we want to fetch exchange rate for real ETH.\n // Map each native currency to the symbol we want to fetch for it.\n const testnetSymbols = Object.values(TESTNET_TICKER_SYMBOLS);\n const nativeCurrenciesToFetch = nativeCurrencies.reduce<\n Record<string, string>\n >((acc, nativeCurrency) => {\n if (!nativeCurrency) {\n return acc;\n }\n\n acc[nativeCurrency] = testnetSymbols.includes(nativeCurrency)\n ? FALL_BACK_VS_CURRENCY\n : nativeCurrency;\n return acc;\n }, {});\n\n const rates = await this.#fetchExchangeRatesWithFallback(\n nativeCurrenciesToFetch,\n );\n\n this.update((state) => {\n state.currencyRates = {\n ...state.currencyRates,\n ...rates,\n };\n });\n } catch (error) {\n console.error('Failed to fetch exchange rates.', error);\n throw error;\n } finally {\n releaseLock();\n }\n }\n\n /**\n * Prepare to discard this controller.\n *\n * This stops any active polling.\n */\n override destroy(): void {\n super.destroy();\n this.stopAllPolling();\n }\n\n /**\n * Updates exchange rate for the current currency.\n *\n * @param input - The input for the poll.\n * @param input.nativeCurrencies - The native currency symbols to poll prices for.\n */\n async _executePoll({\n nativeCurrencies,\n }: CurrencyRatePollingInput): Promise<void> {\n await this.updateExchangeRate(nativeCurrencies);\n }\n}\n\nexport default CurrencyRateController;\n"]}
|
|
1
|
+
{"version":3,"file":"CurrencyRateController.mjs","sourceRoot":"","sources":["../src/CurrencyRateController.ts"],"names":[],"mappings":";;;;;;;;;;;;AAKA,OAAO,EACL,sBAAsB,EACtB,qBAAqB,EACtB,mCAAmC;AAOpC,OAAO,EAAE,+BAA+B,EAAE,qCAAqC;AAE/E,OAAO,EAAE,KAAK,EAAE,oBAAoB;AAIpC,OAAO,EAAE,qBAAqB,EAAE,6CAAyC;AAyBzE,MAAM,IAAI,GAAG,wBAAwB,CAAC;AAEtC,MAAM,yBAAyB,GAAG;IAChC,oBAAoB;IACpB,oBAAoB;CACZ,CAAC;AA4BX,MAAM,QAAQ,GAAqC;IACjD,eAAe,EAAE;QACf,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,QAAQ,EAAE,IAAI;KACf;IACD,aAAa,EAAE;QACb,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,IAAI;QACb,sBAAsB,EAAE,IAAI;QAC5B,QAAQ,EAAE,IAAI;KACf;CACF,CAAC;AAEF,MAAM,YAAY,GAAG;IACnB,eAAe,EAAE,KAAK;IACtB,aAAa,EAAE;QACb,GAAG,EAAE;YACH,cAAc,EAAE,CAAC;YACjB,cAAc,EAAE,CAAC;YACjB,iBAAiB,EAAE,IAAI;SACxB;KACF;CACF,CAAC;AAOF,MAAM,sBAAsB,GAAG,CAAC,KAAa,EAAE,SAAS,GAAG,CAAC,EAAU,EAAE,CACtE,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC;AAcnC;;;GAGG;AACH,MAAM,OAAO,sBAAuB,SAAQ,+BAA+B,EAI1E;IASC;;;;;;;;;;OAUG;IACH,YAAY,EACV,cAAc,GAAG,KAAK,EACtB,QAAQ,GAAG,MAAM,EACjB,mBAAmB,GAAG,G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,CAAC;IAED,oEAAoE;IACpE,MAAM,EACJ,KAAK,EAAE,iBAAiB,EACxB,gBAAgB,EAAE,4BAA4B,GAC/C,GAAG,MAAM,uBAAA,IAAI,mGAAkC,MAAtC,IAAI,EACZ,4BAA4B,EAC5B,eAAe,CAChB,CAAC;IAEF,uEAAuE;IACvE,MAAM,SAAS,GAAG,uBAAA,IAAI,+FAA8B,MAAlC,IAAI,EACpB,MAAM,CAAC,IAAI,CAAC,4BAA4B,CAAC,CAC1C,CAAC;IAEF,4FAA4F;IAC5F,OAAO;QACL,GAAG,SAAS;QACZ,GAAG,iBAAiB;QACpB,GAAG,aAAa;KACjB,CAAC;AACJ,CAAC;AAyEH,eAAe,sBAAsB,CAAC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport {\n TESTNET_TICKER_SYMBOLS,\n FALL_BACK_VS_CURRENCY,\n} from '@metamask/controller-utils';\nimport type { Messenger } from '@metamask/messenger';\nimport type {\n NetworkControllerGetNetworkClientByIdAction,\n NetworkControllerGetStateAction,\n NetworkConfiguration,\n} from '@metamask/network-controller';\nimport { StaticIntervalPollingController } from '@metamask/polling-controller';\nimport type { Hex } from '@metamask/utils';\nimport { Mutex } from 'async-mutex';\n\nimport type { CurrencyRateControllerMethodActions } from './CurrencyRateController-method-action-types';\nimport type { AbstractTokenPricesService } from './token-prices-service/abstract-token-prices-service';\nimport { getNativeTokenAddress } from './token-prices-service/codefi-v2';\n\n/**\n * currencyRates - Object keyed by native currency\n *\n * currencyRates.conversionDate - Timestamp of conversion rate expressed in ms since UNIX epoch\n *\n * currencyRates.conversionRate - Conversion rate from current base asset to the current currency\n *\n * currentCurrency - Currently-active ISO 4217 currency code\n *\n * usdConversionRate - Conversion rate from usd to the current currency\n */\nexport type CurrencyRateState = {\n currentCurrency: string;\n currencyRates: Record<\n string,\n {\n conversionDate: number | null;\n conversionRate: number | null;\n usdConversionRate: number | null;\n }\n >;\n};\n\nconst name = 'CurrencyRateController';\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'setCurrentCurrency',\n 'updateExchangeRate',\n] as const;\n\nexport type CurrencyRateStateChange = ControllerStateChangeEvent<\n typeof name,\n CurrencyRateState\n>;\n\nexport type CurrencyRateControllerEvents = CurrencyRateStateChange;\n\nexport type CurrencyRateControllerGetStateAction = ControllerGetStateAction<\n typeof name,\n CurrencyRateState\n>;\n\nexport type CurrencyRateControllerActions =\n | CurrencyRateControllerGetStateAction\n | CurrencyRateControllerMethodActions;\n\ntype AllowedActions =\n | NetworkControllerGetNetworkClientByIdAction\n | NetworkControllerGetStateAction;\n\nexport type CurrencyRateMessenger = Messenger<\n typeof name,\n CurrencyRateControllerActions | AllowedActions,\n CurrencyRateControllerEvents\n>;\n\nconst metadata: StateMetadata<CurrencyRateState> = {\n currentCurrency: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n currencyRates: {\n includeInStateLogs: true,\n persist: true,\n includeInDebugSnapshot: true,\n usedInUi: true,\n },\n};\n\nconst defaultState = {\n currentCurrency: 'usd',\n currencyRates: {\n ETH: {\n conversionDate: 0,\n conversionRate: 0,\n usdConversionRate: null,\n },\n },\n};\n\n/** The input to start polling for the {@link CurrencyRateController} */\ntype CurrencyRatePollingInput = {\n nativeCurrencies: string[];\n};\n\nconst boundedPrecisionNumber = (value: number, precision = 9): number =>\n Number(value.toFixed(precision));\n\n/**\n * Controller that passively polls on a set interval for an exchange rate from the current network\n * asset to the user's preferred currency.\n */\n/** Result from attempting to fetch rates from an API */\ntype FetchRatesResult = {\n /** Successfully fetched rates */\n rates: CurrencyRateState['currencyRates'];\n /** Currencies that failed and need fallback or null state */\n failedCurrencies: Record<string, string>;\n};\n\n/**\n * Controller that passively polls on a set interval for an exchange rate from the current network\n * asset to the user's preferred currency.\n */\nexport class CurrencyRateController extends StaticIntervalPollingController<CurrencyRatePollingInput>()<\n typeof name,\n CurrencyRateState,\n CurrencyRateMessenger\n> {\n readonly #mutex = new Mutex();\n\n readonly #includeUsdRate: boolean;\n\n readonly #useExternalServices: () => boolean;\n\n readonly #tokenPricesService: AbstractTokenPricesService;\n\n /**\n * Creates a CurrencyRateController instance.\n *\n * @param options - Constructor options.\n * @param options.includeUsdRate - Keep track of the USD rate in addition to the current currency rate.\n * @param options.interval - The polling interval, in milliseconds.\n * @param options.messenger - A reference to the messenger.\n * @param options.state - Initial state to set on this controller.\n * @param options.useExternalServices - Feature Switch for using external services (default: true)\n * @param options.tokenPricesService - An object in charge of retrieving token prices\n */\n constructor({\n includeUsdRate = false,\n interval = 180000,\n useExternalServices = () => true,\n messenger,\n state,\n tokenPricesService,\n }: {\n includeUsdRate?: boolean;\n interval?: number;\n messenger: CurrencyRateMessenger;\n state?: Partial<CurrencyRateState>;\n useExternalServices?: () => boolean;\n tokenPricesService: AbstractTokenPricesService;\n }) {\n super({\n name,\n metadata,\n messenger,\n state: { ...defaultState, ...state },\n });\n this.#includeUsdRate = includeUsdRate;\n this.#useExternalServices = useExternalServices;\n this.setIntervalLength(interval);\n this.#tokenPricesService = tokenPricesService;\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n }\n\n /**\n * Sets a currency to track.\n *\n * @param currentCurrency - ISO 4217 currency code.\n */\n async setCurrentCurrency(currentCurrency: string): Promise<void> {\n const releaseLock = await this.#mutex.acquire();\n const nativeCurrencies = Object.keys(this.state.currencyRates);\n try {\n this.update(() => {\n return {\n ...defaultState,\n currentCurrency,\n };\n });\n } finally {\n releaseLock();\n }\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n this.updateExchangeRate(nativeCurrencies);\n }\n\n /**\n * Attempts to fetch exchange rates from the primary Price API.\n *\n * @param nativeCurrenciesToFetch - Map of native currency to the currency symbol to fetch.\n * @param currentCurrency - The current fiat currency to get rates for.\n * @returns Object containing successful rates and currencies that failed.\n */\n async #fetchRatesFromPriceApi(\n nativeCurrenciesToFetch: Record<string, string>,\n currentCurrency: string,\n ): Promise<FetchRatesResult> {\n const rates: CurrencyRateState['currencyRates'] = {};\n let failedCurrencies: Record<string, string> = {};\n\n try {\n const response = await this.#tokenPricesService.fetchExchangeRates({\n baseCurrency: currentCurrency,\n includeUsdRate: this.#includeUsdRate,\n cryptocurrencies: [...new Set(Object.values(nativeCurrenciesToFetch))],\n });\n\n Object.entries(nativeCurrenciesToFetch).forEach(\n ([nativeCurrency, fetchedCurrency]) => {\n const rate = response[fetchedCurrency.toLowerCase()];\n\n if (rate?.value) {\n rates[nativeCurrency] = {\n conversionDate: Date.now() / 1000,\n conversionRate: boundedPrecisionNumber(1 / rate.value),\n usdConversionRate: rate?.usd\n ? boundedPrecisionNumber(1 / rate.usd)\n : null,\n };\n } else {\n failedCurrencies[nativeCurrency] = fetchedCurrency;\n }\n },\n );\n } catch (error) {\n console.error('Failed to fetch exchange rates.', error);\n failedCurrencies = { ...nativeCurrenciesToFetch };\n }\n\n return { rates, failedCurrencies };\n }\n\n /**\n * Fetches exchange rates from the token prices service as a fallback.\n * This method is designed to never throw - all errors are handled internally\n * and result in currencies being marked as failed.\n *\n * @param currenciesToFetch - Map of native currencies that need fallback fetching.\n * @param currentCurrency - The current fiat currency to get rates for.\n * @returns Object containing successful rates and currencies that failed.\n */\n async #fetchRatesFromTokenPricesService(\n currenciesToFetch: Record<string, string>,\n currentCurrency: string,\n ): Promise<FetchRatesResult> {\n try {\n const rates: CurrencyRateState['currencyRates'] = {};\n const failedCurrencies: Record<string, string> = {};\n\n const networkControllerState = this.messenger.call(\n 'NetworkController:getState',\n );\n const networkConfigurations =\n networkControllerState.networkConfigurationsByChainId;\n\n // Build a map of nativeCurrency -> chainId for currencies to fetch\n const currencyToChainIds = Object.entries(currenciesToFetch).reduce<\n Record<string, { fetchedCurrency: string; chainId: Hex }>\n >((acc, [nativeCurrency, fetchedCurrency]) => {\n const matchingEntry = (\n Object.entries(networkConfigurations) as [Hex, NetworkConfiguration][]\n ).find(\n ([, config]) =>\n config.nativeCurrency.toUpperCase() ===\n fetchedCurrency.toUpperCase(),\n );\n\n if (matchingEntry) {\n acc[nativeCurrency] = { fetchedCurrency, chainId: matchingEntry[0] };\n } else {\n // No matching network configuration - mark as failed\n failedCurrencies[nativeCurrency] = fetchedCurrency;\n }\n return acc;\n }, {});\n\n const currencyToChainIdsEntries = Object.entries(currencyToChainIds);\n const ratesResults = await Promise.allSettled(\n currencyToChainIdsEntries.map(async ([nativeCurrency, { chainId }]) => {\n const nativeTokenAddress = getNativeTokenAddress(chainId);\n const tokenPrices = await this.#tokenPricesService.fetchTokenPrices({\n assets: [{ chainId, tokenAddress: nativeTokenAddress }],\n currency: currentCurrency,\n });\n\n const tokenPrice = tokenPrices.find(\n (item) =>\n item.tokenAddress.toLowerCase() ===\n nativeTokenAddress.toLowerCase(),\n );\n\n return {\n nativeCurrency,\n conversionDate: tokenPrice ? Date.now() / 1000 : null,\n conversionRate: tokenPrice?.price\n ? boundedPrecisionNumber(tokenPrice.price)\n : null,\n usdConversionRate: null,\n };\n }),\n );\n\n ratesResults.forEach((result, index) => {\n const [nativeCurrency, { fetchedCurrency, chainId }] =\n currencyToChainIdsEntries[index];\n\n if (result.status === 'fulfilled' && result.value.conversionRate) {\n rates[nativeCurrency] = {\n conversionDate: result.value.conversionDate,\n conversionRate: result.value.conversionRate,\n usdConversionRate: result.value.usdConversionRate,\n };\n } else {\n if (result.status === 'rejected') {\n console.error(\n `Failed to fetch token price for ${nativeCurrency} on chain ${chainId}`,\n result.reason,\n );\n }\n failedCurrencies[nativeCurrency] = fetchedCurrency;\n }\n });\n\n return { rates, failedCurrencies };\n } catch (error) {\n console.error(\n 'Failed to fetch exchange rates from token prices service.',\n error,\n );\n // Return all currencies as failed\n return { rates: {}, failedCurrencies: { ...currenciesToFetch } };\n }\n }\n\n /**\n * Creates null rate entries for currencies that couldn't be fetched.\n *\n * @param currencies - Array of currency symbols to create null entries for.\n * @returns Null rate entries for all provided currencies.\n */\n #createNullRatesForCurrencies(\n currencies: string[],\n ): CurrencyRateState['currencyRates'] {\n return currencies.reduce<CurrencyRateState['currencyRates']>(\n (acc, nativeCurrency) => {\n acc[nativeCurrency] = {\n conversionDate: null,\n conversionRate: null,\n usdConversionRate: null,\n };\n return acc;\n },\n {},\n );\n }\n\n /**\n * Fetches exchange rates with fallback logic.\n * First tries the Price API, then falls back to token prices service for any failed currencies.\n *\n * @param nativeCurrenciesToFetch - Map of native currency to the currency symbol to fetch.\n * @returns Exchange rates for all requested currencies.\n */\n async #fetchExchangeRatesWithFallback(\n nativeCurrenciesToFetch: Record<string, string>,\n ): Promise<CurrencyRateState['currencyRates']> {\n const { currentCurrency } = this.state;\n\n // Step 1: Try the Price API exchange rates first\n const {\n rates: ratesPriceApi,\n failedCurrencies: failedCurrenciesFromPriceApi,\n } = await this.#fetchRatesFromPriceApi(\n nativeCurrenciesToFetch,\n currentCurrency,\n );\n\n // Step 2: If all currencies succeeded, return early\n if (Object.keys(failedCurrenciesFromPriceApi).length === 0) {\n return ratesPriceApi;\n }\n\n // Step 3: Fallback using token prices service for failed currencies\n const {\n rates: ratesFromFallback,\n failedCurrencies: failedCurrenciesFromFallback,\n } = await this.#fetchRatesFromTokenPricesService(\n failedCurrenciesFromPriceApi,\n currentCurrency,\n );\n\n // Step 4: Create null rates for currencies that failed both approaches\n const nullRates = this.#createNullRatesForCurrencies(\n Object.keys(failedCurrenciesFromFallback),\n );\n\n // Step 5: Merge all results - Price API rates take priority, then fallback, then null rates\n return {\n ...nullRates,\n ...ratesFromFallback,\n ...ratesPriceApi,\n };\n }\n\n /**\n * Updates the exchange rate for the current currency and native currency pairs.\n *\n * @param nativeCurrencies - The native currency symbols to fetch exchange rates for.\n */\n async updateExchangeRate(\n nativeCurrencies: (string | undefined)[],\n ): Promise<void> {\n if (!this.#useExternalServices()) {\n return;\n }\n\n const releaseLock = await this.#mutex.acquire();\n try {\n // For preloaded testnets (Goerli, Sepolia) we want to fetch exchange rate for real ETH.\n // Map each native currency to the symbol we want to fetch for it.\n const testnetSymbols = Object.values(TESTNET_TICKER_SYMBOLS);\n const nativeCurrenciesToFetch = nativeCurrencies.reduce<\n Record<string, string>\n >((acc, nativeCurrency) => {\n if (!nativeCurrency) {\n return acc;\n }\n\n acc[nativeCurrency] = testnetSymbols.includes(nativeCurrency)\n ? FALL_BACK_VS_CURRENCY\n : nativeCurrency;\n return acc;\n }, {});\n\n const rates = await this.#fetchExchangeRatesWithFallback(\n nativeCurrenciesToFetch,\n );\n\n this.update((state) => {\n state.currencyRates = {\n ...state.currencyRates,\n ...rates,\n };\n });\n } catch (error) {\n console.error('Failed to fetch exchange rates.', error);\n throw error;\n } finally {\n releaseLock();\n }\n }\n\n /**\n * Prepare to discard this controller.\n *\n * This stops any active polling.\n */\n override destroy(): void {\n super.destroy();\n this.stopAllPolling();\n }\n\n /**\n * Updates exchange rate for the current currency.\n *\n * @param input - The input for the poll.\n * @param input.nativeCurrencies - The native currency symbols to poll prices for.\n */\n async _executePoll({\n nativeCurrencies,\n }: CurrencyRatePollingInput): Promise<void> {\n await this.updateExchangeRate(nativeCurrencies);\n }\n}\n\nexport default CurrencyRateController;\n"]}
|
|
@@ -493,7 +493,7 @@ async function _MultichainAssetsController_getAssetsMetadataFrom(assets, snapId)
|
|
|
493
493
|
});
|
|
494
494
|
}, _MultichainAssetsController_filterBlockaidSpamTokensOnAdd =
|
|
495
495
|
/**
|
|
496
|
-
* Fail-
|
|
496
|
+
* Fail-open Blockaid filter for newly detected `token:` assets (native/other namespaces unchanged).
|
|
497
497
|
*
|
|
498
498
|
* @param assets - CAIP assets to filter.
|
|
499
499
|
* @returns Filtered list, original order preserved.
|
|
@@ -503,33 +503,25 @@ async function _MultichainAssetsController_filterBlockaidSpamTokensOnAdd(assets)
|
|
|
503
503
|
if (Object.keys(tokensByChain).length === 0) {
|
|
504
504
|
return [...assets];
|
|
505
505
|
}
|
|
506
|
-
const
|
|
506
|
+
const rejectedAssets = new Set();
|
|
507
507
|
for (const [chainName, tokenEntries] of Object.entries(tokensByChain)) {
|
|
508
508
|
const batchOutcomes = await __classPrivateFieldGet(this, _MultichainAssetsController_instances, "m", _MultichainAssetsController_runBatchedBulkTokenScans).call(this, chainName, tokenEntries);
|
|
509
509
|
for (const outcome of batchOutcomes) {
|
|
510
510
|
if (outcome.status === 'rejected') {
|
|
511
|
+
// Fail-open: if API fails, allow all tokens in this batch through
|
|
511
512
|
continue;
|
|
512
513
|
}
|
|
513
514
|
for (const entry of outcome.entries) {
|
|
514
515
|
const scanned = outcome.response[entry.address];
|
|
516
|
+
// Reject only if we have a definitive malicious result
|
|
515
517
|
if (scanned?.result_type &&
|
|
516
|
-
scanned.result_type
|
|
517
|
-
|
|
518
|
+
scanned.result_type === phishing_controller_1.TokenScanResultType.Malicious) {
|
|
519
|
+
rejectedAssets.add(entry.asset);
|
|
518
520
|
}
|
|
519
521
|
}
|
|
520
522
|
}
|
|
521
523
|
}
|
|
522
|
-
return assets.filter((asset) =>
|
|
523
|
-
try {
|
|
524
|
-
if ((0, utils_1.parseCaipAssetType)(asset).assetNamespace === 'token') {
|
|
525
|
-
return keptTokenAssets.has(asset);
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
catch {
|
|
529
|
-
return false;
|
|
530
|
-
}
|
|
531
|
-
return true;
|
|
532
|
-
});
|
|
524
|
+
return assets.filter((asset) => !rejectedAssets.has(asset));
|
|
533
525
|
}, _MultichainAssetsController_findMaliciousTokensAmong =
|
|
534
526
|
/**
|
|
535
527
|
* SPL `token:` assets in state that Blockaid marks malicious (failed batches skipped).
|