@metamask-previews/assets-controllers 74.0.0-preview-2c45ce0 → 74.0.0-preview-e53ed971

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.
@@ -19,7 +19,7 @@ const selectAccountsToGroupIdMap = createAssetListSelector([(state) => state.acc
19
19
  // TODO: We would not need internalAccounts if evmTokens state had the accountId
20
20
  internalAccount.type.startsWith('eip155')
21
21
  ? internalAccount.address
22
- : accountId] = { accountGroupId, type: internalAccount.type, accountId };
22
+ : accountId] = accountGroupId;
23
23
  }
24
24
  }
25
25
  }
@@ -38,11 +38,7 @@ const selectAllEvmAccountNativeBalances = createAssetListSelector([
38
38
  const groupAssets = {};
39
39
  for (const [chainId, chainAccounts] of Object.entries(accountsByChainId)) {
40
40
  for (const [accountAddress, accountBalance] of Object.entries(chainAccounts)) {
41
- const account = accountsMap[accountAddress.toLowerCase()];
42
- if (!account) {
43
- continue;
44
- }
45
- const { accountGroupId, type, accountId } = account;
41
+ const accountGroupId = accountsMap[accountAddress];
46
42
  groupAssets[accountGroupId] ?? (groupAssets[accountGroupId] = {});
47
43
  (_a = groupAssets[accountGroupId])[chainId] ?? (_a[chainId] = []);
48
44
  const groupChainAssets = groupAssets[accountGroupId][chainId];
@@ -58,14 +54,13 @@ const selectAllEvmAccountNativeBalances = createAssetListSelector([
58
54
  };
59
55
  const fiatData = getFiatBalanceForEvmToken(rawBalance, nativeToken.decimals, marketData, currencyRates, chainId, nativeToken.address);
60
56
  groupChainAssets.push({
61
- type: type,
57
+ type: 'evm',
62
58
  assetId: nativeToken.address,
63
59
  isNative: true,
64
60
  address: nativeToken.address,
65
61
  image: nativeToken.image,
66
62
  name: nativeToken.name,
67
63
  symbol: nativeToken.symbol,
68
- accountId,
69
64
  decimals: nativeToken.decimals,
70
65
  balance: stringifyBalanceWithDecimals(hexToBigInt(rawBalance), nativeToken.decimals),
71
66
  fiat: fiatData
@@ -96,11 +91,7 @@ const selectAllEvmAssets = createAssetListSelector([
96
91
  for (const [accountAddress, addressTokens] of Object.entries(chainTokens)) {
97
92
  for (const token of addressTokens) {
98
93
  const tokenAddress = token.address;
99
- const account = accountsMap[accountAddress];
100
- if (!account) {
101
- continue;
102
- }
103
- const { accountGroupId, type, accountId } = account;
94
+ const accountGroupId = accountsMap[accountAddress];
104
95
  if (ignoredEvmTokens[chainId]?.[accountAddress]?.includes(tokenAddress)) {
105
96
  continue;
106
97
  }
@@ -113,14 +104,13 @@ const selectAllEvmAssets = createAssetListSelector([
113
104
  const groupChainAssets = groupAssets[accountGroupId][chainId];
114
105
  const fiatData = getFiatBalanceForEvmToken(rawBalance, token.decimals, marketData, currencyRates, chainId, tokenAddress);
115
106
  groupChainAssets.push({
116
- type: type,
107
+ type: 'evm',
117
108
  assetId: tokenAddress,
118
109
  isNative: false,
119
110
  address: tokenAddress,
120
111
  image: token.image ?? '',
121
112
  name: token.name ?? token.symbol,
122
113
  symbol: token.symbol,
123
- accountId,
124
114
  decimals: token.decimals,
125
115
  balance: stringifyBalanceWithDecimals(hexToBigInt(rawBalance), token.decimals),
126
116
  fiat: fiatData
@@ -159,12 +149,11 @@ const selectAllMultichainAssets = createAssetListSelector([
159
149
  }
160
150
  const { chainId } = caipAsset;
161
151
  const asset = `${caipAsset.assetNamespace}:${caipAsset.assetReference}`;
162
- const account = accountsMap[accountId];
152
+ const accountGroupId = accountsMap[accountId];
163
153
  const assetMetadata = multichainAssetsMetadata[assetId];
164
- if (!account || !assetMetadata) {
154
+ if (!accountGroupId || !assetMetadata) {
165
155
  continue;
166
156
  }
167
- const { accountGroupId, type } = account;
168
157
  groupAssets[accountGroupId] ?? (groupAssets[accountGroupId] = {});
169
158
  (_a = groupAssets[accountGroupId])[chainId] ?? (_a[chainId] = []);
170
159
  const groupChainAssets = groupAssets[accountGroupId][chainId];
@@ -175,13 +164,12 @@ const selectAllMultichainAssets = createAssetListSelector([
175
164
  const fiatData = getFiatBalanceForMultichainAsset(balance, multichainConversionRates, assetId);
176
165
  // TODO: We shouldn't have to rely on fallbacks for name and symbol, they should not be optional
177
166
  groupChainAssets.push({
178
- type: type,
167
+ type: 'multichain',
179
168
  assetId,
180
169
  isNative: MULTICHAIN_NATIVE_ASSET_IDS.includes(assetId),
181
170
  image: assetMetadata.iconUrl,
182
171
  name: assetMetadata.name ?? assetMetadata.symbol ?? asset,
183
172
  symbol: assetMetadata.symbol ?? asset,
184
- accountId,
185
173
  decimals: assetMetadata.units.find((unit) => unit.name === assetMetadata.name &&
186
174
  unit.symbol === assetMetadata.symbol)?.decimals ?? 0,
187
175
  balance: balance.amount,
@@ -227,10 +215,7 @@ function mergeAssets(existingAssets, newAssets) {
227
215
  for (const [accountGroupId, accountAssets] of Object.entries(newAssets)) {
228
216
  const existingAccountGroupAssets = existingAssets[accountGroupId];
229
217
  if (!existingAccountGroupAssets) {
230
- existingAssets[accountGroupId] = {};
231
- for (const [network, chainAssets] of Object.entries(accountAssets)) {
232
- existingAssets[accountGroupId][network] = [...chainAssets];
233
- }
218
+ existingAssets[accountGroupId] = accountAssets;
234
219
  }
235
220
  else {
236
221
  for (const [network, chainAssets] of Object.entries(accountAssets)) {
@@ -1 +1 @@
1
- {"version":3,"file":"token-selectors.mjs","sourceRoot":"","sources":["../../src/selectors/token-selectors.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,mBAAmB,EAAE,mCAAmC;AAGjE,OAAO,EAAE,WAAW,EAAE,kBAAkB,EAAY,wBAAwB;AAC5E,OAAO,EAAE,cAAc,EAAE,iBAAiB;AAE1C,OAAO,EAAE,4BAA4B,EAAE,gCAA4B;AAKnE,OAAO,EAAE,qBAAqB,EAAE,8CAA0C;AAa1E,2GAA2G;AAC3G,MAAM,2BAA2B,GAAG;IAClC,kDAAkD;IAClD,oDAAoD;CACrD,CAAC;AAiEF,MAAM,uBAAuB,GAAG,cAAc,CAAC,SAAS,EAAkB,CAAC;AAE3E,MAAM,0BAA0B,GAAG,uBAAuB,CACxD,CAAC,CAAC,KAAK,EAAE,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;IAE9B,OAAO;QACL,OAAO,EAAE,WAAW;QACpB,cAAc,EAAE,YAAY,CAAC,cAAc;KAC5C,CAAC;AACJ,CAAC;AAED;;;;;;;GAOG;AACH,SAAS,gCAAgC,CACvC,OAAyC,EACzC,yBAAkF,EAClF,OAAkD;IAElD,MAAM,eAAe,GAAG,yBAAyB,CAAC,OAAO,CAAC,CAAC;IAE3D,IAAI,CAAC,eAAe,EAAE,IAAI,EAAE;QAC1B,OAAO,SAAS,CAAC;KAClB;IAED,OAAO;QACL,OAAO,EAAE,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,eAAe,CAAC,IAAI,CAAC;QAC9D,cAAc,EAAE,MAAM,CAAC,eAAe,CAAC,IAAI,CAAC;KAC7C,CAAC;AACJ,CAAC","sourcesContent":["import type { AccountGroupId } from '@metamask/account-api';\nimport type { AccountTreeControllerState } from '@metamask/account-tree-controller';\nimport type { AccountsControllerState } from '@metamask/accounts-controller';\nimport { convertHexToDecimal } from '@metamask/controller-utils';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport type { NetworkState } from '@metamask/network-controller';\nimport { hexToBigInt, parseCaipAssetType, type Hex } from '@metamask/utils';\nimport { createSelector } from 'reselect';\n\nimport { stringifyBalanceWithDecimals } from './stringify-balance';\nimport type { CurrencyRateState } from '../CurrencyRateController';\nimport type { MultichainAssetsControllerState } from '../MultichainAssetsController';\nimport type { MultichainAssetsRatesControllerState } from '../MultichainAssetsRatesController';\nimport type { MultichainBalancesControllerState } from '../MultichainBalancesController';\nimport { getNativeTokenAddress } from '../token-prices-service/codefi-v2';\nimport type { TokenBalancesControllerState } from '../TokenBalancesController';\nimport type { Token, TokenRatesControllerState } from '../TokenRatesController';\nimport type { TokensControllerState } from '../TokensController';\n\ntype AssetsByAccountGroup = {\n [accountGroupId: AccountGroupId]: AccountGroupAssets;\n};\n\nexport type AccountGroupAssets = {\n [network: string]: Asset[];\n};\n\n// If this gets out of hand with other chains, we should probably have a permanent object that defines them\nconst MULTICHAIN_NATIVE_ASSET_IDS = [\n `bip122:000000000019d6689c085ae165831e93/slip44:0`,\n `solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/slip44:501`,\n];\n\ntype EvmAccountType = Extract<InternalAccount['type'], `eip155:${string}`>;\ntype MultichainAccountType = Exclude<\n InternalAccount['type'],\n `eip155:${string}`\n>;\n\nexport type Asset = (\n | {\n type: EvmAccountType;\n assetId: Hex; // This is also the address for EVM tokens\n address: Hex;\n chainId: Hex;\n }\n | {\n type: MultichainAccountType;\n assetId: `${string}:${string}/${string}:${string}`;\n chainId: `${string}:${string}`;\n }\n) & {\n accountId: string;\n image: string;\n name: string;\n symbol: string;\n decimals: number;\n isNative: boolean;\n balance: string;\n fiat:\n | {\n balance: number;\n currency: string;\n conversionRate: number;\n }\n | undefined;\n};\n\nexport type AssetListState = {\n accountTree: AccountTreeControllerState['accountTree'];\n internalAccounts: AccountsControllerState['internalAccounts'];\n allTokens: TokensControllerState['allTokens'];\n allIgnoredTokens: TokensControllerState['allIgnoredTokens'];\n tokenBalances: TokenBalancesControllerState['tokenBalances'];\n marketData: TokenRatesControllerState['marketData'];\n currencyRates: CurrencyRateState['currencyRates'];\n accountsAssets: MultichainAssetsControllerState['accountsAssets'];\n assetsMetadata: MultichainAssetsControllerState['assetsMetadata'];\n balances: MultichainBalancesControllerState['balances'];\n conversionRates: MultichainAssetsRatesControllerState['conversionRates'];\n currentCurrency: CurrencyRateState['currentCurrency'];\n networkConfigurationsByChainId: NetworkState['networkConfigurationsByChainId'];\n // This is the state from AccountTrackerController. The state is different on mobile and extension\n // accountsByChainId with a balance is the only field that both clients have in common\n // This field could be removed once TokenBalancesController returns native balances\n accountsByChainId: Record<\n Hex,\n Record<\n Hex,\n {\n balance: Hex | null;\n }\n >\n >;\n};\n\nconst createAssetListSelector = createSelector.withTypes<AssetListState>();\n\nconst selectAccountsToGroupIdMap = createAssetListSelector(\n [(state) => state.accountTree, (state) => state.internalAccounts],\n (accountTree, internalAccounts) => {\n const accountsMap: Record<\n string,\n {\n accountGroupId: AccountGroupId;\n type: InternalAccount['type'];\n accountId: string;\n }\n > = {};\n for (const { groups } of Object.values(accountTree.wallets)) {\n for (const { id: accountGroupId, accounts } of Object.values(groups)) {\n for (const accountId of accounts) {\n const internalAccount = internalAccounts.accounts[accountId];\n\n accountsMap[\n // TODO: We would not need internalAccounts if evmTokens state had the accountId\n internalAccount.type.startsWith('eip155')\n ? internalAccount.address\n : accountId\n ] = { accountGroupId, type: internalAccount.type, accountId };\n }\n }\n }\n\n return accountsMap;\n },\n);\n\n// TODO: This selector will not be needed once the native balances are part of the evm tokens state\nconst selectAllEvmAccountNativeBalances = createAssetListSelector(\n [\n selectAccountsToGroupIdMap,\n (state) => state.accountsByChainId,\n (state) => state.marketData,\n (state) => state.currencyRates,\n (state) => state.currentCurrency,\n (state) => state.networkConfigurationsByChainId,\n ],\n (\n accountsMap,\n accountsByChainId,\n marketData,\n currencyRates,\n currentCurrency,\n networkConfigurationsByChainId,\n ) => {\n const groupAssets: AssetsByAccountGroup = {};\n\n for (const [chainId, chainAccounts] of Object.entries(\n accountsByChainId,\n ) as [Hex, Record<Hex, { balance: Hex | null }>][]) {\n for (const [accountAddress, accountBalance] of Object.entries(\n chainAccounts,\n )) {\n const account = accountsMap[accountAddress.toLowerCase()];\n if (!account) {\n continue;\n }\n\n const { accountGroupId, type, accountId } = account;\n\n groupAssets[accountGroupId] ??= {};\n groupAssets[accountGroupId][chainId] ??= [];\n const groupChainAssets = groupAssets[accountGroupId][chainId];\n\n const rawBalance = accountBalance.balance || '0x0';\n\n const nativeCurrency =\n networkConfigurationsByChainId[chainId]?.nativeCurrency || 'NATIVE';\n\n const nativeToken = {\n address: getNativeTokenAddress(chainId),\n decimals: 18,\n name: nativeCurrency === 'ETH' ? 'Ethereum' : nativeCurrency,\n symbol: nativeCurrency,\n // This field need to be filled at client level for now\n image: '',\n };\n\n const fiatData = getFiatBalanceForEvmToken(\n rawBalance,\n nativeToken.decimals,\n marketData,\n currencyRates,\n chainId,\n nativeToken.address,\n );\n\n groupChainAssets.push({\n type: type as EvmAccountType,\n assetId: nativeToken.address,\n isNative: true,\n address: nativeToken.address,\n image: nativeToken.image,\n name: nativeToken.name,\n symbol: nativeToken.symbol,\n accountId,\n decimals: nativeToken.decimals,\n balance: stringifyBalanceWithDecimals(\n hexToBigInt(rawBalance),\n nativeToken.decimals,\n ),\n fiat: fiatData\n ? {\n balance: fiatData.balance,\n currency: currentCurrency,\n conversionRate: fiatData.conversionRate,\n }\n : undefined,\n chainId,\n });\n }\n }\n\n return groupAssets;\n },\n);\n\nconst selectAllEvmAssets = createAssetListSelector(\n [\n selectAccountsToGroupIdMap,\n (state) => state.allTokens,\n (state) => state.allIgnoredTokens,\n (state) => state.tokenBalances,\n (state) => state.marketData,\n (state) => state.currencyRates,\n (state) => state.currentCurrency,\n ],\n (\n accountsMap,\n evmTokens,\n ignoredEvmTokens,\n tokenBalances,\n marketData,\n currencyRates,\n currentCurrency,\n ) => {\n const groupAssets: AssetsByAccountGroup = {};\n\n for (const [chainId, chainTokens] of Object.entries(evmTokens) as [\n Hex,\n { [key: string]: Token[] },\n ][]) {\n for (const [accountAddress, addressTokens] of Object.entries(\n chainTokens,\n ) as [Hex, Token[]][]) {\n for (const token of addressTokens) {\n const tokenAddress = token.address as Hex;\n const account = accountsMap[accountAddress];\n if (!account) {\n continue;\n }\n\n const { accountGroupId, type, accountId } = account;\n\n if (\n ignoredEvmTokens[chainId]?.[accountAddress]?.includes(tokenAddress)\n ) {\n continue;\n }\n\n const rawBalance =\n tokenBalances[accountAddress]?.[chainId]?.[tokenAddress];\n\n if (!rawBalance) {\n continue;\n }\n\n groupAssets[accountGroupId] ??= {};\n groupAssets[accountGroupId][chainId] ??= [];\n const groupChainAssets = groupAssets[accountGroupId][chainId];\n\n const fiatData = getFiatBalanceForEvmToken(\n rawBalance,\n token.decimals,\n marketData,\n currencyRates,\n chainId,\n tokenAddress,\n );\n\n groupChainAssets.push({\n type: type as EvmAccountType,\n assetId: tokenAddress,\n isNative: false,\n address: tokenAddress,\n image: token.image ?? '',\n name: token.name ?? token.symbol,\n symbol: token.symbol,\n accountId,\n decimals: token.decimals,\n balance: stringifyBalanceWithDecimals(\n hexToBigInt(rawBalance),\n token.decimals,\n ),\n fiat: fiatData\n ? {\n balance: fiatData.balance,\n currency: currentCurrency,\n conversionRate: fiatData.conversionRate,\n }\n : undefined,\n chainId,\n });\n }\n }\n }\n\n return groupAssets;\n },\n);\n\nconst selectAllMultichainAssets = createAssetListSelector(\n [\n selectAccountsToGroupIdMap,\n (state) => state.accountsAssets,\n (state) => state.assetsMetadata,\n (state) => state.balances,\n (state) => state.conversionRates,\n (state) => state.currentCurrency,\n ],\n (\n accountsMap,\n multichainTokens,\n multichainAssetsMetadata,\n multichainBalances,\n multichainConversionRates,\n currentCurrency,\n ) => {\n const groupAssets: AssetsByAccountGroup = {};\n\n for (const [accountId, accountAssets] of Object.entries(multichainTokens)) {\n for (const assetId of accountAssets) {\n let caipAsset: ReturnType<typeof parseCaipAssetType>;\n try {\n caipAsset = parseCaipAssetType(assetId);\n } catch {\n // TODO: We should log this error when we have the ability to inject a logger from the client\n continue;\n }\n\n const { chainId } = caipAsset;\n const asset = `${caipAsset.assetNamespace}:${caipAsset.assetReference}`;\n\n const account = accountsMap[accountId];\n const assetMetadata = multichainAssetsMetadata[assetId];\n if (!account || !assetMetadata) {\n continue;\n }\n\n const { accountGroupId, type } = account;\n\n groupAssets[accountGroupId] ??= {};\n groupAssets[accountGroupId][chainId] ??= [];\n const groupChainAssets = groupAssets[accountGroupId][chainId];\n\n const balance:\n | {\n amount: string;\n unit: string;\n }\n | undefined = multichainBalances[accountId]?.[assetId];\n\n if (!balance) {\n continue;\n }\n\n const fiatData = getFiatBalanceForMultichainAsset(\n balance,\n multichainConversionRates,\n assetId,\n );\n\n // TODO: We shouldn't have to rely on fallbacks for name and symbol, they should not be optional\n groupChainAssets.push({\n type: type as MultichainAccountType,\n assetId,\n isNative: MULTICHAIN_NATIVE_ASSET_IDS.includes(assetId),\n image: assetMetadata.iconUrl,\n name: assetMetadata.name ?? assetMetadata.symbol ?? asset,\n symbol: assetMetadata.symbol ?? asset,\n accountId,\n decimals:\n assetMetadata.units.find(\n (unit) =>\n unit.name === assetMetadata.name &&\n unit.symbol === assetMetadata.symbol,\n )?.decimals ?? 0,\n balance: balance.amount,\n fiat: fiatData\n ? {\n balance: fiatData.balance,\n currency: currentCurrency,\n conversionRate: fiatData.conversionRate,\n }\n : undefined,\n chainId,\n });\n }\n }\n\n return groupAssets;\n },\n);\n\nconst selectAllAssets = createAssetListSelector(\n [\n selectAllEvmAssets,\n selectAllMultichainAssets,\n selectAllEvmAccountNativeBalances,\n ],\n (evmAssets, multichainAssets, evmAccountNativeBalances) => {\n const groupAssets: AssetsByAccountGroup = {};\n\n mergeAssets(groupAssets, evmAssets);\n\n mergeAssets(groupAssets, multichainAssets);\n\n mergeAssets(groupAssets, evmAccountNativeBalances);\n\n return groupAssets;\n },\n);\n\nexport const selectAssetsBySelectedAccountGroup = createAssetListSelector(\n [selectAllAssets, (state) => state.accountTree],\n (groupAssets, accountTree) => {\n const { selectedAccountGroup } = accountTree;\n if (!selectedAccountGroup) {\n return {};\n }\n return groupAssets[selectedAccountGroup] || {};\n },\n);\n\n// TODO: Once native assets are part of the evm tokens state, this function can be simplified as chains will always be unique\n/**\n * Merges the new assets into the existing assets\n *\n * @param existingAssets - The existing assets\n * @param newAssets - The new assets\n */\nfunction mergeAssets(\n existingAssets: AssetsByAccountGroup,\n newAssets: AssetsByAccountGroup,\n) {\n for (const [accountGroupId, accountAssets] of Object.entries(newAssets) as [\n AccountGroupId,\n AccountGroupAssets,\n ][]) {\n const existingAccountGroupAssets = existingAssets[accountGroupId];\n\n if (!existingAccountGroupAssets) {\n existingAssets[accountGroupId] = {};\n for (const [network, chainAssets] of Object.entries(accountAssets)) {\n existingAssets[accountGroupId][network] = [...chainAssets];\n }\n } else {\n for (const [network, chainAssets] of Object.entries(accountAssets)) {\n existingAccountGroupAssets[network] ??= [];\n existingAccountGroupAssets[network].push(...chainAssets);\n }\n }\n }\n}\n\n/**\n * @param rawBalance - The balance of the token\n * @param decimals - The decimals of the token\n * @param marketData - The market data for the token\n * @param currencyRates - The currency rates for the token\n * @param chainId - The chain id of the token\n * @param tokenAddress - The address of the token\n * @returns The price and currency of the token in the current currency. Returns undefined if the asset is not found in the market data or currency rates.\n */\nfunction getFiatBalanceForEvmToken(\n rawBalance: Hex,\n decimals: number,\n marketData: TokenRatesControllerState['marketData'],\n currencyRates: CurrencyRateState['currencyRates'],\n chainId: Hex,\n tokenAddress: Hex,\n) {\n const tokenMarketData = marketData[chainId]?.[tokenAddress];\n\n if (!tokenMarketData) {\n return undefined;\n }\n\n const currencyRate = currencyRates[tokenMarketData.currency];\n\n if (!currencyRate?.conversionRate) {\n return undefined;\n }\n\n const fiatBalance =\n (convertHexToDecimal(rawBalance) / 10 ** decimals) *\n tokenMarketData.price *\n currencyRate.conversionRate;\n\n return {\n balance: fiatBalance,\n conversionRate: currencyRate.conversionRate,\n };\n}\n\n/**\n * @param balance - The balance of the asset, in the format { amount: string; unit: string }\n * @param balance.amount - The amount of the balance\n * @param balance.unit - The unit of the balance\n * @param multichainConversionRates - The conversion rates for the multichain asset\n * @param assetId - The asset id of the asset\n * @returns The price and currency of the token in the current currency. Returns undefined if the asset is not found in the conversion rates.\n */\nfunction getFiatBalanceForMultichainAsset(\n balance: { amount: string; unit: string },\n multichainConversionRates: MultichainAssetsRatesControllerState['conversionRates'],\n assetId: `${string}:${string}/${string}:${string}`,\n) {\n const assetMarketData = multichainConversionRates[assetId];\n\n if (!assetMarketData?.rate) {\n return undefined;\n }\n\n return {\n balance: Number(balance.amount) * Number(assetMarketData.rate),\n conversionRate: Number(assetMarketData.rate),\n };\n}\n"]}
1
+ {"version":3,"file":"token-selectors.mjs","sourceRoot":"","sources":["../../src/selectors/token-selectors.ts"],"names":[],"mappings":"AAGA,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,GAAG,yBAAyB,CAAC,OAAO,CAAC,CAAC;IAE3D,IAAI,CAAC,eAAe,EAAE,IAAI,EAAE;QAC1B,OAAO,SAAS,CAAC;KAClB;IAED,OAAO;QACL,OAAO,EAAE,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,eAAe,CAAC,IAAI,CAAC;QAC9D,cAAc,EAAE,MAAM,CAAC,eAAe,CAAC,IAAI,CAAC;KAC7C,CAAC;AACJ,CAAC","sourcesContent":["import type { AccountGroupId } from '@metamask/account-api';\nimport type { AccountTreeControllerState } from '@metamask/account-tree-controller';\nimport type { AccountsControllerState } from '@metamask/accounts-controller';\nimport { convertHexToDecimal } from '@metamask/controller-utils';\nimport type { NetworkState } from '@metamask/network-controller';\nimport { hexToBigInt, parseCaipAssetType, type Hex } from '@metamask/utils';\nimport { createSelector } from 'reselect';\n\nimport { stringifyBalanceWithDecimals } from './stringify-balance';\nimport type { CurrencyRateState } from '../CurrencyRateController';\nimport type { MultichainAssetsControllerState } from '../MultichainAssetsController';\nimport type { MultichainAssetsRatesControllerState } from '../MultichainAssetsRatesController';\nimport type { MultichainBalancesControllerState } from '../MultichainBalancesController';\nimport { getNativeTokenAddress } from '../token-prices-service/codefi-v2';\nimport type { TokenBalancesControllerState } from '../TokenBalancesController';\nimport type { Token, TokenRatesControllerState } from '../TokenRatesController';\nimport type { TokensControllerState } from '../TokensController';\n\ntype AssetsByAccountGroup = {\n [accountGroupId: AccountGroupId]: AccountGroupAssets;\n};\n\nexport type AccountGroupAssets = {\n [network: string]: Asset[];\n};\n\n// If this gets out of hand with other chains, we should probably have a permanent object that defines them\nconst MULTICHAIN_NATIVE_ASSET_IDS = [\n `bip122:000000000019d6689c085ae165831e93/slip44:0`,\n `solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/slip44:501`,\n];\n\nexport type Asset = (\n | {\n type: 'evm';\n assetId: Hex; // This is also the address for EVM tokens\n address: Hex;\n chainId: Hex;\n }\n | {\n type: 'multichain';\n assetId: `${string}:${string}/${string}:${string}`;\n chainId: `${string}:${string}`;\n }\n) & {\n image: string;\n name: string;\n symbol: string;\n decimals: number;\n isNative: boolean;\n balance: string;\n fiat:\n | {\n balance: number;\n currency: string;\n conversionRate: number;\n }\n | undefined;\n};\n\nexport type AssetListState = {\n accountTree: AccountTreeControllerState['accountTree'];\n internalAccounts: AccountsControllerState['internalAccounts'];\n allTokens: TokensControllerState['allTokens'];\n allIgnoredTokens: TokensControllerState['allIgnoredTokens'];\n tokenBalances: TokenBalancesControllerState['tokenBalances'];\n marketData: TokenRatesControllerState['marketData'];\n currencyRates: CurrencyRateState['currencyRates'];\n accountsAssets: MultichainAssetsControllerState['accountsAssets'];\n assetsMetadata: MultichainAssetsControllerState['assetsMetadata'];\n balances: MultichainBalancesControllerState['balances'];\n conversionRates: MultichainAssetsRatesControllerState['conversionRates'];\n currentCurrency: CurrencyRateState['currentCurrency'];\n networkConfigurationsByChainId: NetworkState['networkConfigurationsByChainId'];\n // This is the state from AccountTrackerController. The state is different on mobile and extension\n // accountsByChainId with a balance is the only field that both clients have in common\n // This field could be removed once TokenBalancesController returns native balances\n accountsByChainId: Record<\n Hex,\n Record<\n Hex,\n {\n balance: Hex | null;\n }\n >\n >;\n};\n\nconst createAssetListSelector = createSelector.withTypes<AssetListState>();\n\nconst selectAccountsToGroupIdMap = createAssetListSelector(\n [(state) => state.accountTree, (state) => state.internalAccounts],\n (accountTree, internalAccounts) => {\n const accountsMap: Record<string, AccountGroupId> = {};\n for (const { groups } of Object.values(accountTree.wallets)) {\n for (const { id: accountGroupId, accounts } of Object.values(groups)) {\n for (const accountId of accounts) {\n const internalAccount = internalAccounts.accounts[accountId];\n\n accountsMap[\n // TODO: We would not need internalAccounts if evmTokens state had the accountId\n internalAccount.type.startsWith('eip155')\n ? internalAccount.address\n : accountId\n ] = accountGroupId;\n }\n }\n }\n\n return accountsMap;\n },\n);\n\n// TODO: This selector will not be needed once the native balances are part of the evm tokens state\nconst selectAllEvmAccountNativeBalances = createAssetListSelector(\n [\n selectAccountsToGroupIdMap,\n (state) => state.accountsByChainId,\n (state) => state.marketData,\n (state) => state.currencyRates,\n (state) => state.currentCurrency,\n (state) => state.networkConfigurationsByChainId,\n ],\n (\n accountsMap,\n accountsByChainId,\n marketData,\n currencyRates,\n currentCurrency,\n networkConfigurationsByChainId,\n ) => {\n const groupAssets: AssetsByAccountGroup = {};\n\n for (const [chainId, chainAccounts] of Object.entries(\n accountsByChainId,\n ) as [Hex, Record<Hex, { balance: Hex | null }>][]) {\n for (const [accountAddress, accountBalance] of Object.entries(\n chainAccounts,\n )) {\n const accountGroupId = accountsMap[accountAddress];\n groupAssets[accountGroupId] ??= {};\n groupAssets[accountGroupId][chainId] ??= [];\n const groupChainAssets = groupAssets[accountGroupId][chainId];\n\n const rawBalance = accountBalance.balance || '0x0';\n\n const nativeCurrency =\n networkConfigurationsByChainId[chainId]?.nativeCurrency || 'NATIVE';\n\n const nativeToken = {\n address: getNativeTokenAddress(chainId),\n decimals: 18,\n name: nativeCurrency === 'ETH' ? 'Ethereum' : nativeCurrency,\n symbol: nativeCurrency,\n // This field need to be filled at client level for now\n image: '',\n };\n\n const fiatData = getFiatBalanceForEvmToken(\n rawBalance,\n nativeToken.decimals,\n marketData,\n currencyRates,\n chainId,\n nativeToken.address,\n );\n\n groupChainAssets.push({\n type: 'evm',\n assetId: nativeToken.address,\n isNative: true,\n address: nativeToken.address,\n image: nativeToken.image,\n name: nativeToken.name,\n symbol: nativeToken.symbol,\n decimals: nativeToken.decimals,\n balance: stringifyBalanceWithDecimals(\n hexToBigInt(rawBalance),\n nativeToken.decimals,\n ),\n fiat: fiatData\n ? {\n balance: fiatData.balance,\n currency: currentCurrency,\n conversionRate: fiatData.conversionRate,\n }\n : undefined,\n chainId,\n });\n }\n }\n\n return groupAssets;\n },\n);\n\nconst selectAllEvmAssets = createAssetListSelector(\n [\n selectAccountsToGroupIdMap,\n (state) => state.allTokens,\n (state) => state.allIgnoredTokens,\n (state) => state.tokenBalances,\n (state) => state.marketData,\n (state) => state.currencyRates,\n (state) => state.currentCurrency,\n ],\n (\n accountsMap,\n evmTokens,\n ignoredEvmTokens,\n tokenBalances,\n marketData,\n currencyRates,\n currentCurrency,\n ) => {\n const groupAssets: AssetsByAccountGroup = {};\n\n for (const [chainId, chainTokens] of Object.entries(evmTokens) as [\n Hex,\n { [key: string]: Token[] },\n ][]) {\n for (const [accountAddress, addressTokens] of Object.entries(\n chainTokens,\n ) as [Hex, Token[]][]) {\n for (const token of addressTokens) {\n const tokenAddress = token.address as Hex;\n const accountGroupId = accountsMap[accountAddress];\n\n if (\n ignoredEvmTokens[chainId]?.[accountAddress]?.includes(tokenAddress)\n ) {\n continue;\n }\n\n const rawBalance =\n tokenBalances[accountAddress]?.[chainId]?.[tokenAddress];\n\n if (!rawBalance) {\n continue;\n }\n\n groupAssets[accountGroupId] ??= {};\n groupAssets[accountGroupId][chainId] ??= [];\n const groupChainAssets = groupAssets[accountGroupId][chainId];\n\n const fiatData = getFiatBalanceForEvmToken(\n rawBalance,\n token.decimals,\n marketData,\n currencyRates,\n chainId,\n tokenAddress,\n );\n\n groupChainAssets.push({\n type: 'evm',\n assetId: tokenAddress,\n isNative: false,\n address: tokenAddress,\n image: token.image ?? '',\n name: token.name ?? token.symbol,\n symbol: token.symbol,\n decimals: token.decimals,\n balance: stringifyBalanceWithDecimals(\n hexToBigInt(rawBalance),\n token.decimals,\n ),\n fiat: fiatData\n ? {\n balance: fiatData.balance,\n currency: currentCurrency,\n conversionRate: fiatData.conversionRate,\n }\n : undefined,\n chainId,\n });\n }\n }\n }\n\n return groupAssets;\n },\n);\n\nconst selectAllMultichainAssets = createAssetListSelector(\n [\n selectAccountsToGroupIdMap,\n (state) => state.accountsAssets,\n (state) => state.assetsMetadata,\n (state) => state.balances,\n (state) => state.conversionRates,\n (state) => state.currentCurrency,\n ],\n (\n accountsMap,\n multichainTokens,\n multichainAssetsMetadata,\n multichainBalances,\n multichainConversionRates,\n currentCurrency,\n ) => {\n const groupAssets: AssetsByAccountGroup = {};\n\n for (const [accountId, accountAssets] of Object.entries(multichainTokens)) {\n for (const assetId of accountAssets) {\n let caipAsset: ReturnType<typeof parseCaipAssetType>;\n try {\n caipAsset = parseCaipAssetType(assetId);\n } catch {\n // TODO: We should log this error when we have the ability to inject a logger from the client\n continue;\n }\n\n const { chainId } = caipAsset;\n const asset = `${caipAsset.assetNamespace}:${caipAsset.assetReference}`;\n\n const accountGroupId = accountsMap[accountId];\n const assetMetadata = multichainAssetsMetadata[assetId];\n if (!accountGroupId || !assetMetadata) {\n continue;\n }\n\n groupAssets[accountGroupId] ??= {};\n groupAssets[accountGroupId][chainId] ??= [];\n const groupChainAssets = groupAssets[accountGroupId][chainId];\n\n const balance:\n | {\n amount: string;\n unit: string;\n }\n | undefined = multichainBalances[accountId]?.[assetId];\n\n if (!balance) {\n continue;\n }\n\n const fiatData = getFiatBalanceForMultichainAsset(\n balance,\n multichainConversionRates,\n assetId,\n );\n\n // TODO: We shouldn't have to rely on fallbacks for name and symbol, they should not be optional\n groupChainAssets.push({\n type: 'multichain',\n assetId,\n isNative: MULTICHAIN_NATIVE_ASSET_IDS.includes(assetId),\n image: assetMetadata.iconUrl,\n name: assetMetadata.name ?? assetMetadata.symbol ?? asset,\n symbol: assetMetadata.symbol ?? asset,\n decimals:\n assetMetadata.units.find(\n (unit) =>\n unit.name === assetMetadata.name &&\n unit.symbol === assetMetadata.symbol,\n )?.decimals ?? 0,\n balance: balance.amount,\n fiat: fiatData\n ? {\n balance: fiatData.balance,\n currency: currentCurrency,\n conversionRate: fiatData.conversionRate,\n }\n : undefined,\n chainId,\n });\n }\n }\n\n return groupAssets;\n },\n);\n\nconst selectAllAssets = createAssetListSelector(\n [\n selectAllEvmAssets,\n selectAllMultichainAssets,\n selectAllEvmAccountNativeBalances,\n ],\n (evmAssets, multichainAssets, evmAccountNativeBalances) => {\n const groupAssets: AssetsByAccountGroup = {};\n\n mergeAssets(groupAssets, evmAssets);\n\n mergeAssets(groupAssets, multichainAssets);\n\n mergeAssets(groupAssets, evmAccountNativeBalances);\n\n return groupAssets;\n },\n);\n\nexport const selectAssetsBySelectedAccountGroup = createAssetListSelector(\n [selectAllAssets, (state) => state.accountTree],\n (groupAssets, accountTree) => {\n const { selectedAccountGroup } = accountTree;\n if (!selectedAccountGroup) {\n return {};\n }\n return groupAssets[selectedAccountGroup] || {};\n },\n);\n\n// TODO: Once native assets are part of the evm tokens state, this function can be simplified as chains will always be unique\n/**\n * Merges the new assets into the existing assets\n *\n * @param existingAssets - The existing assets\n * @param newAssets - The new assets\n */\nfunction mergeAssets(\n existingAssets: AssetsByAccountGroup,\n newAssets: AssetsByAccountGroup,\n) {\n for (const [accountGroupId, accountAssets] of Object.entries(newAssets) as [\n AccountGroupId,\n AccountGroupAssets,\n ][]) {\n const existingAccountGroupAssets = existingAssets[accountGroupId];\n\n if (!existingAccountGroupAssets) {\n existingAssets[accountGroupId] = accountAssets;\n } else {\n for (const [network, chainAssets] of Object.entries(accountAssets)) {\n existingAccountGroupAssets[network] ??= [];\n existingAccountGroupAssets[network].push(...chainAssets);\n }\n }\n }\n}\n\n/**\n * @param rawBalance - The balance of the token\n * @param decimals - The decimals of the token\n * @param marketData - The market data for the token\n * @param currencyRates - The currency rates for the token\n * @param chainId - The chain id of the token\n * @param tokenAddress - The address of the token\n * @returns The price and currency of the token in the current currency. Returns undefined if the asset is not found in the market data or currency rates.\n */\nfunction getFiatBalanceForEvmToken(\n rawBalance: Hex,\n decimals: number,\n marketData: TokenRatesControllerState['marketData'],\n currencyRates: CurrencyRateState['currencyRates'],\n chainId: Hex,\n tokenAddress: Hex,\n) {\n const tokenMarketData = marketData[chainId]?.[tokenAddress];\n\n if (!tokenMarketData) {\n return undefined;\n }\n\n const currencyRate = currencyRates[tokenMarketData.currency];\n\n if (!currencyRate?.conversionRate) {\n return undefined;\n }\n\n const fiatBalance =\n (convertHexToDecimal(rawBalance) / 10 ** decimals) *\n tokenMarketData.price *\n currencyRate.conversionRate;\n\n return {\n balance: fiatBalance,\n conversionRate: currencyRate.conversionRate,\n };\n}\n\n/**\n * @param balance - The balance of the asset, in the format { amount: string; unit: string }\n * @param balance.amount - The amount of the balance\n * @param balance.unit - The unit of the balance\n * @param multichainConversionRates - The conversion rates for the multichain asset\n * @param assetId - The asset id of the asset\n * @returns The price and currency of the token in the current currency. Returns undefined if the asset is not found in the conversion rates.\n */\nfunction getFiatBalanceForMultichainAsset(\n balance: { amount: string; unit: string },\n multichainConversionRates: MultichainAssetsRatesControllerState['conversionRates'],\n assetId: `${string}:${string}/${string}:${string}`,\n) {\n const assetMarketData = multichainConversionRates[assetId];\n\n if (!assetMarketData?.rate) {\n return undefined;\n }\n\n return {\n balance: Number(balance.amount) * Number(assetMarketData.rate),\n conversionRate: Number(assetMarketData.rate),\n };\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask-previews/assets-controllers",
3
- "version": "74.0.0-preview-2c45ce0",
3
+ "version": "74.0.0-preview-e53ed971",
4
4
  "description": "Controllers which manage interactions involving ERC-20, ERC-721, and ERC-1155 tokens (including NFTs)",
5
5
  "keywords": [
6
6
  "MetaMask",