@metamask-previews/assets-controller 20.4.1-preview-e80304db4 → 20.4.1-preview-1d88b8ba5
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 +0 -11
- package/README.md +0 -3
- package/dist/AssetsController-method-action-types.d.ts +2 -2
- package/dist/AssetsController-method-action-types.js.map +1 -1
- package/dist/AssetsController.d.ts +12 -6
- package/dist/AssetsController.d.ts.map +1 -1
- package/dist/AssetsController.js +18 -11
- package/dist/AssetsController.js.map +1 -1
- package/dist/data-sources/AccountActivityDataSource.d.ts +2 -4
- package/dist/data-sources/AccountActivityDataSource.d.ts.map +1 -1
- package/dist/data-sources/AccountActivityDataSource.js +1 -3
- package/dist/data-sources/AccountActivityDataSource.js.map +1 -1
- package/dist/data-sources/PriceDataSource.d.ts +2 -32
- package/dist/data-sources/PriceDataSource.d.ts.map +1 -1
- package/dist/data-sources/PriceDataSource.js +37 -164
- package/dist/data-sources/PriceDataSource.js.map +1 -1
- package/dist/data-sources/index.d.ts +1 -1
- package/dist/data-sources/index.d.ts.map +1 -1
- package/dist/data-sources/index.js.map +1 -1
- package/dist/index.d.ts +2 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -2
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +0 -5
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +2 -2
- package/dist/data-sources/MarketStatsPriceStream.d.ts +0 -108
- package/dist/data-sources/MarketStatsPriceStream.d.ts.map +0 -1
- package/dist/data-sources/MarketStatsPriceStream.js +0 -297
- package/dist/data-sources/MarketStatsPriceStream.js.map +0 -1
- package/dist/store/MarketStatsStore.d.ts +0 -72
- package/dist/store/MarketStatsStore.d.ts.map +0 -1
- package/dist/store/MarketStatsStore.js +0 -182
- package/dist/store/MarketStatsStore.js.map +0 -1
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Quote cache for `MarketStatsService:pricesUpdated`.
|
|
3
|
-
*
|
|
4
|
-
* The UI holds one instance. A tick updates this object and does not write
|
|
5
|
-
* `AssetsController` state, so it does not persist and does not notify the
|
|
6
|
-
* controller. Polled prices stay in `assetsPrice` and show when this map has
|
|
7
|
-
* no quote.
|
|
8
|
-
*/
|
|
9
|
-
import { isCaipAssetType, parseCaipAssetType } from '@metamask/utils';
|
|
10
|
-
import { safeNormalizeAssetId } from '../utils/index.js';
|
|
11
|
-
/**
|
|
12
|
-
* Key a market-stats asset id the same way on the tick and on the lookup.
|
|
13
|
-
* EVM ERC-20 addresses are checksummed. Anything else is kept as given.
|
|
14
|
-
*
|
|
15
|
-
* @param assetId - CAIP-19 asset id, or any other string the caller uses as a key.
|
|
16
|
-
* @returns The map key.
|
|
17
|
-
*/
|
|
18
|
-
export function marketStatsAssetKey(assetId) {
|
|
19
|
-
if (!isCaipAssetType(assetId)) {
|
|
20
|
-
return assetId;
|
|
21
|
-
}
|
|
22
|
-
return safeNormalizeAssetId(assetId);
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Quote for one asset. The id is keyed with {@link marketStatsAssetKey}.
|
|
26
|
-
*
|
|
27
|
-
* @param quotes - Current quote map.
|
|
28
|
-
* @param assetId - CAIP-19 asset id.
|
|
29
|
-
* @returns The streamed quote, or undefined.
|
|
30
|
-
*/
|
|
31
|
-
export function readLiveQuote(quotes, assetId) {
|
|
32
|
-
if (!assetId) {
|
|
33
|
-
return undefined;
|
|
34
|
-
}
|
|
35
|
-
return quotes.get(marketStatsAssetKey(assetId));
|
|
36
|
-
}
|
|
37
|
-
function readFiniteNumber(value) {
|
|
38
|
-
if (typeof value === 'number') {
|
|
39
|
-
return Number.isFinite(value) ? value : undefined;
|
|
40
|
-
}
|
|
41
|
-
if (typeof value !== 'string' || value.trim() === '') {
|
|
42
|
-
return undefined;
|
|
43
|
-
}
|
|
44
|
-
const numeric = Number(value);
|
|
45
|
-
return Number.isFinite(numeric) ? numeric : undefined;
|
|
46
|
-
}
|
|
47
|
-
function isUnknownArray(value) {
|
|
48
|
-
return Array.isArray(value);
|
|
49
|
-
}
|
|
50
|
-
function isRecord(value) {
|
|
51
|
-
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* Latest market-stats quotes for one currency.
|
|
55
|
-
*/
|
|
56
|
-
export class MarketStatsStore {
|
|
57
|
-
#currency = 'usd';
|
|
58
|
-
#quotes = new Map();
|
|
59
|
-
#listeners = new Set();
|
|
60
|
-
/**
|
|
61
|
-
* Stable reference so `useSyncExternalStore` can take it directly.
|
|
62
|
-
*
|
|
63
|
-
* @returns The current quote map. The same reference until a tick changes it.
|
|
64
|
-
*/
|
|
65
|
-
getSnapshot = () => this.#quotes;
|
|
66
|
-
/**
|
|
67
|
-
* @param listener - Called after the quote map changes.
|
|
68
|
-
* @returns Unsubscribe.
|
|
69
|
-
*/
|
|
70
|
-
subscribe = (listener) => {
|
|
71
|
-
this.#listeners.add(listener);
|
|
72
|
-
return () => {
|
|
73
|
-
this.#listeners.delete(listener);
|
|
74
|
-
};
|
|
75
|
-
};
|
|
76
|
-
/**
|
|
77
|
-
* Remember the selected currency and drop quotes from the previous one.
|
|
78
|
-
* Does not notify. Callers invoke this during render, and the currency
|
|
79
|
-
* selector is what re-renders every reader. That render already reads the
|
|
80
|
-
* empty map.
|
|
81
|
-
*
|
|
82
|
-
* @param currency - Selected currency. Case is ignored.
|
|
83
|
-
*/
|
|
84
|
-
setCurrency(currency) {
|
|
85
|
-
const next = currency.toLowerCase();
|
|
86
|
-
if (this.#currency === next) {
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
this.#currency = next;
|
|
90
|
-
if (this.#quotes.size === 0) {
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
|
-
this.#quotes = new Map();
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Merge one `MarketStatsService:pricesUpdated` payload.
|
|
97
|
-
* Entries in another currency are ignored. The argument is the object
|
|
98
|
-
* passed to `publish`. The extension unwraps its forwarded argument list
|
|
99
|
-
* before calling this.
|
|
100
|
-
*
|
|
101
|
-
* @param payload - Published `{ updates }` object.
|
|
102
|
-
*/
|
|
103
|
-
ingestPrices(payload) {
|
|
104
|
-
if (!isRecord(payload) || !isUnknownArray(payload.updates)) {
|
|
105
|
-
return;
|
|
106
|
-
}
|
|
107
|
-
let changed = false;
|
|
108
|
-
const updated = new Map(this.#quotes);
|
|
109
|
-
for (const entry of payload.updates) {
|
|
110
|
-
if (!isRecord(entry)) {
|
|
111
|
-
continue;
|
|
112
|
-
}
|
|
113
|
-
const { assetId, currency } = entry;
|
|
114
|
-
if (typeof assetId !== 'string' ||
|
|
115
|
-
assetId.length === 0 ||
|
|
116
|
-
typeof currency !== 'string' ||
|
|
117
|
-
currency.toLowerCase() !== this.#currency) {
|
|
118
|
-
continue;
|
|
119
|
-
}
|
|
120
|
-
const price = readFiniteNumber(entry.price);
|
|
121
|
-
if (price === undefined) {
|
|
122
|
-
continue;
|
|
123
|
-
}
|
|
124
|
-
const pricePercentChange1d = readFiniteNumber(entry.pricePercentChange1d);
|
|
125
|
-
const key = marketStatsAssetKey(assetId);
|
|
126
|
-
const previous = updated.get(key);
|
|
127
|
-
if (previous?.price === price &&
|
|
128
|
-
previous.pricePercentChange1d === pricePercentChange1d) {
|
|
129
|
-
continue;
|
|
130
|
-
}
|
|
131
|
-
updated.set(key, pricePercentChange1d === undefined
|
|
132
|
-
? { price }
|
|
133
|
-
: { price, pricePercentChange1d });
|
|
134
|
-
changed = true;
|
|
135
|
-
}
|
|
136
|
-
if (!changed) {
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
this.#replace(updated);
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* A chain reported down leaves the map so the polled price shows until the
|
|
143
|
-
* next tick for that chain.
|
|
144
|
-
*
|
|
145
|
-
* @param payload - Published `MarketStatsService:chainStatusChanged` object.
|
|
146
|
-
*/
|
|
147
|
-
ingestChainStatus(payload) {
|
|
148
|
-
if (!isRecord(payload)) {
|
|
149
|
-
return;
|
|
150
|
-
}
|
|
151
|
-
const { chainIds } = payload;
|
|
152
|
-
if (payload.status !== 'down' || !isUnknownArray(chainIds)) {
|
|
153
|
-
return;
|
|
154
|
-
}
|
|
155
|
-
if (this.#quotes.size === 0) {
|
|
156
|
-
return;
|
|
157
|
-
}
|
|
158
|
-
const down = new Set(chainIds.filter((chainId) => typeof chainId === 'string'));
|
|
159
|
-
let changed = false;
|
|
160
|
-
const updated = new Map(this.#quotes);
|
|
161
|
-
for (const key of updated.keys()) {
|
|
162
|
-
if (!isCaipAssetType(key)) {
|
|
163
|
-
continue;
|
|
164
|
-
}
|
|
165
|
-
if (down.has(parseCaipAssetType(key).chainId)) {
|
|
166
|
-
updated.delete(key);
|
|
167
|
-
changed = true;
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
if (!changed) {
|
|
171
|
-
return;
|
|
172
|
-
}
|
|
173
|
-
this.#replace(updated);
|
|
174
|
-
}
|
|
175
|
-
#replace(quotes) {
|
|
176
|
-
this.#quotes = quotes;
|
|
177
|
-
for (const listener of this.#listeners) {
|
|
178
|
-
listener();
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
//# sourceMappingURL=MarketStatsStore.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"MarketStatsStore.js","sourceRoot":"","sources":["../../src/store/MarketStatsStore.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,eAAe,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAEtE,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AASzD;;;;;;GAMG;AACH,MAAM,UAAU,mBAAmB,CAAC,OAAe;IACjD,IAAI,CAAC,eAAe,CAAC,OAAO,CAAC,EAAE,CAAC;QAC9B,OAAO,OAAO,CA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C5B,OAAO;QACT,CAAC;QAED,MAAM,IAAI,GAAG,IAAI,GAAG,CAClB,QAAQ,CAAC,MAAM,CACb,CAAC,OAAO,EAAqB,EAAE,CAAC,OAAO,OAAO,KAAK,QAAQ,CAC5D,CACF,CAAC;QACF,IAAI,OAAO,GAAG,KAAK,CAAC;QACpB,MAAM,OAAO,GAAG,IAAI,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACtC,KAAK,MAAM,GAAG,IAAI,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC;YACjC,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,EAAE,CAAC;gBAC1B,SAAS;YACX,CAAC;YACD,IAAI,IAAI,CAAC,GAAG,CAAC,kBAAkB,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC;gBAC9C,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;gBACpB,OAAO,GAAG,IAAI,CAAC;YACjB,CAAC;QACH,CAAC;QACD,IAAI,CAAC,OAAO,EAAE,CAAC;YACb,OAAO;QACT,CAAC;QACD,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;IACzB,CAAC;IAED,QAAQ,CAAC,MAA8B;QACrC,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;QACtB,KAAK,MAAM,QAAQ,IAAI,IAAI,CAAC,UAAU,EAAE,CAAC;YACvC,QAAQ,EAAE,CAAC;QACb,CAAC;IACH,CAAC;CACF","sourcesContent":["/**\n * Quote cache for `MarketStatsService:pricesUpdated`.\n *\n * The UI holds one instance. A tick updates this object and does not write\n * `AssetsController` state, so it does not persist and does not notify the\n * controller. Polled prices stay in `assetsPrice` and show when this map has\n * no quote.\n */\n\nimport { isCaipAssetType, parseCaipAssetType } from '@metamask/utils';\n\nimport { safeNormalizeAssetId } from '../utils/index.js';\n\nexport type LiveQuote = {\n price: number;\n pricePercentChange1d?: number;\n};\n\nexport type LivePriceMap = ReadonlyMap<string, LiveQuote>;\n\n/**\n * Key a market-stats asset id the same way on the tick and on the lookup.\n * EVM ERC-20 addresses are checksummed. Anything else is kept as given.\n *\n * @param assetId - CAIP-19 asset id, or any other string the caller uses as a key.\n * @returns The map key.\n */\nexport function marketStatsAssetKey(assetId: string): string {\n if (!isCaipAssetType(assetId)) {\n return assetId;\n }\n return safeNormalizeAssetId(assetId);\n}\n\n/**\n * Quote for one asset. The id is keyed with {@link marketStatsAssetKey}.\n *\n * @param quotes - Current quote map.\n * @param assetId - CAIP-19 asset id.\n * @returns The streamed quote, or undefined.\n */\nexport function readLiveQuote(\n quotes: LivePriceMap,\n assetId: string | undefined,\n): LiveQuote | undefined {\n if (!assetId) {\n return undefined;\n }\n return quotes.get(marketStatsAssetKey(assetId));\n}\n\nfunction readFiniteNumber(value: unknown): number | undefined {\n if (typeof value === 'number') {\n return Number.isFinite(value) ? value : undefined;\n }\n if (typeof value !== 'string' || value.trim() === '') {\n return undefined;\n }\n const numeric = Number(value);\n return Number.isFinite(numeric) ? numeric : undefined;\n}\n\nfunction isUnknownArray(value: unknown): value is readonly unknown[] {\n return Array.isArray(value);\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === 'object' && value !== null && !Array.isArray(value);\n}\n\n/**\n * Latest market-stats quotes for one currency.\n */\nexport class MarketStatsStore {\n #currency = 'usd';\n\n #quotes: LivePriceMap = new Map<string, LiveQuote>();\n\n readonly #listeners = new Set<() => void>();\n\n /**\n * Stable reference so `useSyncExternalStore` can take it directly.\n *\n * @returns The current quote map. The same reference until a tick changes it.\n */\n readonly getSnapshot = (): LivePriceMap => this.#quotes;\n\n /**\n * @param listener - Called after the quote map changes.\n * @returns Unsubscribe.\n */\n readonly subscribe = (listener: () => void): (() => void) => {\n this.#listeners.add(listener);\n return () => {\n this.#listeners.delete(listener);\n };\n };\n\n /**\n * Remember the selected currency and drop quotes from the previous one.\n * Does not notify. Callers invoke this during render, and the currency\n * selector is what re-renders every reader. That render already reads the\n * empty map.\n *\n * @param currency - Selected currency. Case is ignored.\n */\n setCurrency(currency: string): void {\n const next = currency.toLowerCase();\n if (this.#currency === next) {\n return;\n }\n this.#currency = next;\n if (this.#quotes.size === 0) {\n return;\n }\n this.#quotes = new Map();\n }\n\n /**\n * Merge one `MarketStatsService:pricesUpdated` payload.\n * Entries in another currency are ignored. The argument is the object\n * passed to `publish`. The extension unwraps its forwarded argument list\n * before calling this.\n *\n * @param payload - Published `{ updates }` object.\n */\n ingestPrices(payload: unknown): void {\n if (!isRecord(payload) || !isUnknownArray(payload.updates)) {\n return;\n }\n\n let changed = false;\n const updated = new Map(this.#quotes);\n for (const entry of payload.updates) {\n if (!isRecord(entry)) {\n continue;\n }\n const { assetId, currency } = entry;\n if (\n typeof assetId !== 'string' ||\n assetId.length === 0 ||\n typeof currency !== 'string' ||\n currency.toLowerCase() !== this.#currency\n ) {\n continue;\n }\n const price = readFiniteNumber(entry.price);\n if (price === undefined) {\n continue;\n }\n const pricePercentChange1d = readFiniteNumber(entry.pricePercentChange1d);\n const key = marketStatsAssetKey(assetId);\n const previous = updated.get(key);\n if (\n previous?.price === price &&\n previous.pricePercentChange1d === pricePercentChange1d\n ) {\n continue;\n }\n updated.set(\n key,\n pricePercentChange1d === undefined\n ? { price }\n : { price, pricePercentChange1d },\n );\n changed = true;\n }\n if (!changed) {\n return;\n }\n this.#replace(updated);\n }\n\n /**\n * A chain reported down leaves the map so the polled price shows until the\n * next tick for that chain.\n *\n * @param payload - Published `MarketStatsService:chainStatusChanged` object.\n */\n ingestChainStatus(payload: unknown): void {\n if (!isRecord(payload)) {\n return;\n }\n const { chainIds } = payload;\n if (payload.status !== 'down' || !isUnknownArray(chainIds)) {\n return;\n }\n if (this.#quotes.size === 0) {\n return;\n }\n\n const down = new Set(\n chainIds.filter(\n (chainId): chainId is string => typeof chainId === 'string',\n ),\n );\n let changed = false;\n const updated = new Map(this.#quotes);\n for (const key of updated.keys()) {\n if (!isCaipAssetType(key)) {\n continue;\n }\n if (down.has(parseCaipAssetType(key).chainId)) {\n updated.delete(key);\n changed = true;\n }\n }\n if (!changed) {\n return;\n }\n this.#replace(updated);\n }\n\n #replace(quotes: Map<string, LiveQuote>): void {\n this.#quotes = quotes;\n for (const listener of this.#listeners) {\n listener();\n }\n }\n}\n"]}
|