-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sdk): Add asset search by multi-location ✨
- Loading branch information
1 parent
a4db9c3
commit 68fc5fb
Showing
16 changed files
with
7,319 additions
and
5,574 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import type { TAssetJsonMap, TForeignAsset, TNode } from '../../src/types' | ||
import { getNode } from '../../src/utils' | ||
import type { TRegistryAssets } from './fetchXcmRegistry' | ||
import { fetchXcmRegistry } from './fetchXcmRegistry' | ||
|
||
import assetsMapJson from '../../src/maps/assets.json' assert { type: 'json' } | ||
const assetsMap = assetsMapJson as TAssetJsonMap | ||
|
||
export const fetchAssets = async (node: TNode): Promise<TRegistryAssets[]> => { | ||
const data = await fetchXcmRegistry() | ||
|
||
const paraId = assetsMap[node].paraId | ||
const relay = getNode(node).type | ||
|
||
const isAssetHub = node === 'AssetHubPolkadot' || node === 'AssetHubKusama' | ||
|
||
const assets = data[isAssetHub ? 'assets' : 'xcAssets'][relay].find( | ||
item => item.paraID === paraId | ||
)?.data | ||
|
||
if (!assets) { | ||
throw new Error(`No assets found for ${node}`) | ||
} | ||
|
||
return isAssetHub ? assets.filter(asset => !!asset.xcmInteriorKey) : assets | ||
} | ||
|
||
export const fetchOtherAssetsRegistry = async (node: TNode): Promise<TForeignAsset[]> => { | ||
const assets = await fetchAssets(node) | ||
|
||
const isAssetHub = node === 'AssetHubPolkadot' || node === 'AssetHubKusama' | ||
|
||
return assets | ||
.filter(item => item.currencyID !== 'Native') | ||
.map(item => { | ||
const assetField = item.currencyID ?? item.asset | ||
const id = | ||
typeof assetField === 'string' ? assetField : (Object.values(assetField)[0] as string) | ||
const sanitizedId = id.replace(',', '') | ||
return { | ||
assetId: sanitizedId, | ||
symbol: item.symbol, | ||
decimals: item.decimals, | ||
multiLocation: item.xcmV1MultiLocation, | ||
...(isAssetHub && item.xcmInteriorKey | ||
? { xcmInterior: JSON.parse(item.xcmInteriorKey) as object } | ||
: {}) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import axios from 'axios' | ||
|
||
export type TAssets = { | ||
polkadot: TRegistryAssetData[] | ||
kusama: TRegistryAssetData[] | ||
} | ||
|
||
export type TRegistryAssetData = { | ||
relayChain: string | ||
paraID: number | ||
id: string | ||
xcAssetCnt: string | ||
data: TRegistryAssets[] | ||
} | ||
|
||
export type TRegistryAssets = { | ||
paraID: number | ||
relayChain: string | ||
nativeChainID: string | null | ||
symbol: string | ||
decimals: number | ||
xcmV1MultiLocation: object | ||
asset: object | string | ||
currencyID: string | undefined | ||
xcmInteriorKey: string | ||
} | ||
|
||
export const fetchXcmRegistry = async (): Promise<{ xcAssets: TAssets; assets: TAssets }> => { | ||
const response = await axios.get<{ xcAssets: TAssets; assets: TAssets }>( | ||
'https://cdn.jsdelivr.net/gh/colorfulnotion/xcm-global-registry/metadata/xcmgar.json' | ||
) | ||
return response.data | ||
} |
Oops, something went wrong.