Skip to content

Commit

Permalink
cleanup args for checkXcmTxInputs
Browse files Browse the repository at this point in the history
  • Loading branch information
TarikGul committed Nov 21, 2023
1 parent 4bf50eb commit 65b18fa
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 88 deletions.
53 changes: 20 additions & 33 deletions src/AssetTransferApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 (
Expand Down
17 changes: 2 additions & 15 deletions src/createXcmCalls/polkadotXcm/types.ts
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 2 additions & 2 deletions src/createXcmCalls/xTokens/types.ts
Original file line number Diff line number Diff line change
@@ -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;
}
7 changes: 0 additions & 7 deletions src/createXcmTypes/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
51 changes: 20 additions & 31 deletions src/errors/checkXcmTxInputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
*/
Expand All @@ -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
Expand All @@ -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);
}
Expand Down
10 changes: 10 additions & 0 deletions src/errors/types.ts
Original file line number Diff line number Diff line change
@@ -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 };
}
18 changes: 18 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, Keys extends keyof T = keyof T> = Pick<T, Exclude<keyof T, Keys>> &
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 65b18fa

Please sign in to comment.