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 5 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
4 changes: 2 additions & 2 deletions src/registry/Registry.ts
Original file line number Diff line number Diff line change
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 !== undefined && chainInfo.tokens.includes(symbol)) {
bee344 marked this conversation as resolved.
Show resolved Hide resolved
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 !== undefined && chain.specName.toLowerCase() === specName.toLowerCase()) {
bee344 marked this conversation as resolved.
Show resolved Hide resolved
this.specNameToIdCache.set(specName, id);
return id;
}
Expand Down
120 changes: 120 additions & 0 deletions src/registry/parseRegistry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,124 @@ describe('parseRegistry', () => {
// Ensure nothing was overwritten
expect(registry.polkadot['0'].tokens).toStrictEqual(['DOT']);
});
it('Should correctly update the registry with an injectedRegsitry without specName', () => {
const assetsInfo = {};
const foreignAssetsInfo = {};
const opts = {
injectedRegistry: {
westend: {
'0': {
tokens: ['WND', 'WND2'],
assetsInfo,
foreignAssetsInfo,
poolPairsInfo: {},
},
},
},
};
const registry = parseRegistry(reg as ChainInfoRegistry, opts);

expect(registry.westend['0']).toStrictEqual({
tokens: ['WND', 'WND2'],
assetsInfo: {},
foreignAssetsInfo: {},
specName: 'westend',
poolPairsInfo: {},
});
});
it('Should correctly update the registry with an injectedRegsitry without tokens', () => {
const assetsInfo = {};
const foreignAssetsInfo = {};
const opts = {
injectedRegistry: {
westend: {
'0': {
assetsInfo,
foreignAssetsInfo,
poolPairsInfo: {},
specName: 'totoro',
},
},
},
};
const registry = parseRegistry(reg as ChainInfoRegistry, opts);

expect(registry.westend['0']).toStrictEqual({
tokens: ['WND', 'WND2'],
assetsInfo: {},
foreignAssetsInfo: {},
specName: 'totoro',
poolPairsInfo: {},
});
});
it('Should correctly error when a previously missing injectedRegsitry is added without a token', () => {
const expectedErrorMessage = `Must define the tokens property`;

const assetsInfo = {};
const foreignAssetsInfo = {};
const opts = {
injectedRegistry: {
westend: {
'3000': {
assetsInfo,
foreignAssetsInfo,
poolPairsInfo: {},
specName: 'totoro',
},
},
},
};

const err = () => parseRegistry(reg as ChainInfoRegistry, opts);
expect(err).toThrow(expectedErrorMessage);
});
it("Should correctly override the registry's token entry with an injectedRegsitry", () => {
const assetsInfo = {};
const foreignAssetsInfo = {};
const opts = {
injectedRegistry: {
westend: {
'0': {
tokens: ['WOP'],
assetsInfo,
foreignAssetsInfo,
poolPairsInfo: {},
},
},
},
};
const registry = parseRegistry(reg as ChainInfoRegistry, opts);

expect(registry.westend['0']).toStrictEqual({
tokens: ['WOP'],
assetsInfo: {},
foreignAssetsInfo: {},
specName: 'totoro',
poolPairsInfo: {},
});
});
it('Should correctly add a previously missing injectedRegsitry without assetsInfo', () => {
const foreignAssetsInfo = {};
const opts = {
injectedRegistry: {
rococo: {
'2000': {
foreignAssetsInfo,
tokens: ['TST'],
poolPairsInfo: {},
specName: 'testy',
},
},
},
};
const registry = parseRegistry(reg as ChainInfoRegistry, opts);

expect(registry.rococo['2000']).toStrictEqual({
tokens: ['TST'],
assetsInfo: {},
foreignAssetsInfo: {},
specName: 'testy',
poolPairsInfo: {},
});
});
bee344 marked this conversation as resolved.
Show resolved Hide resolved
});
39 changes: 31 additions & 8 deletions src/registry/parseRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,44 @@
// Copyright 2023 Parity Technologies (UK) Ltd.
// Copyright 2023-2024 Parity Technologies (UK) Ltd.

