Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ensure the injectedRegsitry opt does deep comparisons #359

Merged
merged 28 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/AssetTransferApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import {
} from './errors';
import { LocalTxType } from './errors/checkLocalTxInput/types';
import { Registry } from './registry';
import { ChainInfoRegistry } from './registry/types';
import { ChainInfoKeys, ChainInfoRegistry } from './registry/types';
import { sanitizeAddress } from './sanitize/sanitizeAddress';
import {
AssetCallType,
Expand Down Expand Up @@ -90,7 +90,7 @@ import { validateNumber } from './validate';
*/
export class AssetTransferApi {
readonly api: ApiPromise;
readonly opts: AssetTransferApiOpts;
readonly opts: AssetTransferApiOpts<ChainInfoKeys>;
readonly specName: string;
readonly safeXcmVersion: number;
readonly nativeRelayChainAsset: string;
Expand All @@ -101,7 +101,12 @@ export class AssetTransferApi {
};
public registry: Registry;

constructor(api: ApiPromise, specName: string, safeXcmVersion: number, opts: AssetTransferApiOpts = {}) {
constructor(
api: ApiPromise,
specName: string,
safeXcmVersion: number,
opts: AssetTransferApiOpts<ChainInfoKeys> = {},
) {
this.api = api;
this.opts = opts;
this.specName = specName;
Expand Down Expand Up @@ -337,7 +342,7 @@ export class AssetTransferApi {
BaseErrorsEnum.InternalError,
);
}
const fetchedRegistry = (await data.json()) as ChainInfoRegistry;
const fetchedRegistry = (await data.json()) as ChainInfoRegistry<ChainInfoKeys>;
this.registry.setRegistry = fetchedRegistry;

this.registryConfig.registryInitialized = true;
Expand Down
12 changes: 6 additions & 6 deletions src/createXcmTypes/util/foreignAssetsMultiLocationExists.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('foreignMultiAssetMultiLocationExists', () => {

it('Should return true for an existing foreign asset multilocation', async () => {
const expected = true;
const multiLocation = '{"parents":"1","interior":{"X2": [{"Parachain":"2125"}, {"GeneralIndex": "0"}]}}';
const multiLocation = '{"parents":"1","interior":{"X2": [{"Parachain":"1103"}, {"GeneralIndex": "0"}]}}';

const isValid = await foreignAssetsMultiLocationExists(adjustedMockSystemApi, registry, multiLocation, 2);

Expand Down Expand Up @@ -58,16 +58,16 @@ describe('foreignMultiAssetMultiLocationExists', () => {
},
},
});
const multiLocation = '{"parents":"1","interior":{"X2": [{"Parachain":"2125"}, {"GeneralIndex": "0"}]}}';
const multiLocation = '{"parents":"1","interior":{"X2": [{"Parachain":"1103"}, {"GeneralIndex": "0"}]}}';

await foreignAssetsMultiLocationExists(adjustedMockSystemApi, emptyRegistry, multiLocation, 2);

const result = emptyRegistry.cacheLookupForeignAsset('TNKR');
const result = emptyRegistry.cacheLookupForeignAsset('GDZ');

expect(result).toEqual({
multiLocation: '{"Parents":"1","Interior":{"X2":[{"Parachain":"2125"},{"GeneralIndex":"0"}]}}',
name: 'Tinkernet',
symbol: 'TNKR',
multiLocation: '{"Parents":"1","Interior":{"X2":[{"Parachain":"1103"},{"GeneralIndex":"0"}]}}',
name: 'Godzilla',
symbol: 'GDZ',
});
});
});
4 changes: 4 additions & 0 deletions src/errors/BaseError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export enum BaseErrorsEnum {
* The following pallet is not found.
*/
PalletNotFound = 'PalletNotFound',
/**
* The specName was not provided when injecting a new chain in the registry.
*/
SpecNameNotProvided = 'SpecNameNotProvided',
/**
* The direction in which these assets are going to be sent is incorrect.
*/
Expand Down
10 changes: 5 additions & 5 deletions src/errors/checkXcmTxInputs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ describe('checkParaAssets', () => {

await checkAssetIdInput(
adjustedMockSystemApi,
['{"parents":"1","interior":{"X2":[{"Parachain":"2125"},{"GeneralIndex":"0"}]}}'],
['{"parents":"1","interior":{"X2":[{"Parachain":"1103"},{"GeneralIndex":"0"}]}}'],
chainInfo,
'statemine',
Direction.SystemToPara,
Expand All @@ -895,10 +895,10 @@ describe('checkParaAssets', () => {
false,
);

expect(registry.cacheLookupForeignAsset('TNKR')).toEqual({
multiLocation: '{"Parents":"1","Interior":{"X2":[{"Parachain":"2125"},{"GeneralIndex":"0"}]}}',
name: 'Tinkernet',
symbol: 'TNKR',
expect(registry.cacheLookupForeignAsset('GDZ')).toEqual({
multiLocation: '{"Parents":"1","Interior":{"X2":[{"Parachain":"1103"},{"GeneralIndex":"0"}]}}',
name: 'Godzilla',
symbol: 'GDZ',
});
});

Expand Down
14 changes: 7 additions & 7 deletions src/errors/checkXcmTxInputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ export const checkMultiLocationsContainOnlyNativeOrForeignAssetsOfDestChain = (
* @param assetId
* @param relayChainInfo
*/
const checkRelayToSystemAssetId = (assetId: string, relayChainInfo: ChainInfo) => {
const checkRelayToSystemAssetId = (assetId: string, relayChainInfo: ChainInfo<ChainInfoKeys>) => {
const relayChainId = RELAY_CHAIN_IDS[0];
const relayChain = relayChainInfo[relayChainId];
const relayChainNativeAsset = relayChain.tokens[0];
Expand Down Expand Up @@ -271,7 +271,7 @@ const checkRelayToSystemAssetId = (assetId: string, relayChainInfo: ChainInfo) =
* @param assetId
* @param relayChainInfo
*/
const checkRelayToParaAssetId = (assetId: string, relayChainInfo: ChainInfo) => {
const checkRelayToParaAssetId = (assetId: string, relayChainInfo: ChainInfo<ChainInfoKeys>) => {
const relayChainId = RELAY_CHAIN_IDS[0];
const relayChain = relayChainInfo[relayChainId];
const relayChainNativeAsset = relayChain.tokens[0];
Expand Down Expand Up @@ -305,7 +305,7 @@ const checkRelayToParaAssetId = (assetId: string, relayChainInfo: ChainInfo) =>
* @param assetId
* @param relayChainInfo
*/
const checkSystemToRelayAssetId = (assetId: string, relayChainInfo: ChainInfo) => {
const checkSystemToRelayAssetId = (assetId: string, relayChainInfo: ChainInfo<ChainInfoKeys>) => {
const relayChainId = RELAY_CHAIN_IDS[0];
const relayChain = relayChainInfo[relayChainId];
const relayChainNativeAsset = relayChain.tokens[0];
Expand Down Expand Up @@ -659,7 +659,7 @@ const checkSystemToParaAssetId = async (
api: ApiPromise,
assetId: string,
specName: string,
relayChainInfo: ChainInfo,
relayChainInfo: ChainInfo<ChainInfoKeys>,
registry: Registry,
xcmDirection: Direction,
xcmVersion: number,
Expand All @@ -683,7 +683,7 @@ export const checkIsValidSystemChainAssetId = async (
api: ApiPromise,
assetId: string,
specName: string,
relayChainInfo: ChainInfo,
relayChainInfo: ChainInfo<ChainInfoKeys>,
registry: Registry,
xcmDirection: Direction,
xcmVersion: number,
Expand Down Expand Up @@ -744,7 +744,7 @@ const checkSystemToSystemAssetId = async (
api: ApiPromise,
assetId: string,
specName: string,
relayChainInfo: ChainInfo,
relayChainInfo: ChainInfo<ChainInfoKeys>,
registry: Registry,
xcmDirection: Direction,
xcmVersion: number,
Expand Down Expand Up @@ -968,7 +968,7 @@ export const checkLiquidTokenTransferDirectionValidity = (xcmDirection: Directio
export const checkAssetIdInput = async (
api: ApiPromise,
assetIds: string[],
relayChainInfo: ChainInfo,
relayChainInfo: ChainInfo<ChainInfoKeys>,
specName: string,
xcmDirection: Direction,
registry: Registry,
Expand Down
18 changes: 9 additions & 9 deletions src/registry/Registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ import type {
export class Registry {
readonly specName: string;
readonly relayChain: RelayChains;
readonly currentRelayRegistry: ChainInfo;
readonly opts: AssetTransferApiOpts;
readonly currentRelayRegistry: ChainInfo<ChainInfoKeys>;
readonly opts: AssetTransferApiOpts<ChainInfoKeys>;
public specNameToIdCache: Map<string, string>;
public registry: ChainInfoRegistry;
public cache: ChainInfoRegistry;
public registry: ChainInfoRegistry<ChainInfoKeys>;
public cache: ChainInfoRegistry<ChainInfoKeys>;

constructor(specName: string, opts: AssetTransferApiOpts) {
constructor(specName: string, opts: AssetTransferApiOpts<ChainInfoKeys>) {
this.opts = opts;
this.specName = specName;
this.registry = parseRegistry(registry as ChainInfoRegistry, opts);
this.registry = parseRegistry(registry as ChainInfoRegistry<ChainInfoKeys>, opts);
this.relayChain = findRelayChain(this.specName, this.registry);
this.currentRelayRegistry = this.registry[this.relayChain];
this.specNameToIdCache = new Map<string, string>();
Expand Down Expand Up @@ -153,7 +153,7 @@ export class Registry {
*
* @param reg Registry
*/
public set setRegistry(reg: ChainInfoRegistry) {
public set setRegistry(reg: ChainInfoRegistry<ChainInfoKeys>) {
this.registry = parseRegistry(reg, this.opts);
}

Expand Down Expand Up @@ -191,7 +191,7 @@ export class Registry {

for (let i = 0; i < chainIds.length; i++) {
const chainInfo = this.currentRelayRegistry[chainIds[i]];
if (chainInfo.tokens.includes(symbol)) {
if (chainInfo.tokens && chainInfo.tokens.includes(symbol)) {
result.push(Object.assign({}, chainInfo, { chainId: chainIds[i] }));
}
}
Expand Down Expand Up @@ -271,7 +271,7 @@ export class Registry {
for (let i = 0; i < paraIds.length; i++) {
const id = paraIds[i];
const chain = this.currentRelayRegistry[id];
if (chain.specName.toLowerCase() === specName.toLowerCase()) {
if (chain.specName && chain.specName.toLowerCase() === specName.toLowerCase()) {
this.specNameToIdCache.set(specName, id);
return id;
}
Expand Down
4 changes: 2 additions & 2 deletions src/registry/findRelayChain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import reg from '@substrate/asset-transfer-api-registry';

import { findRelayChain } from './findRelayChain';
import { parseRegistry } from './parseRegistry';
import { ChainInfoRegistry } from './types';
import { ChainInfoKeys, ChainInfoRegistry } from './types';

describe('findRelayChain', () => {
const registry = parseRegistry(reg as ChainInfoRegistry, {});
const registry = parseRegistry(reg as ChainInfoRegistry<ChainInfoKeys>, {});
it('Should correctly discover the right relay chain', () => {
const findPolkadot = findRelayChain('statemint', registry);
const findKusama = findRelayChain('statemine', registry);
Expand Down
4 changes: 2 additions & 2 deletions src/registry/findRelayChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import {
WESTEND_ASSET_HUB_SPEC_NAMES,
} from '../consts';
import { BaseError, BaseErrorsEnum } from '../errors';
import type { ChainInfoRegistry, RelayChains } from './types';
import type { ChainInfoKeys, ChainInfoRegistry, RelayChains } from './types';
/**
* Finds the name of the relay chain of a given specName. If the chain does not exist within the registry
* an error will be thrown.
*
* @param specName SpecName of the given chain
* @param registry The registry to search
*/
export const findRelayChain = (specName: string, registry: ChainInfoRegistry): RelayChains => {
export const findRelayChain = (specName: string, registry: ChainInfoRegistry<ChainInfoKeys>): RelayChains => {
const polkadotChains = Object.keys(registry.polkadot).map((val) => registry.polkadot[val].specName);
if (polkadotChains.includes(specName.toLowerCase()) || POLKADOT_ASSET_HUB_SPEC_NAMES.includes(specName.toLowerCase()))
return 'polkadot';
Expand Down
Loading
Loading