diff --git a/packages/sdk/src/client/AssetClient.ts b/packages/sdk/src/client/AssetClient.ts index 32a1ca0a..0d6c2b04 100644 --- a/packages/sdk/src/client/AssetClient.ts +++ b/packages/sdk/src/client/AssetClient.ts @@ -8,7 +8,7 @@ import { ITuple } from '@polkadot/types-codec/types'; import { u32, u64 } from '@polkadot/types-codec'; import { ApiPromise } from '@polkadot/api'; import { SYSTEM_ASSET_ID } from '../consts'; -import { Asset, AssetMetadata } from '../types'; +import { Asset, AssetMetadata, AssetBase } from '../types'; import { findNestedKey } from '../utils/json'; import { PolkadotApiClient } from './PolkadotApi'; @@ -128,7 +128,7 @@ export class AssetClient extends PolkadotApiClient { } const { name, assetType, existentialDeposit } = details; - const { symbol, decimals } = metadata.get(tokenKey)!; + const { symbol, decimals } = metadata.get(tokenKey) ?? {}; const location = locations ? locations.get(tokenKey) : undefined; return { @@ -199,7 +199,7 @@ export class AssetClient extends PolkadotApiClient { } as Asset; } - async getOnChainAssets(): Promise { + async getOnChainAssets(externalAssetsMeta?: AssetBase[]): Promise { const [asset, assetLocations, shares, bonds, assetMetadata] = await Promise.all([ this.api.query.assetRegistry.assets.entries(), @@ -209,9 +209,28 @@ export class AssetClient extends PolkadotApiClient { this.metadataQuery(), ]); - const filteredAssets = asset.filter( - ([_args, state]) => !state.isNone && this.isSupportedType(state.unwrap()) - ); + const filteredAssets = asset.filter(([_args, state]) => { + if (state.isNone) return false; + const details = state.unwrap(); + + if (this.isSupportedType(details)) return true; + + if ( + externalAssetsMeta?.length && + details.assetType.toString() === 'External' + ) { + const id = _args.args[0]; + const meta = externalAssetsMeta.find( + (assetMeta) => assetMeta.id === id.toString() + ); + + if (meta) return true; + + return false; + } + + return false; + }); const assetsMeta: Map = assetMetadata.size ? assetMetadata @@ -251,6 +270,19 @@ export class AssetClient extends PolkadotApiClient { case 'StableSwap': const share = shares.get(id.toString()); return this.getShares(id.toString(), details, assetsMeta, share!); + case 'External': + const token = this.getTokens( + id.toString(), + details, + new Map(), + assetLocations + ); + + const meta = externalAssetsMeta?.find( + (assetMeta) => assetMeta.id === id.toString() + ); + + if (meta) return { ...token, ...meta }; default: return this.getTokens( id.toString(), diff --git a/packages/sdk/src/pool/PoolService.ts b/packages/sdk/src/pool/PoolService.ts index e53a9d6d..e494f81c 100644 --- a/packages/sdk/src/pool/PoolService.ts +++ b/packages/sdk/src/pool/PoolService.ts @@ -15,6 +15,8 @@ import { PoolFees, Pool, Asset, + PoolServiceOptions, + AssetBase, } from '../types'; import { BigNumber } from '../utils/bignumber'; @@ -31,20 +33,24 @@ export class PoolService implements IPoolService { protected readonly stableClient: StableSwapClient; protected onChainAssets: Asset[] = []; + protected externalAssetsMeta: AssetBase[] | undefined = []; protected onChainAssetsLoaded = false; - constructor(api: ApiPromise) { + constructor(api: ApiPromise, options?: PoolServiceOptions) { this.api = api; this.assetClient = new AssetClient(this.api); this.xykClient = new XykPoolClient(this.api); this.omniClient = new OmniPoolClient(this.api); this.lbpClient = new LbpPoolClient(this.api); this.stableClient = new StableSwapClient(this.api); + this.externalAssetsMeta = options?.externalAssets; } async getPools(includeOnly: PoolType[]): Promise { if (!this.onChainAssetsLoaded) { - this.onChainAssets = await this.assetClient.getOnChainAssets(); + this.onChainAssets = await this.assetClient.getOnChainAssets( + this.externalAssetsMeta + ); this.onChainAssetsLoaded = true; } diff --git a/packages/sdk/src/types.ts b/packages/sdk/src/types.ts index 006ce97c..c75a17c8 100644 --- a/packages/sdk/src/types.ts +++ b/packages/sdk/src/types.ts @@ -165,11 +165,7 @@ export type Amount = { decimals: number; }; -export interface Asset { - id: string; - decimals: number; - name: string; - symbol: string; +export interface Asset extends AssetBase { icon: string; type: string; existentialDeposit: string; @@ -177,7 +173,16 @@ export interface Asset { meta?: Record; } +export interface AssetBase extends AssetMetadata { + id: string; + name: string; +} + export interface AssetMetadata { decimals: number; symbol: string; } + +export type PoolServiceOptions = { + externalAssets: AssetBase[]; +};