@metamask/assets-controllers 40.0.0 → 42.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 +28 -1
- package/dist/AccountTrackerController.d.cts +4 -4
- package/dist/AccountTrackerController.d.mts +4 -4
- package/dist/CurrencyRateController.cjs +40 -58
- package/dist/CurrencyRateController.cjs.map +1 -1
- package/dist/CurrencyRateController.d.cts +15 -15
- package/dist/CurrencyRateController.d.cts.map +1 -1
- package/dist/CurrencyRateController.d.mts +15 -15
- package/dist/CurrencyRateController.d.mts.map +1 -1
- package/dist/CurrencyRateController.mjs +41 -59
- package/dist/CurrencyRateController.mjs.map +1 -1
- package/dist/NftController.cjs +8 -0
- package/dist/NftController.cjs.map +1 -1
- package/dist/NftController.d.cts +4 -0
- package/dist/NftController.d.cts.map +1 -1
- package/dist/NftController.d.mts +4 -0
- package/dist/NftController.d.mts.map +1 -1
- package/dist/NftController.mjs +8 -0
- package/dist/NftController.mjs.map +1 -1
- package/dist/RatesController/RatesController.cjs +1 -1
- package/dist/RatesController/RatesController.cjs.map +1 -1
- package/dist/RatesController/RatesController.mjs +1 -1
- package/dist/RatesController/RatesController.mjs.map +1 -1
- package/dist/RatesController/types.cjs.map +1 -1
- package/dist/RatesController/types.d.cts +6 -7
- package/dist/RatesController/types.d.cts.map +1 -1
- package/dist/RatesController/types.d.mts +6 -7
- package/dist/RatesController/types.d.mts.map +1 -1
- package/dist/RatesController/types.mjs.map +1 -1
- package/dist/TokenBalancesController.cjs +8 -0
- package/dist/TokenBalancesController.cjs.map +1 -1
- package/dist/TokenBalancesController.d.cts +4 -0
- package/dist/TokenBalancesController.d.cts.map +1 -1
- package/dist/TokenBalancesController.d.mts +4 -0
- package/dist/TokenBalancesController.d.mts.map +1 -1
- package/dist/TokenBalancesController.mjs +8 -0
- package/dist/TokenBalancesController.mjs.map +1 -1
- package/dist/TokenDetectionController.cjs +58 -16
- package/dist/TokenDetectionController.cjs.map +1 -1
- package/dist/TokenDetectionController.d.cts +7 -5
- package/dist/TokenDetectionController.d.cts.map +1 -1
- package/dist/TokenDetectionController.d.mts +7 -5
- package/dist/TokenDetectionController.d.mts.map +1 -1
- package/dist/TokenDetectionController.mjs +59 -16
- package/dist/TokenDetectionController.mjs.map +1 -1
- package/dist/TokenListController.cjs.map +1 -1
- package/dist/TokenListController.d.cts +5 -5
- package/dist/TokenListController.d.cts.map +1 -1
- package/dist/TokenListController.d.mts +5 -5
- package/dist/TokenListController.d.mts.map +1 -1
- package/dist/TokenListController.mjs.map +1 -1
- package/dist/TokenRatesController.cjs +43 -13
- package/dist/TokenRatesController.cjs.map +1 -1
- package/dist/TokenRatesController.d.cts +12 -8
- package/dist/TokenRatesController.d.cts.map +1 -1
- package/dist/TokenRatesController.d.mts +12 -8
- package/dist/TokenRatesController.d.mts.map +1 -1
- package/dist/TokenRatesController.mjs +43 -13
- package/dist/TokenRatesController.mjs.map +1 -1
- package/dist/TokensController.cjs +8 -0
- package/dist/TokensController.cjs.map +1 -1
- package/dist/TokensController.d.cts +4 -0
- package/dist/TokensController.d.cts.map +1 -1
- package/dist/TokensController.d.mts +4 -0
- package/dist/TokensController.d.mts.map +1 -1
- package/dist/TokensController.mjs +8 -0
- package/dist/TokensController.mjs.map +1 -1
- package/dist/crypto-compare-service/crypto-compare.cjs +8 -6
- package/dist/crypto-compare-service/crypto-compare.cjs.map +1 -1
- package/dist/crypto-compare-service/crypto-compare.d.cts +1 -1
- package/dist/crypto-compare-service/crypto-compare.d.cts.map +1 -1
- package/dist/crypto-compare-service/crypto-compare.d.mts +1 -1
- package/dist/crypto-compare-service/crypto-compare.d.mts.map +1 -1
- package/dist/crypto-compare-service/crypto-compare.mjs +8 -6
- package/dist/crypto-compare-service/crypto-compare.mjs.map +1 -1
- package/dist/multi-chain-accounts-service/multi-chain-accounts.cjs +7 -2
- package/dist/multi-chain-accounts-service/multi-chain-accounts.cjs.map +1 -1
- package/dist/multi-chain-accounts-service/multi-chain-accounts.d.cts +3 -2
- package/dist/multi-chain-accounts-service/multi-chain-accounts.d.cts.map +1 -1
- package/dist/multi-chain-accounts-service/multi-chain-accounts.d.mts +3 -2
- package/dist/multi-chain-accounts-service/multi-chain-accounts.d.mts.map +1 -1
- package/dist/multi-chain-accounts-service/multi-chain-accounts.mjs +7 -2
- package/dist/multi-chain-accounts-service/multi-chain-accounts.mjs.map +1 -1
- package/package.json +9 -9
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,31 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [42.0.0]
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- Add `resetState` method to `NftController`, `TokensController`, `TokenBalancesController` and `TokenRatesController` to reset the controller's state back to their default state ([#4880](https://github.com/MetaMask/core/pull/4880))
|
|
15
|
+
|
|
16
|
+
### Changed
|
|
17
|
+
|
|
18
|
+
- **BREAKING**: A `platform` argument must now be passed to the `TokenDetectionController` constructor, indicating whether the platform is extension or mobile. ([#4877](https://github.com/MetaMask/core/pull/4877))
|
|
19
|
+
- **BREAKING**: The `TokenRatesController` now accepts `{chainId: Hex}` as its polling input to `startPolling()` instead of `{networkClientId: NetworkClientId}` ([#4887](https://github.com/MetaMask/core/pull/4887))
|
|
20
|
+
- When the `TokenRatesController`'s subscription to `TokensController:stateChange` is fired, token prices are now updated across all chain IDs whose tokens changed, instead of just the current chain. ([#4866](https://github.com/MetaMask/core/pull/4866))
|
|
21
|
+
- The `TokenDetectionController` now passes a `x-metamask-clientproduct` header when calling the account API. ([#4877](https://github.com/MetaMask/core/pull/4877))
|
|
22
|
+
|
|
23
|
+
## [41.0.0]
|
|
24
|
+
|
|
25
|
+
### Changed
|
|
26
|
+
|
|
27
|
+
- **BREAKING**: The polling input accepted by `CurrencyRateController` is now an object with a `nativeCurrencies` property that is defined as a `string` array type ([#4852](https://github.com/MetaMask/core/pull/4852))
|
|
28
|
+
- The `input` parameters of the controller's `_executePoll`, `_startPolling`, `onPollingComplete` methods now only accept this new polling input type.
|
|
29
|
+
- The `nativeCurrency` property (`string` type) has been removed.
|
|
30
|
+
- **BREAKING**: `RatesController` now types the `conversionRate` and `usdConversionRate` in its state as `number` instead of `string`, to match what it was actually storing. ([#4852](https://github.com/MetaMask/core/pull/4852))
|
|
31
|
+
- Bump `@metamask/base-controller` from `^7.0.1` to `^7.0.2` ([#4862](https://github.com/MetaMask/core/pull/4862))
|
|
32
|
+
- Bump `@metamask/controller-utils` from `^11.4.0` to `^11.4.1` ([#4862](https://github.com/MetaMask/core/pull/4862))
|
|
33
|
+
- Bump dev dependency `@metamask/approval-controller` from `^7.1.0` to `^7.1.1` ([#4862](https://github.com/MetaMask/core/pull/4862))
|
|
34
|
+
|
|
10
35
|
## [40.0.0]
|
|
11
36
|
|
|
12
37
|
### Changed
|
|
@@ -1164,7 +1189,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
1164
1189
|
|
|
1165
1190
|
- Use Ethers for AssetsContractController ([#845](https://github.com/MetaMask/core/pull/845))
|
|
1166
1191
|
|
|
1167
|
-
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@
|
|
1192
|
+
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@42.0.0...HEAD
|
|
1193
|
+
[42.0.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@41.0.0...@metamask/assets-controllers@42.0.0
|
|
1194
|
+
[41.0.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@40.0.0...@metamask/assets-controllers@41.0.0
|
|
1168
1195
|
[40.0.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@39.0.0...@metamask/assets-controllers@40.0.0
|
|
1169
1196
|
[39.0.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@38.3.0...@metamask/assets-controllers@39.0.0
|
|
1170
1197
|
[38.3.0]: https://github.com/MetaMask/core/compare/@metamask/assets-controllers@38.2.0...@metamask/assets-controllers@38.3.0
|
|
@@ -62,14 +62,14 @@ type AccountTrackerPollingInput = {
|
|
|
62
62
|
networkClientId: NetworkClientId;
|
|
63
63
|
};
|
|
64
64
|
declare const AccountTrackerController_base: (abstract new (...args: any[]) => {
|
|
65
|
-
readonly "__#
|
|
66
|
-
"__#
|
|
65
|
+
readonly "__#787890@#intervalIds": Record<string, NodeJS.Timeout>;
|
|
66
|
+
"__#787890@#intervalLength": number | undefined;
|
|
67
67
|
setIntervalLength(intervalLength: number): void;
|
|
68
68
|
getIntervalLength(): number | undefined;
|
|
69
69
|
_startPolling(input: AccountTrackerPollingInput): void;
|
|
70
70
|
_stopPollingByPollingTokenSetId(key: string): void;
|
|
71
|
-
readonly "__#
|
|
72
|
-
"__#
|
|
71
|
+
readonly "__#787882@#pollingTokenSets": Map<string, Set<string>>;
|
|
72
|
+
"__#787882@#callbacks": Map<string, Set<(input: AccountTrackerPollingInput) => void>>;
|
|
73
73
|
_executePoll(input: AccountTrackerPollingInput): Promise<void>;
|
|
74
74
|
/**
|
|
75
75
|
* The action that can be performed to get the state of the {@link AccountTrackerController}.
|
|
@@ -62,14 +62,14 @@ type AccountTrackerPollingInput = {
|
|
|
62
62
|
networkClientId: NetworkClientId;
|
|
63
63
|
};
|
|
64
64
|
declare const AccountTrackerController_base: (abstract new (...args: any[]) => {
|
|
65
|
-
readonly "__#
|
|
66
|
-
"__#
|
|
65
|
+
readonly "__#787890@#intervalIds": Record<string, NodeJS.Timeout>;
|
|
66
|
+
"__#787890@#intervalLength": number | undefined;
|
|
67
67
|
setIntervalLength(intervalLength: number): void;
|
|
68
68
|
getIntervalLength(): number | undefined;
|
|
69
69
|
_startPolling(input: AccountTrackerPollingInput): void;
|
|
70
70
|
_stopPollingByPollingTokenSetId(key: string): void;
|
|
71
|
-
readonly "__#
|
|
72
|
-
"__#
|
|
71
|
+
readonly "__#787882@#pollingTokenSets": Map<string, Set<string>>;
|
|
72
|
+
"__#787882@#callbacks": Map<string, Set<(input: AccountTrackerPollingInput) => void>>;
|
|
73
73
|
_executePoll(input: AccountTrackerPollingInput): Promise<void>;
|
|
74
74
|
/**
|
|
75
75
|
* The action that can be performed to get the state of the {@link AccountTrackerController}.
|
|
@@ -33,9 +33,9 @@ class CurrencyRateController extends (0, polling_controller_1.StaticIntervalPoll
|
|
|
33
33
|
* @param options.interval - The polling interval, in milliseconds.
|
|
34
34
|
* @param options.messenger - A reference to the messaging system.
|
|
35
35
|
* @param options.state - Initial state to set on this controller.
|
|
36
|
-
* @param options.
|
|
36
|
+
* @param options.fetchMultiExchangeRate - Fetches the exchange rate from an external API. This option is primarily meant for use in unit tests.
|
|
37
37
|
*/
|
|
38
|
-
constructor({ includeUsdRate = false, interval = 180000, messenger, state,
|
|
38
|
+
constructor({ includeUsdRate = false, interval = 180000, messenger, state, fetchMultiExchangeRate = crypto_compare_service_1.fetchMultiExchangeRate, }) {
|
|
39
39
|
super({
|
|
40
40
|
name,
|
|
41
41
|
metadata,
|
|
@@ -45,7 +45,7 @@ class CurrencyRateController extends (0, polling_controller_1.StaticIntervalPoll
|
|
|
45
45
|
this.mutex = new async_mutex_1.Mutex();
|
|
46
46
|
this.includeUsdRate = includeUsdRate;
|
|
47
47
|
this.setIntervalLength(interval);
|
|
48
|
-
this.
|
|
48
|
+
this.fetchMultiExchangeRate = fetchMultiExchangeRate;
|
|
49
49
|
}
|
|
50
50
|
/**
|
|
51
51
|
* Sets a currency to track.
|
|
@@ -67,68 +67,50 @@ class CurrencyRateController extends (0, polling_controller_1.StaticIntervalPoll
|
|
|
67
67
|
releaseLock();
|
|
68
68
|
}
|
|
69
69
|
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
70
|
-
// eslint-disable-next-line @typescript-eslint/no-
|
|
71
|
-
|
|
70
|
+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
71
|
+
this.updateExchangeRate(nativeCurrencies);
|
|
72
72
|
}
|
|
73
73
|
/**
|
|
74
|
-
* Updates the exchange rate for the current currency and native currency
|
|
74
|
+
* Updates the exchange rate for the current currency and native currency pairs.
|
|
75
75
|
*
|
|
76
|
-
* @param
|
|
76
|
+
* @param nativeCurrencies - The native currency symbols to fetch exchange rates for.
|
|
77
77
|
*/
|
|
78
|
-
async updateExchangeRate(
|
|
78
|
+
async updateExchangeRate(nativeCurrencies) {
|
|
79
79
|
const releaseLock = await this.mutex.acquire();
|
|
80
|
-
const { currentCurrency, currencyRates } = this.state;
|
|
81
|
-
let conversionDate = null;
|
|
82
|
-
let conversionRate = null;
|
|
83
|
-
let usdConversionRate = null;
|
|
84
|
-
// For preloaded testnets (Goerli, Sepolia) we want to fetch exchange rate for real ETH.
|
|
85
|
-
const nativeCurrencyForExchangeRate = Object.values(controller_utils_1.TESTNET_TICKER_SYMBOLS).includes(nativeCurrency)
|
|
86
|
-
? controller_utils_1.FALL_BACK_VS_CURRENCY // ETH
|
|
87
|
-
: nativeCurrency;
|
|
88
|
-
let shouldUpdateState = true;
|
|
89
80
|
try {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
81
|
+
const { currentCurrency } = this.state;
|
|
82
|
+
// For preloaded testnets (Goerli, Sepolia) we want to fetch exchange rate for real ETH.
|
|
83
|
+
// Map each native currency to the symbol we want to fetch for it.
|
|
84
|
+
const testnetSymbols = Object.values(controller_utils_1.TESTNET_TICKER_SYMBOLS);
|
|
85
|
+
const nativeCurrenciesToFetch = nativeCurrencies.reduce((acc, nativeCurrency) => {
|
|
86
|
+
acc[nativeCurrency] = testnetSymbols.includes(nativeCurrency)
|
|
87
|
+
? controller_utils_1.FALL_BACK_VS_CURRENCY
|
|
88
|
+
: nativeCurrency;
|
|
89
|
+
return acc;
|
|
90
|
+
}, {});
|
|
91
|
+
const fetchExchangeRateResponse = await this.fetchMultiExchangeRate(currentCurrency, [...new Set(Object.values(nativeCurrenciesToFetch))], this.includeUsdRate);
|
|
92
|
+
const rates = Object.entries(nativeCurrenciesToFetch).reduce((acc, [nativeCurrency, fetchedCurrency]) => {
|
|
93
|
+
const rate = fetchExchangeRateResponse[fetchedCurrency.toLowerCase()];
|
|
94
|
+
acc[nativeCurrency] = {
|
|
95
|
+
conversionDate: rate !== undefined ? Date.now() / 1000 : null,
|
|
96
|
+
conversionRate: rate?.[currentCurrency.toLowerCase()] ?? null,
|
|
97
|
+
usdConversionRate: rate?.usd ?? null,
|
|
98
|
+
};
|
|
99
|
+
return acc;
|
|
100
|
+
}, {});
|
|
101
|
+
this.update((state) => {
|
|
102
|
+
state.currencyRates = {
|
|
103
|
+
...state.currencyRates,
|
|
104
|
+
...rates,
|
|
105
|
+
};
|
|
106
|
+
});
|
|
102
107
|
}
|
|
103
108
|
catch (error) {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
// Don't update state on transient / unexpected errors
|
|
107
|
-
shouldUpdateState = false;
|
|
108
|
-
throw error;
|
|
109
|
-
}
|
|
109
|
+
console.error('Failed to fetch exchange rates.', error);
|
|
110
|
+
throw error;
|
|
110
111
|
}
|
|
111
112
|
finally {
|
|
112
|
-
|
|
113
|
-
if (shouldUpdateState) {
|
|
114
|
-
this.update(() => {
|
|
115
|
-
return {
|
|
116
|
-
currencyRates: {
|
|
117
|
-
...currencyRates,
|
|
118
|
-
[nativeCurrency]: {
|
|
119
|
-
conversionDate,
|
|
120
|
-
conversionRate,
|
|
121
|
-
usdConversionRate,
|
|
122
|
-
},
|
|
123
|
-
},
|
|
124
|
-
currentCurrency,
|
|
125
|
-
};
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
finally {
|
|
130
|
-
releaseLock();
|
|
131
|
-
}
|
|
113
|
+
releaseLock();
|
|
132
114
|
}
|
|
133
115
|
}
|
|
134
116
|
/**
|
|
@@ -144,10 +126,10 @@ class CurrencyRateController extends (0, polling_controller_1.StaticIntervalPoll
|
|
|
144
126
|
* Updates exchange rate for the current currency.
|
|
145
127
|
*
|
|
146
128
|
* @param input - The input for the poll.
|
|
147
|
-
* @param input.
|
|
129
|
+
* @param input.nativeCurrencies - The native currency symbols to poll prices for.
|
|
148
130
|
*/
|
|
149
|
-
async _executePoll({
|
|
150
|
-
await this.updateExchangeRate(
|
|
131
|
+
async _executePoll({ nativeCurrencies, }) {
|
|
132
|
+
await this.updateExchangeRate(nativeCurrencies);
|
|
151
133
|
}
|
|
152
134
|
}
|
|
153
135
|
exports.CurrencyRateController = CurrencyRateController;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CurrencyRateController.cjs","sourceRoot":"","sources":["../src/CurrencyRateController.ts"],"names":[],"mappings":";;;AAKA,iEAGoC;AAEpC,qEAA+E;AAC/E,6CAAoC;AAEpC,+EAAyF;AAsBzF,MAAM,IAAI,GAAG,wBAAwB,CAAC;AA0BtC,MAAM,QAAQ,GAAG;IACf,eAAe,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE;IACnD,aAAa,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE;CAClD,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;;;GAGG;AACH,MAAa,sBAAuB,SAAQ,IAAA,oDAA+B,GAI1E;IAOC;;;;;;;;;OASG;IACH,YAAY,EACV,cAAc,GAAG,KAAK,EACtB,QAAQ,GAAG,MAAM,EACjB,SAAS,EACT,KAAK,EACL,iBAAiB,GAAG,0CAAwB,GAO7C;QACC,KAAK,CAAC;YACJ,IAAI;YACJ,QAAQ;YACR,SAAS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C,YAAY,CAAC,EACjB,cAAc,GACW;QACzB,MAAM,IAAI,CAAC,kBAAkB,CAAC,cAAc,CAAC,CAAC;IAChD,CAAC;CACF;AAnKD,wDAmKC;AAED,kBAAe,sBAAsB,CAAC","sourcesContent":["import type {\n RestrictedControllerMessenger,\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n} from '@metamask/base-controller';\nimport {\n TESTNET_TICKER_SYMBOLS,\n FALL_BACK_VS_CURRENCY,\n} from '@metamask/controller-utils';\nimport type { NetworkControllerGetNetworkClientByIdAction } from '@metamask/network-controller';\nimport { StaticIntervalPollingController } from '@metamask/polling-controller';\nimport { Mutex } from 'async-mutex';\n\nimport { fetchExchangeRate as defaultFetchExchangeRate } from './crypto-compare-service';\n\n/**\n * @type CurrencyRateState\n * @property currencyRates - Object keyed by native currency\n * @property currencyRates.conversionDate - Timestamp of conversion rate expressed in ms since UNIX epoch\n * @property currencyRates.conversionRate - Conversion rate from current base asset to the current currency\n * @property currentCurrency - Currently-active ISO 4217 currency code\n * @property 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 = NetworkControllerGetNetworkClientByIdAction;\n\ntype CurrencyRateMessenger = RestrictedControllerMessenger<\n typeof name,\n CurrencyRateControllerActions | AllowedActions,\n CurrencyRateControllerEvents,\n AllowedActions['type'],\n never\n>;\n\nconst metadata = {\n currentCurrency: { persist: true, anonymous: true },\n currencyRates: { persist: true, anonymous: true },\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 nativeCurrency: 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 private readonly mutex = new Mutex();\n\n private readonly fetchExchangeRate;\n\n private readonly includeUsdRate;\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 messaging system.\n * @param options.state - Initial state to set on this controller.\n * @param options.fetchExchangeRate - Fetches the exchange rate from an external API. This option is primarily meant for use in unit tests.\n */\n constructor({\n includeUsdRate = false,\n interval = 180000,\n messenger,\n state,\n fetchExchangeRate = defaultFetchExchangeRate,\n }: {\n includeUsdRate?: boolean;\n interval?: number;\n messenger: CurrencyRateMessenger;\n state?: Partial<CurrencyRateState>;\n fetchExchangeRate?: typeof defaultFetchExchangeRate;\n }) {\n super({\n name,\n metadata,\n messenger,\n state: { ...defaultState, ...state },\n });\n this.includeUsdRate = includeUsdRate;\n this.setIntervalLength(interval);\n this.fetchExchangeRate = fetchExchangeRate;\n }\n\n /**\n * Sets a currency to track.\n *\n * @param currentCurrency - ISO 4217 currency code.\n */\n async setCurrentCurrency(currentCurrency: string) {\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-misused-promises\n nativeCurrencies.forEach(this.updateExchangeRate.bind(this));\n }\n\n /**\n * Updates the exchange rate for the current currency and native currency pair.\n *\n * @param nativeCurrency - The ticker symbol for the chain.\n */\n async updateExchangeRate(nativeCurrency: string): Promise<void> {\n const releaseLock = await this.mutex.acquire();\n const { currentCurrency, currencyRates } = this.state;\n\n let conversionDate: number | null = null;\n let conversionRate: number | null = null;\n let usdConversionRate: number | null = null;\n\n // For preloaded testnets (Goerli, Sepolia) we want to fetch exchange rate for real ETH.\n const nativeCurrencyForExchangeRate = Object.values(\n TESTNET_TICKER_SYMBOLS,\n ).includes(nativeCurrency)\n ? FALL_BACK_VS_CURRENCY // ETH\n : nativeCurrency;\n\n let shouldUpdateState = true;\n try {\n if (\n currentCurrency &&\n nativeCurrency &&\n // if either currency is an empty string we can skip the comparison\n // because it will result in an error from the api and ultimately\n // a null conversionRate either way.\n currentCurrency !== '' &&\n nativeCurrency !== ''\n ) {\n const fetchExchangeRateResponse = await this.fetchExchangeRate(\n currentCurrency,\n nativeCurrencyForExchangeRate,\n this.includeUsdRate,\n );\n conversionRate = fetchExchangeRateResponse.conversionRate;\n usdConversionRate = fetchExchangeRateResponse.usdConversionRate;\n conversionDate = Date.now() / 1000;\n }\n } catch (error) {\n if (\n !(\n error instanceof Error &&\n error.message.includes('market does not exist for this coin pair')\n )\n ) {\n // Don't update state on transient / unexpected errors\n shouldUpdateState = false;\n throw error;\n }\n } finally {\n try {\n if (shouldUpdateState) {\n this.update(() => {\n return {\n currencyRates: {\n ...currencyRates,\n [nativeCurrency]: {\n conversionDate,\n conversionRate,\n usdConversionRate,\n },\n },\n currentCurrency,\n };\n });\n }\n } finally {\n releaseLock();\n }\n }\n }\n\n /**\n * Prepare to discard this controller.\n *\n * This stops any active polling.\n */\n override destroy() {\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.nativeCurrency - The native currency symbol to poll prices for.\n */\n async _executePoll({\n nativeCurrency,\n }: CurrencyRatePollingInput): Promise<void> {\n await this.updateExchangeRate(nativeCurrency);\n }\n}\n\nexport default CurrencyRateController;\n"]}
|
|
1
|
+
{"version":3,"file":"CurrencyRateController.cjs","sourceRoot":"","sources":["../src/CurrencyRateController.ts"],"names":[],"mappings":";;;AAKA,iEAGoC;AAEpC,qEAA+E;AAC/E,6CAAoC;AAEpC,+EAAmG;AAsBnG,MAAM,IAAI,GAAG,wBAAwB,CAAC;AA0BtC,MAAM,QAAQ,GAAG;IACf,eAAe,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE;IACnD,aAAa,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE;CAClD,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;;;GAGG;AACH,MAAa,sBAAuB,SAAQ,IAAA,oDAA+B,GAI1E;IAOC;;;;;;;;;OASG;IACH,YAAY,EACV,cAAc,GAAG,KAAK,EACtB,QAAQ,GAAG,MAAM,EACjB,SAAS,EACT,KAAK,EACL,sBAAsB,GAAG,+CAA6B,GAOvD;QACC,KAAK,CAAC;YACJ,IAAI;YACJ,QAAQ;YACR,SAAS;YACT,KAAK,EAAE,EAAE,GAAG,YAAY,EAAE,GAAG,KAAK,EAAE;SACrC,CAAC,CAAC;QAlCY,UAAK,GAAG,IAAI,mBAAK,EAAE,CAAC;QAmCnC,IAAI,CAAC,cAAc,GAAG,cAAc,CAAC;QACrC,IAAI,CAAC,iBAAiB,CAAC,QAAQ,CAAC,CAAC;QACjC,IAAI,CAAC,sBAAsB,GAAG,sBAAsB,CAAC;IACvD,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,kBAAkB,CAAC,eAAuB;QAC9C,MAAM,WAAW,GAAG,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AuB,CAAC,CAAC,MAAM,CAC1D,CAAC,GAAG,EAAE,CAAC,cAAc,EAAE,eAAe,CAAC,EAAE,EAAE;gBACzC,MAAM,IAAI,GAAG,yBAAyB,CAAC,eAAe,CAAC,WAAW,EAAE,CAAC,CAAC;gBACtE,GAAG,CAAC,cAAc,CAAC,GAAG;oBACpB,cAAc,EAAE,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,CAAC,IAAI;oBAC7D,cAAc,EAAE,IAAI,EAAE,CAAC,eAAe,CAAC,WAAW,EAAE,CAAC,IAAI,IAAI;oBAC7D,iBAAiB,EAAE,IAAI,EAAE,GAAG,IAAI,IAAI;iBACrC,CAAC;gBACF,OAAO,GAAG,CAAC;YACb,CAAC,EACD,EAAwC,CACzC,CAAC;YAEF,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;gBACpB,KAAK,CAAC,aAAa,GAAG;oBACpB,GAAG,KAAK,CAAC,aAAa;oBACtB,GAAG,KAAK;iBACT,CAAC;YACJ,CAAC,CAAC,CAAC;SACJ;QAAC,OAAO,KAAK,EAAE;YACd,OAAO,CAAC,KAAK,CAAC,iCAAiC,EAAE,KAAK,CAAC,CAAC;YACxD,MAAM,KAAK,CAAC;SACb;gBAAS;YACR,WAAW,EAAE,CAAC;SACf;IACH,CAAC;IAED;;;;OAIG;IACM,OAAO;QACd,KAAK,CAAC,OAAO,EAAE,CAAC;QAChB,IAAI,CAAC,cAAc,EAAE,CAAC;IACxB,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,YAAY,CAAC,EACjB,gBAAgB,GACS;QACzB,MAAM,IAAI,CAAC,kBAAkB,CAAC,gBAAgB,CAAC,CAAC;IAClD,CAAC;CACF;AAjJD,wDAiJC;AAED,kBAAe,sBAAsB,CAAC","sourcesContent":["import type {\n RestrictedControllerMessenger,\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n} from '@metamask/base-controller';\nimport {\n TESTNET_TICKER_SYMBOLS,\n FALL_BACK_VS_CURRENCY,\n} from '@metamask/controller-utils';\nimport type { NetworkControllerGetNetworkClientByIdAction } from '@metamask/network-controller';\nimport { StaticIntervalPollingController } from '@metamask/polling-controller';\nimport { Mutex } from 'async-mutex';\n\nimport { fetchMultiExchangeRate as defaultFetchMultiExchangeRate } from './crypto-compare-service';\n\n/**\n * @type CurrencyRateState\n * @property currencyRates - Object keyed by native currency\n * @property currencyRates.conversionDate - Timestamp of conversion rate expressed in ms since UNIX epoch\n * @property currencyRates.conversionRate - Conversion rate from current base asset to the current currency\n * @property currentCurrency - Currently-active ISO 4217 currency code\n * @property 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 = NetworkControllerGetNetworkClientByIdAction;\n\ntype CurrencyRateMessenger = RestrictedControllerMessenger<\n typeof name,\n CurrencyRateControllerActions | AllowedActions,\n CurrencyRateControllerEvents,\n AllowedActions['type'],\n never\n>;\n\nconst metadata = {\n currentCurrency: { persist: true, anonymous: true },\n currencyRates: { persist: true, anonymous: true },\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\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 private readonly mutex = new Mutex();\n\n private readonly fetchMultiExchangeRate;\n\n private readonly includeUsdRate;\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 messaging system.\n * @param options.state - Initial state to set on this controller.\n * @param options.fetchMultiExchangeRate - Fetches the exchange rate from an external API. This option is primarily meant for use in unit tests.\n */\n constructor({\n includeUsdRate = false,\n interval = 180000,\n messenger,\n state,\n fetchMultiExchangeRate = defaultFetchMultiExchangeRate,\n }: {\n includeUsdRate?: boolean;\n interval?: number;\n messenger: CurrencyRateMessenger;\n state?: Partial<CurrencyRateState>;\n fetchMultiExchangeRate?: typeof defaultFetchMultiExchangeRate;\n }) {\n super({\n name,\n metadata,\n messenger,\n state: { ...defaultState, ...state },\n });\n this.includeUsdRate = includeUsdRate;\n this.setIntervalLength(interval);\n this.fetchMultiExchangeRate = fetchMultiExchangeRate;\n }\n\n /**\n * Sets a currency to track.\n *\n * @param currentCurrency - ISO 4217 currency code.\n */\n async setCurrentCurrency(currentCurrency: string) {\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 * 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(nativeCurrencies: string[]): Promise<void> {\n const releaseLock = await this.mutex.acquire();\n try {\n const { currentCurrency } = this.state;\n\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 (acc, nativeCurrency) => {\n acc[nativeCurrency] = testnetSymbols.includes(nativeCurrency)\n ? FALL_BACK_VS_CURRENCY\n : nativeCurrency;\n return acc;\n },\n {} as Record<string, string>,\n );\n\n const fetchExchangeRateResponse = await this.fetchMultiExchangeRate(\n currentCurrency,\n [...new Set(Object.values(nativeCurrenciesToFetch))],\n this.includeUsdRate,\n );\n\n const rates = Object.entries(nativeCurrenciesToFetch).reduce(\n (acc, [nativeCurrency, fetchedCurrency]) => {\n const rate = fetchExchangeRateResponse[fetchedCurrency.toLowerCase()];\n acc[nativeCurrency] = {\n conversionDate: rate !== undefined ? Date.now() / 1000 : null,\n conversionRate: rate?.[currentCurrency.toLowerCase()] ?? null,\n usdConversionRate: rate?.usd ?? null,\n };\n return acc;\n },\n {} as CurrencyRateState['currencyRates'],\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() {\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,6 @@
|
|
|
1
1
|
import type { RestrictedControllerMessenger, ControllerGetStateAction, ControllerStateChangeEvent } from "@metamask/base-controller";
|
|
2
2
|
import type { NetworkControllerGetNetworkClientByIdAction } from "@metamask/network-controller";
|
|
3
|
-
import {
|
|
3
|
+
import { fetchMultiExchangeRate as defaultFetchMultiExchangeRate } from "./crypto-compare-service/index.cjs";
|
|
4
4
|
/**
|
|
5
5
|
* @type CurrencyRateState
|
|
6
6
|
* @property currencyRates - Object keyed by native currency
|
|
@@ -26,17 +26,17 @@ type AllowedActions = NetworkControllerGetNetworkClientByIdAction;
|
|
|
26
26
|
type CurrencyRateMessenger = RestrictedControllerMessenger<typeof name, CurrencyRateControllerActions | AllowedActions, CurrencyRateControllerEvents, AllowedActions['type'], never>;
|
|
27
27
|
/** The input to start polling for the {@link CurrencyRateController} */
|
|
28
28
|
type CurrencyRatePollingInput = {
|
|
29
|
-
|
|
29
|
+
nativeCurrencies: string[];
|
|
30
30
|
};
|
|
31
31
|
declare const CurrencyRateController_base: (abstract new (...args: any[]) => {
|
|
32
|
-
readonly "__#
|
|
33
|
-
"__#
|
|
32
|
+
readonly "__#787890@#intervalIds": Record<string, NodeJS.Timeout>;
|
|
33
|
+
"__#787890@#intervalLength": number | undefined;
|
|
34
34
|
setIntervalLength(intervalLength: number): void;
|
|
35
35
|
getIntervalLength(): number | undefined;
|
|
36
36
|
_startPolling(input: CurrencyRatePollingInput): void;
|
|
37
37
|
_stopPollingByPollingTokenSetId(key: string): void;
|
|
38
|
-
readonly "__#
|
|
39
|
-
"__#
|
|
38
|
+
readonly "__#787882@#pollingTokenSets": Map<string, Set<string>>;
|
|
39
|
+
"__#787882@#callbacks": Map<string, Set<(input: CurrencyRatePollingInput) => void>>;
|
|
40
40
|
_executePoll(input: CurrencyRatePollingInput): Promise<void>;
|
|
41
41
|
startPolling(input: CurrencyRatePollingInput): string;
|
|
42
42
|
stopAllPolling(): void;
|
|
@@ -49,7 +49,7 @@ declare const CurrencyRateController_base: (abstract new (...args: any[]) => {
|
|
|
49
49
|
*/
|
|
50
50
|
export declare class CurrencyRateController extends CurrencyRateController_base<typeof name, CurrencyRateState, CurrencyRateMessenger> {
|
|
51
51
|
private readonly mutex;
|
|
52
|
-
private readonly
|
|
52
|
+
private readonly fetchMultiExchangeRate;
|
|
53
53
|
private readonly includeUsdRate;
|
|
54
54
|
/**
|
|
55
55
|
* Creates a CurrencyRateController instance.
|
|
@@ -59,14 +59,14 @@ export declare class CurrencyRateController extends CurrencyRateController_base<
|
|
|
59
59
|
* @param options.interval - The polling interval, in milliseconds.
|
|
60
60
|
* @param options.messenger - A reference to the messaging system.
|
|
61
61
|
* @param options.state - Initial state to set on this controller.
|
|
62
|
-
* @param options.
|
|
62
|
+
* @param options.fetchMultiExchangeRate - Fetches the exchange rate from an external API. This option is primarily meant for use in unit tests.
|
|
63
63
|
*/
|
|
64
|
-
constructor({ includeUsdRate, interval, messenger, state,
|
|
64
|
+
constructor({ includeUsdRate, interval, messenger, state, fetchMultiExchangeRate, }: {
|
|
65
65
|
includeUsdRate?: boolean;
|
|
66
66
|
interval?: number;
|
|
67
67
|
messenger: CurrencyRateMessenger;
|
|
68
68
|
state?: Partial<CurrencyRateState>;
|
|
69
|
-
|
|
69
|
+
fetchMultiExchangeRate?: typeof defaultFetchMultiExchangeRate;
|
|
70
70
|
});
|
|
71
71
|
/**
|
|
72
72
|
* Sets a currency to track.
|
|
@@ -75,11 +75,11 @@ export declare class CurrencyRateController extends CurrencyRateController_base<
|
|
|
75
75
|
*/
|
|
76
76
|
setCurrentCurrency(currentCurrency: string): Promise<void>;
|
|
77
77
|
/**
|
|
78
|
-
* Updates the exchange rate for the current currency and native currency
|
|
78
|
+
* Updates the exchange rate for the current currency and native currency pairs.
|
|
79
79
|
*
|
|
80
|
-
* @param
|
|
80
|
+
* @param nativeCurrencies - The native currency symbols to fetch exchange rates for.
|
|
81
81
|
*/
|
|
82
|
-
updateExchangeRate(
|
|
82
|
+
updateExchangeRate(nativeCurrencies: string[]): Promise<void>;
|
|
83
83
|
/**
|
|
84
84
|
* Prepare to discard this controller.
|
|
85
85
|
*
|
|
@@ -90,9 +90,9 @@ export declare class CurrencyRateController extends CurrencyRateController_base<
|
|
|
90
90
|
* Updates exchange rate for the current currency.
|
|
91
91
|
*
|
|
92
92
|
* @param input - The input for the poll.
|
|
93
|
-
* @param input.
|
|
93
|
+
* @param input.nativeCurrencies - The native currency symbols to poll prices for.
|
|
94
94
|
*/
|
|
95
|
-
_executePoll({
|
|
95
|
+
_executePoll({ nativeCurrencies, }: CurrencyRatePollingInput): Promise<void>;
|
|
96
96
|
}
|
|
97
97
|
export default CurrencyRateController;
|
|
98
98
|
//# sourceMappingURL=CurrencyRateController.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CurrencyRateController.d.cts","sourceRoot":"","sources":["../src/CurrencyRateController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,6BAA6B,EAC7B,wBAAwB,EACxB,0BAA0B,EAC3B,kCAAkC;AAKnC,OAAO,KAAK,EAAE,2CAA2C,EAAE,qCAAqC;AAIhG,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"CurrencyRateController.d.cts","sourceRoot":"","sources":["../src/CurrencyRateController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,6BAA6B,EAC7B,wBAAwB,EACxB,0BAA0B,EAC3B,kCAAkC;AAKnC,OAAO,KAAK,EAAE,2CAA2C,EAAE,qCAAqC;AAIhG,OAAO,EAAE,sBAAsB,IAAI,6BAA6B,EAAE,2CAAiC;AAEnG;;;;;;;GAOG;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;AAEtC,MAAM,MAAM,uBAAuB,GAAG,0BAA0B,CAC9D,OAAO,IAAI,EACX,iBAAiB,CAClB,CAAC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AC,EACjB,gBAAgB,GACjB,EAAE,wBAAwB,GAAG,OAAO,CAAC,IAAI,CAAC;CAG5C;AAED,eAAe,sBAAsB,CAAC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { RestrictedControllerMessenger, ControllerGetStateAction, ControllerStateChangeEvent } from "@metamask/base-controller";
|
|
2
2
|
import type { NetworkControllerGetNetworkClientByIdAction } from "@metamask/network-controller";
|
|
3
|
-
import {
|
|
3
|
+
import { fetchMultiExchangeRate as defaultFetchMultiExchangeRate } from "./crypto-compare-service/index.mjs";
|
|
4
4
|
/**
|
|
5
5
|
* @type CurrencyRateState
|
|
6
6
|
* @property currencyRates - Object keyed by native currency
|
|
@@ -26,17 +26,17 @@ type AllowedActions = NetworkControllerGetNetworkClientByIdAction;
|
|
|
26
26
|
type CurrencyRateMessenger = RestrictedControllerMessenger<typeof name, CurrencyRateControllerActions | AllowedActions, CurrencyRateControllerEvents, AllowedActions['type'], never>;
|
|
27
27
|
/** The input to start polling for the {@link CurrencyRateController} */
|
|
28
28
|
type CurrencyRatePollingInput = {
|
|
29
|
-
|
|
29
|
+
nativeCurrencies: string[];
|
|
30
30
|
};
|
|
31
31
|
declare const CurrencyRateController_base: (abstract new (...args: any[]) => {
|
|
32
|
-
readonly "__#
|
|
33
|
-
"__#
|
|
32
|
+
readonly "__#787890@#intervalIds": Record<string, NodeJS.Timeout>;
|
|
33
|
+
"__#787890@#intervalLength": number | undefined;
|
|
34
34
|
setIntervalLength(intervalLength: number): void;
|
|
35
35
|
getIntervalLength(): number | undefined;
|
|
36
36
|
_startPolling(input: CurrencyRatePollingInput): void;
|
|
37
37
|
_stopPollingByPollingTokenSetId(key: string): void;
|
|
38
|
-
readonly "__#
|
|
39
|
-
"__#
|
|
38
|
+
readonly "__#787882@#pollingTokenSets": Map<string, Set<string>>;
|
|
39
|
+
"__#787882@#callbacks": Map<string, Set<(input: CurrencyRatePollingInput) => void>>;
|
|
40
40
|
_executePoll(input: CurrencyRatePollingInput): Promise<void>;
|
|
41
41
|
startPolling(input: CurrencyRatePollingInput): string;
|
|
42
42
|
stopAllPolling(): void;
|
|
@@ -49,7 +49,7 @@ declare const CurrencyRateController_base: (abstract new (...args: any[]) => {
|
|
|
49
49
|
*/
|
|
50
50
|
export declare class CurrencyRateController extends CurrencyRateController_base<typeof name, CurrencyRateState, CurrencyRateMessenger> {
|
|
51
51
|
private readonly mutex;
|
|
52
|
-
private readonly
|
|
52
|
+
private readonly fetchMultiExchangeRate;
|
|
53
53
|
private readonly includeUsdRate;
|
|
54
54
|
/**
|
|
55
55
|
* Creates a CurrencyRateController instance.
|
|
@@ -59,14 +59,14 @@ export declare class CurrencyRateController extends CurrencyRateController_base<
|
|
|
59
59
|
* @param options.interval - The polling interval, in milliseconds.
|
|
60
60
|
* @param options.messenger - A reference to the messaging system.
|
|
61
61
|
* @param options.state - Initial state to set on this controller.
|
|
62
|
-
* @param options.
|
|
62
|
+
* @param options.fetchMultiExchangeRate - Fetches the exchange rate from an external API. This option is primarily meant for use in unit tests.
|
|
63
63
|
*/
|
|
64
|
-
constructor({ includeUsdRate, interval, messenger, state,
|
|
64
|
+
constructor({ includeUsdRate, interval, messenger, state, fetchMultiExchangeRate, }: {
|
|
65
65
|
includeUsdRate?: boolean;
|
|
66
66
|
interval?: number;
|
|
67
67
|
messenger: CurrencyRateMessenger;
|
|
68
68
|
state?: Partial<CurrencyRateState>;
|
|
69
|
-
|
|
69
|
+
fetchMultiExchangeRate?: typeof defaultFetchMultiExchangeRate;
|
|
70
70
|
});
|
|
71
71
|
/**
|
|
72
72
|
* Sets a currency to track.
|
|
@@ -75,11 +75,11 @@ export declare class CurrencyRateController extends CurrencyRateController_base<
|
|
|
75
75
|
*/
|
|
76
76
|
setCurrentCurrency(currentCurrency: string): Promise<void>;
|
|
77
77
|
/**
|
|
78
|
-
* Updates the exchange rate for the current currency and native currency
|
|
78
|
+
* Updates the exchange rate for the current currency and native currency pairs.
|
|
79
79
|
*
|
|
80
|
-
* @param
|
|
80
|
+
* @param nativeCurrencies - The native currency symbols to fetch exchange rates for.
|
|
81
81
|
*/
|
|
82
|
-
updateExchangeRate(
|
|
82
|
+
updateExchangeRate(nativeCurrencies: string[]): Promise<void>;
|
|
83
83
|
/**
|
|
84
84
|
* Prepare to discard this controller.
|
|
85
85
|
*
|
|
@@ -90,9 +90,9 @@ export declare class CurrencyRateController extends CurrencyRateController_base<
|
|
|
90
90
|
* Updates exchange rate for the current currency.
|
|
91
91
|
*
|
|
92
92
|
* @param input - The input for the poll.
|
|
93
|
-
* @param input.
|
|
93
|
+
* @param input.nativeCurrencies - The native currency symbols to poll prices for.
|
|
94
94
|
*/
|
|
95
|
-
_executePoll({
|
|
95
|
+
_executePoll({ nativeCurrencies, }: CurrencyRatePollingInput): Promise<void>;
|
|
96
96
|
}
|
|
97
97
|
export default CurrencyRateController;
|
|
98
98
|
//# sourceMappingURL=CurrencyRateController.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CurrencyRateController.d.mts","sourceRoot":"","sources":["../src/CurrencyRateController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,6BAA6B,EAC7B,wBAAwB,EACxB,0BAA0B,EAC3B,kCAAkC;AAKnC,OAAO,KAAK,EAAE,2CAA2C,EAAE,qCAAqC;AAIhG,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"CurrencyRateController.d.mts","sourceRoot":"","sources":["../src/CurrencyRateController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,6BAA6B,EAC7B,wBAAwB,EACxB,0BAA0B,EAC3B,kCAAkC;AAKnC,OAAO,KAAK,EAAE,2CAA2C,EAAE,qCAAqC;AAIhG,OAAO,EAAE,sBAAsB,IAAI,6BAA6B,EAAE,2CAAiC;AAEnG;;;;;;;GAOG;AACH,MAAM,MAAM,iBAAiB,GAAG;IAC9B,eAAe,EAAE,MAAM,CAAC;IACxB,aAAa,EAAE,MAAM,CACnB,MAAM,EACN;QACE,cAA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CAAC,CAAC;QACnC,sBAAsB,CAAC,EAAE,OAAO,6BAA6B,CAAC;KAC/D;IAYD;;;;OAIG;IACG,kBAAkB,CAAC,eAAe,EAAE,MAAM;IAkBhD;;;;OAIG;IACG,kBAAkB,CAAC,gBAAgB,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAmDnE;;;;OAIG;IACM,OAAO;IAKhB;;;;;OAKG;IACG,YAAY,CAAC,EACjB,gBAAgB,GACjB,EAAE,wBAAwB,GAAG,OAAO,CAAC,IAAI,CAAC;CAG5C;AAED,eAAe,sBAAsB,CAAC"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { TESTNET_TICKER_SYMBOLS, FALL_BACK_VS_CURRENCY } from "@metamask/controller-utils";
|
|
2
2
|
import { StaticIntervalPollingController } from "@metamask/polling-controller";
|
|
3
3
|
import { Mutex } from "async-mutex";
|
|
4
|
-
import {
|
|
4
|
+
import { fetchMultiExchangeRate as defaultFetchMultiExchangeRate } from "./crypto-compare-service/index.mjs";
|
|
5
5
|
const name = 'CurrencyRateController';
|
|
6
6
|
const metadata = {
|
|
7
7
|
currentCurrency: { persist: true, anonymous: true },
|
|
@@ -30,9 +30,9 @@ export class CurrencyRateController extends StaticIntervalPollingController() {
|
|
|
30
30
|
* @param options.interval - The polling interval, in milliseconds.
|
|
31
31
|
* @param options.messenger - A reference to the messaging system.
|
|
32
32
|
* @param options.state - Initial state to set on this controller.
|
|
33
|
-
* @param options.
|
|
33
|
+
* @param options.fetchMultiExchangeRate - Fetches the exchange rate from an external API. This option is primarily meant for use in unit tests.
|
|
34
34
|
*/
|
|
35
|
-
constructor({ includeUsdRate = false, interval = 180000, messenger, state,
|
|
35
|
+
constructor({ includeUsdRate = false, interval = 180000, messenger, state, fetchMultiExchangeRate = defaultFetchMultiExchangeRate, }) {
|
|
36
36
|
super({
|
|
37
37
|
name,
|
|
38
38
|
metadata,
|
|
@@ -42,7 +42,7 @@ export class CurrencyRateController extends StaticIntervalPollingController() {
|
|
|
42
42
|
this.mutex = new Mutex();
|
|
43
43
|
this.includeUsdRate = includeUsdRate;
|
|
44
44
|
this.setIntervalLength(interval);
|
|
45
|
-
this.
|
|
45
|
+
this.fetchMultiExchangeRate = fetchMultiExchangeRate;
|
|
46
46
|
}
|
|
47
47
|
/**
|
|
48
48
|
* Sets a currency to track.
|
|
@@ -64,68 +64,50 @@ export class CurrencyRateController extends StaticIntervalPollingController() {
|
|
|
64
64
|
releaseLock();
|
|
65
65
|
}
|
|
66
66
|
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
67
|
-
// eslint-disable-next-line @typescript-eslint/no-
|
|
68
|
-
|
|
67
|
+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
68
|
+
this.updateExchangeRate(nativeCurrencies);
|
|
69
69
|
}
|
|
70
70
|
/**
|
|
71
|
-
* Updates the exchange rate for the current currency and native currency
|
|
71
|
+
* Updates the exchange rate for the current currency and native currency pairs.
|
|
72
72
|
*
|
|
73
|
-
* @param
|
|
73
|
+
* @param nativeCurrencies - The native currency symbols to fetch exchange rates for.
|
|
74
74
|
*/
|
|
75
|
-
async updateExchangeRate(
|
|
75
|
+
async updateExchangeRate(nativeCurrencies) {
|
|
76
76
|
const releaseLock = await this.mutex.acquire();
|
|
77
|
-
const { currentCurrency, currencyRates } = this.state;
|
|
78
|
-
let conversionDate = null;
|
|
79
|
-
let conversionRate = null;
|
|
80
|
-
let usdConversionRate = null;
|
|
81
|
-
// For preloaded testnets (Goerli, Sepolia) we want to fetch exchange rate for real ETH.
|
|
82
|
-
const nativeCurrencyForExchangeRate = Object.values(TESTNET_TICKER_SYMBOLS).includes(nativeCurrency)
|
|
83
|
-
? FALL_BACK_VS_CURRENCY // ETH
|
|
84
|
-
: nativeCurrency;
|
|
85
|
-
let shouldUpdateState = true;
|
|
86
77
|
try {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
78
|
+
const { currentCurrency } = this.state;
|
|
79
|
+
// For preloaded testnets (Goerli, Sepolia) we want to fetch exchange rate for real ETH.
|
|
80
|
+
// Map each native currency to the symbol we want to fetch for it.
|
|
81
|
+
const testnetSymbols = Object.values(TESTNET_TICKER_SYMBOLS);
|
|
82
|
+
const nativeCurrenciesToFetch = nativeCurrencies.reduce((acc, nativeCurrency) => {
|
|
83
|
+
acc[nativeCurrency] = testnetSymbols.includes(nativeCurrency)
|
|
84
|
+
? FALL_BACK_VS_CURRENCY
|
|
85
|
+
: nativeCurrency;
|
|
86
|
+
return acc;
|
|
87
|
+
}, {});
|
|
88
|
+
const fetchExchangeRateResponse = await this.fetchMultiExchangeRate(currentCurrency, [...new Set(Object.values(nativeCurrenciesToFetch))], this.includeUsdRate);
|
|
89
|
+
const rates = Object.entries(nativeCurrenciesToFetch).reduce((acc, [nativeCurrency, fetchedCurrency]) => {
|
|
90
|
+
const rate = fetchExchangeRateResponse[fetchedCurrency.toLowerCase()];
|
|
91
|
+
acc[nativeCurrency] = {
|
|
92
|
+
conversionDate: rate !== undefined ? Date.now() / 1000 : null,
|
|
93
|
+
conversionRate: rate?.[currentCurrency.toLowerCase()] ?? null,
|
|
94
|
+
usdConversionRate: rate?.usd ?? null,
|
|
95
|
+
};
|
|
96
|
+
return acc;
|
|
97
|
+
}, {});
|
|
98
|
+
this.update((state) => {
|
|
99
|
+
state.currencyRates = {
|
|
100
|
+
...state.currencyRates,
|
|
101
|
+
...rates,
|
|
102
|
+
};
|
|
103
|
+
});
|
|
99
104
|
}
|
|
100
105
|
catch (error) {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
// Don't update state on transient / unexpected errors
|
|
104
|
-
shouldUpdateState = false;
|
|
105
|
-
throw error;
|
|
106
|
-
}
|
|
106
|
+
console.error('Failed to fetch exchange rates.', error);
|
|
107
|
+
throw error;
|
|
107
108
|
}
|
|
108
109
|
finally {
|
|
109
|
-
|
|
110
|
-
if (shouldUpdateState) {
|
|
111
|
-
this.update(() => {
|
|
112
|
-
return {
|
|
113
|
-
currencyRates: {
|
|
114
|
-
...currencyRates,
|
|
115
|
-
[nativeCurrency]: {
|
|
116
|
-
conversionDate,
|
|
117
|
-
conversionRate,
|
|
118
|
-
usdConversionRate,
|
|
119
|
-
},
|
|
120
|
-
},
|
|
121
|
-
currentCurrency,
|
|
122
|
-
};
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
finally {
|
|
127
|
-
releaseLock();
|
|
128
|
-
}
|
|
110
|
+
releaseLock();
|
|
129
111
|
}
|
|
130
112
|
}
|
|
131
113
|
/**
|
|
@@ -141,10 +123,10 @@ export class CurrencyRateController extends StaticIntervalPollingController() {
|
|
|
141
123
|
* Updates exchange rate for the current currency.
|
|
142
124
|
*
|
|
143
125
|
* @param input - The input for the poll.
|
|
144
|
-
* @param input.
|
|
126
|
+
* @param input.nativeCurrencies - The native currency symbols to poll prices for.
|
|
145
127
|
*/
|
|
146
|
-
async _executePoll({
|
|
147
|
-
await this.updateExchangeRate(
|
|
128
|
+
async _executePoll({ nativeCurrencies, }) {
|
|
129
|
+
await this.updateExchangeRate(nativeCurrencies);
|
|
148
130
|
}
|
|
149
131
|
}
|
|
150
132
|
export default CurrencyRateController;
|