Skip to content

Commit

Permalink
added external asset test script
Browse files Browse the repository at this point in the history
  • Loading branch information
nohaapav committed Mar 20, 2024
1 parent 15ac878 commit 6071a2b
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 56 deletions.
73 changes: 25 additions & 48 deletions packages/sdk/src/client/AssetClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export class AssetClient extends PolkadotApiClient {
} as Asset;
}

async getOnChainAssets(externalAssetsMeta?: AssetBase[]): Promise<Asset[]> {
async getOnChainAssets(external?: AssetBase[]): Promise<Asset[]> {
const [asset, assetLocations, shares, bonds, assetMetadata] =
await Promise.all([
this.api.query.assetRegistry.assets.entries(),
Expand All @@ -209,28 +209,21 @@ export class AssetClient extends PolkadotApiClient {
this.metadataQuery(),
]);

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;
const filteredAssets = asset.filter(
([
{
args: [id],
},
state,
]) => {
if (state.isNone) {
return false;
}
const details = state.unwrap();
const ext = external?.find((a) => a.id === id.toString());
return this.isSupportedAsset(details, ext);
}

return false;
});
);

const assetsMeta: Map<string, AssetMetadata> = assetMetadata.size
? assetMetadata
Expand Down Expand Up @@ -271,18 +264,14 @@ 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 token = this.getTokens(
id.toString(),
details,
new Map(),
assetLocations
);

const meta = externalAssetsMeta?.find(
(assetMeta) => assetMeta.id === id.toString()
);

if (meta) return { ...token, ...meta };
return { ...token, ...base };
default:
return this.getTokens(
id.toString(),
Expand All @@ -295,8 +284,14 @@ export class AssetClient extends PolkadotApiClient {
);
}

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

private parseLocation(
Expand All @@ -309,22 +304,4 @@ export class AssetClient extends PolkadotApiClient {
return undefined;
}
}

private parseMeta(
details: PalletAssetRegistryAssetDetails,
metadata?: PalletAssetRegistryAssetMetadata
): AssetMetadata {
const symbol = details.symbol.isSome
? details.symbol.toHuman()
: metadata?.symbol.toHuman();

const decimals = details.decimals.isSome
? details.decimals.toHuman()
: metadata?.decimals.toHuman();

return {
decimals: Number(decimals),
symbol,
} as AssetMetadata;
}
}
18 changes: 14 additions & 4 deletions packages/sdk/src/pool/PoolService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ import {
PoolFees,
Pool,
Asset,
PoolServiceOptions,
AssetBase,
} from '../types';
import { BigNumber } from '../utils/bignumber';

import { ApiPromise } from '@polkadot/api';
import { SubmittableExtrinsic } from '@polkadot/api/promise/types';

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

export class PoolService implements IPoolService {
protected readonly api: ApiPromise;
protected readonly assetClient: AssetClient;
Expand All @@ -32,8 +35,12 @@ export class PoolService implements IPoolService {
protected readonly lbpClient: LbpPoolClient;
protected readonly stableClient: StableSwapClient;

protected readonly options: PoolServiceOptions;
protected readonly defaultOptions: PoolServiceOptions = {
externalAssets: [],
};

protected onChainAssets: Asset[] = [];
protected externalAssetsMeta: AssetBase[] | undefined = [];
protected onChainAssetsLoaded = false;

constructor(api: ApiPromise, options?: PoolServiceOptions) {
Expand All @@ -43,13 +50,16 @@ export class PoolService implements IPoolService {
this.omniClient = new OmniPoolClient(this.api);
this.lbpClient = new LbpPoolClient(this.api);
this.stableClient = new StableSwapClient(this.api);
this.externalAssetsMeta = options?.externalAssets;
this.options = {
...this.defaultOptions,
...options,
};
}

async getPools(includeOnly: PoolType[]): Promise<PoolBase[]> {
if (!this.onChainAssetsLoaded) {
this.onChainAssets = await this.assetClient.getOnChainAssets(
this.externalAssetsMeta
this.options.externalAssets
);
this.onChainAssetsLoaded = true;
}
Expand Down
4 changes: 0 additions & 4 deletions packages/sdk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,3 @@ export interface AssetMetadata {
decimals: number;
symbol: string;
}

export type PoolServiceOptions = {
externalAssets: AssetBase[];
};
27 changes: 27 additions & 0 deletions packages/sdk/test/script/examples/router/withExternalAssets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { ApiPromise } from '@polkadot/api';
import { ApiUrl, PolkadotExecutor } from '../../executor';
import { PoolService } from '../../../../src/pool';
import { TradeRouter } from '../../../../src/api';

class GetAllAssetsExample extends PolkadotExecutor {
async script(api: ApiPromise): Promise<any> {
const external = [
{
decimals: 8,
id: '1000008',
name: 'Danger Coin',
symbol: 'DANGER',
},
];

const poolService = new PoolService(api, { externalAssets: external });
const router = new TradeRouter(poolService);
return router.getAllAssets();
}
}

new GetAllAssetsExample(
ApiUrl.Nice,
'Get all assets (external included)',
true
).run();

0 comments on commit 6071a2b

Please sign in to comment.