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

Rework the Registry to be a public class #183

Merged
merged 3 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 0 additions & 3 deletions src/AssetsTransferApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ describe('AssetTransferAPI', () => {
describe('SystemToRelay', () => {
it('Should corectly return Native', () => {
const assetType = systemAssetsApi['fetchAssetType'](
'statemint',
'0',
['DOT'],
Direction.SystemToRelay
Expand All @@ -102,7 +101,6 @@ describe('AssetTransferAPI', () => {
describe('RelayToSystem', () => {
it('Should correctly return Native', () => {
const assetType = systemAssetsApi['fetchAssetType'](
'polkadot',
'1000',
['DOT'],
Direction.RelayToSystem
Expand All @@ -114,7 +112,6 @@ describe('AssetTransferAPI', () => {
describe('SystemToPara', () => {
it('Should correctly return Foreign', () => {
const assetType = systemAssetsApi['fetchAssetType'](
'statemint',
'2000',
['1'],
Direction.SystemToPara
Expand Down
30 changes: 8 additions & 22 deletions src/AssetsTransferApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ import {
checkXcmTxInputs,
checkXcmVersion,
} from './errors';
import { findRelayChain } from './registry/findRelayChain';
import { parseRegistry } from './registry/parseRegistry';
import type { ChainInfoRegistry } from './registry/types';
import { Registry } from './registry';
import { sanitizeAddress } from './sanitize/sanitizeAddress';
import {
AssetsTransferApiOpts,
Expand Down Expand Up @@ -59,7 +57,7 @@ export class AssetsTransferApi {
readonly _opts: AssetsTransferApiOpts;
readonly _specName: string;
readonly _safeXcmVersion: number;
readonly _registry: ChainInfoRegistry;
readonly registry: Registry;

constructor(
api: ApiPromise,
Expand All @@ -71,7 +69,7 @@ export class AssetsTransferApi {
this._opts = opts;
this._specName = specName;
this._safeXcmVersion = safeXcmVersion;
this._registry = parseRegistry(opts);
this.registry = new Registry(specName, this._opts);
}

/**
Expand All @@ -97,7 +95,7 @@ export class AssetsTransferApi {
*/
checkBaseInputTypes(destChainId, destAddr, assetIds, amounts);

const { _api, _specName, _safeXcmVersion, _registry } = this;
const { _api, _specName, _safeXcmVersion, registry } = this;
const isOriginSystemParachain = SYSTEM_PARACHAINS_NAMES.includes(
_specName.toLowerCase()
);
Expand All @@ -120,12 +118,7 @@ export class AssetsTransferApi {
* This will throw a BaseError if the inputs are incorrect and don't
* fit the constraints for creating a local asset transfer.
*/
const localAssetType = checkLocalTxInput(
assetIds,
amounts,
_specName,
_registry
); // Throws an error when any of the inputs are incorrect.
const localAssetType = checkLocalTxInput(assetIds, amounts, registry); // Throws an error when any of the inputs are incorrect.
const method = opts?.keepAlive ? 'transferKeepAlive' : 'transfer';
if (isLocalSystemTx) {
let tx: SubmittableExtrinsic<'promise', ISubmittableResult>;
Expand Down Expand Up @@ -182,15 +175,10 @@ export class AssetsTransferApi {
xcmDirection,
destChainId,
_specName,
_registry
registry
);

const assetType = this.fetchAssetType(
_specName,
destChainId,
assetIds,
xcmDirection
);
const assetType = this.fetchAssetType(destChainId, assetIds, xcmDirection);

let txMethod: Methods;
let transaction: SubmittableExtrinsic<'promise', ISubmittableResult>;
Expand Down Expand Up @@ -402,7 +390,6 @@ export class AssetsTransferApi {
}

private fetchAssetType(
specName: string,
destChainId: string,
assets: string[],
xcmDirection: Direction
Expand All @@ -415,8 +402,7 @@ export class AssetsTransferApi {
return AssetType.Foreign;
}

const relayChainName = findRelayChain(specName, this._registry);
const relayChainInfo = this._registry[relayChainName];
const relayChainInfo = this.registry.currentRelayRegistry;

/**
* We can assume all the assets in `assets` are either foreign or native since we check
Expand Down
9 changes: 1 addition & 8 deletions src/createXcmTypes/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@ import type {
WeightLimitV2,
} from '@polkadot/types/interfaces';

type RequireOnlyOne<T, Keys extends keyof T = keyof T> = Pick<
T,
Exclude<keyof T, Keys>
> &
{
[K in Keys]-?: Required<Pick<T, K>> &
Partial<Record<Exclude<Keys, K>, undefined>>;
}[Keys];
import type { RequireOnlyOne } from '../types';

export interface ICreateXcmType {
createBeneficiary: (
Expand Down
22 changes: 9 additions & 13 deletions src/errors/checkLocalTxInputs.spec.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,43 @@
// Copyright 2023 Parity Technologies (UK) Ltd.

import { parseRegistry } from '../registry/parseRegistry';
import { Registry } from '../registry';
import { checkLocalTxInput } from './checkLocalTxInputs';

describe('checkLocalTxInput', () => {
const registry = parseRegistry({});
const registry = new Registry('statemine', {});

it('Should correctly return Balances with an empty assetIds', () => {
const res = checkLocalTxInput([], ['10000'], 'statemine', registry);
const res = checkLocalTxInput([], ['10000'], registry);
expect(res).toEqual('Balances');
});
it('Should correctly return Balances with a native token', () => {
const res = checkLocalTxInput(['KSM'], ['10000'], 'statemine', registry);
const res = checkLocalTxInput(['KSM'], ['10000'], registry);
expect(res).toEqual('Balances');
});
it('Should correctly return Assets with a valid assetId', () => {
const res = checkLocalTxInput(['1984'], ['10000'], 'statemine', registry);
const res = checkLocalTxInput(['1984'], ['10000'], registry);
expect(res).toEqual('Assets');
});
it('Should correctly throw an error for incorrect length on `assetIds`', () => {
const err = () =>
checkLocalTxInput(['1', '2'], ['10000'], 'statemine', registry);
const err = () => checkLocalTxInput(['1', '2'], ['10000'], registry);
expect(err).toThrowError(
'Local transactions must have the `assetIds` input be a length of 1 or 0, and the `amounts` input be a length of 1'
);
});
it('Should correctly throw an error for incorrect length on `amounts`', () => {
const err = () =>
checkLocalTxInput(['1'], ['10000', '20000'], 'statemine', registry);
const err = () => checkLocalTxInput(['1'], ['10000', '20000'], registry);
expect(err).toThrowError(
'Local transactions must have the `assetIds` input be a length of 1 or 0, and the `amounts` input be a length of 1'
);
});
it('Should correctly throw an error with an incorrect assetId', () => {
const err = () =>
checkLocalTxInput(['TST'], ['10000'], 'statemine', registry);
const err = () => checkLocalTxInput(['TST'], ['10000'], registry);
expect(err).toThrowError(
'The assetId passed in is not a valid number: TST'
);
});
it("Should correctly throw an error when the assetId doesn't exist", () => {
const err = () =>
checkLocalTxInput(['9876111'], ['10000'], 'statemine', registry);
const err = () => checkLocalTxInput(['9876111'], ['10000'], registry);
expect(err).toThrowError(
'The assetId 9876111 does not exist in the registry.'
);
Expand Down
9 changes: 3 additions & 6 deletions src/errors/checkLocalTxInputs.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Copyright 2023 Parity Technologies (UK) Ltd.

import { SYSTEM_PARACHAINS_IDS } from '../consts';
import { findRelayChain } from '../registry/findRelayChain';
import type { ChainInfoRegistry } from '../registry/types';
import { Registry } from '../registry';
import { BaseError } from './BaseError';

enum LocalTxType {
Expand All @@ -20,11 +19,9 @@ enum LocalTxType {
export const checkLocalTxInput = (
assetIds: string[],
amounts: string[],
specName: string,
registry: ChainInfoRegistry
registry: Registry
): LocalTxType => {
const relayChain = findRelayChain(specName, registry);
const relayChainInfo = registry[relayChain];
const relayChainInfo = registry.currentRelayRegistry;
const systemParachainInfo = relayChainInfo[SYSTEM_PARACHAINS_IDS[0]];

// Ensure the lengths in assetIds and amounts is correct
Expand Down
39 changes: 13 additions & 26 deletions src/errors/checkXcmTxInputs.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ChainInfoRegistry } from 'src/registry/types';
// Copyright 2023 Parity Technologies (UK) Ltd.

import { findRelayChain } from '../registry/findRelayChain';
import { parseRegistry } from '../registry/parseRegistry';
import { Registry } from '../registry';
import { Direction } from '../types';
import {
checkAssetIdInput,
Expand All @@ -10,11 +9,11 @@ import {
checkRelayAssetIdLength,
} from './checkXcmTxInputs';

const runTests = (tests: Test[], registry: ChainInfoRegistry) => {
const runTests = (tests: Test[]) => {
for (const test of tests) {
const [destChainId, specName, testInputs, direction, errorMessage] = test;
const relayChainName = findRelayChain(specName, registry);
const currentRegistry = registry[relayChainName];
const registry = new Registry(specName, {});
const currentRegistry = registry.currentRelayRegistry;

const err = () =>
checkAssetIdInput(
Expand Down Expand Up @@ -67,8 +66,6 @@ type Test = [

describe('checkAssetIds', () => {
it('Should error when an assetId is found that is empty or a blank space', () => {
const registry = parseRegistry({});

const tests: Test[] = [
[
'1000',
Expand All @@ -86,12 +83,10 @@ describe('checkAssetIds', () => {
],
];

runTests(tests, registry);
runTests(tests);
});

it('Should error when direction is RelayToSystem and assetId does not match relay chains native token', () => {
const registry = parseRegistry({});

const tests: Test[] = [
[
'1000',
Expand All @@ -116,12 +111,10 @@ describe('checkAssetIds', () => {
],
];

runTests(tests, registry);
runTests(tests);
});

it('Should error when direction is RelayToPara and assetId does not match relay chains native token', () => {
const registry = parseRegistry({});

const tests: Test[] = [
[
'2004',
Expand All @@ -139,12 +132,10 @@ describe('checkAssetIds', () => {
],
];

runTests(tests, registry);
runTests(tests);
});

it('Should error when direction is SystemToRelay and an assetId is not native to the relay chain', () => {
const registry = parseRegistry({});

const tests: Test[] = [
[
'0',
Expand All @@ -169,12 +160,10 @@ describe('checkAssetIds', () => {
],
];

runTests(tests, registry);
runTests(tests);
});

it('Should error when direction is SystemToPara and integer assetId is not found in system parachains assets', () => {
const registry = parseRegistry({});

const tests: Test[] = [
[
'2004',
Expand All @@ -201,8 +190,8 @@ describe('checkAssetIds', () => {

for (const test of tests) {
const [destChainId, specName, testInputs, direction, errorMessage] = test;
const relayChainName = findRelayChain(specName, registry);
const currentRegistry = registry[relayChainName];
const registry = new Registry(specName, {});
const currentRegistry = registry.currentRelayRegistry;

const err = () =>
checkAssetIdInput(
Expand All @@ -217,8 +206,6 @@ describe('checkAssetIds', () => {
});

it('Should error when direction is SystemToPara and the string assetId is not found in the system parachains tokens or assets', () => {
const registry = parseRegistry({});

const tests: Test[] = [
[
'2004',
Expand All @@ -245,8 +232,8 @@ describe('checkAssetIds', () => {

for (const test of tests) {
const [destChainId, specName, testInputs, direction, errorMessage] = test;
const relayChainName = findRelayChain(specName, registry);
const currentRegistry = registry[relayChainName];
const registry = new Registry(specName, {});
const currentRegistry = registry.currentRelayRegistry;

const err = () =>
checkAssetIdInput(
Expand Down
9 changes: 4 additions & 5 deletions src/errors/checkXcmTxInputs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RELAY_CHAIN_IDS, SYSTEM_PARACHAINS_IDS } from '../consts';
import { findRelayChain } from '../registry/findRelayChain';
import type { ChainInfo, ChainInfoRegistry } from '../registry/types';
import { Registry } from '../registry';
import type { ChainInfo } from '../registry/types';
import { Direction } from '../types';
import { BaseError } from './BaseError';

Expand Down Expand Up @@ -277,10 +277,9 @@ export const checkXcmTxInputs = (
xcmDirection: Direction,
destChainId: string,
specName: string,
registry: ChainInfoRegistry
registry: Registry
) => {
const relayChainName = findRelayChain(specName, registry);
const relayChainInfo: ChainInfo = registry[relayChainName];
const relayChainInfo = registry.currentRelayRegistry;
/**
* Checks to ensure that assetId's are either valid integer numbers or native asset token symbols
*/
Expand Down
Loading