Skip to content

Commit

Permalink
Asset registry: Get all supported assets on two chains (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
Szegoo authored Aug 23, 2023
1 parent dde0fb3 commit c77568d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
38 changes: 37 additions & 1 deletion __tests__/assetRegistry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('AssetRegistry', () => {
]);
});

test('Getting assets by para id works', async () => {
test('Getting assets by para id works', async () => {
const assets = await AssetRegistry.getAssetsOnBlockchain(
'polkadot',
0
Expand Down Expand Up @@ -107,6 +107,42 @@ describe('AssetRegistry', () => {
expect(isUsdtSupported).toBe(true);
});

test("Getting all assets supported on both chains works", async () => {
const statemine = 1000;
const moonriver = 2023;
const assets = await AssetRegistry.assetsSupportedOnBothChains("kusama", statemine, moonriver);
expect(assets).toStrictEqual([
{
asset: { Token: "1984" },
currencyID: "1984",
decimals: 6,
name: "Tether USD",
symbol: "USDt",
xcmInteriorKey: [
{ network: "kusama", },
{ parachain: 1000, },
{ palletInstance: 50, },
{ generalIndex: 1984, },
],
},
{
asset: { Token: "8", },
confidence: 0,
currencyID: "8",
decimals: 10,
inferred: true,
name: "RMRK.app",
symbol: "RMRK",
xcmInteriorKey: [
{ network: "kusama", },
{ parachain: 1000, },
{ palletInstance: 50, },
{ generalIndex: 8, },
],
},
]);
}, 10000);

test("Getting assets of Kusama works", async () => {
const assets = await AssetRegistry.getAssetsOnBlockchain("kusama", "kusama");

Expand Down
30 changes: 28 additions & 2 deletions src/utils/assetRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,28 @@ class AssetRegistry {
return xcmInteriorKey.slice(1, junctionCount + 1);
}

public static async assetsSupportedOnBothChains(
relay: RelayChain,
chainA: ChainId,
chainB: ChainId,
): Promise<any[]> {
const assetsOnChainA = await this.getAssetsOnBlockchain(relay, chainA);
const assetsOnChainB = await this.getAssetsOnBlockchain(relay, chainB);

const assetsOnBoth: Asset[] = [];
for (let i = 0; i < assetsOnChainA.length; i++) {
const asset: Asset = assetsOnChainA[i];

const isSupported = this.isSupported(asset.xcmInteriorKey, assetsOnChainB);

if (isSupported) {
assetsOnBoth.push(asset);
}
}

return assetsOnBoth;
}

public static async isSupportedOnBothChains(
relay: RelayChain,
chainA: ChainId,
Expand All @@ -156,14 +178,18 @@ class AssetRegistry {
public static async isSupportedOnChain(
relay: RelayChain,
chain: ChainId,
asset: any
xcmAsset: any
): Promise<boolean> {
const assets = await this.getAssetsOnBlockchain(relay, chain);

return this.isSupported(xcmAsset, assets);
}

private static isSupported(xcmAsset: any, assets: Asset[]): boolean {
const found = assets.find(
(el: Asset) =>
el.xcmInteriorKey &&
JSON.stringify(el.xcmInteriorKey) === JSON.stringify(asset)
JSON.stringify(el.xcmInteriorKey) === JSON.stringify(xcmAsset)
);

if (found) return true;
Expand Down

0 comments on commit c77568d

Please sign in to comment.