Skip to content

Commit 99313eb

Browse files
committed
feat: assets-controllers extract and export getAssetId
1 parent e3d42e8 commit 99313eb

3 files changed

Lines changed: 54 additions & 22 deletions

File tree

packages/assets-controllers/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ export {
176176
SUPPORTED_CHAIN_IDS,
177177
getNativeTokenAddress,
178178
SPOT_PRICES_SUPPORT_INFO,
179+
getAssetId,
179180
} from './token-prices-service';
180181
export {
181182
fetchRwas,

packages/assets-controllers/src/token-prices-service/codefi-v2.ts

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,6 @@ export const ZERO_ADDRESS: Hex =
228228
*/
229229
const chainIdToNativeTokenAddress: Record<Hex, Hex> = {
230230
'0x89': '0x0000000000000000000000000000000000001010', // Polygon
231-
'0x1e': '0x542fda317318ebf1d3deaf76e0b632741a7e677d', // Rootstock Mainnet - Native symbol: RBTC
232231
'0x64': '0xe91d153e0b41518a2ce8dd3d7944fa863463a97d', // Gnosis
233232
'0x3dc': '0x779ded0c9e1022225f8e0630b35a9b54be713736', // Stable - Native symbol: USDT0
234233
'0x440': '0xdeaddeaddeaddeaddeaddeaddeaddeaddead0000', // Metis Andromeda
@@ -571,6 +570,52 @@ export function resetSupportedCurrenciesCache(): void {
571570
lastFetchedCurrencies = null;
572571
}
573572

573+
/**
574+
* Derives the CAIP-19 asset ID used to query the Price API for a token on a
575+
* given chain.
576+
*
577+
* For native tokens, uses the hardcoded {@link SPOT_PRICES_SUPPORT_INFO} entry
578+
* when defined, otherwise falls back to the provided native asset identifiers
579+
* (sourced from NetworkEnablementController). For ERC20 tokens, constructs the
580+
* CAIP-19 ID dynamically.
581+
*
582+
* @param args - The arguments to this function.
583+
* @param args.chainId - The hexadecimal chain ID the token lives on.
584+
* @param args.tokenAddress - The token's address.
585+
* @param args.nativeAssetIdentifiers - Map of CAIP-2 chain IDs to native asset
586+
* identifiers, used as a fallback for native tokens.
587+
* @returns The CAIP-19 asset ID, or undefined if it cannot be determined.
588+
*/
589+
export function getAssetId({
590+
chainId,
591+
tokenAddress,
592+
nativeAssetIdentifiers,
593+
}: {
594+
chainId: Hex;
595+
tokenAddress: string;
596+
nativeAssetIdentifiers: NativeAssetIdentifiersMap;
597+
}): CaipAssetType | undefined {
598+
const caipChainId = toCaipChainId(
599+
KnownCaipNamespace.Eip155,
600+
hexToNumber(chainId).toString(),
601+
);
602+
603+
const nativeAddress = getNativeTokenAddress(chainId);
604+
const isNativeToken =
605+
nativeAddress.toLowerCase() === tokenAddress.toLowerCase();
606+
607+
if (isNativeToken) {
608+
const hardcodedId = (
609+
SPOT_PRICES_SUPPORT_INFO as Partial<Record<Hex, string>>
610+
)[chainId];
611+
return (hardcodedId ?? nativeAssetIdentifiers[caipChainId]) as
612+
| CaipAssetType
613+
| undefined;
614+
}
615+
616+
return `${caipChainId}/erc20:${tokenAddress.toLowerCase()}` as CaipAssetType;
617+
}
618+
574619
/**
575620
* This version of the token prices service uses V2 of the Codefi Price API to
576621
* fetch token prices.
@@ -744,34 +789,19 @@ export class CodefiTokenPricesServiceV2 implements AbstractTokenPricesService<
744789
// Filter out assets that are not supported by V3 of the Price API.
745790
.filter((asset) => supportedChainIdsV3.includes(asset.chainId))
746791
.map((asset) => {
747-
const caipChainId = toCaipChainId(
748-
KnownCaipNamespace.Eip155,
749-
hexToNumber(asset.chainId).toString(),
750-
);
751-
752-
const nativeAddress = getNativeTokenAddress(asset.chainId);
753-
const isNativeToken =
754-
nativeAddress.toLowerCase() === asset.tokenAddress.toLowerCase();
755-
756-
let assetId: string | undefined;
757-
if (isNativeToken) {
758-
// For native tokens, use hardcoded SPOT_PRICES_SUPPORT_INFO when defined,
759-
// otherwise use nativeAssetIdentifiers from NetworkEnablementController by default.
760-
assetId =
761-
SPOT_PRICES_SUPPORT_INFO[asset.chainId] ??
762-
this.#nativeAssetIdentifiers[caipChainId];
763-
} else {
764-
// For ERC20 tokens, construct the CAIP-19 ID dynamically
765-
assetId = `${caipChainId}/erc20:${asset.tokenAddress.toLowerCase()}`;
766-
}
792+
const assetId = getAssetId({
793+
chainId: asset.chainId,
794+
tokenAddress: asset.tokenAddress,
795+
nativeAssetIdentifiers: this.#nativeAssetIdentifiers,
796+
});
767797

768798
if (!assetId) {
769799
return undefined;
770800
}
771801

772802
return {
773803
...asset,
774-
assetId: assetId as CaipAssetType,
804+
assetId,
775805
};
776806
})
777807
.filter(

packages/assets-controllers/src/token-prices-service/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ export {
1010
getSupportedNetworks,
1111
resetSupportedNetworksCache,
1212
SPOT_PRICES_SUPPORT_INFO,
13+
getAssetId,
1314
} from './codefi-v2';

0 commit comments

Comments
 (0)