@metamask-previews/assets-controller 5.0.0-preview-5f5149c21 → 5.0.0-preview-60aa3d02f
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +0 -4
- package/dist/data-sources/AccountsApiDataSource.cjs +1 -1
- package/dist/data-sources/AccountsApiDataSource.cjs.map +1 -1
- package/dist/data-sources/AccountsApiDataSource.d.cts +1 -1
- package/dist/data-sources/AccountsApiDataSource.d.cts.map +1 -1
- package/dist/data-sources/AccountsApiDataSource.d.mts +1 -1
- package/dist/data-sources/AccountsApiDataSource.d.mts.map +1 -1
- package/dist/data-sources/AccountsApiDataSource.mjs +1 -1
- package/dist/data-sources/AccountsApiDataSource.mjs.map +1 -1
- package/dist/data-sources/BackendWebsocketDataSource.cjs +1 -1
- package/dist/data-sources/BackendWebsocketDataSource.cjs.map +1 -1
- package/dist/data-sources/BackendWebsocketDataSource.d.cts +2 -2
- package/dist/data-sources/BackendWebsocketDataSource.d.cts.map +1 -1
- package/dist/data-sources/BackendWebsocketDataSource.d.mts +2 -2
- package/dist/data-sources/BackendWebsocketDataSource.d.mts.map +1 -1
- package/dist/data-sources/BackendWebsocketDataSource.mjs +1 -1
- package/dist/data-sources/BackendWebsocketDataSource.mjs.map +1 -1
- package/dist/data-sources/PriceDataSource.cjs +1 -1
- package/dist/data-sources/PriceDataSource.cjs.map +1 -1
- package/dist/data-sources/PriceDataSource.d.cts +1 -1
- package/dist/data-sources/PriceDataSource.d.cts.map +1 -1
- package/dist/data-sources/PriceDataSource.d.mts +1 -1
- package/dist/data-sources/PriceDataSource.d.mts.map +1 -1
- package/dist/data-sources/PriceDataSource.mjs +1 -1
- package/dist/data-sources/PriceDataSource.mjs.map +1 -1
- package/dist/data-sources/RpcDataSource.cjs +2 -2
- package/dist/data-sources/RpcDataSource.cjs.map +1 -1
- package/dist/data-sources/RpcDataSource.d.cts +2 -2
- package/dist/data-sources/RpcDataSource.d.cts.map +1 -1
- package/dist/data-sources/RpcDataSource.d.mts +2 -2
- package/dist/data-sources/RpcDataSource.d.mts.map +1 -1
- package/dist/data-sources/RpcDataSource.mjs +2 -2
- package/dist/data-sources/RpcDataSource.mjs.map +1 -1
- package/dist/data-sources/SnapDataSource.cjs +1 -1
- package/dist/data-sources/SnapDataSource.cjs.map +1 -1
- package/dist/data-sources/SnapDataSource.d.cts +2 -2
- package/dist/data-sources/SnapDataSource.d.cts.map +1 -1
- package/dist/data-sources/SnapDataSource.d.mts +2 -2
- package/dist/data-sources/SnapDataSource.d.mts.map +1 -1
- package/dist/data-sources/SnapDataSource.mjs +1 -1
- package/dist/data-sources/SnapDataSource.mjs.map +1 -1
- package/dist/data-sources/StakedBalanceDataSource.cjs +1 -1
- package/dist/data-sources/StakedBalanceDataSource.cjs.map +1 -1
- package/dist/data-sources/StakedBalanceDataSource.d.cts +2 -2
- package/dist/data-sources/StakedBalanceDataSource.d.cts.map +1 -1
- package/dist/data-sources/StakedBalanceDataSource.d.mts +2 -2
- package/dist/data-sources/StakedBalanceDataSource.d.mts.map +1 -1
- package/dist/data-sources/StakedBalanceDataSource.mjs +1 -1
- package/dist/data-sources/StakedBalanceDataSource.mjs.map +1 -1
- package/dist/data-sources/TokenDataSource.cjs +1 -1
- package/dist/data-sources/TokenDataSource.cjs.map +1 -1
- package/dist/data-sources/TokenDataSource.d.cts.map +1 -1
- package/dist/data-sources/TokenDataSource.d.mts.map +1 -1
- package/dist/data-sources/TokenDataSource.mjs +1 -1
- package/dist/data-sources/TokenDataSource.mjs.map +1 -1
- package/dist/utils/formatStateForTransactionPay.cjs.map +1 -1
- package/dist/utils/formatStateForTransactionPay.d.cts +1 -1
- package/dist/utils/formatStateForTransactionPay.d.cts.map +1 -1
- package/dist/utils/formatStateForTransactionPay.d.mts +1 -1
- package/dist/utils/formatStateForTransactionPay.d.mts.map +1 -1
- package/dist/utils/formatStateForTransactionPay.mjs.map +1 -1
- package/package.json +15 -15
|
@@ -1 +1 @@
|
|
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Q,EAAE,EAAE,CACX,QAAQ,CAAC,OAAO,CAAC,WAAW,EAAE,KAAK,OAAO,CAAC,WAAW,EAAE,CAC3D,EACD,CAAC;gBACD,gBAAgB,CAAC,UAAU,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;YAC3C,CAAC;QACH,CAAC;QAAC,MAAM,CAAC;YACP,2BAA2B;QAC7B,CAAC;IACH,CAAC;IAED,MAAM,SAAS,GAA4C,EAAE,CAAC;IAC9D,KAAK,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,gBAAgB,CAAC,EAAE,CAAC;QACjE,SAAS,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,EAAE,MAAM,EAAE,CAAC;IACtC,CAAC;IAED,MAAM,aAAa,GAAG,IAAA,2DAA4B,EAAC;QACjD,WAAW;QACX,gBAAgB;QAChB,sBAAsB;QACtB,8BAA8B;KAC/B,CAAC,CAAC;IAEH,OAAO;QACL,aAAa;QACb,iBAAiB;QACjB,SAAS;QACT,UAAU,EAAE,aAAa,CAAC,UAAU;QACpC,aAAa,EAAE,aAAa,CAAC,aAAa;QAC1C,eAAe,EAAE,aAAa,CAAC,eAAe;KAC/C,CAAC;AACJ,CAAC;AAtHD,oEAsHC","sourcesContent":["import { toChecksumAddress } from '@ethereumjs/util';\nimport { numberToHex } from '@metamask/utils';\nimport { parseCaipAssetType, parseCaipChainId } from '@metamask/utils';\n\nimport type {\n AssetBalance,\n AssetMetadata,\n AssetPrice,\n Caip19AssetId,\n} from '../types';\nimport { formatExchangeRatesForBridge } from './formatExchangeRatesForBridge';\nimport type { BridgeExchangeRatesFormat } from './formatExchangeRatesForBridge';\n\n/** Account with id and address for mapping state to legacy format. */\nexport type AccountForLegacyFormat = { id: string; address: string };\n\n/**\n * Legacy Token shape expected by TokensController / transaction-pay-controller.\n */\nexport type LegacyToken = {\n address: string;\n decimals: number;\n symbol: string;\n name?: string;\n [key: string]: unknown;\n};\n\n/**\n * Legacy state shape that transaction-pay-controller reads from\n * TokenBalancesController, AccountTrackerController, TokensController,\n * TokenRatesController, and CurrencyRateController.\n */\nexport type TransactionPayLegacyFormat = {\n /** TokenBalancesController:getState().tokenBalances */\n tokenBalances: Record<string, Record<string, Record<string, `0x${string}`>>>;\n /** AccountTrackerController:getState().accountsByChainId */\n accountsByChainId: Record<\n string,\n Record<string, { balance: string; stakedBalance?: string }>\n >;\n /** TokensController:getState().allTokens (chainId -> key -> Token[]) */\n allTokens: Record<string, Record<string, LegacyToken[]>>;\n /** TokenRatesController:getState().marketData */\n marketData: BridgeExchangeRatesFormat['marketData'];\n /** CurrencyRateController:getState().currencyRates */\n currencyRates: BridgeExchangeRatesFormat['currencyRates'];\n /** CurrencyRateController:getState().currentCurrency */\n currentCurrency: string;\n};\n\nfunction amountToHex(amount: string): `0x${string}` {\n const hexString = BigInt(amount).toString(16);\n return `0x${hexString}`;\n}\n\nfunction getAmountFromBalance(balance: AssetBalance): string {\n return typeof balance === 'object' && balance !== null && 'amount' in balance\n ? String((balance as { amount: string }).amount)\n : '0';\n}\n\n/**\n * Converts AssetsController state into the legacy format consumed by\n * transaction-pay-controller (TokenBalancesController, AccountTrackerController,\n * TokensController, TokenRatesController, CurrencyRateController shapes).\n *\n * @param params - Conversion parameters.\n * @param params.assetsBalance - Per-account balances by asset ID.\n * @param params.assetsInfo - Metadata by asset ID.\n * @param params.assetsPrice - Prices by asset ID.\n * @param params.selectedCurrency - Current currency code.\n * @param params.accounts - List of accounts (id + address) to map state for.\n * @param params.nativeAssetIdentifiers - Optional CAIP-2 chain ID to native asset ID.\n * @param params.networkConfigurationsByChainId - Optional chain ID to network config (for native symbol).\n * @returns Legacy-compatible state for transaction-pay-controller.\n */\nexport function formatStateForTransactionPay(params: {\n assetsBalance: Record<string, Record<string, AssetBalance>>;\n assetsInfo: Record<string, AssetMetadata>;\n assetsPrice: Record<string, AssetPrice>;\n selectedCurrency: string;\n accounts: AccountForLegacyFormat[];\n nativeAssetIdentifiers?: Record<string, string>;\n networkConfigurationsByChainId?: Record<string, { nativeCurrency?: string }>;\n}): TransactionPayLegacyFormat {\n const {\n assetsBalance,\n assetsInfo,\n assetsPrice,\n selectedCurrency,\n accounts,\n nativeAssetIdentifiers = {},\n networkConfigurationsByChainId = {},\n } = params;\n\n const tokenBalances: TransactionPayLegacyFormat['tokenBalances'] = {};\n const accountsByChainId: TransactionPayLegacyFormat['accountsByChainId'] = {};\n const allTokensByChain: Record<string, LegacyToken[]> = {};\n\n for (const account of accounts) {\n const accountAddressLower = account.address.toLowerCase();\n const accountBalances = assetsBalance[account.id];\n if (!accountBalances) {\n continue;\n }\n\n for (const [assetId, balance] of Object.entries(accountBalances)) {\n try {\n const parsed = parseCaipAssetType(assetId as Caip19AssetId);\n const chainIdParsed = parseCaipChainId(parsed.chainId);\n if (chainIdParsed.namespace !== 'eip155') {\n continue;\n }\n const chainIdHex = numberToHex(parseInt(chainIdParsed.reference, 10));\n const amount = getAmountFromBalance(balance);\n const balanceHex = amountToHex(amount);\n\n if (parsed.assetNamespace === 'slip44') {\n const nativeAddress =\n '0x0000000000000000000000000000000000000000' as const;\n const checksumAddress = toChecksumAddress(account.address);\n tokenBalances[accountAddressLower] ??= {};\n tokenBalances[accountAddressLower][chainIdHex] ??= {};\n tokenBalances[accountAddressLower][chainIdHex][nativeAddress] =\n balanceHex;\n accountsByChainId[chainIdHex] ??= {};\n accountsByChainId[chainIdHex][checksumAddress] = {\n balance: balanceHex,\n };\n } else if (parsed.assetNamespace === 'erc20') {\n const tokenAddress = toChecksumAddress(String(parsed.assetReference));\n tokenBalances[accountAddressLower] ??= {};\n tokenBalances[accountAddressLower][chainIdHex] ??= {};\n tokenBalances[accountAddressLower][chainIdHex][tokenAddress] =\n balanceHex;\n }\n } catch {\n // Skip malformed asset IDs\n }\n }\n }\n\n for (const [assetId, metadata] of Object.entries(assetsInfo)) {\n try {\n const parsed = parseCaipAssetType(assetId as Caip19AssetId);\n const chainIdParsed = parseCaipChainId(parsed.chainId);\n if (chainIdParsed.namespace !== 'eip155') {\n continue;\n }\n const chainIdHex = numberToHex(parseInt(chainIdParsed.reference, 10));\n const address =\n parsed.assetNamespace === 'slip44'\n ? '0x0000000000000000000000000000000000000000'\n : toChecksumAddress(String(parsed.assetReference));\n const token: LegacyToken = {\n address,\n decimals: metadata.decimals,\n symbol: metadata.symbol,\n name: metadata.name,\n };\n allTokensByChain[chainIdHex] ??= [];\n if (\n !allTokensByChain[chainIdHex].some(\n (existing) =>\n existing.address.toLowerCase() === address.toLowerCase(),\n )\n ) {\n allTokensByChain[chainIdHex].push(token);\n }\n } catch {\n // Skip malformed asset IDs\n }\n }\n\n const allTokens: TransactionPayLegacyFormat['allTokens'] = {};\n for (const [chainId, tokens] of Object.entries(allTokensByChain)) {\n allTokens[chainId] = { '': tokens };\n }\n\n const exchangeRates = formatExchangeRatesForBridge({\n assetsPrice,\n selectedCurrency,\n nativeAssetIdentifiers,\n networkConfigurationsByChainId,\n });\n\n return {\n tokenBalances,\n accountsByChainId,\n allTokens,\n marketData: exchangeRates.marketData,\n currencyRates: exchangeRates.currencyRates,\n currentCurrency: exchangeRates.currentCurrency,\n };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"formatStateForTransactionPay.cjs","sourceRoot":"","sources":["../../src/utils/formatStateForTransactionPay.ts"],"names":[],"mappings":";;;AAAA,2CAAqD;AACrD,2CAA8C;AAC9C,2CAAuE;AAEvE,qFAA8E;AA8C9E,SAAS,WAAW,CAAC,MAAc;IACjC,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;IAC9C,OAAO,KAAK,SAAS,EAAE,CAAC;AAC1B,CAAC;AAED,SAAS,oBAAoB,CAAC,OAAqB;IACjD,OAAO,OAAO,OAAO,KAAK,QAAQ,IAAI,OAAO,KAAK,IAAI,IAAI,QAAQ,IAAI,OAAO;QAC3E,CAAC,CAAC,MAAM,CAAE,OAA8B,CAAC,MAA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,GAAG,IAAA,2DAA4B,EAAC;QACjD,WAAW;QACX,gBAAgB;QAChB,sBAAsB;QACtB,8BAA8B;KAC/B,CAAC,CAAC;IAEH,OAAO;QACL,aAAa;QACb,iBAAiB;QACjB,SAAS;QACT,UAAU,EAAE,aAAa,CAAC,UAAU;QACpC,aAAa,EAAE,aAAa,CAAC,aAAa;QAC1C,eAAe,EAAE,aAAa,CAAC,eAAe;KAC/C,CAAC;AACJ,CAAC;AAtHD,oEAsHC","sourcesContent":["import { toChecksumAddress } from '@ethereumjs/util';\nimport { numberToHex } from '@metamask/utils';\nimport { parseCaipAssetType, parseCaipChainId } from '@metamask/utils';\n\nimport { formatExchangeRatesForBridge } from './formatExchangeRatesForBridge';\nimport type { BridgeExchangeRatesFormat } from './formatExchangeRatesForBridge';\nimport type {\n AssetBalance,\n AssetMetadata,\n AssetPrice,\n Caip19AssetId,\n} from '../types';\n\n/** Account with id and address for mapping state to legacy format. */\nexport type AccountForLegacyFormat = { id: string; address: string };\n\n/**\n * Legacy Token shape expected by TokensController / transaction-pay-controller.\n */\nexport type LegacyToken = {\n address: string;\n decimals: number;\n symbol: string;\n name?: string;\n [key: string]: unknown;\n};\n\n/**\n * Legacy state shape that transaction-pay-controller reads from\n * TokenBalancesController, AccountTrackerController, TokensController,\n * TokenRatesController, and CurrencyRateController.\n */\nexport type TransactionPayLegacyFormat = {\n /** TokenBalancesController:getState().tokenBalances */\n tokenBalances: Record<string, Record<string, Record<string, `0x${string}`>>>;\n /** AccountTrackerController:getState().accountsByChainId */\n accountsByChainId: Record<\n string,\n Record<string, { balance: string; stakedBalance?: string }>\n >;\n /** TokensController:getState().allTokens (chainId -> key -> Token[]) */\n allTokens: Record<string, Record<string, LegacyToken[]>>;\n /** TokenRatesController:getState().marketData */\n marketData: BridgeExchangeRatesFormat['marketData'];\n /** CurrencyRateController:getState().currencyRates */\n currencyRates: BridgeExchangeRatesFormat['currencyRates'];\n /** CurrencyRateController:getState().currentCurrency */\n currentCurrency: string;\n};\n\nfunction amountToHex(amount: string): `0x${string}` {\n const hexString = BigInt(amount).toString(16);\n return `0x${hexString}`;\n}\n\nfunction getAmountFromBalance(balance: AssetBalance): string {\n return typeof balance === 'object' && balance !== null && 'amount' in balance\n ? String((balance as { amount: string }).amount)\n : '0';\n}\n\n/**\n * Converts AssetsController state into the legacy format consumed by\n * transaction-pay-controller (TokenBalancesController, AccountTrackerController,\n * TokensController, TokenRatesController, CurrencyRateController shapes).\n *\n * @param params - Conversion parameters.\n * @param params.assetsBalance - Per-account balances by asset ID.\n * @param params.assetsInfo - Metadata by asset ID.\n * @param params.assetsPrice - Prices by asset ID.\n * @param params.selectedCurrency - Current currency code.\n * @param params.accounts - List of accounts (id + address) to map state for.\n * @param params.nativeAssetIdentifiers - Optional CAIP-2 chain ID to native asset ID.\n * @param params.networkConfigurationsByChainId - Optional chain ID to network config (for native symbol).\n * @returns Legacy-compatible state for transaction-pay-controller.\n */\nexport function formatStateForTransactionPay(params: {\n assetsBalance: Record<string, Record<string, AssetBalance>>;\n assetsInfo: Record<string, AssetMetadata>;\n assetsPrice: Record<string, AssetPrice>;\n selectedCurrency: string;\n accounts: AccountForLegacyFormat[];\n nativeAssetIdentifiers?: Record<string, string>;\n networkConfigurationsByChainId?: Record<string, { nativeCurrency?: string }>;\n}): TransactionPayLegacyFormat {\n const {\n assetsBalance,\n assetsInfo,\n assetsPrice,\n selectedCurrency,\n accounts,\n nativeAssetIdentifiers = {},\n networkConfigurationsByChainId = {},\n } = params;\n\n const tokenBalances: TransactionPayLegacyFormat['tokenBalances'] = {};\n const accountsByChainId: TransactionPayLegacyFormat['accountsByChainId'] = {};\n const allTokensByChain: Record<string, LegacyToken[]> = {};\n\n for (const account of accounts) {\n const accountAddressLower = account.address.toLowerCase();\n const accountBalances = assetsBalance[account.id];\n if (!accountBalances) {\n continue;\n }\n\n for (const [assetId, balance] of Object.entries(accountBalances)) {\n try {\n const parsed = parseCaipAssetType(assetId as Caip19AssetId);\n const chainIdParsed = parseCaipChainId(parsed.chainId);\n if (chainIdParsed.namespace !== 'eip155') {\n continue;\n }\n const chainIdHex = numberToHex(parseInt(chainIdParsed.reference, 10));\n const amount = getAmountFromBalance(balance);\n const balanceHex = amountToHex(amount);\n\n if (parsed.assetNamespace === 'slip44') {\n const nativeAddress =\n '0x0000000000000000000000000000000000000000' as const;\n const checksumAddress = toChecksumAddress(account.address);\n tokenBalances[accountAddressLower] ??= {};\n tokenBalances[accountAddressLower][chainIdHex] ??= {};\n tokenBalances[accountAddressLower][chainIdHex][nativeAddress] =\n balanceHex;\n accountsByChainId[chainIdHex] ??= {};\n accountsByChainId[chainIdHex][checksumAddress] = {\n balance: balanceHex,\n };\n } else if (parsed.assetNamespace === 'erc20') {\n const tokenAddress = toChecksumAddress(String(parsed.assetReference));\n tokenBalances[accountAddressLower] ??= {};\n tokenBalances[accountAddressLower][chainIdHex] ??= {};\n tokenBalances[accountAddressLower][chainIdHex][tokenAddress] =\n balanceHex;\n }\n } catch {\n // Skip malformed asset IDs\n }\n }\n }\n\n for (const [assetId, metadata] of Object.entries(assetsInfo)) {\n try {\n const parsed = parseCaipAssetType(assetId as Caip19AssetId);\n const chainIdParsed = parseCaipChainId(parsed.chainId);\n if (chainIdParsed.namespace !== 'eip155') {\n continue;\n }\n const chainIdHex = numberToHex(parseInt(chainIdParsed.reference, 10));\n const address =\n parsed.assetNamespace === 'slip44'\n ? '0x0000000000000000000000000000000000000000'\n : toChecksumAddress(String(parsed.assetReference));\n const token: LegacyToken = {\n address,\n decimals: metadata.decimals,\n symbol: metadata.symbol,\n name: metadata.name,\n };\n allTokensByChain[chainIdHex] ??= [];\n if (\n !allTokensByChain[chainIdHex].some(\n (existing) =>\n existing.address.toLowerCase() === address.toLowerCase(),\n )\n ) {\n allTokensByChain[chainIdHex].push(token);\n }\n } catch {\n // Skip malformed asset IDs\n }\n }\n\n const allTokens: TransactionPayLegacyFormat['allTokens'] = {};\n for (const [chainId, tokens] of Object.entries(allTokensByChain)) {\n allTokens[chainId] = { '': tokens };\n }\n\n const exchangeRates = formatExchangeRatesForBridge({\n assetsPrice,\n selectedCurrency,\n nativeAssetIdentifiers,\n networkConfigurationsByChainId,\n });\n\n return {\n tokenBalances,\n accountsByChainId,\n allTokens,\n marketData: exchangeRates.marketData,\n currencyRates: exchangeRates.currencyRates,\n currentCurrency: exchangeRates.currentCurrency,\n };\n}\n"]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { AssetBalance, AssetMetadata, AssetPrice } from "../types.cjs";
|
|
2
1
|
import type { BridgeExchangeRatesFormat } from "./formatExchangeRatesForBridge.cjs";
|
|
2
|
+
import type { AssetBalance, AssetMetadata, AssetPrice } from "../types.cjs";
|
|
3
3
|
/** Account with id and address for mapping state to legacy format. */
|
|
4
4
|
export type AccountForLegacyFormat = {
|
|
5
5
|
id: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"formatStateForTransactionPay.d.cts","sourceRoot":"","sources":["../../src/utils/formatStateForTransactionPay.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"formatStateForTransactionPay.d.cts","sourceRoot":"","sources":["../../src/utils/formatStateForTransactionPay.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,yBAAyB,EAAE,2CAAuC;AAChF,OAAO,KAAK,EACV,YAAY,EACZ,aAAa,EACb,UAAU,EAEX,qBAAiB;AAElB,sEAAsE;AACtE,MAAM,MAAM,sBAAsB,GAAG;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC;AAErE;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG;IACxB,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB,CAAC;AAEF;;;;GAIG;AACH,MAAM,M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
-
import type { AssetBalance, AssetMetadata, AssetPrice } from "../types.mjs";
|
|
2
1
|
import type { BridgeExchangeRatesFormat } from "./formatExchangeRatesForBridge.mjs";
|
|
2
|
+
import type { AssetBalance, AssetMetadata, AssetPrice } from "../types.mjs";
|
|
3
3
|
/** Account with id and address for mapping state to legacy format. */
|
|
4
4
|
export type AccountForLegacyFormat = {
|
|
5
5
|
id: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"formatStateForTransactionPay.d.mts","sourceRoot":"","sources":["../../src/utils/formatStateForTransactionPay.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"formatStateForTransactionPay.d.mts","sourceRoot":"","sources":["../../src/utils/formatStateForTransactionPay.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,yBAAyB,EAAE,2CAAuC;AAChF,OAAO,KAAK,EACV,YAAY,EACZ,aAAa,EACb,UAAU,EAEX,qBAAiB;AAElB,sEAAsE;AACtE,MAAM,MAAM,sBAAsB,GAAG;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC;AAErE;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG;IACxB,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,0BAA0B,GAAG;IACvC,uDAAuD;IACvD,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,KAAK,MAAM,EAAE,CAAC,CAAC,CAAC,CAAC;IAC7E,4DAA4D;IAC5D,iBAAiB,EAAE,MAAM,CACvB,MAAM,EACN,MAAM,CAAC,MAAM,EAAE;QAAE,OAAO,EAAE,MAAM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,2BAA2B;QAC7B,CAAC;IACH,CAAC;IAED,MAAM,SAAS,GAA4C,EAAE,CAAC;IAC9D,KAAK,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,gBAAgB,CAAC,EAAE,CAAC;QACjE,SAAS,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,EAAE,MAAM,EAAE,CAAC;IACtC,CAAC;IAED,MAAM,aAAa,GAAG,4BAA4B,CAAC;QACjD,WAAW;QACX,gBAAgB;QAChB,sBAAsB;QACtB,8BAA8B;KAC/B,CAAC,CAAC;IAEH,OAAO;QACL,aAAa;QACb,iBAAiB;QACjB,SAAS;QACT,UAAU,EAAE,aAAa,CAAC,UAAU;QACpC,aAAa,EAAE,aAAa,CAAC,aAAa;QAC1C,eAAe,EAAE,aAAa,CAAC,eAAe;KAC/C,CAAC;AACJ,CAAC","sourcesContent":["import { toChecksumAddress } from '@ethereumjs/util';\nimport { numberToHex } from '@metamask/utils';\nimport { parseCaipAssetType, parseCaipChainId } from '@metamask/utils';\n\nimport type {\n AssetBalance,\n AssetMetadata,\n AssetPrice,\n Caip19AssetId,\n} from '../types';\nimport { formatExchangeRatesForBridge } from './formatExchangeRatesForBridge';\nimport type { BridgeExchangeRatesFormat } from './formatExchangeRatesForBridge';\n\n/** Account with id and address for mapping state to legacy format. */\nexport type AccountForLegacyFormat = { id: string; address: string };\n\n/**\n * Legacy Token shape expected by TokensController / transaction-pay-controller.\n */\nexport type LegacyToken = {\n address: string;\n decimals: number;\n symbol: string;\n name?: string;\n [key: string]: unknown;\n};\n\n/**\n * Legacy state shape that transaction-pay-controller reads from\n * TokenBalancesController, AccountTrackerController, TokensController,\n * TokenRatesController, and CurrencyRateController.\n */\nexport type TransactionPayLegacyFormat = {\n /** TokenBalancesController:getState().tokenBalances */\n tokenBalances: Record<string, Record<string, Record<string, `0x${string}`>>>;\n /** AccountTrackerController:getState().accountsByChainId */\n accountsByChainId: Record<\n string,\n Record<string, { balance: string; stakedBalance?: string }>\n >;\n /** TokensController:getState().allTokens (chainId -> key -> Token[]) */\n allTokens: Record<string, Record<string, LegacyToken[]>>;\n /** TokenRatesController:getState().marketData */\n marketData: BridgeExchangeRatesFormat['marketData'];\n /** CurrencyRateController:getState().currencyRates */\n currencyRates: BridgeExchangeRatesFormat['currencyRates'];\n /** CurrencyRateController:getState().currentCurrency */\n currentCurrency: string;\n};\n\nfunction amountToHex(amount: string): `0x${string}` {\n const hexString = BigInt(amount).toString(16);\n return `0x${hexString}`;\n}\n\nfunction getAmountFromBalance(balance: AssetBalance): string {\n return typeof balance === 'object' && balance !== null && 'amount' in balance\n ? String((balance as { amount: string }).amount)\n : '0';\n}\n\n/**\n * Converts AssetsController state into the legacy format consumed by\n * transaction-pay-controller (TokenBalancesController, AccountTrackerController,\n * TokensController, TokenRatesController, CurrencyRateController shapes).\n *\n * @param params - Conversion parameters.\n * @param params.assetsBalance - Per-account balances by asset ID.\n * @param params.assetsInfo - Metadata by asset ID.\n * @param params.assetsPrice - Prices by asset ID.\n * @param params.selectedCurrency - Current currency code.\n * @param params.accounts - List of accounts (id + address) to map state for.\n * @param params.nativeAssetIdentifiers - Optional CAIP-2 chain ID to native asset ID.\n * @param params.networkConfigurationsByChainId - Optional chain ID to network config (for native symbol).\n * @returns Legacy-compatible state for transaction-pay-controller.\n */\nexport function formatStateForTransactionPay(params: {\n assetsBalance: Record<string, Record<string, AssetBalance>>;\n assetsInfo: Record<string, AssetMetadata>;\n assetsPrice: Record<string, AssetPrice>;\n selectedCurrency: string;\n accounts: AccountForLegacyFormat[];\n nativeAssetIdentifiers?: Record<string, string>;\n networkConfigurationsByChainId?: Record<string, { nativeCurrency?: string }>;\n}): TransactionPayLegacyFormat {\n const {\n assetsBalance,\n assetsInfo,\n assetsPrice,\n selectedCurrency,\n accounts,\n nativeAssetIdentifiers = {},\n networkConfigurationsByChainId = {},\n } = params;\n\n const tokenBalances: TransactionPayLegacyFormat['tokenBalances'] = {};\n const accountsByChainId: TransactionPayLegacyFormat['accountsByChainId'] = {};\n const allTokensByChain: Record<string, LegacyToken[]> = {};\n\n for (const account of accounts) {\n const accountAddressLower = account.address.toLowerCase();\n const accountBalances = assetsBalance[account.id];\n if (!accountBalances) {\n continue;\n }\n\n for (const [assetId, balance] of Object.entries(accountBalances)) {\n try {\n const parsed = parseCaipAssetType(assetId as Caip19AssetId);\n const chainIdParsed = parseCaipChainId(parsed.chainId);\n if (chainIdParsed.namespace !== 'eip155') {\n continue;\n }\n const chainIdHex = numberToHex(parseInt(chainIdParsed.reference, 10));\n const amount = getAmountFromBalance(balance);\n const balanceHex = amountToHex(amount);\n\n if (parsed.assetNamespace === 'slip44') {\n const nativeAddress =\n '0x0000000000000000000000000000000000000000' as const;\n const checksumAddress = toChecksumAddress(account.address);\n tokenBalances[accountAddressLower] ??= {};\n tokenBalances[accountAddressLower][chainIdHex] ??= {};\n tokenBalances[accountAddressLower][chainIdHex][nativeAddress] =\n balanceHex;\n accountsByChainId[chainIdHex] ??= {};\n accountsByChainId[chainIdHex][checksumAddress] = {\n balance: balanceHex,\n };\n } else if (parsed.assetNamespace === 'erc20') {\n const tokenAddress = toChecksumAddress(String(parsed.assetReference));\n tokenBalances[accountAddressLower] ??= {};\n tokenBalances[accountAddressLower][chainIdHex] ??= {};\n tokenBalances[accountAddressLower][chainIdHex][tokenAddress] =\n balanceHex;\n }\n } catch {\n // Skip malformed asset IDs\n }\n }\n }\n\n for (const [assetId, metadata] of Object.entries(assetsInfo)) {\n try {\n const parsed = parseCaipAssetType(assetId as Caip19AssetId);\n const chainIdParsed = parseCaipChainId(parsed.chainId);\n if (chainIdParsed.namespace !== 'eip155') {\n continue;\n }\n const chainIdHex = numberToHex(parseInt(chainIdParsed.reference, 10));\n const address =\n parsed.assetNamespace === 'slip44'\n ? '0x0000000000000000000000000000000000000000'\n : toChecksumAddress(String(parsed.assetReference));\n const token: LegacyToken = {\n address,\n decimals: metadata.decimals,\n symbol: metadata.symbol,\n name: metadata.name,\n };\n allTokensByChain[chainIdHex] ??= [];\n if (\n !allTokensByChain[chainIdHex].some(\n (existing) =>\n existing.address.toLowerCase() === address.toLowerCase(),\n )\n ) {\n allTokensByChain[chainIdHex].push(token);\n }\n } catch {\n // Skip malformed asset IDs\n }\n }\n\n const allTokens: TransactionPayLegacyFormat['allTokens'] = {};\n for (const [chainId, tokens] of Object.entries(allTokensByChain)) {\n allTokens[chainId] = { '': tokens };\n }\n\n const exchangeRates = formatExchangeRatesForBridge({\n assetsPrice,\n selectedCurrency,\n nativeAssetIdentifiers,\n networkConfigurationsByChainId,\n });\n\n return {\n tokenBalances,\n accountsByChainId,\n allTokens,\n marketData: exchangeRates.marketData,\n currencyRates: exchangeRates.currencyRates,\n currentCurrency: exchangeRates.currentCurrency,\n };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"formatStateForTransactionPay.mjs","sourceRoot":"","sources":["../../src/utils/formatStateForTransactionPay.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,yBAAyB;AACrD,OAAO,EAAE,WAAW,EAAE,wBAAwB;AAC9C,OAAO,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,wBAAwB;AAEvE,OAAO,EAAE,4BAA4B,EAAE,2CAAuC;AA8C9E,SAAS,WAAW,CAAC,MAAc;IACjC,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;IAC9C,OAAO,KAAK,SAAS,EAAE,CAAC;AAC1B,CAAC;AAED,SAAS,oBAAoB,CAAC,OAAqB;IACjD,OAAO,OAAO,OAAO,KAAK,QAAQ,IAAI,OAAO,KAAK,IAAI,IAAI,QAAQ,IAAI,OAAO;QAC3E,CAAC,CAAC,MAAM,CAAE,OAA8B,CAAC,MAAM,CAAC;QAChD,CAAC,CAAC,GAAG,CAAC;AACV,CAAC;AAED;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,4BAA4B,CAAC,MAQ5C;;IACC,MAAM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QACpC,aAAa,EAAE,aAAa,CAAC,aAAa;QAC1C,eAAe,EAAE,aAAa,CAAC,eAAe;KAC/C,CAAC;AACJ,CAAC","sourcesContent":["import { toChecksumAddress } from '@ethereumjs/util';\nimport { numberToHex } from '@metamask/utils';\nimport { parseCaipAssetType, parseCaipChainId } from '@metamask/utils';\n\nimport { formatExchangeRatesForBridge } from './formatExchangeRatesForBridge';\nimport type { BridgeExchangeRatesFormat } from './formatExchangeRatesForBridge';\nimport type {\n AssetBalance,\n AssetMetadata,\n AssetPrice,\n Caip19AssetId,\n} from '../types';\n\n/** Account with id and address for mapping state to legacy format. */\nexport type AccountForLegacyFormat = { id: string; address: string };\n\n/**\n * Legacy Token shape expected by TokensController / transaction-pay-controller.\n */\nexport type LegacyToken = {\n address: string;\n decimals: number;\n symbol: string;\n name?: string;\n [key: string]: unknown;\n};\n\n/**\n * Legacy state shape that transaction-pay-controller reads from\n * TokenBalancesController, AccountTrackerController, TokensController,\n * TokenRatesController, and CurrencyRateController.\n */\nexport type TransactionPayLegacyFormat = {\n /** TokenBalancesController:getState().tokenBalances */\n tokenBalances: Record<string, Record<string, Record<string, `0x${string}`>>>;\n /** AccountTrackerController:getState().accountsByChainId */\n accountsByChainId: Record<\n string,\n Record<string, { balance: string; stakedBalance?: string }>\n >;\n /** TokensController:getState().allTokens (chainId -> key -> Token[]) */\n allTokens: Record<string, Record<string, LegacyToken[]>>;\n /** TokenRatesController:getState().marketData */\n marketData: BridgeExchangeRatesFormat['marketData'];\n /** CurrencyRateController:getState().currencyRates */\n currencyRates: BridgeExchangeRatesFormat['currencyRates'];\n /** CurrencyRateController:getState().currentCurrency */\n currentCurrency: string;\n};\n\nfunction amountToHex(amount: string): `0x${string}` {\n const hexString = BigInt(amount).toString(16);\n return `0x${hexString}`;\n}\n\nfunction getAmountFromBalance(balance: AssetBalance): string {\n return typeof balance === 'object' && balance !== null && 'amount' in balance\n ? String((balance as { amount: string }).amount)\n : '0';\n}\n\n/**\n * Converts AssetsController state into the legacy format consumed by\n * transaction-pay-controller (TokenBalancesController, AccountTrackerController,\n * TokensController, TokenRatesController, CurrencyRateController shapes).\n *\n * @param params - Conversion parameters.\n * @param params.assetsBalance - Per-account balances by asset ID.\n * @param params.assetsInfo - Metadata by asset ID.\n * @param params.assetsPrice - Prices by asset ID.\n * @param params.selectedCurrency - Current currency code.\n * @param params.accounts - List of accounts (id + address) to map state for.\n * @param params.nativeAssetIdentifiers - Optional CAIP-2 chain ID to native asset ID.\n * @param params.networkConfigurationsByChainId - Optional chain ID to network config (for native symbol).\n * @returns Legacy-compatible state for transaction-pay-controller.\n */\nexport function formatStateForTransactionPay(params: {\n assetsBalance: Record<string, Record<string, AssetBalance>>;\n assetsInfo: Record<string, AssetMetadata>;\n assetsPrice: Record<string, AssetPrice>;\n selectedCurrency: string;\n accounts: AccountForLegacyFormat[];\n nativeAssetIdentifiers?: Record<string, string>;\n networkConfigurationsByChainId?: Record<string, { nativeCurrency?: string }>;\n}): TransactionPayLegacyFormat {\n const {\n assetsBalance,\n assetsInfo,\n assetsPrice,\n selectedCurrency,\n accounts,\n nativeAssetIdentifiers = {},\n networkConfigurationsByChainId = {},\n } = params;\n\n const tokenBalances: TransactionPayLegacyFormat['tokenBalances'] = {};\n const accountsByChainId: TransactionPayLegacyFormat['accountsByChainId'] = {};\n const allTokensByChain: Record<string, LegacyToken[]> = {};\n\n for (const account of accounts) {\n const accountAddressLower = account.address.toLowerCase();\n const accountBalances = assetsBalance[account.id];\n if (!accountBalances) {\n continue;\n }\n\n for (const [assetId, balance] of Object.entries(accountBalances)) {\n try {\n const parsed = parseCaipAssetType(assetId as Caip19AssetId);\n const chainIdParsed = parseCaipChainId(parsed.chainId);\n if (chainIdParsed.namespace !== 'eip155') {\n continue;\n }\n const chainIdHex = numberToHex(parseInt(chainIdParsed.reference, 10));\n const amount = getAmountFromBalance(balance);\n const balanceHex = amountToHex(amount);\n\n if (parsed.assetNamespace === 'slip44') {\n const nativeAddress =\n '0x0000000000000000000000000000000000000000' as const;\n const checksumAddress = toChecksumAddress(account.address);\n tokenBalances[accountAddressLower] ??= {};\n tokenBalances[accountAddressLower][chainIdHex] ??= {};\n tokenBalances[accountAddressLower][chainIdHex][nativeAddress] =\n balanceHex;\n accountsByChainId[chainIdHex] ??= {};\n accountsByChainId[chainIdHex][checksumAddress] = {\n balance: balanceHex,\n };\n } else if (parsed.assetNamespace === 'erc20') {\n const tokenAddress = toChecksumAddress(String(parsed.assetReference));\n tokenBalances[accountAddressLower] ??= {};\n tokenBalances[accountAddressLower][chainIdHex] ??= {};\n tokenBalances[accountAddressLower][chainIdHex][tokenAddress] =\n balanceHex;\n }\n } catch {\n // Skip malformed asset IDs\n }\n }\n }\n\n for (const [assetId, metadata] of Object.entries(assetsInfo)) {\n try {\n const parsed = parseCaipAssetType(assetId as Caip19AssetId);\n const chainIdParsed = parseCaipChainId(parsed.chainId);\n if (chainIdParsed.namespace !== 'eip155') {\n continue;\n }\n const chainIdHex = numberToHex(parseInt(chainIdParsed.reference, 10));\n const address =\n parsed.assetNamespace === 'slip44'\n ? '0x0000000000000000000000000000000000000000'\n : toChecksumAddress(String(parsed.assetReference));\n const token: LegacyToken = {\n address,\n decimals: metadata.decimals,\n symbol: metadata.symbol,\n name: metadata.name,\n };\n allTokensByChain[chainIdHex] ??= [];\n if (\n !allTokensByChain[chainIdHex].some(\n (existing) =>\n existing.address.toLowerCase() === address.toLowerCase(),\n )\n ) {\n allTokensByChain[chainIdHex].push(token);\n }\n } catch {\n // Skip malformed asset IDs\n }\n }\n\n const allTokens: TransactionPayLegacyFormat['allTokens'] = {};\n for (const [chainId, tokens] of Object.entries(allTokensByChain)) {\n allTokens[chainId] = { '': tokens };\n }\n\n const exchangeRates = formatExchangeRatesForBridge({\n assetsPrice,\n selectedCurrency,\n nativeAssetIdentifiers,\n networkConfigurationsByChainId,\n });\n\n return {\n tokenBalances,\n accountsByChainId,\n allTokens,\n marketData: exchangeRates.marketData,\n currencyRates: exchangeRates.currencyRates,\n currentCurrency: exchangeRates.currentCurrency,\n };\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,26 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask-previews/assets-controller",
|
|
3
|
-
"version": "5.0.0-preview-
|
|
3
|
+
"version": "5.0.0-preview-60aa3d02f",
|
|
4
4
|
"description": "Tracks assets balances/prices and handles token detection across all digital assets",
|
|
5
5
|
"keywords": [
|
|
6
|
-
"
|
|
7
|
-
"
|
|
6
|
+
"MetaMask",
|
|
7
|
+
"Ethereum"
|
|
8
8
|
],
|
|
9
9
|
"homepage": "https://github.com/MetaMask/core/tree/main/packages/assets-controller#readme",
|
|
10
10
|
"bugs": {
|
|
11
11
|
"url": "https://github.com/MetaMask/core/issues"
|
|
12
12
|
},
|
|
13
|
-
"license": "MIT",
|
|
14
13
|
"repository": {
|
|
15
14
|
"type": "git",
|
|
16
15
|
"url": "https://github.com/MetaMask/core.git"
|
|
17
16
|
},
|
|
18
|
-
"
|
|
19
|
-
"dist/"
|
|
20
|
-
],
|
|
17
|
+
"license": "MIT",
|
|
21
18
|
"sideEffects": false,
|
|
22
|
-
"main": "./dist/index.cjs",
|
|
23
|
-
"types": "./dist/index.d.cts",
|
|
24
19
|
"exports": {
|
|
25
20
|
".": {
|
|
26
21
|
"import": {
|
|
@@ -34,10 +29,11 @@
|
|
|
34
29
|
},
|
|
35
30
|
"./package.json": "./package.json"
|
|
36
31
|
},
|
|
37
|
-
"
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
32
|
+
"main": "./dist/index.cjs",
|
|
33
|
+
"types": "./dist/index.d.cts",
|
|
34
|
+
"files": [
|
|
35
|
+
"dist/"
|
|
36
|
+
],
|
|
41
37
|
"scripts": {
|
|
42
38
|
"build": "ts-bridge --project tsconfig.build.json --verbose --clean --no-references",
|
|
43
39
|
"build:all": "ts-bridge --project tsconfig.build.json --verbose --clean",
|
|
@@ -75,7 +71,7 @@
|
|
|
75
71
|
"@metamask/preferences-controller": "^23.1.0",
|
|
76
72
|
"@metamask/snaps-controllers": "^19.0.0",
|
|
77
73
|
"@metamask/snaps-utils": "^12.1.2",
|
|
78
|
-
"@metamask/transaction-controller": "^64.
|
|
74
|
+
"@metamask/transaction-controller": "^64.0.0",
|
|
79
75
|
"@metamask/utils": "^11.9.0",
|
|
80
76
|
"async-mutex": "^0.5.0",
|
|
81
77
|
"bignumber.js": "^9.1.2",
|
|
@@ -83,7 +79,7 @@
|
|
|
83
79
|
"p-limit": "^3.1.0"
|
|
84
80
|
},
|
|
85
81
|
"devDependencies": {
|
|
86
|
-
"@metamask/auto-changelog": "^
|
|
82
|
+
"@metamask/auto-changelog": "^3.4.4",
|
|
87
83
|
"@ts-bridge/cli": "^0.6.4",
|
|
88
84
|
"@types/jest": "^29.5.14",
|
|
89
85
|
"@types/lodash": "^4.14.191",
|
|
@@ -97,5 +93,9 @@
|
|
|
97
93
|
},
|
|
98
94
|
"engines": {
|
|
99
95
|
"node": "^18.18 || >=20"
|
|
96
|
+
},
|
|
97
|
+
"publishConfig": {
|
|
98
|
+
"access": "public",
|
|
99
|
+
"registry": "https://registry.npmjs.org/"
|
|
100
100
|
}
|
|
101
101
|
}
|