Skip to content

Commit

Permalink
Merge pull request #31 from vkulinich-cl/add-external-tokens
Browse files Browse the repository at this point in the history
Add on chain external tokens
  • Loading branch information
nohaapav authored Mar 20, 2024
2 parents d903dbd + cd9e76c commit 15ac878
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 13 deletions.
44 changes: 38 additions & 6 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 } from '../types';
import { Asset, AssetMetadata, AssetBase } from '../types';
import { findNestedKey } from '../utils/json';

import { PolkadotApiClient } from './PolkadotApi';
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -199,7 +199,7 @@ export class AssetClient extends PolkadotApiClient {
} as Asset;
}

async getOnChainAssets(): Promise<Asset[]> {
async getOnChainAssets(externalAssetsMeta?: AssetBase[]): Promise<Asset[]> {
const [asset, assetLocations, shares, bonds, assetMetadata] =
await Promise.all([
this.api.query.assetRegistry.assets.entries(),
Expand All @@ -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<string, AssetMetadata> = assetMetadata.size
? assetMetadata
Expand Down Expand Up @@ -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(),
Expand Down
10 changes: 8 additions & 2 deletions packages/sdk/src/pool/PoolService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
PoolFees,
Pool,
Asset,
PoolServiceOptions,
AssetBase,
} from '../types';
import { BigNumber } from '../utils/bignumber';

Expand All @@ -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<PoolBase[]> {
if (!this.onChainAssetsLoaded) {
this.onChainAssets = await this.assetClient.getOnChainAssets();
this.onChainAssets = await this.assetClient.getOnChainAssets(
this.externalAssetsMeta
);
this.onChainAssetsLoaded = true;
}

Expand Down
15 changes: 10 additions & 5 deletions packages/sdk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,19 +165,24 @@ 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;
origin?: number;
meta?: Record<string, string>;
}

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

export interface AssetMetadata {
decimals: number;
symbol: string;
}

export type PoolServiceOptions = {
externalAssets: AssetBase[];
};

0 comments on commit 15ac878

Please sign in to comment.