Skip to content

Commit

Permalink
fix: remove getChainIdBySpecName and add caching system (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
TarikGul authored Sep 19, 2023
1 parent 69e26ac commit abb5052
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 86 deletions.
3 changes: 1 addition & 2 deletions src/AssetTransferApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
import { establishXcmPallet, XcmPalletName } from './createXcmCalls/util/establishXcmPallet';
import { assetIdsContainRelayAsset } from './createXcmTypes/util/assetIdsContainsRelayAsset';
import { getAssetId } from './createXcmTypes/util/getAssetId';
import { getChainIdBySpecName } from './createXcmTypes/util/getChainIdBySpecName';
import { isParachainPrimaryNativeAsset } from './createXcmTypes/util/isParachainPrimaryNativeAsset';
import { isSystemChain } from './createXcmTypes/util/isSystemChain';
import { multiLocationAssetIsParachainsNativeAsset } from './createXcmTypes/util/multiLocationAssetIsParachainsNativeAsset';
Expand Down Expand Up @@ -161,7 +160,7 @@ export class AssetTransferApi {
*/
const addr = sanitizeAddress(destAddr);

const originChainId = getChainIdBySpecName(registry, _specName);
const originChainId = registry.lookupChainIdBySpecName(_specName);
const isLocalSystemTx = isOriginSystemParachain && isDestSystemParachain && originChainId === destChainId;
const isLocalRelayTx = destChainId === '0' && RELAY_CHAIN_NAMES.includes(_specName.toLowerCase());
const relayChainID = RELAY_CHAIN_IDS[0];
Expand Down
5 changes: 2 additions & 3 deletions src/createXcmTypes/SystemToPara.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import type {
import type { XcmV3MultiassetMultiAssets } from '@polkadot/types/lookup';
import { isEthereumAddress } from '@polkadot/util-crypto';

import { getChainIdBySpecName } from '../createXcmTypes/util/getChainIdBySpecName';
import { BaseError, BaseErrorsEnum } from '../errors';
import type { Registry } from '../registry';
import { MultiAsset } from '../types';
Expand Down Expand Up @@ -205,7 +204,7 @@ export const SystemToPara: ICreateXcmType = {
isLiquidTokenTransfer
);

const systemChainId = getChainIdBySpecName(registry, specName);
const systemChainId = registry.lookupChainIdBySpecName(specName);
if (!isSystemChain(systemChainId)) {
throw new BaseError(
`specName ${specName} did not match a valid system chain ID. Found ID ${systemChainId}`,
Expand Down Expand Up @@ -248,7 +247,7 @@ export const createSystemToParaMultiAssets = async (
): Promise<MultiAsset[]> => {
let multiAssets: MultiAsset[] = [];
const palletId = fetchPalletInstanceId(api, isLiquidTokenTransfer, isForeignAssetsTransfer);
const systemChainId = getChainIdBySpecName(registry, specName);
const systemChainId = registry.lookupChainIdBySpecName(specName);

if (!isSystemChain(systemChainId)) {
throw new BaseError(
Expand Down
5 changes: 2 additions & 3 deletions src/createXcmTypes/SystemToSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import type {
} from '@polkadot/types/interfaces';
import type { XcmV3MultiassetMultiAssets } from '@polkadot/types/lookup';

import { getChainIdBySpecName } from '../createXcmTypes/util/getChainIdBySpecName';
import { BaseError, BaseErrorsEnum } from '../errors';
import type { Registry } from '../registry';
import { getFeeAssetItemIndex } from '../util/getFeeAssetItemIndex';
Expand Down Expand Up @@ -198,7 +197,7 @@ export const SystemToSystem: ICreateXcmType = {
isLiquidTokenTransfer
);

const systemChainId = getChainIdBySpecName(registry, specName);
const systemChainId = registry.lookupChainIdBySpecName(specName);

if (!isSystemChain(systemChainId)) {
throw new BaseError(
Expand Down Expand Up @@ -242,7 +241,7 @@ export const createSystemToSystemMultiAssets = async (
isLiquidTokenTransfer: boolean
): Promise<MultiAsset[]> => {
let multiAssets: MultiAsset[] = [];
const systemChainId = getChainIdBySpecName(registry, specName);
const systemChainId = registry.lookupChainIdBySpecName(specName);
const palletId = fetchPalletInstanceId(api, isLiquidTokenTransfer, isForeignAssetsTransfer);

if (!isSystemChain(systemChainId)) {
Expand Down
3 changes: 1 addition & 2 deletions src/createXcmTypes/util/getAssetId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { Registry } from '../../registry';
import { validateNumber } from '../../validate';
import { foreignAssetMultiLocationIsInCacheOrRegistry } from './foreignAssetMultiLocationIsInCacheOrRegistry';
import { foreignAssetsMultiLocationExists } from './foreignAssetsMultiLocationExists';
import { getChainIdBySpecName } from './getChainIdBySpecName';

/**
*
Expand All @@ -30,7 +29,7 @@ export const getAssetId = async (
specName: string,
isForeignAssetsTransfer?: boolean
): Promise<string> => {
const currentChainId = getChainIdBySpecName(registry, specName);
const currentChainId = registry.lookupChainIdBySpecName(specName);
const assetIsValidInt = validateNumber(asset);
const isParachain = new BN(currentChainId).gte(new BN(2000));

Expand Down
24 changes: 0 additions & 24 deletions src/createXcmTypes/util/getChainIdBySpecName.spec.ts

This file was deleted.

34 changes: 0 additions & 34 deletions src/createXcmTypes/util/getChainIdBySpecName.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/createXcmTypes/util/isParachainPrimaryNativeAsset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import { Registry } from '../../registry';
import { Direction } from '../../types';
import { getChainIdBySpecName } from './getChainIdBySpecName';

export const isParachainPrimaryNativeAsset = (
registry: Registry,
Expand All @@ -26,7 +25,7 @@ export const isParachainPrimaryNativeAsset = (
return true;
}

const currentChainId = getChainIdBySpecName(registry, specName);
const currentChainId = registry.lookupChainIdBySpecName(specName);
const { tokens } = registry.currentRelayRegistry[currentChainId];
const primaryParachainNativeAsset = tokens[0];
if (primaryParachainNativeAsset.toLowerCase() === assetId.toLowerCase()) {
Expand Down
5 changes: 2 additions & 3 deletions src/errors/checkLocalTxInputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { ApiPromise } from '@polkadot/api';
import { foreignAssetMultiLocationIsInCacheOrRegistry } from '../createXcmTypes/util/foreignAssetMultiLocationIsInCacheOrRegistry';
import { foreignAssetsMultiLocationExists } from '../createXcmTypes/util/foreignAssetsMultiLocationExists';
import { getAssetId } from '../createXcmTypes/util/getAssetId';
import { getChainIdBySpecName } from '../createXcmTypes/util/getChainIdBySpecName';
import { checkLiquidTokenValidity } from '../errors/checkXcmTxInputs';
import { Registry } from '../registry';
import { BaseError, BaseErrorsEnum } from './BaseError';
Expand Down Expand Up @@ -65,7 +64,7 @@ export const checkLocalTxInput = async (
}
} else if (isLiquidTokenTransfer) {
const relayChainInfo = registry.currentRelayRegistry;
const systemChainId = getChainIdBySpecName(registry, specName);
const systemChainId = registry.lookupChainIdBySpecName(specName);
const systemParachainInfo = relayChainInfo[systemChainId];

// If anything is incorrect this will throw an error.
Expand All @@ -74,7 +73,7 @@ export const checkLocalTxInput = async (
return LocalTxType.PoolAssets;
} else {
const relayChainInfo = registry.currentRelayRegistry;
const systemChainId = getChainIdBySpecName(registry, specName);
const systemChainId = registry.lookupChainIdBySpecName(specName);
const systemParachainInfo = relayChainInfo[systemChainId];

/**
Expand Down
5 changes: 2 additions & 3 deletions src/errors/checkXcmTxInputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { XcmPalletName } from '../createXcmCalls/util/establishXcmPallet';
import { CheckXcmTxInputsOpts } from '../createXcmTypes/types';
import { foreignAssetMultiLocationIsInCacheOrRegistry } from '../createXcmTypes/util/foreignAssetMultiLocationIsInCacheOrRegistry';
import { foreignAssetsMultiLocationExists } from '../createXcmTypes/util/foreignAssetsMultiLocationExists';
import { getChainIdBySpecName } from '../createXcmTypes/util/getChainIdBySpecName';
import { isParachainPrimaryNativeAsset } from '../createXcmTypes/util/isParachainPrimaryNativeAsset';
import { multiLocationAssetIsParachainsNativeAsset } from '../createXcmTypes/util/multiLocationAssetIsParachainsNativeAsset';
import { Registry } from '../registry';
Expand Down Expand Up @@ -433,7 +432,7 @@ const checkSystemAssets = async (
isForeignAssetsTransfer: boolean,
isLiquidTokenTransfer?: boolean
) => {
const currentChainId = getChainIdBySpecName(registry, specName);
const currentChainId = registry.lookupChainIdBySpecName(specName);

if (isForeignAssetsTransfer) {
// check that the asset id is a valid multilocation
Expand Down Expand Up @@ -685,7 +684,7 @@ export const checkIsValidSystemChainAssetId = async (
isForeignAssetsTransfer: boolean,
isLiquidTokenTransfer: boolean
) => {
const systemChainId = getChainIdBySpecName(registry, specName);
const systemChainId = registry.lookupChainIdBySpecName(specName);
const systemParachainInfo = relayChainInfo[systemChainId];

if (typeof assetId === 'string') {
Expand Down
15 changes: 14 additions & 1 deletion src/registry/Registry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,20 @@ describe('Registry', () => {
expect(res).toEqual(expected);
});
});

describe('lookupChainIdBySpecName', () => {
it('Should return the correct result', () => {
const res = registry.lookupChainIdBySpecName('moonbeam');
expect(res).toEqual('2004');
});
it('Should correctly cache specNames when they have Ids', () => {
registry.lookupChainIdBySpecName('statemint');
registry.lookupChainIdBySpecName('moonbeam');
registry.lookupChainIdBySpecName('acala');
expect(registry.specNameToIdCache.has('statemint')).toEqual(true);
expect(registry.specNameToIdCache.has('moonbeam')).toEqual(true);
expect(registry.specNameToIdCache.has('acala')).toEqual(true);
});
});
describe('Registry cache', () => {
it('Should correctly add an asset to the assetsInfo cache ', () => {
registry.setAssetInCache('1984', 'USDt');
Expand Down
56 changes: 47 additions & 9 deletions src/registry/Registry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { ASSET_HUB_CHAIN_ID } from '../consts';
import { getChainIdBySpecName } from '../createXcmTypes/util/getChainIdBySpecName';
import {
ASSET_HUB_CHAIN_ID,
KUSAMA_ASSET_HUB_SPEC_NAMES,
POLKADOT_ASSET_HUB_SPEC_NAMES,
WESTEND_ASSET_HUB_SPEC_NAMES,
} from '../consts';
import type { AssetTransferApiOpts } from '../types';
import { findRelayChain, parseRegistry } from './';
import type {
Expand All @@ -18,13 +22,15 @@ export class Registry {
readonly currentRelayRegistry: ChainInfo;
readonly xcAssets: XCMChainInfoRegistry;
cache: ChainInfoRegistry;
specNameToIdCache: Map<string, string>;

constructor(specName: string, opts: AssetTransferApiOpts) {
this.specName = specName;
this.registry = parseRegistry(opts);
this.relayChain = findRelayChain(this.specName, this.registry);
this.currentRelayRegistry = this.registry[this.relayChain];
this.xcAssets = this.registry.xcAssets;
this.specNameToIdCache = new Map<string, string>();
this.cache = {
polkadot: {},
kusama: {},
Expand All @@ -42,7 +48,7 @@ export class Registry {
* Initialize the cache for the current chain that is connected
*/
private initializeCurrentChainIdCache() {
const currentChainId = getChainIdBySpecName(this, this.specName);
const currentChainId = this.lookupChainIdBySpecName(this.specName);
if (!this.cache[this.relayChain][currentChainId]) {
this.cache[this.relayChain][currentChainId] = {
assetsInfo: {},
Expand Down Expand Up @@ -74,7 +80,7 @@ export class Registry {
* @param assetKey string
*/
public cacheLookupForeignAsset(assetKey: string): ForeignAssetsData | undefined {
const currentChainId = getChainIdBySpecName(this, this.specName);
const currentChainId = this.lookupChainIdBySpecName(this.specName);
const lookup = this.cache[this.relayChain][currentChainId]['foreignAssetsInfo'];

return lookup[assetKey] ? lookup[assetKey] : undefined;
Expand All @@ -87,7 +93,7 @@ export class Registry {
* @param assetValue ForeignAssetData
*/
public setForeignAssetInCache(assetKey: string, assetValue: ForeignAssetsData) {
const currentChainId = getChainIdBySpecName(this, this.specName);
const currentChainId = this.lookupChainIdBySpecName(this.specName);

this.cache[this.relayChain][currentChainId]['foreignAssetsInfo'][assetKey] = assetValue;
}
Expand All @@ -98,7 +104,7 @@ export class Registry {
* @param assetKey string
*/
public cacheLookupPoolAsset(assetKey: string): { lpToken: string; pairInfo: string } | undefined {
const currentChainId = getChainIdBySpecName(this, this.specName);
const currentChainId = this.lookupChainIdBySpecName(this.specName);
const lookup = this.cache[this.relayChain][currentChainId]['poolPairsInfo'];

return lookup[assetKey] ? lookup[assetKey] : undefined;
Expand All @@ -111,7 +117,7 @@ export class Registry {
* @param assetValue { lpToken: string; pairInfo: string }
*/
public setLiquidPoolTokenInCache(assetKey: string, assetValue: { lpToken: string; pairInfo: string }) {
const currentChainId = getChainIdBySpecName(this, this.specName);
const currentChainId = this.lookupChainIdBySpecName(this.specName);

this.cache[this.relayChain][currentChainId]['poolPairsInfo'][assetKey] = assetValue;
}
Expand All @@ -122,7 +128,7 @@ export class Registry {
* @param assetKey string
*/
public cacheLookupAsset(assetKey: string): string | undefined {
const currentChainId = getChainIdBySpecName(this, this.specName);
const currentChainId = this.lookupChainIdBySpecName(this.specName);
const lookup = this.cache[this.relayChain][currentChainId]['assetsInfo'];

return lookup[assetKey] ? lookup[assetKey] : undefined;
Expand All @@ -135,7 +141,7 @@ export class Registry {
* @param assetValue string
*/
public setAssetInCache(assetKey: string, assetValue: string) {
const currentChainId = getChainIdBySpecName(this, this.specName);
const currentChainId = this.lookupChainIdBySpecName(this.specName);

this.cache[this.relayChain][currentChainId]['assetsInfo'][assetKey] = assetValue;
}
Expand Down Expand Up @@ -227,4 +233,36 @@ export class Registry {
}
return [];
}

/**
* Return the Id of a parachain given its specName.
*
* @param specName
*/
public lookupChainIdBySpecName(specName: string): string {
if (this.specNameToIdCache.has(specName)) {
return this.specNameToIdCache.get(specName) as string;
}

if (
POLKADOT_ASSET_HUB_SPEC_NAMES.includes(specName.toLowerCase()) ||
KUSAMA_ASSET_HUB_SPEC_NAMES.includes(specName.toLowerCase()) ||
WESTEND_ASSET_HUB_SPEC_NAMES.includes(specName.toLowerCase())
) {
this.specNameToIdCache.set(specName, '1000');
return '1000';
}

const paraIds = Object.keys(this.currentRelayRegistry);
for (let i = 0; i < paraIds.length; i++) {
const id = paraIds[i];
const chain = this.currentRelayRegistry[id];
if (chain.specName.toLowerCase() === specName.toLowerCase()) {
this.specNameToIdCache.set(specName, id);
return id;
}
}

return '';
}
}

0 comments on commit abb5052

Please sign in to comment.