From b23d2f2ed903a8a524c08b24ef2bfbff1340a61f Mon Sep 17 00:00:00 2001 From: tarikgul Date: Tue, 12 Sep 2023 20:32:02 -0400 Subject: [PATCH] add generalization --- src/config/disabledOpts.ts | 18 ++++++++++++++++++ src/errors/disableOpts.ts | 13 +++++++------ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/config/disabledOpts.ts b/src/config/disabledOpts.ts index 5352de71..eb8b28f4 100644 --- a/src/config/disabledOpts.ts +++ b/src/config/disabledOpts.ts @@ -1,10 +1,12 @@ // Copyright 2023 Parity Technologies (UK) Ltd. +import { BaseError, BaseErrorsEnum } from '../errors/BaseError'; import { Format, TransferArgsOpts } from '../types'; interface DisabledOptionsInfo { disabled: boolean; chains: string[]; + error: (opts: string, chain: string) => never; } type MappedOpts = Extract, string>; @@ -13,41 +15,57 @@ type DisabledOptions = { [key in MappedOpts]: DisabledOptionsInfo; }; +const callError = (opt: string, chain: string) => { + throw new BaseError( + `${opt} is disbaled for ${chain}.`, + BaseErrorsEnum.DisabledOption + ); +}; + export const disabledOpts: DisabledOptions = { format: { disabled: false, chains: [], + error: (opt: string, chain: string) => callError(opt, chain), }, paysWithFeeOrigin: { disabled: true, chains: ['westend', 'westmint'], + error: (opt: string, chain: string) => callError(opt, chain), }, paysWithFeeDest: { disabled: false, chains: [], + error: (opt: string, chain: string) => callError(opt, chain), }, sendersAddr: { disabled: false, chains: [], + error: (opt: string, chain: string) => callError(opt, chain), }, isLimited: { disabled: false, chains: [], + error: (opt: string, chain: string) => callError(opt, chain), }, weightLimit: { disabled: false, chains: [], + error: (opt: string, chain: string) => callError(opt, chain), }, xcmVersion: { disabled: false, chains: [], + error: (opt: string, chain: string) => callError(opt, chain), }, keepAlive: { disabled: false, chains: [], + error: (opt: string, chain: string) => callError(opt, chain), }, transferLiquidToken: { disabled: false, chains: [], + error: (opt: string, chain: string) => callError(opt, chain), }, }; diff --git a/src/errors/disableOpts.ts b/src/errors/disableOpts.ts index be506bd8..b7cbd0d8 100644 --- a/src/errors/disableOpts.ts +++ b/src/errors/disableOpts.ts @@ -2,7 +2,6 @@ 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. @@ -16,11 +15,13 @@ export const disableOpts = ( ) => { if (opts.paysWithFeeOrigin) { const { paysWithFeeOrigin } = disabledOpts; - if (paysWithFeeOrigin.chains.includes(specName.toLowerCase())) { - throw new BaseError( - `paysWithFeeOrigin is disbaled for ${specName.toLowerCase()}.`, - BaseErrorsEnum.DisabledOption - ); + const chain = specName.toLowerCase(); + if (paysWithFeeOrigin.chains.includes(chain)) { + paysWithFeeOrigin.error(`paysWithFeeOrigin`, chain); + } + + if (paysWithFeeOrigin.chains.includes('*')) { + paysWithFeeOrigin.error(`paysWithFeeOrigin`, `all chains`); } } };