From dacfd560dcfce2e747cf0c1f33f4e799b63986ff Mon Sep 17 00:00:00 2001 From: vkulinich Date: Sun, 21 Jan 2024 16:25:21 +0100 Subject: [PATCH 1/2] Support new Asset Registry --- packages/sdk/src/client/AssetClient.ts | 134 +++++++++++++++---------- packages/sdk/src/types.ts | 5 + 2 files changed, 84 insertions(+), 55 deletions(-) diff --git a/packages/sdk/src/client/AssetClient.ts b/packages/sdk/src/client/AssetClient.ts index 0f323123..aee39894 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 } from '../types'; +import { Asset, AssetMetadata } from '../types'; import { findNestedKey } from '../utils/json'; import { PolkadotApiClient } from './PolkadotApi'; @@ -58,9 +58,7 @@ export class AssetClient extends PolkadotApiClient { } } - async metadataQuery(): Promise< - Map - > { + async metadataQuery(): Promise> { try { const entries = await this.api.query.assetRegistry.assetMetadataMap.entries(); @@ -72,7 +70,17 @@ export class AssetClient extends PolkadotApiClient { }, value, ]) => { - return [id.toString(), value.unwrap()]; + { + const data = value.unwrap(); + + return [ + id.toString(), + { + decimals: data.decimals.toNumber(), + symbol: data.symbol.toHuman() as string, + }, + ]; + } } ) ); @@ -105,7 +113,7 @@ export class AssetClient extends PolkadotApiClient { private getTokens( tokenKey: string, details: PalletAssetRegistryAssetDetails, - metadata: Map, + metadata: Map, locations?: Map ): Asset { if (tokenKey == SYSTEM_ASSET_ID) { @@ -128,9 +136,9 @@ export class AssetClient extends PolkadotApiClient { return { id: tokenKey, name: name.toHuman(), - symbol: symbol.toHuman(), - decimals: decimals.toNumber(), - icon: symbol.toHuman(), + symbol: symbol, + decimals: decimals, + icon: symbol, type: assetType.toHuman(), origin: location && this.parseLocation(location), existentialDeposit: existentialDeposit.toString(), @@ -140,7 +148,7 @@ export class AssetClient extends PolkadotApiClient { private getBonds( tokenKey: string, details: PalletAssetRegistryAssetDetails, - metadata: Map, + metadata: Map, bond: ITuple<[u32, u64]> ): Asset { const [underlyingAsset, maturity] = bond; @@ -169,7 +177,7 @@ export class AssetClient extends PolkadotApiClient { private getShares( tokenKey: string, details: PalletAssetRegistryAssetDetails, - metadata: Map, + metadata: Map, share: PalletStableswapPoolInfo ): Asset { const { assets } = share; @@ -194,53 +202,69 @@ export class AssetClient extends PolkadotApiClient { } async getOnChainAssets(): Promise { - const [asset, assetMetadata, assetLocations, shares, bonds] = - await Promise.all([ - this.api.query.assetRegistry.assets.entries(), - this.metadataQuery(), - this.locationsQuery(), - this.safeSharesQuery(), - this.safeBondsQuery(), - ]); + const assetMetadata = await this.metadataQuery(); - return asset - .filter(([_args, state]) => this.isSupportedType(state.unwrap())) - .map( - ([ - { - args: [id], - }, - value, - ]) => { - const details: PalletAssetRegistryAssetDetails = value.unwrap(); - const { assetType } = details; - switch (assetType.toString()) { - case 'Bond': - const bond = bonds.get(id.toString()); - return this.getBonds( - id.toString(), - details, - assetMetadata, - bond! - ); - case 'StableSwap': - const share = shares.get(id.toString()); - return this.getShares( - id.toString(), - details, - assetMetadata, - share! - ); - default: - return this.getTokens( + const [asset, assetLocations, shares, bonds] = await Promise.all([ + this.api.query.assetRegistry.assets.entries(), + this.locationsQuery(), + this.safeSharesQuery(), + this.safeBondsQuery(), + ]); + + const filteredAssets = asset.filter( + ([_args, state]) => !state.isNone && this.isSupportedType(state.unwrap()) + ); + + const assetsMeta: Map = assetMetadata.size + ? assetMetadata + : new Map( + filteredAssets.map( + ([ + { + args: [id], + }, + value, + ]) => { + const data = value.unwrap(); + return [ id.toString(), - details, - assetMetadata, - assetLocations - ); - } + { + //@ts-ignore + decimals: Number(data.decimals.toString()), + //@ts-ignore + symbol: data.symbol.toHuman(), + }, + ]; + } + ) + ); + + return filteredAssets.map( + ([ + { + args: [id], + }, + value, + ]) => { + const details: PalletAssetRegistryAssetDetails = value.unwrap(); + const { assetType } = details; + switch (assetType.toString()) { + case 'Bond': + const bond = bonds.get(id.toString()); + return this.getBonds(id.toString(), details, assetsMeta, bond!); + case 'StableSwap': + const share = shares.get(id.toString()); + return this.getShares(id.toString(), details, assetsMeta, share!); + default: + return this.getTokens( + id.toString(), + details, + assetsMeta, + assetLocations + ); } - ); + } + ); } private isSupportedType(details: PalletAssetRegistryAssetDetails): boolean { diff --git a/packages/sdk/src/types.ts b/packages/sdk/src/types.ts index 3b2e1e6a..006ce97c 100644 --- a/packages/sdk/src/types.ts +++ b/packages/sdk/src/types.ts @@ -176,3 +176,8 @@ export interface Asset { origin?: number; meta?: Record; } + +export interface AssetMetadata { + decimals: number; + symbol: string; +} From 37fbde74ef95761d12e0fd41c21ebc798379c9cb Mon Sep 17 00:00:00 2001 From: vkulinich Date: Tue, 23 Jan 2024 14:37:21 +0100 Subject: [PATCH 2/2] Address PR comments --- packages/sdk/src/client/AssetClient.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/sdk/src/client/AssetClient.ts b/packages/sdk/src/client/AssetClient.ts index aee39894..b9d68c9c 100644 --- a/packages/sdk/src/client/AssetClient.ts +++ b/packages/sdk/src/client/AssetClient.ts @@ -202,14 +202,14 @@ export class AssetClient extends PolkadotApiClient { } async getOnChainAssets(): Promise { - const assetMetadata = await this.metadataQuery(); - - const [asset, assetLocations, shares, bonds] = await Promise.all([ - this.api.query.assetRegistry.assets.entries(), - this.locationsQuery(), - this.safeSharesQuery(), - this.safeBondsQuery(), - ]); + const [asset, assetLocations, shares, bonds, assetMetadata] = + await Promise.all([ + this.api.query.assetRegistry.assets.entries(), + this.locationsQuery(), + this.safeSharesQuery(), + this.safeBondsQuery(), + this.metadataQuery(), + ]); const filteredAssets = asset.filter( ([_args, state]) => !state.isNone && this.isSupportedType(state.unwrap())