import { ASSET_HUB_CHAIN_ID } from '../consts';
import type { AssetTransferApiOpts } from '../types';
import type { ChainInfoRegistry } from './types';
import type { AssetTransferApiInjectedOpts } from '../types';
import type { ChainInfo, ChainInfoRegistry, InjectedChainInfo } from './types';

export const parseRegistry = (registry: ChainInfoRegistry, assetsOpts: AssetTransferApiOpts): ChainInfoRegistry => {
const updateRegistry = (injectedChain: InjectedChainInfo, registry: ChainInfoRegistry, registryChain: string) => {
bee344 marked this conversation as resolved.
Show resolved Hide resolved
for (const key of Object.keys(injectedChain)) {
const info = registry[registryChain] as unknown as ChainInfo;
if (info[key] !== undefined) {
Object.assign(info[key], injectedChain[key]);
} else {
for (const property of Object.keys(info[0])) {
if (injectedChain[key][property] === undefined) {
if (property === 'specName' || property === 'tokens') {
bee344 marked this conversation as resolved.
Show resolved Hide resolved
throw Error(`Must define the ${property} property`);
} else {
injectedChain[key][property] = {};
}
}
Object.assign(info, injectedChain);
}
}
}
};

export const parseRegistry = (
registry: ChainInfoRegistry,
assetsOpts: AssetTransferApiInjectedOpts,
): ChainInfoRegistry => {
if (assetsOpts.injectedRegistry) {
const { injectedRegistry } = assetsOpts;
const polkadot = injectedRegistry.polkadot;
const kusama = injectedRegistry.kusama;
const westend = injectedRegistry.westend;
const rococo = injectedRegistry.rococo;

if (polkadot) Object.assign(registry.polkadot, polkadot);
if (kusama) Object.assign(registry.kusama, kusama);
if (westend) Object.assign(registry.westend, westend);
if (rococo) Object.assign(registry.rococo, rococo);
if (polkadot) updateRegistry(polkadot, registry, 'polkadot');
if (kusama) updateRegistry(kusama, registry, 'kusama');
if (westend) updateRegistry(westend, registry, 'westend');
if (rococo) updateRegistry(rococo, registry, 'rococo');
}

/**
Expand Down
20 changes: 20 additions & 0 deletions src/registry/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,26 @@ export type ChainInfoRegistry = {
rococo: ChainInfo;
};

export interface InjectedChainInfoKeys {
specName?: string;
tokens?: string[];
assetsInfo?: AssetsInfo;
foreignAssetsInfo?: ForeignAssetsInfo;
poolPairsInfo?: PoolPairsData;
xcAssetsData?: SanitizedXcAssetsData[];
}

export type InjectedChainInfo = {
[x: string]: InjectedChainInfoKeys;
};

export type InjectedChainInfoRegistry = {
polkadot: InjectedChainInfo;
kusama: InjectedChainInfo;
westend: InjectedChainInfo;
rococo: InjectedChainInfo;
};

export type RelayChains = 'polkadot' | 'kusama' | 'westend' | 'rococo';

export type InterMultiLocationJunctionType = 'here' | 'x1' | 'x2' | 'x3' | 'x4' | 'x5' | 'x6' | 'x7' | 'x8';
Expand Down
14 changes: 13 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import BN from 'bn.js';

import { XcmPalletName } from './createXcmCalls/util/establishXcmPallet';
import type { Registry } from './registry';
import type { ChainInfoRegistry } from './registry/types';
import type { ChainInfoRegistry, InjectedChainInfoRegistry } from './registry/types';

export type RequireOnlyOne<T, Keys extends keyof T = keyof T> = Pick<T, Exclude<keyof T, Keys>> &
{
Expand Down Expand Up @@ -161,6 +161,18 @@ export type AssetTransferApiOpts = {
registryType?: RegistryTypes;
};

export type AssetTransferApiInjectedOpts = {
bee344 marked this conversation as resolved.
Show resolved Hide resolved
/**
* Option to inject chain information into the registry.
*/
injectedRegistry?: RequireAtLeastOne<InjectedChainInfoRegistry>;
/**
* Whether or not to apply the registry from the npm package `asset-transfer-api-registry`,
* or the hosted CDN which updates frequently.
*/
registryType?: RegistryTypes;
};

/**
* Types that the registry can be initialized as.
*
Expand Down