Skip to content

Commit

Permalink
add disabled options config and function
Browse files Browse the repository at this point in the history
  • Loading branch information
TarikGul committed Sep 12, 2023
1 parent 247ffee commit 0ccbb33
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/AssetsTransferApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export class AssetsTransferApi {
* Ensure that the options passed in are compatible with eachother.
* It will throw an error if any are incorrect.
*/
checkBaseInputOptions(opts);
checkBaseInputOptions(opts, this._specName);
/**
* Ensure all the inputs are the corrects primitive and or object types.
* It will throw an error if any are incorrect.
Expand Down
53 changes: 53 additions & 0 deletions src/config/disabledOpts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2023 Parity Technologies (UK) Ltd.

import { Format, TransferArgsOpts } from '../types';

interface DisabledOptionsInfo {
disabled: boolean;
chains: string[];
}

type MappedOpts = Extract<keyof TransferArgsOpts<Format>, string>;

type DisabledOptions = {
[key in MappedOpts]: DisabledOptionsInfo;
};

export const disabledOpts: DisabledOptions = {
format: {
disabled: false,
chains: [],
},
paysWithFeeOrigin: {
disabled: true,
chains: ['westend', 'westmint'],
},
paysWithFeeDest: {
disabled: false,
chains: [],
},
sendersAddr: {
disabled: false,
chains: [],
},
isLimited: {
disabled: false,
chains: [],
},
weightLimit: {
disabled: false,
chains: [],
},
xcmVersion: {
disabled: false,
chains: [],
},
keepAlive: {
disabled: false,
chains: [],
},
transferLiquidToken: {
disabled: false,
chains: [],
},
};
4 changes: 4 additions & 0 deletions src/errors/BaseError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ export enum BaseErrorsEnum {
* The inputted address is invalid.
*/
InvalidAddress = 'InvalidAddress',
/**
* The following option is disabled given the inputs.
*/
DisabledOption = 'DisabledOption',
}

export class BaseError extends Error {
Expand Down
12 changes: 7 additions & 5 deletions src/errors/checkBaseInputOptions.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright 2023 Parity Technologies (UK) Ltd.

import type { Format, TransferArgsOpts } from '../types';
import { checkBaseInputOptions } from './checkBaseInputOptions';

Expand All @@ -7,7 +9,7 @@ describe('checkBaseInputOptions', () => {
format: 'call',
paysWithFeeOrigin: '1984',
} as TransferArgsOpts<Format>;
const err = () => checkBaseInputOptions(opts);
const err = () => checkBaseInputOptions(opts, 'statemine');

expect(err).toThrow(
'PaysWithFeeOrigin is only compatible with the format type payload. Received: call'
Expand All @@ -18,7 +20,7 @@ describe('checkBaseInputOptions', () => {
format: 'submittable',
paysWithFeeOrigin: '1984',
} as TransferArgsOpts<Format>;
const err = () => checkBaseInputOptions(opts);
const err = () => checkBaseInputOptions(opts, 'statemine');

expect(err).toThrow(
'PaysWithFeeOrigin is only compatible with the format type payload. Received: submittable'
Expand All @@ -29,7 +31,7 @@ describe('checkBaseInputOptions', () => {
format: 'payload',
paysWithFeeOrigin: '1984',
} as TransferArgsOpts<Format>;
const err = () => checkBaseInputOptions(opts);
const err = () => checkBaseInputOptions(opts, 'statemine');

expect(err).toThrow(
"The 'sendersAddr' option must be present when constructing a 'payload' format."
Expand All @@ -41,7 +43,7 @@ describe('checkBaseInputOptions', () => {
paysWithFeeOrigin: '1984',
sendersAddr: '0x000',
} as TransferArgsOpts<Format>;
const err = () => checkBaseInputOptions(opts);
const err = () => checkBaseInputOptions(opts, 'statemine');

expect(err).toThrow(
'The inputted sendersAddr is not valid. Invalid base58 character "0" (0x30) at index 0'
Expand All @@ -53,7 +55,7 @@ describe('checkBaseInputOptions', () => {
paysWithFeeOrigin: '1984',
sendersAddr: 'FBeL7DanUDs5SZrxZY1CizMaPgG9vZgJgvr52C2dg81SsF1',
} as TransferArgsOpts<Format>;
const res = () => checkBaseInputOptions(opts);
const res = () => checkBaseInputOptions(opts, 'statemine');

expect(res).not.toThrow();
});
Expand Down
10 changes: 9 additions & 1 deletion src/errors/checkBaseInputOptions.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
// Copyright 2023 Parity Technologies (UK) Ltd.

import { Format, TransferArgsOpts } from '../types';
import { validateAddress } from '../validate';
import { BaseError, BaseErrorsEnum } from './BaseError';
import { disableOpts } from './disableOpts';

/**
* Ensure that options that require certain inputs are validated.
*
* @param opts
*/
export const checkBaseInputOptions = (opts: TransferArgsOpts<Format>) => {
export const checkBaseInputOptions = (
opts: TransferArgsOpts<Format>,
specName: string
) => {
const { paysWithFeeOrigin, format, sendersAddr } = opts;

disableOpts(opts, specName);

if (paysWithFeeOrigin) {
if (format === 'call' || format === 'submittable') {
throw new BaseError(
Expand Down
10 changes: 10 additions & 0 deletions src/errors/disableOpts.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright 2023 Parity Technologies (UK) Ltd.

import { disableOpts } from './disableOpts';

describe('disableOpts', () => {
it('Should error for paysWithFeeOrigin', () => {
const err = () => disableOpts({ paysWithFeeOrigin: 'DOT' }, 'westend');
expect(err).toThrow('paysWithFeeOrigin is disbaled for westend.');
});
});
26 changes: 26 additions & 0 deletions src/errors/disableOpts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2023 Parity Technologies (UK) Ltd.

import { disabledOpts } from '../config/disabledOpts';
import type { Format, TransferArgsOpts } from '../types';
import { BaseError, BaseErrorsEnum } from './BaseError';

/**
* This checks specific options to ensure they are disabled when met in certain conditions.
*
* @param opts Options for `createTransferTransaction`
* @param specName SpecName of the current chain
*/
export const disableOpts = <T extends Format>(
opts: TransferArgsOpts<T>,
specName: string
) => {
if (opts.paysWithFeeOrigin) {
const { paysWithFeeOrigin } = disabledOpts;
if (paysWithFeeOrigin.chains.includes(specName.toLowerCase())) {
throw new BaseError(
`paysWithFeeOrigin is disbaled for ${specName.toLowerCase()}.`,
BaseErrorsEnum.DisabledOption
);
}
}
};

0 comments on commit 0ccbb33

Please sign in to comment.