diff --git a/src/AssetTransferApi.ts b/src/AssetTransferApi.ts index 4960e0c5..b0468b40 100644 --- a/src/AssetTransferApi.ts +++ b/src/AssetTransferApi.ts @@ -271,39 +271,6 @@ export class AssetTransferApi { }); } } - - await checkXcmTxInputs( - _api, - destChainId, - assetIds, - amounts, - xcmDirection, - xcmPallet, - _specName, - registry, - isForeignAssetsTransfer, - isLiquidTokenTransfer, - isPrimaryParachainNativeAsset, - { - xcmVersion: declaredXcmVersion, - paysWithFeeDest, - isLimited, - weightLimit, - } - ); - - const assetType = this.fetchAssetType(xcmDirection, isForeignAssetsTransfer); - const assetCallType = this.fetchCallType( - originChainId, - destChainId, - assetIds, - xcmDirection, - assetType, - isForeignAssetsTransfer, - isPrimaryParachainNativeAsset, - registry - ); - const baseArgs = { api: _api, direction: xcmDirection as XcmDirection, @@ -324,6 +291,26 @@ export class AssetTransferApi { isForeignAssetsTransfer, }; + await checkXcmTxInputs( + { ...baseArgs, xcmPallet }, + { + ...baseOpts, + isPrimaryParachainNativeAsset, + } + ); + + const assetType = this.fetchAssetType(xcmDirection, isForeignAssetsTransfer); + const assetCallType = this.fetchCallType( + originChainId, + destChainId, + assetIds, + xcmDirection, + assetType, + isForeignAssetsTransfer, + isPrimaryParachainNativeAsset, + registry + ); + let txMethod: Methods; let transaction: SubmittableExtrinsic<'promise', ISubmittableResult>; if ( diff --git a/src/createXcmCalls/polkadotXcm/types.ts b/src/createXcmCalls/polkadotXcm/types.ts index ec51a780..be141218 100644 --- a/src/createXcmCalls/polkadotXcm/types.ts +++ b/src/createXcmCalls/polkadotXcm/types.ts @@ -1,18 +1,5 @@ // Copyright 2023 Parity Technologies (UK) Ltd. -import type { ApiPromise } from '@polkadot/api'; +import type { XcmBaseArgs } from '../../types'; -import type { Registry } from '../../registry'; -import type { XcmDirection } from '../../types'; - -export interface PolkadotXcmBaseArgs { - api: ApiPromise; - direction: XcmDirection; - destAddr: string; - assetIds: string[]; - amounts: string[]; - destChainId: string; - xcmVersion: number; - specName: string; - registry: Registry; -} +export type PolkadotXcmBaseArgs = XcmBaseArgs; diff --git a/src/createXcmCalls/xTokens/types.ts b/src/createXcmCalls/xTokens/types.ts index dd8785d1..0eb4df87 100644 --- a/src/createXcmCalls/xTokens/types.ts +++ b/src/createXcmCalls/xTokens/types.ts @@ -1,6 +1,6 @@ -import type { PolkadotXcmBaseArgs } from '../polkadotXcm/types'; +import type { XcmBaseArgs } from '../../types'; import type { XcmPalletName } from '../util/establishXcmPallet'; -export interface XTokensBaseArgs extends PolkadotXcmBaseArgs { +export interface XTokensBaseArgs extends XcmBaseArgs { xcmPallet: XcmPalletName; } diff --git a/src/createXcmTypes/types.ts b/src/createXcmTypes/types.ts index 90a3b499..f57fc2a9 100644 --- a/src/createXcmTypes/types.ts +++ b/src/createXcmTypes/types.ts @@ -279,13 +279,6 @@ export interface CreateWeightLimitOpts { weightLimit?: { refTime?: string; proofSize?: string }; } -export interface CheckXcmTxInputsOpts { - xcmVersion: number; - paysWithFeeDest?: string; - isLimited?: boolean; - weightLimit?: { refTime?: string; proofSize?: string }; -} - export interface ICreateXcmType { createBeneficiary: (accountId: string, xcmVersion: number) => XcmDestBenificiary; createDest: (destId: string, xcmVersion: number) => XcmDestBenificiary; diff --git a/src/errors/checkXcmTxInputs.ts b/src/errors/checkXcmTxInputs.ts index 9885a0b7..7593a859 100644 --- a/src/errors/checkXcmTxInputs.ts +++ b/src/errors/checkXcmTxInputs.ts @@ -6,16 +6,17 @@ import { isEthereumAddress } from '@polkadot/util-crypto'; import { MAX_ASSETS_FOR_TRANSFER, RELAY_CHAIN_IDS } from '../consts'; import { XcmPalletName } from '../createXcmCalls/util/establishXcmPallet'; -import { CheckXcmTxInputsOpts } from '../createXcmTypes/types'; import { foreignAssetMultiLocationIsInCacheOrRegistry } from '../createXcmTypes/util/foreignAssetMultiLocationIsInCacheOrRegistry'; import { foreignAssetsMultiLocationExists } from '../createXcmTypes/util/foreignAssetsMultiLocationExists'; import { isParachainPrimaryNativeAsset } from '../createXcmTypes/util/isParachainPrimaryNativeAsset'; import { multiLocationAssetIsParachainsNativeAsset } from '../createXcmTypes/util/multiLocationAssetIsParachainsNativeAsset'; import { Registry } from '../registry'; import type { ChainInfo, ChainInfoKeys } from '../registry/types'; +import type { XcmBaseArgsWithPallet } from '../types'; import { AssetInfo, Direction } from '../types'; import { validateNumber } from '../validate'; import { BaseError, BaseErrorsEnum } from './BaseError'; +import type { CheckXcmTxInputsOpts } from './types'; /** * Ensure when sending tx's to or from the relay chain that the length of the assetIds array @@ -1041,24 +1042,12 @@ export const checkAssetIdInput = async ( * @param specName * @param registry */ -export const checkXcmTxInputs = async ( - api: ApiPromise, - destChainId: string, - assetIds: string[], - amounts: string[], - xcmDirection: Direction, - xcmPallet: XcmPalletName, - specName: string, - registry: Registry, - isForeignAssetsTransfer: boolean, - isLiquidTokenTransfer: boolean, - isParachainPrimaryNativeAsset: boolean, - opts: CheckXcmTxInputsOpts -) => { - const { xcmVersion, paysWithFeeDest } = opts; +export const checkXcmTxInputs = async (baseArgs: XcmBaseArgsWithPallet, opts: CheckXcmTxInputsOpts) => { + const { api, direction, assetIds, amounts, destChainId, xcmVersion, specName, registry, xcmPallet } = baseArgs; + const { paysWithFeeDest, isForeignAssetsTransfer, isLiquidTokenTransfer, isPrimaryParachainNativeAsset } = opts; const relayChainInfo = registry.currentRelayRegistry; - if (isParachainPrimaryNativeAsset) { + if (isPrimaryParachainNativeAsset) { /** * Checks that the assetIds length is correct for primary native parachain asset tx */ @@ -1072,12 +1061,12 @@ export const checkXcmTxInputs = async ( /** * Checks that the XcmVersion works with `PaysWithFeeDest` option */ - checkXcmVersionIsValidForPaysWithFeeDest(xcmDirection, xcmVersion, paysWithFeeDest); + checkXcmVersionIsValidForPaysWithFeeDest(direction, xcmVersion, paysWithFeeDest); /** * Checks that the direction of the `transferLiquidToken` option is correct. */ - checkLiquidTokenTransferDirectionValidity(xcmDirection, isLiquidTokenTransfer); + checkLiquidTokenTransferDirectionValidity(direction, isLiquidTokenTransfer); /** * Checks to ensure that assetId's have a length no greater than MAX_ASSETS_FOR_TRANSFER @@ -1102,55 +1091,55 @@ export const checkXcmTxInputs = async ( assetIds, relayChainInfo, specName, - xcmDirection, + direction, registry, xcmVersion, isForeignAssetsTransfer, isLiquidTokenTransfer ); - if (xcmDirection === Direction.RelayToSystem) { + if (direction === Direction.RelayToSystem) { checkRelayAssetIdLength(assetIds); checkRelayAmountsLength(amounts); } - if (xcmDirection === Direction.RelayToPara) { + if (direction === Direction.RelayToPara) { checkRelayAssetIdLength(assetIds); checkRelayAmountsLength(amounts); } - if (xcmDirection === Direction.SystemToRelay) { + if (direction === Direction.SystemToRelay) { checkRelayAssetIdLength(assetIds); checkRelayAmountsLength(amounts); } - if (xcmDirection === Direction.SystemToPara) { + if (direction === Direction.SystemToPara) { if (isForeignAssetsTransfer) { checkMultiLocationIdLength(assetIds); checkMultiLocationAmountsLength(amounts); checkAssetsAmountMatch(assetIds, amounts); checkAssetsAmountMatch(assetIds, amounts); - checkMultiLocationsContainOnlyNativeOrForeignAssetsOfDestChain(xcmDirection, destChainId, assetIds); + checkMultiLocationsContainOnlyNativeOrForeignAssetsOfDestChain(direction, destChainId, assetIds); } checkAssetsAmountMatch(assetIds, amounts); } - if (xcmDirection === Direction.SystemToSystem) { + if (direction === Direction.SystemToSystem) { if (isForeignAssetsTransfer) { checkMultiLocationIdLength(assetIds); checkMultiLocationAmountsLength(amounts); checkAssetsAmountMatch(assetIds, amounts); - checkMultiLocationsContainOnlyNativeOrForeignAssetsOfDestChain(xcmDirection, destChainId, assetIds); + checkMultiLocationsContainOnlyNativeOrForeignAssetsOfDestChain(direction, destChainId, assetIds); } checkIfNativeRelayChainAssetPresentInMultiAssetIdList(assetIds, registry); } - if (xcmDirection === Direction.ParaToSystem || xcmDirection === Direction.ParaToPara) { - CheckXTokensPalletOriginIsNonForeignAssetTx(xcmDirection, xcmPallet, isForeignAssetsTransfer); - checkAssetsAmountMatch(assetIds, amounts, isParachainPrimaryNativeAsset); + if (direction === Direction.ParaToSystem || direction === Direction.ParaToPara) { + CheckXTokensPalletOriginIsNonForeignAssetTx(direction, xcmPallet, isForeignAssetsTransfer); + checkAssetsAmountMatch(assetIds, amounts, isPrimaryParachainNativeAsset); } - if (xcmDirection === Direction.ParaToRelay) { + if (direction === Direction.ParaToRelay) { checkRelayAssetIdLength(assetIds); checkRelayAmountsLength(amounts); } diff --git a/src/errors/types.ts b/src/errors/types.ts new file mode 100644 index 00000000..10d3dadc --- /dev/null +++ b/src/errors/types.ts @@ -0,0 +1,10 @@ +// Copyright 2023 Parity Technologies (UK) Ltd. + +export interface CheckXcmTxInputsOpts { + isForeignAssetsTransfer: boolean; + isLiquidTokenTransfer: boolean; + isPrimaryParachainNativeAsset: boolean; + paysWithFeeDest?: string; + isLimited?: boolean; + weightLimit?: { refTime?: string; proofSize?: string }; +} diff --git a/src/types.ts b/src/types.ts index c49bb586..e7e282b5 100644 --- a/src/types.ts +++ b/src/types.ts @@ -6,6 +6,8 @@ import type { InteriorMultiLocation } from '@polkadot/types/interfaces'; import type { ISubmittableResult } from '@polkadot/types/types'; import BN from 'bn.js'; +import { XcmPalletName } from './createXcmCalls/util/establishXcmPallet'; +import type { Registry } from './registry'; import type { ChainInfoRegistry } from './registry/types'; export type RequireOnlyOne = Pick> & @@ -301,6 +303,22 @@ export interface UnsignedTransaction extends SignerPayloadJSON { assetId: BN; } +export interface XcmBaseArgs { + api: ApiPromise; + direction: XcmDirection; + destAddr: string; + assetIds: string[]; + amounts: string[]; + destChainId: string; + xcmVersion: number; + specName: string; + registry: Registry; +} + +export interface XcmBaseArgsWithPallet extends XcmBaseArgs { + xcmPallet: XcmPalletName; +} + export interface LocalDest { Id: string; }