@metamask/assets-controllers 88.0.0 → 89.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 +25 -1
- package/dist/AccountTrackerController.cjs +19 -7
- package/dist/AccountTrackerController.cjs.map +1 -1
- package/dist/AccountTrackerController.d.cts +1 -1
- package/dist/AccountTrackerController.d.cts.map +1 -1
- package/dist/AccountTrackerController.d.mts +1 -1
- package/dist/AccountTrackerController.d.mts.map +1 -1
- package/dist/AccountTrackerController.mjs +19 -7
- package/dist/AccountTrackerController.mjs.map +1 -1
- package/dist/CurrencyRateController.cjs +66 -15
- package/dist/CurrencyRateController.cjs.map +1 -1
- package/dist/CurrencyRateController.d.cts +4 -8
- package/dist/CurrencyRateController.d.cts.map +1 -1
- package/dist/CurrencyRateController.d.mts +4 -8
- package/dist/CurrencyRateController.d.mts.map +1 -1
- package/dist/CurrencyRateController.mjs +66 -15
- package/dist/CurrencyRateController.mjs.map +1 -1
- package/dist/DeFiPositionsController/DeFiPositionsController.d.cts +1 -1
- package/dist/DeFiPositionsController/DeFiPositionsController.d.mts +1 -1
- package/dist/MultichainAssetsRatesController/MultichainAssetsRatesController.d.cts +1 -1
- package/dist/MultichainAssetsRatesController/MultichainAssetsRatesController.d.mts +1 -1
- package/dist/NftController.cjs +0 -1
- package/dist/NftController.cjs.map +1 -1
- package/dist/NftController.d.cts.map +1 -1
- package/dist/NftController.d.mts.map +1 -1
- package/dist/NftController.mjs +0 -1
- package/dist/NftController.mjs.map +1 -1
- package/dist/NftDetectionController.cjs.map +1 -1
- package/dist/NftDetectionController.d.cts +5 -0
- package/dist/NftDetectionController.d.cts.map +1 -1
- package/dist/NftDetectionController.d.mts +5 -0
- package/dist/NftDetectionController.d.mts.map +1 -1
- package/dist/NftDetectionController.mjs.map +1 -1
- package/dist/RatesController/RatesController.cjs +3 -2
- package/dist/RatesController/RatesController.cjs.map +1 -1
- package/dist/RatesController/RatesController.d.cts +3 -0
- package/dist/RatesController/RatesController.d.cts.map +1 -1
- package/dist/RatesController/RatesController.d.mts +3 -0
- package/dist/RatesController/RatesController.d.mts.map +1 -1
- package/dist/RatesController/RatesController.mjs +3 -2
- package/dist/RatesController/RatesController.mjs.map +1 -1
- package/dist/RatesController/types.cjs.map +1 -1
- package/dist/RatesController/types.d.cts +1 -1
- package/dist/RatesController/types.d.cts.map +1 -1
- package/dist/RatesController/types.d.mts +1 -1
- package/dist/RatesController/types.d.mts.map +1 -1
- package/dist/RatesController/types.mjs.map +1 -1
- package/dist/Standards/NftStandards/ERC721/ERC721Standard.cjs.map +1 -1
- package/dist/Standards/NftStandards/ERC721/ERC721Standard.d.cts.map +1 -1
- package/dist/Standards/NftStandards/ERC721/ERC721Standard.d.mts.map +1 -1
- package/dist/Standards/NftStandards/ERC721/ERC721Standard.mjs.map +1 -1
- package/dist/TokenBalancesController.cjs +12 -4
- package/dist/TokenBalancesController.cjs.map +1 -1
- package/dist/TokenBalancesController.d.cts +1 -1
- package/dist/TokenBalancesController.d.cts.map +1 -1
- package/dist/TokenBalancesController.d.mts +1 -1
- package/dist/TokenBalancesController.d.mts.map +1 -1
- package/dist/TokenBalancesController.mjs +12 -4
- package/dist/TokenBalancesController.mjs.map +1 -1
- package/dist/TokenDetectionController.cjs +30 -11
- package/dist/TokenDetectionController.cjs.map +1 -1
- package/dist/TokenDetectionController.d.cts +1 -1
- package/dist/TokenDetectionController.d.cts.map +1 -1
- package/dist/TokenDetectionController.d.mts +1 -1
- package/dist/TokenDetectionController.d.mts.map +1 -1
- package/dist/TokenDetectionController.mjs +31 -12
- package/dist/TokenDetectionController.mjs.map +1 -1
- package/dist/TokenListController.d.cts +1 -1
- package/dist/TokenListController.d.mts +1 -1
- package/dist/TokenRatesController.cjs +34 -52
- package/dist/TokenRatesController.cjs.map +1 -1
- package/dist/TokenRatesController.d.cts +3 -1
- package/dist/TokenRatesController.d.cts.map +1 -1
- package/dist/TokenRatesController.d.mts +3 -1
- package/dist/TokenRatesController.d.mts.map +1 -1
- package/dist/TokenRatesController.mjs +35 -53
- package/dist/TokenRatesController.mjs.map +1 -1
- package/dist/TokensController.cjs +6 -20
- package/dist/TokensController.cjs.map +1 -1
- package/dist/TokensController.d.cts +2 -0
- package/dist/TokensController.d.cts.map +1 -1
- package/dist/TokensController.d.mts +2 -0
- package/dist/TokensController.d.mts.map +1 -1
- package/dist/TokensController.mjs +6 -20
- package/dist/TokensController.mjs.map +1 -1
- package/dist/assetsUtil.cjs +2 -46
- package/dist/assetsUtil.cjs.map +1 -1
- package/dist/assetsUtil.d.cts +2 -0
- package/dist/assetsUtil.d.cts.map +1 -1
- package/dist/assetsUtil.d.mts +2 -0
- package/dist/assetsUtil.d.mts.map +1 -1
- package/dist/assetsUtil.mjs +2 -46
- package/dist/assetsUtil.mjs.map +1 -1
- package/dist/crypto-compare-service/crypto-compare.cjs +1 -8
- package/dist/crypto-compare-service/crypto-compare.cjs.map +1 -1
- package/dist/crypto-compare-service/crypto-compare.d.cts.map +1 -1
- package/dist/crypto-compare-service/crypto-compare.d.mts.map +1 -1
- package/dist/crypto-compare-service/crypto-compare.mjs +1 -8
- package/dist/crypto-compare-service/crypto-compare.mjs.map +1 -1
- package/dist/multi-chain-accounts-service/api-balance-fetcher.cjs +43 -51
- package/dist/multi-chain-accounts-service/api-balance-fetcher.cjs.map +1 -1
- package/dist/multi-chain-accounts-service/api-balance-fetcher.d.cts +6 -2
- package/dist/multi-chain-accounts-service/api-balance-fetcher.d.cts.map +1 -1
- package/dist/multi-chain-accounts-service/api-balance-fetcher.d.mts +6 -2
- package/dist/multi-chain-accounts-service/api-balance-fetcher.d.mts.map +1 -1
- package/dist/multi-chain-accounts-service/api-balance-fetcher.mjs +44 -52
- package/dist/multi-chain-accounts-service/api-balance-fetcher.mjs.map +1 -1
- package/dist/rpc-service/rpc-balance-fetcher.cjs +1 -1
- package/dist/rpc-service/rpc-balance-fetcher.cjs.map +1 -1
- package/dist/rpc-service/rpc-balance-fetcher.d.cts +6 -2
- package/dist/rpc-service/rpc-balance-fetcher.d.cts.map +1 -1
- package/dist/rpc-service/rpc-balance-fetcher.d.mts +6 -2
- package/dist/rpc-service/rpc-balance-fetcher.d.mts.map +1 -1
- package/dist/rpc-service/rpc-balance-fetcher.mjs +1 -1
- package/dist/rpc-service/rpc-balance-fetcher.mjs.map +1 -1
- package/dist/token-prices-service/codefi-v2.cjs +2 -0
- package/dist/token-prices-service/codefi-v2.cjs.map +1 -1
- package/dist/token-prices-service/codefi-v2.d.cts +2 -0
- package/dist/token-prices-service/codefi-v2.d.cts.map +1 -1
- package/dist/token-prices-service/codefi-v2.d.mts +2 -0
- package/dist/token-prices-service/codefi-v2.d.mts.map +1 -1
- package/dist/token-prices-service/codefi-v2.mjs +2 -0
- package/dist/token-prices-service/codefi-v2.mjs.map +1 -1
- package/package.json +5 -5
package/dist/assetsUtil.mjs
CHANGED
|
@@ -46,6 +46,7 @@ export function compareNftMetadata(newNftMetadata, nft) {
|
|
|
46
46
|
}
|
|
47
47
|
/**
|
|
48
48
|
* Checks whether the existing nft object has all the keys of the new incoming nft metadata object
|
|
49
|
+
*
|
|
49
50
|
* @param newNftMetadata - New nft metadata object
|
|
50
51
|
* @param nft - Existing nft object to compare with
|
|
51
52
|
* @returns Whether the existing nft object has all the new keys from the new Nft metadata object
|
|
@@ -103,8 +104,6 @@ export const formatAggregatorNames = (aggregators) => {
|
|
|
103
104
|
*/
|
|
104
105
|
export const formatIconUrlWithProxy = ({ chainId, tokenAddress, }) => {
|
|
105
106
|
const chainIdDecimal = convertHexToDecimal(chainId).toString();
|
|
106
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
107
|
-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
108
107
|
return `https://static.cx.metamask.io/api/v1/tokenIcons/${chainIdDecimal}/${tokenAddress.toLowerCase()}.png`;
|
|
109
108
|
};
|
|
110
109
|
/**
|
|
@@ -112,65 +111,25 @@ export const formatIconUrlWithProxy = ({ chainId, tokenAddress, }) => {
|
|
|
112
111
|
*/
|
|
113
112
|
export var SupportedTokenDetectionNetworks;
|
|
114
113
|
(function (SupportedTokenDetectionNetworks) {
|
|
115
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
116
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
117
114
|
SupportedTokenDetectionNetworks["mainnet"] = "0x1";
|
|
118
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
119
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
120
115
|
SupportedTokenDetectionNetworks["bsc"] = "0x38";
|
|
121
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
122
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
123
116
|
SupportedTokenDetectionNetworks["polygon"] = "0x89";
|
|
124
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
125
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
126
117
|
SupportedTokenDetectionNetworks["avax"] = "0xa86a";
|
|
127
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
128
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
129
118
|
SupportedTokenDetectionNetworks["aurora"] = "0x4e454152";
|
|
130
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
131
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
132
119
|
SupportedTokenDetectionNetworks["linea_goerli"] = "0xe704";
|
|
133
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
134
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
135
120
|
SupportedTokenDetectionNetworks["linea_mainnet"] = "0xe708";
|
|
136
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
137
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
138
121
|
SupportedTokenDetectionNetworks["arbitrum"] = "0xa4b1";
|
|
139
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
140
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
141
122
|
SupportedTokenDetectionNetworks["optimism"] = "0xa";
|
|
142
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
143
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
144
123
|
SupportedTokenDetectionNetworks["base"] = "0x2105";
|
|
145
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
146
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
147
124
|
SupportedTokenDetectionNetworks["zksync"] = "0x144";
|
|
148
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
149
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
150
125
|
SupportedTokenDetectionNetworks["cronos"] = "0x19";
|
|
151
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
152
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
153
126
|
SupportedTokenDetectionNetworks["celo"] = "0xa4ec";
|
|
154
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
155
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
156
127
|
SupportedTokenDetectionNetworks["gnosis"] = "0x64";
|
|
157
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
158
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
159
128
|
SupportedTokenDetectionNetworks["fantom"] = "0xfa";
|
|
160
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
161
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
162
129
|
SupportedTokenDetectionNetworks["polygon_zkevm"] = "0x44d";
|
|
163
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
164
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
165
130
|
SupportedTokenDetectionNetworks["moonbeam"] = "0x504";
|
|
166
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
167
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
168
131
|
SupportedTokenDetectionNetworks["moonriver"] = "0x505";
|
|
169
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
170
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
171
132
|
SupportedTokenDetectionNetworks["sei"] = "0x531";
|
|
172
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
173
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
174
133
|
SupportedTokenDetectionNetworks["monad_mainnet"] = "0x8f";
|
|
175
134
|
})(SupportedTokenDetectionNetworks || (SupportedTokenDetectionNetworks = {}));
|
|
176
135
|
/**
|
|
@@ -178,11 +137,7 @@ export var SupportedTokenDetectionNetworks;
|
|
|
178
137
|
*/
|
|
179
138
|
export var SupportedStakedBalanceNetworks;
|
|
180
139
|
(function (SupportedStakedBalanceNetworks) {
|
|
181
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
182
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
183
140
|
SupportedStakedBalanceNetworks["mainnet"] = "0x1";
|
|
184
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
185
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
186
141
|
SupportedStakedBalanceNetworks["hoodi"] = "0x88bb0";
|
|
187
142
|
})(SupportedStakedBalanceNetworks || (SupportedStakedBalanceNetworks = {}));
|
|
188
143
|
/**
|
|
@@ -363,6 +318,7 @@ export async function fetchTokenContractExchangeRates({ tokenPricesService, nati
|
|
|
363
318
|
}
|
|
364
319
|
/**
|
|
365
320
|
* Function to search for a specific value in a given map and return the key
|
|
321
|
+
*
|
|
366
322
|
* @param map - map input to search value
|
|
367
323
|
* @param value - the value to search for
|
|
368
324
|
* @returns returns key that corresponds to the value
|
package/dist/assetsUtil.mjs.map
CHANGED
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kB,CAAC,MAAM,EACzB,WAAW,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,CAChC,CAAC;IACF,OAAO,GAAG,WAAW,IAAI,cAAc,EAAE,CAAC;AAC5C,CAAC","sourcesContent":["import type { BigNumber } from '@ethersproject/bignumber';\nimport {\n convertHexToDecimal,\n toChecksumHexAddress,\n} from '@metamask/controller-utils';\nimport type { Hex } from '@metamask/utils';\nimport {\n hexToNumber,\n KnownCaipNamespace,\n remove0x,\n toCaipChainId,\n} from '@metamask/utils';\nimport BN from 'bn.js';\nimport { CID } from 'multiformats/cid';\n\nimport type { Nft, NftMetadata } from './NftController';\nimport type { AbstractTokenPricesService } from './token-prices-service';\nimport { type ContractExchangeRates } from './TokenRatesController';\n\n/**\n * The maximum number of token addresses that should be sent to the Price API in\n * a single request.\n */\nexport const TOKEN_PRICES_BATCH_SIZE = 30;\n\n/**\n * Compares nft metadata entries to any nft entry.\n * We need this method when comparing a new fetched nft metadata, in case a entry changed to a defined value,\n * there's a need to update the nft in state.\n *\n * @param newNftMetadata - Nft metadata object.\n * @param nft - Nft object to compare with.\n * @returns Whether there are differences.\n */\nexport function compareNftMetadata(newNftMetadata: NftMetadata, nft: Nft) {\n const keys: (keyof NftMetadata)[] = [\n 'image',\n 'backgroundColor',\n 'imagePreview',\n 'imageThumbnail',\n 'imageOriginal',\n 'animation',\n 'animationOriginal',\n 'externalLink',\n 'tokenURI',\n 'chainId',\n ];\n const differentValues = keys.reduce((value, key) => {\n if (newNftMetadata[key] && newNftMetadata[key] !== nft[key]) {\n return value + 1;\n }\n return value;\n }, 0);\n return differentValues > 0;\n}\n\n/**\n * Checks whether the existing nft object has all the keys of the new incoming nft metadata object\n * @param newNftMetadata - New nft metadata object\n * @param nft - Existing nft object to compare with\n * @returns Whether the existing nft object has all the new keys from the new Nft metadata object\n */\nexport function hasNewCollectionFields(\n newNftMetadata: NftMetadata,\n nft: Nft,\n): boolean {\n const keysNewNftMetadata = Object.keys(newNftMetadata.collection ?? {});\n const keysExistingNft = new Set(Object.keys(nft.collection ?? {}));\n\n return keysNewNftMetadata.some((key) => !keysExistingNft.has(key));\n}\n\nconst aggregatorNameByKey: Record<string, string> = {\n aave: 'Aave',\n bancor: 'Bancor',\n cmc: 'CMC',\n cryptocom: 'Crypto.com',\n coinGecko: 'CoinGecko',\n oneInch: '1inch',\n paraswap: 'Paraswap',\n pmm: 'PMM',\n zapper: 'Zapper',\n zerion: 'Zerion',\n zeroEx: '0x',\n synthetix: 'Synthetix',\n yearn: 'Yearn',\n apeswap: 'ApeSwap',\n binanceDex: 'BinanceDex',\n pancakeTop100: 'PancakeTop100',\n pancakeExtended: 'PancakeExtended',\n balancer: 'Balancer',\n quickswap: 'QuickSwap',\n matcha: 'Matcha',\n pangolinDex: 'PangolinDex',\n pangolinDexStableCoin: 'PangolinDexStableCoin',\n pangolinDexAvaxBridge: 'PangolinDexAvaxBridge',\n traderJoe: 'TraderJoe',\n airswapLight: 'AirswapLight',\n kleros: 'Kleros',\n};\n\n/**\n * Formats aggregator names to presentable format.\n *\n * @param aggregators - List of token list names in camelcase.\n * @returns Formatted aggregator names.\n */\nexport const formatAggregatorNames = (aggregators: string[]) => {\n return aggregators.map(\n (key) =>\n aggregatorNameByKey[key] ||\n `${key[0].toUpperCase()}${key.substring(1, key.length)}`,\n );\n};\n\n/**\n * Format token list assets to use image proxy from Codefi.\n *\n * @param params - Object that contains chainID and tokenAddress.\n * @param params.chainId - ChainID of network in 0x-prefixed hexadecimal format.\n * @param params.tokenAddress - Address of token in mixed or lowercase.\n * @returns Formatted image url\n */\nexport const formatIconUrlWithProxy = ({\n chainId,\n tokenAddress,\n}: {\n chainId: Hex;\n tokenAddress: string;\n}) => {\n const chainIdDecimal = convertHexToDecimal(chainId).toString();\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n return `https://static.cx.metamask.io/api/v1/tokenIcons/${chainIdDecimal}/${tokenAddress.toLowerCase()}.png`;\n};\n\n/**\n * Networks where token detection is supported - Values are in hex format\n */\nexport enum SupportedTokenDetectionNetworks {\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n mainnet = '0x1', // decimal: 1\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n bsc = '0x38', // decimal: 56\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n polygon = '0x89', // decimal: 137\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n avax = '0xa86a', // decimal: 43114\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n aurora = '0x4e454152', // decimal: 1313161554\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n linea_goerli = '0xe704', // decimal: 59140\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n linea_mainnet = '0xe708', // decimal: 59144\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n arbitrum = '0xa4b1', // decimal: 42161\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n optimism = '0xa', // decimal: 10\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n base = '0x2105', // decimal: 8453\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n zksync = '0x144', // decimal: 324\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n cronos = '0x19', // decimal: 25\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n celo = '0xa4ec', // decimal: 42220\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n gnosis = '0x64', // decimal: 100\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n fantom = '0xfa', // decimal: 250\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n polygon_zkevm = '0x44d', // decimal: 1101\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n moonbeam = '0x504', // decimal: 1284\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n moonriver = '0x505', // decimal: 1285\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n sei = '0x531', // decimal: 1329\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n monad_mainnet = '0x8f', // decimal: 143\n}\n\n/**\n * Networks where staked balance is supported - Values are in hex format\n */\nexport enum SupportedStakedBalanceNetworks {\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n mainnet = '0x1', // decimal: 1\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n hoodi = '0x88bb0', // decimal: 560048\n}\n\n/**\n * Check if token detection is enabled for certain networks.\n *\n * @param chainId - ChainID of network\n * @returns Whether the current network supports token detection\n */\nexport function isTokenDetectionSupportedForNetwork(chainId: Hex): boolean {\n return Object.values<Hex>(SupportedTokenDetectionNetworks).includes(chainId);\n}\n\n/**\n * Check if token list polling is enabled for a given network.\n * Currently this method is used to support e2e testing for consumers of this package.\n *\n * @param chainId - ChainID of network\n * @returns Whether the current network supports tokenlists\n */\nexport function isTokenListSupportedForNetwork(chainId: Hex): boolean {\n return isTokenDetectionSupportedForNetwork(chainId);\n}\n\n/**\n * Removes IPFS protocol prefix from input string.\n *\n * @param ipfsUrl - An IPFS url (e.g. ipfs://{content id})\n * @returns IPFS content identifier and (possibly) path in a string\n * @throws Will throw if the url passed is not IPFS.\n */\nexport function removeIpfsProtocolPrefix(ipfsUrl: string) {\n if (ipfsUrl.startsWith('ipfs://ipfs/')) {\n return ipfsUrl.replace('ipfs://ipfs/', '');\n } else if (ipfsUrl.startsWith('ipfs://')) {\n return ipfsUrl.replace('ipfs://', '');\n }\n // this method should not be used with non-ipfs urls (i.e. startsWith('ipfs://') === true)\n throw new Error('this method should not be used with non ipfs urls');\n}\n\n/**\n * Extracts content identifier and path from an input string.\n *\n * @param ipfsUrl - An IPFS URL minus the IPFS protocol prefix\n * @returns IFPS content identifier (cid) and sub path as string.\n * @throws Will throw if the url passed is not ipfs.\n */\nexport async function getIpfsCIDv1AndPath(ipfsUrl: string): Promise<{\n cid: string;\n path?: string;\n}> {\n const url = removeIpfsProtocolPrefix(ipfsUrl);\n\n // check if there is a path\n // (CID is everything preceding first forward slash, path is everything after)\n const index = url.indexOf('/');\n const cid = index !== -1 ? url.substring(0, index) : url;\n const path = index !== -1 ? url.substring(index) : undefined;\n\n // We want to ensure that the CID is v1 (https://docs.ipfs.io/concepts/content-addressing/#identifier-formats)\n // because most cid v0s appear to be incompatible with IPFS subdomains\n return {\n cid: CID.parse(cid).toV1().toString(),\n path,\n };\n}\n\n/**\n * Formats URL correctly for use retrieving assets hosted on IPFS.\n *\n * @param ipfsGateway - The users preferred IPFS gateway (full URL or just host).\n * @param ipfsUrl - The IFPS URL pointed at the asset.\n * @param subdomainSupported - Boolean indicating whether the URL should be formatted with subdomains or not.\n * @returns A formatted URL, with the user's preferred IPFS gateway and format (subdomain or not), pointing to an asset hosted on IPFS.\n */\nexport async function getFormattedIpfsUrl(\n ipfsGateway: string,\n ipfsUrl: string,\n subdomainSupported: boolean,\n): Promise<string> {\n const { host, protocol, origin } = new URL(addUrlProtocolPrefix(ipfsGateway));\n if (subdomainSupported) {\n const { cid, path } = await getIpfsCIDv1AndPath(ipfsUrl);\n return `${protocol}//${cid}.ipfs.${host}${path ?? ''}`;\n }\n const cidAndPath = removeIpfsProtocolPrefix(ipfsUrl);\n return `${origin}/ipfs/${cidAndPath}`;\n}\n\n/**\n * Adds URL protocol prefix to input URL string if missing.\n *\n * @param urlString - An IPFS URL.\n * @returns A URL with a https:// prepended.\n */\nexport function addUrlProtocolPrefix(urlString: string): string {\n if (!urlString.match(/(^http:\\/\\/)|(^https:\\/\\/)/u)) {\n return `https://${urlString}`;\n }\n return urlString;\n}\n\n/**\n * Converts an Ethers BigNumber to a BN.\n *\n * @param bigNumber - An Ethers BigNumber instance.\n * @returns A BN object.\n */\nexport function ethersBigNumberToBN(bigNumber: BigNumber): BN {\n return new BN(remove0x(bigNumber.toHexString()), 'hex');\n}\n\n/**\n * Partitions a list of values into groups that are at most `batchSize` in\n * length.\n *\n * @param values - The list of values.\n * @param args - The remaining arguments.\n * @param args.batchSize - The desired maximum number of values per batch.\n * @returns The list of batches.\n */\nexport function divideIntoBatches<Value>(\n values: Value[],\n { batchSize }: { batchSize: number },\n): Value[][] {\n const batches = [];\n for (let i = 0; i < values.length; i += batchSize) {\n batches.push(values.slice(i, i + batchSize));\n }\n return batches;\n}\n\n/**\n * Constructs a result from processing batches of the given values\n * sequentially.\n *\n * @param args - The arguments to this function.\n * @param args.values - A list of values to iterate over.\n * @param args.batchSize - The maximum number of values in each batch.\n * @param args.eachBatch - A function to call for each batch. This function is\n * similar to the function that `Array.prototype.reduce` takes, in that it\n * receives the object that is being built, each batch in the list of batches\n * and the index, and should return an updated version of the object.\n * @param args.initialResult - The initial value of the final data structure,\n * i.e., the value that will be fed into the first call of `eachBatch`.\n * @returns The built result.\n */\nexport async function reduceInBatchesSerially<Value, Result>({\n values,\n batchSize,\n eachBatch,\n initialResult,\n}: {\n values: Value[];\n batchSize: number;\n eachBatch: (\n workingResult: Partial<Result>,\n batch: Value[],\n index: number,\n ) => Partial<Result> | Promise<Partial<Result>>;\n initialResult: Partial<Result>;\n}): Promise<Result> {\n const batches = divideIntoBatches(values, { batchSize });\n let workingResult = initialResult;\n for (const [index, batch] of batches.entries()) {\n workingResult = await eachBatch(workingResult, batch, index);\n }\n // There's no way around this — we have to assume that in the end, the result\n // matches the intended type.\n const finalResult = workingResult as Result;\n return finalResult;\n}\n\n/**\n * Retrieves token prices for a set of contract addresses in a specific currency and chainId.\n *\n * @param args - The arguments to function.\n * @param args.tokenPricesService - An object in charge of retrieving token prices.\n * @param args.nativeCurrency - The native currency to request price in.\n * @param args.tokenAddresses - The list of contract addresses.\n * @param args.chainId - The chainId of the tokens.\n * @returns The prices for the requested tokens.\n */\nexport async function fetchTokenContractExchangeRates({\n tokenPricesService,\n nativeCurrency,\n tokenAddresses,\n chainId,\n}: {\n tokenPricesService: AbstractTokenPricesService;\n nativeCurrency: string;\n tokenAddresses: Hex[];\n chainId: Hex;\n}): Promise<ContractExchangeRates> {\n const isChainIdSupported =\n tokenPricesService.validateChainIdSupported(chainId);\n const isCurrencySupported =\n tokenPricesService.validateCurrencySupported(nativeCurrency);\n\n if (!isChainIdSupported || !isCurrencySupported) {\n return {};\n }\n\n const tokenPricesByTokenAddress = await reduceInBatchesSerially<\n Hex,\n Awaited<ReturnType<AbstractTokenPricesService['fetchTokenPrices']>>\n >({\n values: [...tokenAddresses].sort(),\n batchSize: TOKEN_PRICES_BATCH_SIZE,\n eachBatch: async (allTokenPricesByTokenAddress, batch) => {\n const tokenPricesByTokenAddressForBatch =\n await tokenPricesService.fetchTokenPrices({\n tokenAddresses: batch,\n chainId,\n currency: nativeCurrency,\n });\n\n return {\n ...allTokenPricesByTokenAddress,\n ...tokenPricesByTokenAddressForBatch,\n };\n },\n initialResult: {},\n });\n\n return Object.entries(tokenPricesByTokenAddress).reduce(\n (obj, [tokenAddress, tokenPrice]) => {\n return {\n ...obj,\n [toChecksumHexAddress(tokenAddress)]: tokenPrice?.price,\n };\n },\n {},\n );\n}\n\n/**\n * Function to search for a specific value in a given map and return the key\n * @param map - map input to search value\n * @param value - the value to search for\n * @returns returns key that corresponds to the value\n */\nexport function getKeyByValue(map: Map<string, string>, value: string) {\n for (const [key, val] of map.entries()) {\n if (val === value) {\n return key;\n }\n }\n return null; // Return null if no match is found\n}\n\n/**\n * Converts a hex chainId and account address to a CAIP account reference.\n *\n * @param chainId - The hex chain ID\n * @param accountAddress - The account address\n * @returns The CAIP account reference in format \"namespace:reference:address\"\n */\nexport function accountAddressToCaipReference(\n chainId: Hex,\n accountAddress: string,\n) {\n const caipChainId = toCaipChainId(\n KnownCaipNamespace.Eip155,\n hexToNumber(chainId).toString(),\n );\n return `${caipChainId}:${accountAddress}`;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"assetsUtil.mjs","sourceRoot":"","sources":["../src/assetsUtil.ts"],"names":[],"mappings":";;;;;;AACA,OAAO,EACL,mBAAmB,EACnB,oBAAoB,EACrB,mCAAmC;AAEpC,OAAO,EACL,WAAW,EACX,kBAAkB,EAClB,QAAQ,EACR,aAAa,EACd,wBAAwB;AACzB,OAAO,GAAE,cAAc;;AACvB,OAAO,EAAE,GAAG,EAAE,yBAAyB;AAMvC;;;GAGG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,EAAE,CAAC;AAE1C;;;;;;;;GAQG;AACH,MAAM,UAAU,kBAAkB,CAAC,cAA2B,EAAE,GAAQ;IACtE,MAAM,IAAI,GAA0B;QAClC,OAAO;QACP,iBAAiB;QACjB,cAAc;QACd,gBAAgB;QAChB,eAAe;QACf,WAAW;QACX,mBAAmB;QACnB,cAAc;QACd,UAAU;QACV,SAAS;KACV,CAAC;IACF,MAAM,eAAe,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE;QACjD,IAAI,cAAc,CAAC,GAAG,CAAC,IAAI,cAAc,CAAC,GAAG,CAA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\nimport {\n convertHexToDecimal,\n toChecksumHexAddress,\n} from '@metamask/controller-utils';\nimport type { Hex } from '@metamask/utils';\nimport {\n hexToNumber,\n KnownCaipNamespace,\n remove0x,\n toCaipChainId,\n} from '@metamask/utils';\nimport BN from 'bn.js';\nimport { CID } from 'multiformats/cid';\n\nimport type { Nft, NftMetadata } from './NftController';\nimport type { AbstractTokenPricesService } from './token-prices-service';\nimport { type ContractExchangeRates } from './TokenRatesController';\n\n/**\n * The maximum number of token addresses that should be sent to the Price API in\n * a single request.\n */\nexport const TOKEN_PRICES_BATCH_SIZE = 30;\n\n/**\n * Compares nft metadata entries to any nft entry.\n * We need this method when comparing a new fetched nft metadata, in case a entry changed to a defined value,\n * there's a need to update the nft in state.\n *\n * @param newNftMetadata - Nft metadata object.\n * @param nft - Nft object to compare with.\n * @returns Whether there are differences.\n */\nexport function compareNftMetadata(newNftMetadata: NftMetadata, nft: Nft) {\n const keys: (keyof NftMetadata)[] = [\n 'image',\n 'backgroundColor',\n 'imagePreview',\n 'imageThumbnail',\n 'imageOriginal',\n 'animation',\n 'animationOriginal',\n 'externalLink',\n 'tokenURI',\n 'chainId',\n ];\n const differentValues = keys.reduce((value, key) => {\n if (newNftMetadata[key] && newNftMetadata[key] !== nft[key]) {\n return value + 1;\n }\n return value;\n }, 0);\n return differentValues > 0;\n}\n\n/**\n * Checks whether the existing nft object has all the keys of the new incoming nft metadata object\n *\n * @param newNftMetadata - New nft metadata object\n * @param nft - Existing nft object to compare with\n * @returns Whether the existing nft object has all the new keys from the new Nft metadata object\n */\nexport function hasNewCollectionFields(\n newNftMetadata: NftMetadata,\n nft: Nft,\n): boolean {\n const keysNewNftMetadata = Object.keys(newNftMetadata.collection ?? {});\n const keysExistingNft = new Set(Object.keys(nft.collection ?? {}));\n\n return keysNewNftMetadata.some((key) => !keysExistingNft.has(key));\n}\n\nconst aggregatorNameByKey: Record<string, string> = {\n aave: 'Aave',\n bancor: 'Bancor',\n cmc: 'CMC',\n cryptocom: 'Crypto.com',\n coinGecko: 'CoinGecko',\n oneInch: '1inch',\n paraswap: 'Paraswap',\n pmm: 'PMM',\n zapper: 'Zapper',\n zerion: 'Zerion',\n zeroEx: '0x',\n synthetix: 'Synthetix',\n yearn: 'Yearn',\n apeswap: 'ApeSwap',\n binanceDex: 'BinanceDex',\n pancakeTop100: 'PancakeTop100',\n pancakeExtended: 'PancakeExtended',\n balancer: 'Balancer',\n quickswap: 'QuickSwap',\n matcha: 'Matcha',\n pangolinDex: 'PangolinDex',\n pangolinDexStableCoin: 'PangolinDexStableCoin',\n pangolinDexAvaxBridge: 'PangolinDexAvaxBridge',\n traderJoe: 'TraderJoe',\n airswapLight: 'AirswapLight',\n kleros: 'Kleros',\n};\n\n/**\n * Formats aggregator names to presentable format.\n *\n * @param aggregators - List of token list names in camelcase.\n * @returns Formatted aggregator names.\n */\nexport const formatAggregatorNames = (aggregators: string[]) => {\n return aggregators.map(\n (key) =>\n aggregatorNameByKey[key] ||\n `${key[0].toUpperCase()}${key.substring(1, key.length)}`,\n );\n};\n\n/**\n * Format token list assets to use image proxy from Codefi.\n *\n * @param params - Object that contains chainID and tokenAddress.\n * @param params.chainId - ChainID of network in 0x-prefixed hexadecimal format.\n * @param params.tokenAddress - Address of token in mixed or lowercase.\n * @returns Formatted image url\n */\nexport const formatIconUrlWithProxy = ({\n chainId,\n tokenAddress,\n}: {\n chainId: Hex;\n tokenAddress: string;\n}) => {\n const chainIdDecimal = convertHexToDecimal(chainId).toString();\n return `https://static.cx.metamask.io/api/v1/tokenIcons/${chainIdDecimal}/${tokenAddress.toLowerCase()}.png`;\n};\n\n/**\n * Networks where token detection is supported - Values are in hex format\n */\nexport enum SupportedTokenDetectionNetworks {\n mainnet = '0x1', // decimal: 1\n bsc = '0x38', // decimal: 56\n polygon = '0x89', // decimal: 137\n avax = '0xa86a', // decimal: 43114\n aurora = '0x4e454152', // decimal: 1313161554\n linea_goerli = '0xe704', // decimal: 59140\n linea_mainnet = '0xe708', // decimal: 59144\n arbitrum = '0xa4b1', // decimal: 42161\n optimism = '0xa', // decimal: 10\n base = '0x2105', // decimal: 8453\n zksync = '0x144', // decimal: 324\n cronos = '0x19', // decimal: 25\n celo = '0xa4ec', // decimal: 42220\n gnosis = '0x64', // decimal: 100\n fantom = '0xfa', // decimal: 250\n polygon_zkevm = '0x44d', // decimal: 1101\n moonbeam = '0x504', // decimal: 1284\n moonriver = '0x505', // decimal: 1285\n sei = '0x531', // decimal: 1329\n monad_mainnet = '0x8f', // decimal: 143\n}\n\n/**\n * Networks where staked balance is supported - Values are in hex format\n */\nexport enum SupportedStakedBalanceNetworks {\n mainnet = '0x1', // decimal: 1\n hoodi = '0x88bb0', // decimal: 560048\n}\n\n/**\n * Check if token detection is enabled for certain networks.\n *\n * @param chainId - ChainID of network\n * @returns Whether the current network supports token detection\n */\nexport function isTokenDetectionSupportedForNetwork(chainId: Hex): boolean {\n return Object.values<Hex>(SupportedTokenDetectionNetworks).includes(chainId);\n}\n\n/**\n * Check if token list polling is enabled for a given network.\n * Currently this method is used to support e2e testing for consumers of this package.\n *\n * @param chainId - ChainID of network\n * @returns Whether the current network supports tokenlists\n */\nexport function isTokenListSupportedForNetwork(chainId: Hex): boolean {\n return isTokenDetectionSupportedForNetwork(chainId);\n}\n\n/**\n * Removes IPFS protocol prefix from input string.\n *\n * @param ipfsUrl - An IPFS url (e.g. ipfs://{content id})\n * @returns IPFS content identifier and (possibly) path in a string\n * @throws Will throw if the url passed is not IPFS.\n */\nexport function removeIpfsProtocolPrefix(ipfsUrl: string) {\n if (ipfsUrl.startsWith('ipfs://ipfs/')) {\n return ipfsUrl.replace('ipfs://ipfs/', '');\n } else if (ipfsUrl.startsWith('ipfs://')) {\n return ipfsUrl.replace('ipfs://', '');\n }\n // this method should not be used with non-ipfs urls (i.e. startsWith('ipfs://') === true)\n throw new Error('this method should not be used with non ipfs urls');\n}\n\n/**\n * Extracts content identifier and path from an input string.\n *\n * @param ipfsUrl - An IPFS URL minus the IPFS protocol prefix\n * @returns IFPS content identifier (cid) and sub path as string.\n * @throws Will throw if the url passed is not ipfs.\n */\nexport async function getIpfsCIDv1AndPath(ipfsUrl: string): Promise<{\n cid: string;\n path?: string;\n}> {\n const url = removeIpfsProtocolPrefix(ipfsUrl);\n\n // check if there is a path\n // (CID is everything preceding first forward slash, path is everything after)\n const index = url.indexOf('/');\n const cid = index !== -1 ? url.substring(0, index) : url;\n const path = index !== -1 ? url.substring(index) : undefined;\n\n // We want to ensure that the CID is v1 (https://docs.ipfs.io/concepts/content-addressing/#identifier-formats)\n // because most cid v0s appear to be incompatible with IPFS subdomains\n return {\n cid: CID.parse(cid).toV1().toString(),\n path,\n };\n}\n\n/**\n * Formats URL correctly for use retrieving assets hosted on IPFS.\n *\n * @param ipfsGateway - The users preferred IPFS gateway (full URL or just host).\n * @param ipfsUrl - The IFPS URL pointed at the asset.\n * @param subdomainSupported - Boolean indicating whether the URL should be formatted with subdomains or not.\n * @returns A formatted URL, with the user's preferred IPFS gateway and format (subdomain or not), pointing to an asset hosted on IPFS.\n */\nexport async function getFormattedIpfsUrl(\n ipfsGateway: string,\n ipfsUrl: string,\n subdomainSupported: boolean,\n): Promise<string> {\n const { host, protocol, origin } = new URL(addUrlProtocolPrefix(ipfsGateway));\n if (subdomainSupported) {\n const { cid, path } = await getIpfsCIDv1AndPath(ipfsUrl);\n return `${protocol}//${cid}.ipfs.${host}${path ?? ''}`;\n }\n const cidAndPath = removeIpfsProtocolPrefix(ipfsUrl);\n return `${origin}/ipfs/${cidAndPath}`;\n}\n\n/**\n * Adds URL protocol prefix to input URL string if missing.\n *\n * @param urlString - An IPFS URL.\n * @returns A URL with a https:// prepended.\n */\nexport function addUrlProtocolPrefix(urlString: string): string {\n if (!urlString.match(/(^http:\\/\\/)|(^https:\\/\\/)/u)) {\n return `https://${urlString}`;\n }\n return urlString;\n}\n\n/**\n * Converts an Ethers BigNumber to a BN.\n *\n * @param bigNumber - An Ethers BigNumber instance.\n * @returns A BN object.\n */\nexport function ethersBigNumberToBN(bigNumber: BigNumber): BN {\n return new BN(remove0x(bigNumber.toHexString()), 'hex');\n}\n\n/**\n * Partitions a list of values into groups that are at most `batchSize` in\n * length.\n *\n * @param values - The list of values.\n * @param args - The remaining arguments.\n * @param args.batchSize - The desired maximum number of values per batch.\n * @returns The list of batches.\n */\nexport function divideIntoBatches<Value>(\n values: Value[],\n { batchSize }: { batchSize: number },\n): Value[][] {\n const batches = [];\n for (let i = 0; i < values.length; i += batchSize) {\n batches.push(values.slice(i, i + batchSize));\n }\n return batches;\n}\n\n/**\n * Constructs a result from processing batches of the given values\n * sequentially.\n *\n * @param args - The arguments to this function.\n * @param args.values - A list of values to iterate over.\n * @param args.batchSize - The maximum number of values in each batch.\n * @param args.eachBatch - A function to call for each batch. This function is\n * similar to the function that `Array.prototype.reduce` takes, in that it\n * receives the object that is being built, each batch in the list of batches\n * and the index, and should return an updated version of the object.\n * @param args.initialResult - The initial value of the final data structure,\n * i.e., the value that will be fed into the first call of `eachBatch`.\n * @returns The built result.\n */\nexport async function reduceInBatchesSerially<Value, Result>({\n values,\n batchSize,\n eachBatch,\n initialResult,\n}: {\n values: Value[];\n batchSize: number;\n eachBatch: (\n workingResult: Partial<Result>,\n batch: Value[],\n index: number,\n ) => Partial<Result> | Promise<Partial<Result>>;\n initialResult: Partial<Result>;\n}): Promise<Result> {\n const batches = divideIntoBatches(values, { batchSize });\n let workingResult = initialResult;\n for (const [index, batch] of batches.entries()) {\n workingResult = await eachBatch(workingResult, batch, index);\n }\n // There's no way around this — we have to assume that in the end, the result\n // matches the intended type.\n const finalResult = workingResult as Result;\n return finalResult;\n}\n\n/**\n * Retrieves token prices for a set of contract addresses in a specific currency and chainId.\n *\n * @param args - The arguments to function.\n * @param args.tokenPricesService - An object in charge of retrieving token prices.\n * @param args.nativeCurrency - The native currency to request price in.\n * @param args.tokenAddresses - The list of contract addresses.\n * @param args.chainId - The chainId of the tokens.\n * @returns The prices for the requested tokens.\n */\nexport async function fetchTokenContractExchangeRates({\n tokenPricesService,\n nativeCurrency,\n tokenAddresses,\n chainId,\n}: {\n tokenPricesService: AbstractTokenPricesService;\n nativeCurrency: string;\n tokenAddresses: Hex[];\n chainId: Hex;\n}): Promise<ContractExchangeRates> {\n const isChainIdSupported =\n tokenPricesService.validateChainIdSupported(chainId);\n const isCurrencySupported =\n tokenPricesService.validateCurrencySupported(nativeCurrency);\n\n if (!isChainIdSupported || !isCurrencySupported) {\n return {};\n }\n\n const tokenPricesByTokenAddress = await reduceInBatchesSerially<\n Hex,\n Awaited<ReturnType<AbstractTokenPricesService['fetchTokenPrices']>>\n >({\n values: [...tokenAddresses].sort(),\n batchSize: TOKEN_PRICES_BATCH_SIZE,\n eachBatch: async (allTokenPricesByTokenAddress, batch) => {\n const tokenPricesByTokenAddressForBatch =\n await tokenPricesService.fetchTokenPrices({\n tokenAddresses: batch,\n chainId,\n currency: nativeCurrency,\n });\n\n return {\n ...allTokenPricesByTokenAddress,\n ...tokenPricesByTokenAddressForBatch,\n };\n },\n initialResult: {},\n });\n\n return Object.entries(tokenPricesByTokenAddress).reduce(\n (obj, [tokenAddress, tokenPrice]) => {\n return {\n ...obj,\n [toChecksumHexAddress(tokenAddress)]: tokenPrice?.price,\n };\n },\n {},\n );\n}\n\n/**\n * Function to search for a specific value in a given map and return the key\n *\n * @param map - map input to search value\n * @param value - the value to search for\n * @returns returns key that corresponds to the value\n */\nexport function getKeyByValue(map: Map<string, string>, value: string) {\n for (const [key, val] of map.entries()) {\n if (val === value) {\n return key;\n }\n }\n return null; // Return null if no match is found\n}\n\n/**\n * Converts a hex chainId and account address to a CAIP account reference.\n *\n * @param chainId - The hex chain ID\n * @param accountAddress - The account address\n * @returns The CAIP account reference in format \"namespace:reference:address\"\n */\nexport function accountAddressToCaipReference(\n chainId: Hex,\n accountAddress: string,\n) {\n const caipChainId = toCaipChainId(\n KnownCaipNamespace.Eip155,\n hexToNumber(chainId).toString(),\n );\n return `${caipChainId}:${accountAddress}`;\n}\n"]}
|
|
@@ -58,8 +58,6 @@ function getMultiPricingURL(fsyms, tsyms, includeUSDRate = false) {
|
|
|
58
58
|
* @param json.Response - The response status.
|
|
59
59
|
* @param json.Message - The error message.
|
|
60
60
|
*/
|
|
61
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
62
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
63
61
|
function handleErrorResponse(json) {
|
|
64
62
|
if (json.Response === 'Error') {
|
|
65
63
|
throw new Error(json.Message);
|
|
@@ -83,14 +81,9 @@ async function fetchExchangeRate(currency, nativeCurrency, includeUSDRate) {
|
|
|
83
81
|
const conversionRate = Number(json[currency.toUpperCase()]);
|
|
84
82
|
const usdConversionRate = Number(json.USD);
|
|
85
83
|
if (!Number.isFinite(conversionRate)) {
|
|
86
|
-
throw new Error(`Invalid response for ${currency.toUpperCase()}: ${
|
|
87
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
88
|
-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
89
|
-
json[currency.toUpperCase()]}`);
|
|
84
|
+
throw new Error(`Invalid response for ${currency.toUpperCase()}: ${json[currency.toUpperCase()]}`);
|
|
90
85
|
}
|
|
91
86
|
if (includeUSDRate && !Number.isFinite(usdConversionRate)) {
|
|
92
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
93
|
-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
94
87
|
throw new Error(`Invalid response for usdConversionRate: ${json.USD}`);
|
|
95
88
|
}
|
|
96
89
|
return {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"crypto-compare.cjs","sourceRoot":"","sources":["../../src/crypto-compare-service/crypto-compare.ts"],"names":[],"mappings":";;;AAAA,iEAAyD;AAEzD,kDAA8C;AAE9C;;;GAGG;AACH,MAAM,qBAAqB,GAAG,IAAI,GAAG,CAAC;IACpC,CAAC,KAAK,EAAE,QAAQ,CAAC;IACjB,CAAC,MAAM,EAAE,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,CAAC;IAED,OAAO;QACL,cAAc;QACd,iBAAiB;KAClB,CAAC;AACJ,CAAC;AArCD,8CAqCC;AAED;;;;;;;GAOG;AACI,KAAK,UAAU,sBAAsB,CAC1C,YAAoB,EACpB,gBAA0B,EAC1B,cAAuB;IAEvB,MAAM,KAAK,GAAG,gBAAgB,CAAC,GAAG,CAChC,CAAC,cAAc,EAAE,EAAE,CACjB,qBAAqB,CAAC,GAAG,CAAC,cAAc,CAAC,IAAI,cAAc,CAC9D,CAAC;IACF,MAAM,GAAG,GAAG,kBAAkB,CAAC,KAAK,EAAE,CAAC,YAAY,CAAC,EAAE,cAAc,CAAC,CAAC;IACtE,MAAM,QAAQ,GAAG,MAAM,IAAA,8BAAW,EAAC,GAAG,CAAC,CAAC;IACxC,mBAAmB,CAAC,QAAQ,CAAC,CAAC;IAE9B,MAAM,KAAK,GAA2C,EAAE,CAAC;IACzD,KAAK,MAAM,CAAC,cAAc,EAAE,MAAM,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,QAAQ,CAG3D,EAAE,CAAC;QACJ,MAAM,GAAG,GAAG,IAAA,0BAAa,EAAC,qBAAqB,EAAE,cAAc,CAAC,CAAC;QACjE,KAAK,CAAC,GAAG,EAAE,WAAW,EAAE,IAAI,cAAc,CAAC,WAAW,EAAE,CAAC,GAAG;YAC1D,CAAC,YAAY,CAAC,WAAW,EAAE,CAAC,EAAE,MAAM,CAAC,YAAY,CAAC,WAAW,EAAE,CAAC;YAChE,GAAG,CAAC,cAAc,IAAI,EAAE,GAAG,EAAE,MAAM,CAAC,GAAG,EAAE,CAAC;SAC3C,CAAC;IACJ,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC;AA1BD,wDA0BC","sourcesContent":["import { handleFetch } from '@metamask/controller-utils';\n\nimport { getKeyByValue } from '../assetsUtil';\n\n/**\n * A map from native currency symbol to CryptoCompare identifier.\n * This is only needed when the values don't match.\n */\nconst nativeSymbolOverrides = new Map([\n ['MNT', 'MANTLE'],\n ['OMNI', 'OMNINET'],\n]);\n\nconst CRYPTO_COMPARE_DOMAIN = 'https://min-api.cryptocompare.com';\n\n/**\n * Get the CryptoCompare API URL for getting the conversion rate from the given native currency to\n * the given currency. Optionally, the conversion rate from the native currency to USD can also be\n * included in the response.\n *\n * @param currentCurrency - The currency to get a conversion rate for.\n * @param nativeCurrency - The native currency to convert from.\n * @param includeUSDRate - Whether or not the native currency to USD conversion rate should be\n * included in the response as well.\n * @returns The API URL for getting the conversion rate.\n */\nfunction getPricingURL(\n currentCurrency: string,\n nativeCurrency: string,\n includeUSDRate?: boolean,\n) {\n return (\n `${CRYPTO_COMPARE_DOMAIN}/data/price?fsym=` +\n `${nativeCurrency}&tsyms=${currentCurrency}` +\n `${includeUSDRate && currentCurrency.toUpperCase() !== 'USD' ? ',USD' : ''}`\n );\n}\n\n/**\n * Get the CryptoCompare API URL for getting the conversion rate from a given array of native currencies\n * to the given currencies. Optionally, the conversion rate from the native currency to USD can also be\n * included in the response.\n *\n * @param fsyms - The native currencies to get conversion rates for.\n * @param tsyms - The currencies to convert to.\n * @param includeUSDRate - Whether or not the native currency to USD conversion rate should be included.\n * @returns The API URL for getting the conversion rates.\n */\nfunction getMultiPricingURL(\n fsyms: string[],\n tsyms: string[],\n includeUSDRate = false,\n) {\n const updatedTsyms =\n includeUSDRate && !tsyms.some((t) => t.toUpperCase() === 'USD')\n ? [...tsyms, 'USD']\n : tsyms;\n\n const params = new URLSearchParams();\n params.append('fsyms', fsyms.join(','));\n params.append('tsyms', updatedTsyms.join(','));\n\n const url = new URL(`${CRYPTO_COMPARE_DOMAIN}/data/pricemulti`);\n url.search = params.toString();\n return url.toString();\n}\n\n/**\n * Handles an error response from the CryptoCompare API.\n * Expected error response format\n * { Response: \"Error\", Message: \"...\", HasWarning: false }\n *\n * @param json - The JSON response from the CryptoCompare API.\n * @param json.Response - The response status.\n * @param json.Message - The error message.\n */\nfunction handleErrorResponse(json: { Response?: string; Message?: string }) {\n if (json.Response === 'Error') {\n throw new Error(json.Message);\n }\n}\n\n/**\n * Fetches the exchange rate for a given currency.\n *\n * @param currency - ISO 4217 currency code.\n * @param nativeCurrency - Symbol for base asset.\n * @param includeUSDRate - Whether to add the USD rate to the fetch.\n * @returns Promise resolving to exchange rate for given currency.\n */\nexport async function fetchExchangeRate(\n currency: string,\n nativeCurrency: string,\n includeUSDRate?: boolean,\n): Promise<{\n conversionRate: number;\n usdConversionRate: number;\n}> {\n currency = currency.toUpperCase();\n nativeCurrency = nativeCurrency.toUpperCase();\n currency = nativeSymbolOverrides.get(currency) ?? currency;\n nativeCurrency = nativeSymbolOverrides.get(nativeCurrency) ?? nativeCurrency;\n\n const json = await handleFetch(\n getPricingURL(currency, nativeCurrency, includeUSDRate),\n );\n\n handleErrorResponse(json);\n const conversionRate = Number(json[currency.toUpperCase()]);\n\n const usdConversionRate = Number(json.USD);\n if (!Number.isFinite(conversionRate)) {\n throw new Error(\n `Invalid response for ${currency.toUpperCase()}: ${\n json[currency.toUpperCase()]\n }`,\n );\n }\n\n if (includeUSDRate && !Number.isFinite(usdConversionRate)) {\n throw new Error(`Invalid response for usdConversionRate: ${json.USD}`);\n }\n\n return {\n conversionRate,\n usdConversionRate,\n };\n}\n\n/**\n * Fetches the exchange rates for multiple currencies.\n *\n * @param fiatCurrency - The currency of the rates (ISO 4217).\n * @param cryptocurrencies - The cryptocurrencies to get conversion rates for. Min length: 1. Max length: 300.\n * @param includeUSDRate - Whether to add the USD rate to the fetch.\n * @returns Promise resolving to exchange rates for given currencies.\n */\nexport async function fetchMultiExchangeRate(\n fiatCurrency: string,\n cryptocurrencies: string[],\n includeUSDRate: boolean,\n): Promise<Record<string, Record<string, number>>> {\n const fsyms = cryptocurrencies.map(\n (nativeCurrency) =>\n nativeSymbolOverrides.get(nativeCurrency) ?? nativeCurrency,\n );\n const url = getMultiPricingURL(fsyms, [fiatCurrency], includeUSDRate);\n const response = await handleFetch(url);\n handleErrorResponse(response);\n\n const rates: Record<string, Record<string, number>> = {};\n for (const [cryptocurrency, values] of Object.entries(response) as [\n string,\n Record<string, number>,\n ][]) {\n const key = getKeyByValue(nativeSymbolOverrides, cryptocurrency);\n rates[key?.toLowerCase() ?? cryptocurrency.toLowerCase()] = {\n [fiatCurrency.toLowerCase()]: values[fiatCurrency.toUpperCase()],\n ...(includeUSDRate && { usd: values.USD }),\n };\n }\n\n return rates;\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"crypto-compare.d.cts","sourceRoot":"","sources":["../../src/crypto-compare-service/crypto-compare.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"crypto-compare.d.cts","sourceRoot":"","sources":["../../src/crypto-compare-service/crypto-compare.ts"],"names":[],"mappings":"AAkFA;;;;;;;GAOG;AACH,wBAAsB,iBAAiB,CACrC,QAAQ,EAAE,MAAM,EAChB,cAAc,EAAE,MAAM,EACtB,cAAc,CAAC,EAAE,OAAO,GACvB,OAAO,CAAC;IACT,cAAc,EAAE,MAAM,CAAC;IACvB,iBAAiB,EAAE,MAAM,CAAC;CAC3B,CAAC,CA8BD;AAED;;;;;;;GAOG;AACH,wBAAsB,sBAAsB,CAC1C,YAAY,EAAE,MAAM,EACpB,gBAAgB,EAAE,MAAM,EAAE,EAC1B,cAAc,EAAE,OAAO,GACtB,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,CAsBjD"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"crypto-compare.d.mts","sourceRoot":"","sources":["../../src/crypto-compare-service/crypto-compare.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"crypto-compare.d.mts","sourceRoot":"","sources":["../../src/crypto-compare-service/crypto-compare.ts"],"names":[],"mappings":"AAkFA;;;;;;;GAOG;AACH,wBAAsB,iBAAiB,CACrC,QAAQ,EAAE,MAAM,EAChB,cAAc,EAAE,MAAM,EACtB,cAAc,CAAC,EAAE,OAAO,GACvB,OAAO,CAAC;IACT,cAAc,EAAE,MAAM,CAAC;IACvB,iBAAiB,EAAE,MAAM,CAAC;CAC3B,CAAC,CA8BD;AAED;;;;;;;GAOG;AACH,wBAAsB,sBAAsB,CAC1C,YAAY,EAAE,MAAM,EACpB,gBAAgB,EAAE,MAAM,EAAE,EAC1B,cAAc,EAAE,OAAO,GACtB,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,CAsBjD"}
|
|
@@ -55,8 +55,6 @@ function getMultiPricingURL(fsyms, tsyms, includeUSDRate = false) {
|
|
|
55
55
|
* @param json.Response - The response status.
|
|
56
56
|
* @param json.Message - The error message.
|
|
57
57
|
*/
|
|
58
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
59
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
60
58
|
function handleErrorResponse(json) {
|
|
61
59
|
if (json.Response === 'Error') {
|
|
62
60
|
throw new Error(json.Message);
|
|
@@ -80,14 +78,9 @@ export async function fetchExchangeRate(currency, nativeCurrency, includeUSDRate
|
|
|
80
78
|
const conversionRate = Number(json[currency.toUpperCase()]);
|
|
81
79
|
const usdConversionRate = Number(json.USD);
|
|
82
80
|
if (!Number.isFinite(conversionRate)) {
|
|
83
|
-
throw new Error(`Invalid response for ${currency.toUpperCase()}: ${
|
|
84
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
85
|
-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
86
|
-
json[currency.toUpperCase()]}`);
|
|
81
|
+
throw new Error(`Invalid response for ${currency.toUpperCase()}: ${json[currency.toUpperCase()]}`);
|
|
87
82
|
}
|
|
88
83
|
if (includeUSDRate && !Number.isFinite(usdConversionRate)) {
|
|
89
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
90
|
-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
91
84
|
throw new Error(`Invalid response for usdConversionRate: ${json.USD}`);
|
|
92
85
|
}
|
|
93
86
|
return {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"crypto-compare.mjs","sourceRoot":"","sources":["../../src/crypto-compare-service/crypto-compare.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,mCAAmC;AAEzD,OAAO,EAAE,aAAa,EAAE,0BAAsB;AAE9C;;;GAGG;AACH,MAAM,qBAAqB,GAAG,IAAI,GAAG,CAAC;IACpC,CAAC,KAAK,EAAE,QAAQ,CAAC;IACjB,CAAC,MAAM,EAAE,SAAS,CAAC;CACpB,CAAC,CAAC;AAEH,MAAM,qBAAqB,GAAG,mCAAmC,CAAC;AAElE;;;;;;;;;;GAUG;AACH,SAAS,aAAa,CACpB,eAAuB,EACvB,cAAsB,EACtB,cAAwB;IAExB,OAAO,CACL,GAAG,qBAAqB,mBAAmB;QAC3C,GAAG,cAAc,UAAU,eAAe,EAAE;QAC5C,GAAG,cAAc,IAAI,eAAe,CAAC,WAAW,EAAE,KAAK,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAC7E,CAAC;AACJ,CAAC;AAED;;;;;;;;;GASG;AACH,SAAS,kBAAkB,CACzB,KAAe,EACf,KAAe,EACf,cAAc,GAAG,KAAK;IAEtB,MAAM,YAAY,GAChB,cAAc,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,WAAW,EAAE,KAAK,KAAK,CAAC;QAC7D,CAAC,CAAC,CAAC,GAAG,KAAK,EAAE,KAAK,CAAC;QACnB,CAAC,CAAC,KAAK,CAAC;IAEZ,MAAM,MAAM,GAAG,IAAI,eAAe,EAAE,CAAC;IACrC,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;IACxC,MAAM,CAAC,MAAM,CAAC,OAAO,EA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,CAAC,YAAY,CAAC,WAAW,EAAE,CAAC,EAAE,MAAM,CAAC,YAAY,CAAC,WAAW,EAAE,CAAC;YAChE,GAAG,CAAC,cAAc,IAAI,EAAE,GAAG,EAAE,MAAM,CAAC,GAAG,EAAE,CAAC;SAC3C,CAAC;IACJ,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["import { handleFetch } from '@metamask/controller-utils';\n\nimport { getKeyByValue } from '../assetsUtil';\n\n/**\n * A map from native currency symbol to CryptoCompare identifier.\n * This is only needed when the values don't match.\n */\nconst nativeSymbolOverrides = new Map([\n ['MNT', 'MANTLE'],\n ['OMNI', 'OMNINET'],\n]);\n\nconst CRYPTO_COMPARE_DOMAIN = 'https://min-api.cryptocompare.com';\n\n/**\n * Get the CryptoCompare API URL for getting the conversion rate from the given native currency to\n * the given currency. Optionally, the conversion rate from the native currency to USD can also be\n * included in the response.\n *\n * @param currentCurrency - The currency to get a conversion rate for.\n * @param nativeCurrency - The native currency to convert from.\n * @param includeUSDRate - Whether or not the native currency to USD conversion rate should be\n * included in the response as well.\n * @returns The API URL for getting the conversion rate.\n */\nfunction getPricingURL(\n currentCurrency: string,\n nativeCurrency: string,\n includeUSDRate?: boolean,\n) {\n return (\n `${CRYPTO_COMPARE_DOMAIN}/data/price?fsym=` +\n `${nativeCurrency}&tsyms=${currentCurrency}` +\n `${includeUSDRate && currentCurrency.toUpperCase() !== 'USD' ? ',USD' : ''}`\n );\n}\n\n/**\n * Get the CryptoCompare API URL for getting the conversion rate from a given array of native currencies\n * to the given currencies. Optionally, the conversion rate from the native currency to USD can also be\n * included in the response.\n *\n * @param fsyms - The native currencies to get conversion rates for.\n * @param tsyms - The currencies to convert to.\n * @param includeUSDRate - Whether or not the native currency to USD conversion rate should be included.\n * @returns The API URL for getting the conversion rates.\n */\nfunction getMultiPricingURL(\n fsyms: string[],\n tsyms: string[],\n includeUSDRate = false,\n) {\n const updatedTsyms =\n includeUSDRate && !tsyms.some((t) => t.toUpperCase() === 'USD')\n ? [...tsyms, 'USD']\n : tsyms;\n\n const params = new URLSearchParams();\n params.append('fsyms', fsyms.join(','));\n params.append('tsyms', updatedTsyms.join(','));\n\n const url = new URL(`${CRYPTO_COMPARE_DOMAIN}/data/pricemulti`);\n url.search = params.toString();\n return url.toString();\n}\n\n/**\n * Handles an error response from the CryptoCompare API.\n * Expected error response format\n * { Response: \"Error\", Message: \"...\", HasWarning: false }\n *\n * @param json - The JSON response from the CryptoCompare API.\n * @param json.Response - The response status.\n * @param json.Message - The error message.\n */\nfunction handleErrorResponse(json: { Response?: string; Message?: string }) {\n if (json.Response === 'Error') {\n throw new Error(json.Message);\n }\n}\n\n/**\n * Fetches the exchange rate for a given currency.\n *\n * @param currency - ISO 4217 currency code.\n * @param nativeCurrency - Symbol for base asset.\n * @param includeUSDRate - Whether to add the USD rate to the fetch.\n * @returns Promise resolving to exchange rate for given currency.\n */\nexport async function fetchExchangeRate(\n currency: string,\n nativeCurrency: string,\n includeUSDRate?: boolean,\n): Promise<{\n conversionRate: number;\n usdConversionRate: number;\n}> {\n currency = currency.toUpperCase();\n nativeCurrency = nativeCurrency.toUpperCase();\n currency = nativeSymbolOverrides.get(currency) ?? currency;\n nativeCurrency = nativeSymbolOverrides.get(nativeCurrency) ?? nativeCurrency;\n\n const json = await handleFetch(\n getPricingURL(currency, nativeCurrency, includeUSDRate),\n );\n\n handleErrorResponse(json);\n const conversionRate = Number(json[currency.toUpperCase()]);\n\n const usdConversionRate = Number(json.USD);\n if (!Number.isFinite(conversionRate)) {\n throw new Error(\n `Invalid response for ${currency.toUpperCase()}: ${\n json[currency.toUpperCase()]\n }`,\n );\n }\n\n if (includeUSDRate && !Number.isFinite(usdConversionRate)) {\n throw new Error(`Invalid response for usdConversionRate: ${json.USD}`);\n }\n\n return {\n conversionRate,\n usdConversionRate,\n };\n}\n\n/**\n * Fetches the exchange rates for multiple currencies.\n *\n * @param fiatCurrency - The currency of the rates (ISO 4217).\n * @param cryptocurrencies - The cryptocurrencies to get conversion rates for. Min length: 1. Max length: 300.\n * @param includeUSDRate - Whether to add the USD rate to the fetch.\n * @returns Promise resolving to exchange rates for given currencies.\n */\nexport async function fetchMultiExchangeRate(\n fiatCurrency: string,\n cryptocurrencies: string[],\n includeUSDRate: boolean,\n): Promise<Record<string, Record<string, number>>> {\n const fsyms = cryptocurrencies.map(\n (nativeCurrency) =>\n nativeSymbolOverrides.get(nativeCurrency) ?? nativeCurrency,\n );\n const url = getMultiPricingURL(fsyms, [fiatCurrency], includeUSDRate);\n const response = await handleFetch(url);\n handleErrorResponse(response);\n\n const rates: Record<string, Record<string, number>> = {};\n for (const [cryptocurrency, values] of Object.entries(response) as [\n string,\n Record<string, number>,\n ][]) {\n const key = getKeyByValue(nativeSymbolOverrides, cryptocurrency);\n rates[key?.toLowerCase() ?? cryptocurrency.toLowerCase()] = {\n [fiatCurrency.toLowerCase()]: values[fiatCurrency.toUpperCase()],\n ...(includeUSDRate && { usd: values.USD }),\n };\n }\n\n return rates;\n}\n"]}
|
|
@@ -25,6 +25,8 @@ const assetsUtil_1 = require("../assetsUtil.cjs");
|
|
|
25
25
|
const constants_1 = require("../constants.cjs");
|
|
26
26
|
// Maximum number of account addresses that can be sent to the accounts API in a single request
|
|
27
27
|
const ACCOUNTS_API_BATCH_SIZE = 50;
|
|
28
|
+
// Timeout for accounts API requests (30 seconds)
|
|
29
|
+
const ACCOUNTS_API_TIMEOUT_MS = 30000;
|
|
28
30
|
const checksum = (addr) => (0, controller_utils_1.toChecksumHexAddress)(addr);
|
|
29
31
|
const toCaipAccount = (chainId, account) => (0, assetsUtil_1.accountAddressToCaipReference)(chainId, account);
|
|
30
32
|
class AccountsApiBalanceFetcher {
|
|
@@ -49,20 +51,20 @@ class AccountsApiBalanceFetcher {
|
|
|
49
51
|
}
|
|
50
52
|
}
|
|
51
53
|
if (!caipAddrs.length) {
|
|
52
|
-
return [];
|
|
54
|
+
return { balances: [] };
|
|
53
55
|
}
|
|
54
|
-
//
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
// Mark that we had an API error so we don't add fake zero balances
|
|
62
|
-
apiError = true;
|
|
63
|
-
console.error('Failed to fetch balances from API:', error);
|
|
64
|
-
balances = undefined;
|
|
56
|
+
// Let errors propagate to TokenBalancesController for RPC fallback
|
|
57
|
+
// Use timeout to prevent hanging API calls (30 seconds)
|
|
58
|
+
const apiResponse = await (0, controller_utils_1.safelyExecuteWithTimeout)(() => __classPrivateFieldGet(this, _AccountsApiBalanceFetcher_instances, "m", _AccountsApiBalanceFetcher_fetchBalances).call(this, caipAddrs), false, // don't log error here, let it propagate
|
|
59
|
+
ACCOUNTS_API_TIMEOUT_MS);
|
|
60
|
+
// If API call timed out or failed, throw error to trigger RPC fallback
|
|
61
|
+
if (!apiResponse) {
|
|
62
|
+
throw new Error('Accounts API request timed out or failed');
|
|
65
63
|
}
|
|
64
|
+
// Extract unprocessed networks and convert to hex chain IDs
|
|
65
|
+
const unprocessedChainIds = apiResponse.unprocessedNetworks
|
|
66
|
+
? apiResponse.unprocessedNetworks.map((chainId) => (0, controller_utils_1.toHex)(chainId))
|
|
67
|
+
: undefined;
|
|
66
68
|
const stakedBalances = await __classPrivateFieldGet(this, _AccountsApiBalanceFetcher_instances, "m", _AccountsApiBalanceFetcher_fetchStakedBalances).call(this, caipAddrs);
|
|
67
69
|
const results = [];
|
|
68
70
|
// Collect all unique addresses and chains from the CAIP addresses
|
|
@@ -80,8 +82,8 @@ class AccountsApiBalanceFetcher {
|
|
|
80
82
|
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
|
|
81
83
|
const nativeBalancesFromAPI = new Map(); // key: `${address}-${chainId}`
|
|
82
84
|
// Process regular API balances
|
|
83
|
-
if (balances) {
|
|
84
|
-
const apiBalances = balances.flatMap((b) => {
|
|
85
|
+
if (apiResponse.balances) {
|
|
86
|
+
const apiBalances = apiResponse.balances.flatMap((b) => {
|
|
85
87
|
const addressPart = b.accountAddress?.split(':')[2];
|
|
86
88
|
if (!addressPart) {
|
|
87
89
|
return [];
|
|
@@ -126,46 +128,29 @@ class AccountsApiBalanceFetcher {
|
|
|
126
128
|
});
|
|
127
129
|
results.push(...apiBalances);
|
|
128
130
|
}
|
|
129
|
-
//
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
if (!existingBalance) {
|
|
137
|
-
// Add zero native balance entry if API succeeded but didn't return one
|
|
138
|
-
results.push({
|
|
139
|
-
success: true,
|
|
140
|
-
value: new bn_js_1.default('0'),
|
|
141
|
-
account: address,
|
|
142
|
-
token: ZERO_ADDRESS,
|
|
143
|
-
chainId,
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
});
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
else {
|
|
150
|
-
// If API failed, add error entries for all requested addresses/chains
|
|
151
|
-
addressChainMap.forEach((chains, address) => {
|
|
152
|
-
chains.forEach((chainId) => {
|
|
131
|
+
// Add zero native balance entries for addresses that API didn't return
|
|
132
|
+
addressChainMap.forEach((chains, address) => {
|
|
133
|
+
chains.forEach((chainId) => {
|
|
134
|
+
const key = `${address}-${chainId}`;
|
|
135
|
+
const existingBalance = nativeBalancesFromAPI.get(key);
|
|
136
|
+
if (!existingBalance) {
|
|
137
|
+
// Add zero native balance entry if API succeeded but didn't return one
|
|
153
138
|
results.push({
|
|
154
|
-
success:
|
|
139
|
+
success: true,
|
|
140
|
+
value: new bn_js_1.default('0'),
|
|
155
141
|
account: address,
|
|
156
142
|
token: ZERO_ADDRESS,
|
|
157
143
|
chainId,
|
|
158
144
|
});
|
|
159
|
-
}
|
|
145
|
+
}
|
|
160
146
|
});
|
|
161
|
-
}
|
|
147
|
+
});
|
|
162
148
|
// Add staked balances
|
|
163
149
|
results.push(...stakedBalances);
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
}
|
|
168
|
-
return results;
|
|
150
|
+
return {
|
|
151
|
+
balances: results,
|
|
152
|
+
unprocessedChainIds,
|
|
153
|
+
};
|
|
169
154
|
}
|
|
170
155
|
}
|
|
171
156
|
exports.AccountsApiBalanceFetcher = AccountsApiBalanceFetcher;
|
|
@@ -274,18 +259,25 @@ _AccountsApiBalanceFetcher_platform = new WeakMap(), _AccountsApiBalanceFetcher_
|
|
|
274
259
|
}, _AccountsApiBalanceFetcher_fetchBalances = async function _AccountsApiBalanceFetcher_fetchBalances(addrs) {
|
|
275
260
|
// If we have fewer than or equal to the batch size, make a single request
|
|
276
261
|
if (addrs.length <= ACCOUNTS_API_BATCH_SIZE) {
|
|
277
|
-
|
|
278
|
-
return balances;
|
|
262
|
+
return await (0, multi_chain_accounts_1.fetchMultiChainBalancesV4)({ accountAddresses: addrs }, __classPrivateFieldGet(this, _AccountsApiBalanceFetcher_platform, "f"));
|
|
279
263
|
}
|
|
264
|
+
const allUnprocessedNetworks = new Set();
|
|
280
265
|
const allBalances = await (0, assetsUtil_1.reduceInBatchesSerially)({
|
|
281
266
|
values: addrs,
|
|
282
267
|
batchSize: ACCOUNTS_API_BATCH_SIZE,
|
|
283
268
|
eachBatch: async (workingResult, batch) => {
|
|
284
|
-
const
|
|
285
|
-
|
|
269
|
+
const response = await (0, multi_chain_accounts_1.fetchMultiChainBalancesV4)({ accountAddresses: batch }, __classPrivateFieldGet(this, _AccountsApiBalanceFetcher_platform, "f"));
|
|
270
|
+
// Collect unprocessed networks from each batch
|
|
271
|
+
if (response.unprocessedNetworks) {
|
|
272
|
+
response.unprocessedNetworks.forEach((network) => allUnprocessedNetworks.add(network));
|
|
273
|
+
}
|
|
274
|
+
return [...(workingResult || []), ...response.balances];
|
|
286
275
|
},
|
|
287
276
|
initialResult: [],
|
|
288
277
|
});
|
|
289
|
-
return
|
|
278
|
+
return {
|
|
279
|
+
balances: allBalances,
|
|
280
|
+
unprocessedNetworks: Array.from(allUnprocessedNetworks),
|
|
281
|
+
};
|
|
290
282
|
};
|
|
291
283
|
//# sourceMappingURL=api-balance-fetcher.cjs.map
|