Skip to content

Commit

Permalink
get shared assets
Browse files Browse the repository at this point in the history
  • Loading branch information
cuteolaf committed Aug 21, 2023
1 parent 09c594d commit a4122ae
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/consts/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type RELAY_CHAIN_OPTION = 'polkadot' | 'kusama';
export type RELAY_CHAIN_OPTION = 'polkadot' | 'kusama';
const RELAY_CHAIN_ENDPOINTS = {
polkadot: "wss://polkadot.api.onfinality.io/public-ws",
kusama: "wss://kusama.api.onfinality.io/public-ws"
Expand Down
29 changes: 28 additions & 1 deletion src/pages/transfer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ import {
CircularProgress,
FormControl,
FormLabel,
List,
ListItem,
MenuItem,
TextField,
} from '@mui/material';
import styles from '@styles/pages/transfer.module.scss';
import { useCallback, useEffect, useState } from 'react';

import AssetRegistry, { Asset } from '@/utils/assetRegistry';

import { RELAY_CHAIN } from '@/consts';
import { useRelayApi } from '@/contexts/RelayApi';
import { useToast } from '@/contexts/Toast';
import { useIdentity } from '@/contracts';
Expand All @@ -21,8 +26,9 @@ const TransferPage = () => {
const {
state: { api: relayApi },
} = useRelayApi();
const { toastSuccess, toastError } = useToast();
const { toastError } = useToast();
const [loadingAssets, setLoadingAssets] = useState(false);
const [assets, setAssets] = useState<Asset[]>([]);

const loadAssets = useCallback(async () => {
if (sourceChainId === undefined || destChainId === undefined) return;
Expand All @@ -38,7 +44,21 @@ const TransferPage = () => {
toastError(
"There's no HRMP channel open between the source and destination chain"
);
setAssets([]);
} else {
const _assets = await AssetRegistry.getSharedAssets(
RELAY_CHAIN,
sourceChainId,
destChainId
);
setAssets(_assets);
}
} else {
const _assets = await AssetRegistry.getAssetsOnBlockchain(
RELAY_CHAIN,
sourceChainId
);
setAssets(_assets);
}

setLoadingAssets(false);
Expand Down Expand Up @@ -85,6 +105,13 @@ const TransferPage = () => {
))}
</TextField>
</FormControl>
{!loadingAssets && (
<List>
{assets.map((asset, index) => (
<ListItem key={index}>{asset.name}</ListItem>
))}
</List>
)}
</Box>
<Backdrop
sx={{ color: '#fff', zIndex: (theme) => theme.zIndex.drawer + 1 }}
Expand Down
35 changes: 25 additions & 10 deletions src/utils/assetRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import axios from 'axios';

import { RELAY_CHAIN_OPTION } from '@/consts';
type ChainId = number | string;

type RelayChain = 'polkadot' | 'kusama';

type Asset = {
export type Asset = {
asset: any;
name: string;
symbol: string;
Expand All @@ -14,7 +13,7 @@ type Asset = {
confidence: number;
};

type MultiAsset = {
export type MultiAsset = {
parents: number;
interior:
| 'Here'
Expand All @@ -33,13 +32,13 @@ const xcmGAR =

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

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

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

public static async isSupportedOnBothChains(
relay: RelayChain,
relay: RELAY_CHAIN_OPTION,
chainA: ChainId,
chainB: ChainId,
asset: any
Expand All @@ -154,11 +153,11 @@ class AssetRegistry {
}

public static async isSupportedOnChain(
relay: RelayChain,
chain: ChainId,
relay: RELAY_CHAIN_OPTION,
chainId: ChainId,
asset: any
): Promise<boolean> {
const assets = await this.getAssetsOnBlockchain(relay, chain);
const assets = await this.getAssetsOnBlockchain(relay, chainId);

const found = assets.find(
(el: Asset) =>
Expand All @@ -170,6 +169,22 @@ class AssetRegistry {

return false;
}

public static async getSharedAssets(network: RELAY_CHAIN_OPTION, chainA: ChainId, chainB: ChainId): Promise<Asset[]> {
const assetsA = await this.getAssetsOnBlockchain(network, chainA);
const assetsB = await this.getAssetsOnBlockchain(network, chainB);
const assets: Asset[] = [];

assetsA.forEach((asset) => {
const found = assetsB.find(
(el: Asset) =>
el.xcmInteriorKey &&
JSON.stringify(el.xcmInteriorKey) === JSON.stringify(asset)
);
if (found) assets.push(asset);
});
return assets;
}
}

export default AssetRegistry;

0 comments on commit a4122ae

Please sign in to comment.