Skip to content

Commit

Permalink
refactor methods of AssetRegistry (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
cuteolaf authored Aug 20, 2023
1 parent 86e7742 commit dde0fb3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
26 changes: 26 additions & 0 deletions __tests__/assetRegistry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,32 @@ describe('AssetRegistry', () => {
]);
});

test('Getting assets by para id works', async () => {
const assets = await AssetRegistry.getAssetsOnBlockchain(
'polkadot',
0
);

expect(assets).toStrictEqual([
{
asset: {
Token: 'DOT',
},
name: 'DOT',
symbol: 'DOT',
decimals: 10,
xcmInteriorKey: [
{
network: 'polkadot',
},
'here',
],
inferred: true,
confidence: 0,
},
]);
});

test("Checking whether an asset exists on both chains works", async () => {
const GLMR = [
{
Expand Down
28 changes: 16 additions & 12 deletions src/utils/assetRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import axios from 'axios';

type ChainId = number | string;

type RelayChain = 'polkadot' | 'kusama';

type Asset = {
asset: any;
name: string;
Expand Down Expand Up @@ -29,13 +33,13 @@ const xcmGAR =

class AssetRegistry {
public static async getAssetsOnBlockchain(
network: 'polkadot' | 'kusama',
chain: string
relay: RelayChain,
chain: ChainId
): Promise<Asset[]> {
const blockchains = (await axios.get(xcmGAR)).data;

const blockchain = blockchains.assets[network].find(
(b: any) => b.id.toLowerCase() == chain.toLowerCase()
const blockchain = blockchains.assets[relay].find(
(b: any) => (typeof chain === 'string') ? b.id.toLowerCase() == chain.toLowerCase() : b.paraID === chain
);

if (!blockchain) {
Expand Down Expand Up @@ -138,23 +142,23 @@ class AssetRegistry {
}

public static async isSupportedOnBothChains(
network: 'polkadot' | 'kusama',
chainA: string,
chainB: string,
relay: RelayChain,
chainA: ChainId,
chainB: ChainId,
asset: any
): Promise<boolean> {
const foundOnChainA = await this.isSupportedOnChain(network, chainA, asset);
const foundOnChainB = await this.isSupportedOnChain(network, chainB, asset);
const foundOnChainA = await this.isSupportedOnChain(relay, chainA, asset);
const foundOnChainB = await this.isSupportedOnChain(relay, chainB, asset);

return foundOnChainA && foundOnChainB;
}

public static async isSupportedOnChain(
network: 'polkadot' | 'kusama',
chain: string,
relay: RelayChain,
chain: ChainId,
asset: any
): Promise<boolean> {
const assets = await this.getAssetsOnBlockchain(network, chain);
const assets = await this.getAssetsOnBlockchain(relay, chain);

const found = assets.find(
(el: Asset) =>
Expand Down

0 comments on commit dde0fb3

Please sign in to comment.