Skip to content

Commit

Permalink
Merge pull request #32 from vkulinich-cl/update-external-token
Browse files Browse the repository at this point in the history
Update External Tokens
  • Loading branch information
nohaapav authored Mar 21, 2024
2 parents 64029e5 + 82098aa commit 35f1959
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 30 deletions.
67 changes: 42 additions & 25 deletions packages/sdk/src/client/AssetClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, AssetBase } from '../types';
import { Asset, AssetMetadata, ExternalAsset } from '../types';
import { findNestedKey } from '../utils/json';

import { PolkadotApiClient } from './PolkadotApi';
Expand Down Expand Up @@ -112,7 +112,7 @@ export class AssetClient extends PolkadotApiClient {
tokenKey: string,
details: PalletAssetRegistryAssetDetails,
metadata: Map<string, AssetMetadata>,
locations?: Map<string, HydradxRuntimeXcmAssetLocation>
location?: HydradxRuntimeXcmAssetLocation
): Asset {
if (tokenKey == SYSTEM_ASSET_ID) {
const defaultAssetEd = this.api.consts.balances.existentialDeposit;
Expand All @@ -129,7 +129,6 @@ export class AssetClient extends PolkadotApiClient {

const { name, assetType, existentialDeposit } = details;
const { symbol, decimals } = metadata.get(tokenKey) ?? {};
const location = locations ? locations.get(tokenKey) : undefined;

return {
id: tokenKey,
Expand All @@ -138,7 +137,7 @@ export class AssetClient extends PolkadotApiClient {
decimals: decimals,
icon: symbol,
type: assetType.toHuman(),
origin: location && this.parseLocation(location),
origin: location && this.parseLocation(location, 'parachain'),
existentialDeposit: existentialDeposit.toString(),
} as Asset;
}
Expand Down Expand Up @@ -199,7 +198,7 @@ export class AssetClient extends PolkadotApiClient {
} as Asset;
}

async getOnChainAssets(external?: AssetBase[]): Promise<Asset[]> {
async getOnChainAssets(external?: ExternalAsset[]): Promise<Asset[]> {
const [asset, assetLocations, shares, bonds, assetMetadata] =
await Promise.all([
this.api.query.assetRegistry.assets.entries(),
Expand All @@ -220,8 +219,17 @@ export class AssetClient extends PolkadotApiClient {
return false;
}
const details = state.unwrap();
const ext = external?.find((a) => a.id === id.toString());
return this.isSupportedAsset(details, ext);

if (external && details.assetType.toString() === 'External') {
const location = assetLocations.get(id.toString());
const index =
location && this.parseLocation(location, 'generalIndex');
const ext = external.find((ext) => ext.id === index?.toString());

return !!ext;
}

return this.isSupportedAsset(details);
}
);

Expand Down Expand Up @@ -255,6 +263,8 @@ export class AssetClient extends PolkadotApiClient {
value,
]) => {
const details: PalletAssetRegistryAssetDetails = value.unwrap();
const location = assetLocations.get(id.toString());

const { assetType } = details;
switch (assetType.toString()) {
case 'Bond':
Expand All @@ -264,42 +274,49 @@ export class AssetClient extends PolkadotApiClient {
const share = shares.get(id.toString());
return this.getShares(id.toString(), details, assetsMeta, share!);
case 'External':
const base = external?.find((a) => a.id === id.toString());
const ext = external?.find((a) => {
const index =
location && this.parseLocation(location, 'generalIndex');

return a.id === index?.toString();
});

const token = this.getTokens(
id.toString(),
details,
new Map(),
assetLocations
location
);
return { ...token, ...base };

return ext
? {
...token,
decimals: ext.decimals,
name: ext.name,
symbol: ext.symbol,
}
: token;
default:
return this.getTokens(
id.toString(),
details,
assetsMeta,
assetLocations
);
return this.getTokens(id.toString(), details, assetsMeta, location);
}
}
);
}

private isSupportedAsset(
details: PalletAssetRegistryAssetDetails,
external?: AssetBase
): boolean {
private isSupportedAsset(details: PalletAssetRegistryAssetDetails): boolean {
const type = details.assetType.toString();
const isSupported = this.SUPPORTED_TYPES.includes(type);
const isExternal = !!external && type === 'External';
return isSupported || isExternal;

return isSupported;
}

private parseLocation(
location: HydradxRuntimeXcmAssetLocation
location: HydradxRuntimeXcmAssetLocation,
key: string
): number | undefined {
if (location) {
const entry = findNestedKey(location.toJSON(), 'parachain');
return entry && entry.parachain;
const entry = findNestedKey(location.toJSON(), key);
return entry && entry[key];
} else {
return undefined;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/src/pool/PoolService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
PoolFees,
Pool,
Asset,
AssetBase,
ExternalAsset,
} from '../types';
import { BigNumber } from '../utils/bignumber';

Expand Down Expand Up @@ -43,7 +43,7 @@ export class PoolService implements IPoolService {
this.stableClient = new StableSwapClient(this.api);
}

async syncRegistry(external?: AssetBase[]) {
async syncRegistry(external?: ExternalAsset[]) {
this.onChainAssets = await this.assetClient.getOnChainAssets(external);
this.onChainAssetsLoaded = true;
}
Expand Down
7 changes: 5 additions & 2 deletions packages/sdk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,19 @@ export type Amount = {
decimals: number;
};

export interface Asset extends AssetBase {
export interface Asset extends AssetMetadata {
id: string;
name: string;
icon: string;
type: string;
existentialDeposit: string;
origin?: number;
meta?: Record<string, string>;
}

export interface AssetBase extends AssetMetadata {
export interface ExternalAsset extends AssetMetadata {
id: string;
origin: number;
name: string;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class GetAllAssetsExample extends PolkadotExecutor {
const external = [
{
decimals: 8,
id: '1000008',
origin: 1000,
id: '666',
name: 'Danger Coin',
symbol: 'DANGER',
},
Expand Down

0 comments on commit 35f1959

Please sign in to comment.