From 8b7634b37e352afbc41fbe7d1698c51fcd9b8016 Mon Sep 17 00:00:00 2001 From: tarikgul Date: Mon, 20 Nov 2023 19:47:24 -0500 Subject: [PATCH] refactor args for polkadotXcm calls --- src/AssetTransferApi.ts | 122 ++++--------- .../limitedReserveTransferAssets.spec.ts | 163 +++++++----------- .../limitedReserveTransferAssets.ts | 25 +-- .../polkadotXcm/limitedTeleportAssets.spec.ts | 66 +++---- .../polkadotXcm/limitedTeleportAssets.ts | 25 +-- .../polkadotXcm/reserveTransferAssets.spec.ts | 104 +++++------ .../polkadotXcm/reserveTransferAssets.ts | 25 +-- .../polkadotXcm/teleportAssets.spec.ts | 56 +++--- .../polkadotXcm/teleportAssets.ts | 25 +-- src/createXcmCalls/polkadotXcm/types.ts | 18 ++ 10 files changed, 219 insertions(+), 410 deletions(-) create mode 100644 src/createXcmCalls/polkadotXcm/types.ts diff --git a/src/AssetTransferApi.ts b/src/AssetTransferApi.ts index 2e0bad9f..8aab91d6 100644 --- a/src/AssetTransferApi.ts +++ b/src/AssetTransferApi.ts @@ -304,6 +304,26 @@ export class AssetTransferApi { registry ); + const baseArgs = { + api: _api, + direction: xcmDirection as XcmDirection, + destAddr: addr, + assetIds, + amounts, + destChainId, + xcmVersion: declaredXcmVersion, + specName: _specName, + registry: this.registry, + }; + + const baseOpts = { + isLimited, + weightLimit, + paysWithFeeDest, + isLiquidTokenTransfer, + isForeignAssetsTransfer, + }; + let txMethod: Methods; let transaction: SubmittableExtrinsic<'promise', ISubmittableResult>; if ( @@ -326,13 +346,7 @@ export class AssetTransferApi { _specName, this.registry, xcmPallet, - { - isLimited, - weightLimit, - paysWithFeeDest, - isForeignAssetsTransfer, - isLiquidTokenTransfer, - } + baseOpts ); } else if (paysWithFeeDest && paysWithFeeDest.includes('parents')) { txMethod = 'transferMultiassetWithFee'; @@ -347,13 +361,7 @@ export class AssetTransferApi { _specName, this.registry, xcmPallet, - { - isLimited, - weightLimit, - paysWithFeeDest, - isForeignAssetsTransfer, - isLiquidTokenTransfer, - } + baseOpts ); } else { txMethod = 'transferMultiassets'; @@ -368,94 +376,30 @@ export class AssetTransferApi { _specName, this.registry, xcmPallet, - { - isLimited, - weightLimit, - paysWithFeeDest, - isForeignAssetsTransfer, - isLiquidTokenTransfer, - } + baseOpts ); } } else if (assetCallType === AssetCallType.Reserve) { if (isLimited) { txMethod = 'limitedReserveTransferAssets'; - transaction = await limitedReserveTransferAssets( - _api, - xcmDirection as XcmDirection, - addr, - assetIds, - amounts, - destChainId, - declaredXcmVersion, - _specName, - this.registry, - { - isLimited, - weightLimit, - paysWithFeeDest, - isLiquidTokenTransfer, - isForeignAssetsTransfer, - } - ); + transaction = await limitedReserveTransferAssets(baseArgs, baseOpts); } else { txMethod = 'reserveTransferAssets'; - transaction = await reserveTransferAssets( - _api, - xcmDirection as XcmDirection, - addr, - assetIds, - amounts, - destChainId, - declaredXcmVersion, - _specName, - this.registry, - { - paysWithFeeDest, - isLiquidTokenTransfer, - isForeignAssetsTransfer, - } - ); + transaction = await reserveTransferAssets(baseArgs, baseOpts); } } else { if (isLimited) { txMethod = 'limitedTeleportAssets'; - transaction = await limitedTeleportAssets( - _api, - xcmDirection as XcmDirection, - addr, - assetIds, - amounts, - destChainId, - declaredXcmVersion, - _specName, - this.registry, - { - isLimited, - weightLimit, - paysWithFeeDest, - isForeignAssetsTransfer, - isLiquidTokenTransfer: false, - } - ); + transaction = await limitedTeleportAssets(baseArgs, { + ...baseOpts, + isLiquidTokenTransfer: false, + }); } else { txMethod = 'teleportAssets'; - transaction = await teleportAssets( - _api, - xcmDirection as XcmDirection, - addr, - assetIds, - amounts, - destChainId, - declaredXcmVersion, - _specName, - this.registry, - { - paysWithFeeDest, - isForeignAssetsTransfer, - isLiquidTokenTransfer: false, - } - ); + transaction = await teleportAssets(baseArgs, { + ...baseOpts, + isLiquidTokenTransfer: false, + }); } } diff --git a/src/createXcmCalls/polkadotXcm/limitedReserveTransferAssets.spec.ts b/src/createXcmCalls/polkadotXcm/limitedReserveTransferAssets.spec.ts index e80c6f0c..9d596ac1 100644 --- a/src/createXcmCalls/polkadotXcm/limitedReserveTransferAssets.spec.ts +++ b/src/createXcmCalls/polkadotXcm/limitedReserveTransferAssets.spec.ts @@ -4,13 +4,28 @@ import type { ApiPromise } from '@polkadot/api'; import { Registry } from '../../registry'; import { adjustedMockSystemApi } from '../../testHelpers/adjustedMockSystemApi'; -import { Direction } from '../../types'; +import { Direction, XcmDirection } from '../../types'; import { limitedReserveTransferAssets } from './limitedReserveTransferAssets'; describe('limitedReserveTransferAssets', () => { const registry = new Registry('statemine', {}); describe('SystemToPara', () => { const isLiquidTokenTransfer = false; + const baseArgs = { + api: adjustedMockSystemApi, + direction: Direction.SystemToPara as XcmDirection, + destAddr: '0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b', + assetIds: ['1'], + amounts: ['100'], + destChainId: '2023', + xcmVersion: 2, + specName: 'statemine', + registry, + }; + const FAbaseArgs = { + ...baseArgs, + assetIds: ['{"parents":"1","interior":{ "X2":[{"Parachain":"2125"},{"GeneralIndex":"0"}]}}'], + }; it('Should correctly construct a tx for a system parachain with V2', async () => { const isLimited = true; const refTime = '1000'; @@ -18,27 +33,16 @@ describe('limitedReserveTransferAssets', () => { const paysWithFeeDest = undefined; const isForeignAssetsTransfer = false; - const ext = await limitedReserveTransferAssets( - adjustedMockSystemApi, - Direction.SystemToPara, - '0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b', - ['1'], - ['100'], - '2023', - 2, - 'statemine', - registry, - { - isLimited, - weightLimit: { - refTime, - proofSize, - }, - paysWithFeeDest, - isLiquidTokenTransfer, - isForeignAssetsTransfer, - } - ); + const ext = await limitedReserveTransferAssets(baseArgs, { + isLimited, + weightLimit: { + refTime, + proofSize, + }, + paysWithFeeDest, + isLiquidTokenTransfer, + isForeignAssetsTransfer, + }); expect(ext.toHex()).toBe( '0x0d01041f08010101009d1f0100010100f5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b0104000002043205040091010000000001a10f411f' @@ -51,27 +55,16 @@ describe('limitedReserveTransferAssets', () => { const paysWithFeeDest = undefined; const isForeignAssetsTransfer = false; - const ext = await limitedReserveTransferAssets( - adjustedMockSystemApi, - Direction.SystemToPara, - '0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b', - ['1'], - ['100'], - '2023', - 2, - 'statemine', - registry, - { - isLimited, - weightLimit: { - refTime, - proofSize, - }, - paysWithFeeDest, - isLiquidTokenTransfer, - isForeignAssetsTransfer, - } - ); + const ext = await limitedReserveTransferAssets(baseArgs, { + isLimited, + weightLimit: { + refTime, + proofSize, + }, + paysWithFeeDest, + isLiquidTokenTransfer, + isForeignAssetsTransfer, + }); expect(ext.toHex()).toBe( '0x1501041f08010101009d1f0100010100f5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b010400000204320504009101000000000102286bee411f' @@ -84,52 +77,31 @@ describe('limitedReserveTransferAssets', () => { const proofSize = '2000'; const mockApi = { tx: {} } as unknown as ApiPromise; + const mockApiBaseArgs = { ...baseArgs, api: mockApi }; const paysWithFeeDest = undefined; const isForeignAssetsTransfer = true; await expect(async () => { - await limitedReserveTransferAssets( - mockApi, - Direction.SystemToPara, - '0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b', - ['1'], - ['100'], - '2023', - 2, - 'statemine', - registry, - { - isLimited, - weightLimit: { - refTime, - proofSize, - }, - paysWithFeeDest, - isLiquidTokenTransfer, - isForeignAssetsTransfer, - } - ); + await limitedReserveTransferAssets(mockApiBaseArgs, { + isLimited, + weightLimit: { + refTime, + proofSize, + }, + paysWithFeeDest, + isLiquidTokenTransfer, + isForeignAssetsTransfer, + }); }).rejects.toThrowError("Can't find the `polkadotXcm` or `xcmPallet` pallet with the given API"); }); it('Should correctly construct a foreign asset tx for a system parachain with V2', async () => { const paysWithFeeDest = undefined; const isForeignAssetsTransfer = true; - const ext = await limitedReserveTransferAssets( - adjustedMockSystemApi, - Direction.SystemToPara, - '0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b', - ['{"parents":"1","interior":{ "X2":[{"Parachain":"2125"},{"GeneralIndex":"0"}]}}'], - ['100'], - '2023', - 2, - 'statemine', - registry, - { - paysWithFeeDest, - isLiquidTokenTransfer, - isForeignAssetsTransfer, - } - ); + const ext = await limitedReserveTransferAssets(FAbaseArgs, { + paysWithFeeDest, + isLiquidTokenTransfer, + isForeignAssetsTransfer, + }); expect(ext.toHex()).toBe( '0x0901041f08010101009d1f0100010100f5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b0104000103043500352105000091010000000000' @@ -143,27 +115,16 @@ describe('limitedReserveTransferAssets', () => { const paysWithFeeDest = undefined; const isForeignAssetsTransfer = true; - const ext = await limitedReserveTransferAssets( - adjustedMockSystemApi, - Direction.SystemToPara, - '0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b', - ['{"parents":"1","interior":{ "X2":[{"Parachain":"2125"},{"GeneralIndex":"0"}]}}'], - ['100'], - '2023', - 2, - 'statemine', - registry, - { - isLimited, - weightLimit: { - refTime, - proofSize, - }, - paysWithFeeDest, - isLiquidTokenTransfer, - isForeignAssetsTransfer, - } - ); + const ext = await limitedReserveTransferAssets(FAbaseArgs, { + isLimited, + weightLimit: { + refTime, + proofSize, + }, + paysWithFeeDest, + isLiquidTokenTransfer, + isForeignAssetsTransfer, + }); expect(ext.toHex()).toBe( '0x2101041f08010101009d1f0100010100f5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b010400010304350035210500009101000000000102286bee411f' diff --git a/src/createXcmCalls/polkadotXcm/limitedReserveTransferAssets.ts b/src/createXcmCalls/polkadotXcm/limitedReserveTransferAssets.ts index 89807c2c..aedbe44e 100644 --- a/src/createXcmCalls/polkadotXcm/limitedReserveTransferAssets.ts +++ b/src/createXcmCalls/polkadotXcm/limitedReserveTransferAssets.ts @@ -1,43 +1,26 @@ // Copyright 2023 Parity Technologies (UK) Ltd. -import type { ApiPromise } from '@polkadot/api'; import type { SubmittableExtrinsic } from '@polkadot/api/submittable/types'; import type { ISubmittableResult } from '@polkadot/types/types'; import { createXcmTypes } from '../../createXcmTypes'; -import type { Registry } from '../../registry'; -import { XcmDirection } from '../../types'; import { normalizeArrToStr } from '../../util/normalizeArrToStr'; import type { CreateXcmCallOpts } from '../types'; import { establishXcmPallet } from '../util/establishXcmPallet'; +import type { PolkadotXcmBaseArgs } from './types'; /** * Build a Polkadot-js SubmittableExtrinsic for a `limitedReserveTransferAssets` * call. * - * @param api ApiPromise - * @param direction Denotes the xcm direction of the call. - * @param destAddr The address the funds will be transfered too. - * @param assetIds An array of asset ids. Note, this should be the same size and order as amounts. - * @param amounts An array of amounts. Note, this should be the same size and order as assetIds. - * @param destChainId The id of the destination chain. This will be zero for a relay chain. - * @param xcmVersion Supported XCM version. - * @param specName The specName for the current chain - * @param registry Registry + * @param baseArgs The base args needed to construct this call. * @param opts CreateXcmCallOpts */ export const limitedReserveTransferAssets = async ( - api: ApiPromise, - direction: XcmDirection, - destAddr: string, - assetIds: string[], - amounts: string[], - destChainId: string, - xcmVersion: number, - specName: string, - registry: Registry, + baseArgs: PolkadotXcmBaseArgs, opts: CreateXcmCallOpts ): Promise> => { + const { api, direction, destAddr, assetIds, amounts, destChainId, xcmVersion, specName, registry } = baseArgs; const { isLimited, weightLimit, paysWithFeeDest, isLiquidTokenTransfer, isForeignAssetsTransfer } = opts; const pallet = establishXcmPallet(api); const ext = api.tx[pallet].limitedReserveTransferAssets; diff --git a/src/createXcmCalls/polkadotXcm/limitedTeleportAssets.spec.ts b/src/createXcmCalls/polkadotXcm/limitedTeleportAssets.spec.ts index 56b4c8c9..41c63ffe 100644 --- a/src/createXcmCalls/polkadotXcm/limitedTeleportAssets.spec.ts +++ b/src/createXcmCalls/polkadotXcm/limitedTeleportAssets.spec.ts @@ -2,13 +2,24 @@ import type { ApiPromise } from '@polkadot/api'; import { Registry } from '../../registry'; import { mockSystemApi } from '../../testHelpers/mockSystemApi'; -import { Direction } from '../../types'; +import { Direction, XcmDirection } from '../../types'; import { limitedTeleportAssets } from './limitedTeleportAssets'; describe('limitedTeleportAssets', () => { const registry = new Registry('statemine', {}); describe('SystemToPara', () => { const isLiquidTokenTransfer = false; + const baseArgs = { + api: mockSystemApi, + direction: Direction.SystemToPara as XcmDirection, + destAddr: '0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b', + assetIds: ['1'], + amounts: ['100'], + destChainId: '1000', + xcmVersion: 2, + specName: 'statemine', + registry, + }; it('Should correctly construct a tx for a system parachain with V2', async () => { const isLimited = true; const refTime = '1000000000'; @@ -17,27 +28,16 @@ describe('limitedTeleportAssets', () => { const paysWithFeeDest = undefined; const isForeignAssetsTransfer = false; - const ext = await limitedTeleportAssets( - mockSystemApi, - Direction.SystemToPara, - '0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b', - ['1'], - ['100'], - '1000', - 2, - 'statemine', - registry, - { - isLimited, - weightLimit: { - refTime, - proofSize, - }, - paysWithFeeDest, - isLiquidTokenTransfer, - isForeignAssetsTransfer, - } - ); + const ext = await limitedTeleportAssets(baseArgs, { + isLimited, + weightLimit: { + refTime, + proofSize, + }, + paysWithFeeDest, + isLiquidTokenTransfer, + isForeignAssetsTransfer, + }); expect(ext.toHex()).toBe( '0x1501041f0901010100a10f0100010100f5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b010400000204320504009101000000000102286bee411f' @@ -45,26 +45,16 @@ describe('limitedTeleportAssets', () => { }); it('Should error when a api does not support the required pallets', async () => { const mockApi = { tx: {} } as unknown as ApiPromise; + const mockApiBaseArgs = { ...baseArgs, api: mockApi }; const paysWithFeeDest = undefined; const isForeignAssetsTransfer = false; await expect(async () => { - await limitedTeleportAssets( - mockApi, - Direction.SystemToPara, - '0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b', - ['1'], - ['100'], - '1000', - 2, - 'statemine', - registry, - { - paysWithFeeDest, - isLiquidTokenTransfer, - isForeignAssetsTransfer, - } - ); + await limitedTeleportAssets(mockApiBaseArgs, { + paysWithFeeDest, + isLiquidTokenTransfer, + isForeignAssetsTransfer, + }); }).rejects.toThrowError("Can't find the `polkadotXcm` or `xcmPallet` pallet with the given API"); }); }); diff --git a/src/createXcmCalls/polkadotXcm/limitedTeleportAssets.ts b/src/createXcmCalls/polkadotXcm/limitedTeleportAssets.ts index 0efd7031..24539605 100644 --- a/src/createXcmCalls/polkadotXcm/limitedTeleportAssets.ts +++ b/src/createXcmCalls/polkadotXcm/limitedTeleportAssets.ts @@ -1,42 +1,25 @@ // Copyright 2023 Parity Technologies (UK) Ltd. -import type { ApiPromise } from '@polkadot/api'; import type { SubmittableExtrinsic } from '@polkadot/api/submittable/types'; import type { ISubmittableResult } from '@polkadot/types/types'; import { createXcmTypes } from '../../createXcmTypes'; -import type { Registry } from '../../registry'; -import { XcmDirection } from '../../types'; import { normalizeArrToStr } from '../../util/normalizeArrToStr'; import type { CreateXcmCallOpts } from '../types'; import { establishXcmPallet } from '../util/establishXcmPallet'; +import type { PolkadotXcmBaseArgs } from './types'; /** * Build a Polkadot-js SubmittableExtrinsic for a `limitedTeleportAssets` call. * - * @param api ApiPromise - * @param direction Denotes the xcm direction of the call. - * @param destAddr The address the funds will be transfered too. - * @param assetIds An array of asset ids. Note, this should be the same size and order as amounts. - * @param amounts An array of amounts. Note, this should be the same size and order as assetIds. - * @param destChainId The id of the destination chain. This will be zero for a relay chain. - * @param xcmVersion Supported XCM version. - * @param specName The specName for the current chain - * @param registry Registry + * @param baseArgs The base args needed to construct this call. * @param opts CreateXcmCallOpts */ export const limitedTeleportAssets = async ( - api: ApiPromise, - direction: XcmDirection, - destAddr: string, - assetIds: string[], - amounts: string[], - destChainId: string, - xcmVersion: number, - specName: string, - registry: Registry, + baseArgs: PolkadotXcmBaseArgs, opts: CreateXcmCallOpts ): Promise> => { + const { api, direction, destAddr, assetIds, amounts, destChainId, xcmVersion, specName, registry } = baseArgs; const { isLimited, weightLimit, paysWithFeeDest, isForeignAssetsTransfer } = opts; const pallet = establishXcmPallet(api); const ext = api.tx[pallet].limitedTeleportAssets; diff --git a/src/createXcmCalls/polkadotXcm/reserveTransferAssets.spec.ts b/src/createXcmCalls/polkadotXcm/reserveTransferAssets.spec.ts index 1d126c1a..1d5450e9 100644 --- a/src/createXcmCalls/polkadotXcm/reserveTransferAssets.spec.ts +++ b/src/createXcmCalls/polkadotXcm/reserveTransferAssets.spec.ts @@ -4,33 +4,38 @@ import type { ApiPromise } from '@polkadot/api'; import { Registry } from '../../registry'; import { adjustedMockSystemApi } from '../../testHelpers/adjustedMockSystemApi'; -import { Direction } from '../../types'; +import { Direction, XcmDirection } from '../../types'; import { reserveTransferAssets } from './reserveTransferAssets'; describe('reserveTransferAssets', () => { const registry = new Registry('statemine', {}); describe('SystemToPara', () => { const isLiquidTokenTransfer = false; + const baseArgs = { + api: adjustedMockSystemApi, + direction: Direction.SystemToPara as XcmDirection, + destAddr: '0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b', + assetIds: ['1'], + amounts: ['100'], + destChainId: '1000', + xcmVersion: 2, + specName: 'statemine', + registry, + }; + const FAbaseArgs = { + ...baseArgs, + assetIds: ['{"parents":"1","interior":{ "X2":[{"Parachain":"2125"},{"GeneralIndex":"0"}]}}'], + destChainId: '2023', + }; it('Should correctly construct a tx for a system parachain with V2', async () => { const paysWithFeeDest = undefined; const isForeignAssetsTransfer = false; - const ext = await reserveTransferAssets( - adjustedMockSystemApi, - Direction.SystemToPara, - '0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b', - ['1'], - ['100'], - '1000', - 2, - 'statemine', - registry, - { - paysWithFeeDest, - isLiquidTokenTransfer, - isForeignAssetsTransfer, - } - ); + const ext = await reserveTransferAssets(baseArgs, { + paysWithFeeDest, + isLiquidTokenTransfer, + isForeignAssetsTransfer, + }); expect(ext.toHex()).toBe( '0xf8041f0201010100a10f0100010100f5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b01040000020432050400910100000000' @@ -38,26 +43,16 @@ describe('reserveTransferAssets', () => { }); it('Should error when a api does not support the required pallets', async () => { const mockApi = { tx: {} } as unknown as ApiPromise; + const mockApiBaseArgs = { ...baseArgs, api: mockApi }; const paysWithFeeDest = undefined; const isForeignAssetsTransfer = false; await expect(async () => { - await reserveTransferAssets( - mockApi, - Direction.SystemToPara, - '0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b', - ['1'], - ['100'], - '1000', - 2, - 'statemine', - registry, - { - paysWithFeeDest, - isLiquidTokenTransfer, - isForeignAssetsTransfer, - } - ); + await reserveTransferAssets(mockApiBaseArgs, { + paysWithFeeDest, + isLiquidTokenTransfer, + isForeignAssetsTransfer, + }); }).rejects.toThrowError("Can't find the `polkadotXcm` or `xcmPallet` pallet with the given API"); }); @@ -65,22 +60,11 @@ describe('reserveTransferAssets', () => { const paysWithFeeDest = undefined; const isForeignAssetsTransfer = true; - const ext = await reserveTransferAssets( - adjustedMockSystemApi, - Direction.SystemToPara, - '0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b', - ['{"parents":"1","interior":{ "X2":[{"Parachain":"2125"},{"GeneralIndex":"0"}]}}'], - ['100'], - '2023', - 2, - 'statemine', - registry, - { - paysWithFeeDest, - isLiquidTokenTransfer, - isForeignAssetsTransfer, - } - ); + const ext = await reserveTransferAssets(FAbaseArgs, { + paysWithFeeDest, + isLiquidTokenTransfer, + isForeignAssetsTransfer, + }); expect(ext.toHex()).toBe( '0x0501041f02010101009d1f0100010100f5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b01040001030435003521050000910100000000' @@ -90,23 +74,13 @@ describe('reserveTransferAssets', () => { it('Should correctly construct a foreign asset tx for a system parachain with V3 TEST', async () => { const paysWithFeeDest = undefined; const isForeignAssetsTransfer = true; + const xcmVersionBaseArgs = { ...FAbaseArgs, xcmVersion: 3 }; - const ext = await reserveTransferAssets( - adjustedMockSystemApi, - Direction.SystemToPara, - '0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b', - ['{"parents":"1","interior":{ "X2":[{"Parachain":"2125"},{"GeneralIndex":"0"}]}}'], - ['100'], - '2023', - 3, - 'statemine', - registry, - { - paysWithFeeDest, - isLiquidTokenTransfer, - isForeignAssetsTransfer, - } - ); + const ext = await reserveTransferAssets(xcmVersionBaseArgs, { + paysWithFeeDest, + isLiquidTokenTransfer, + isForeignAssetsTransfer, + }); expect(ext.toHex()).toBe( '0x0501041f02030101009d1f0300010100f5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b03040001030435003521050000910100000000' diff --git a/src/createXcmCalls/polkadotXcm/reserveTransferAssets.ts b/src/createXcmCalls/polkadotXcm/reserveTransferAssets.ts index 28895ec6..97b45dcd 100644 --- a/src/createXcmCalls/polkadotXcm/reserveTransferAssets.ts +++ b/src/createXcmCalls/polkadotXcm/reserveTransferAssets.ts @@ -1,42 +1,25 @@ // Copyright 2023 Parity Technologies (UK) Ltd. -import type { ApiPromise } from '@polkadot/api'; import type { SubmittableExtrinsic } from '@polkadot/api/submittable/types'; import type { ISubmittableResult } from '@polkadot/types/types'; import { createXcmTypes } from '../../createXcmTypes'; -import type { Registry } from '../../registry'; -import { XcmDirection } from '../../types'; import { normalizeArrToStr } from '../../util/normalizeArrToStr'; import type { CreateXcmCallOpts } from '../types'; import { establishXcmPallet } from '../util/establishXcmPallet'; +import type { PolkadotXcmBaseArgs } from './types'; /** * Build a Polkadot-js SubmittableExtrinsic for a `reserveTransferAssets` call. * - * @param api ApiPromise - * @param direction Denotes the xcm direction of the call. - * @param destAddr The address the funds will be transfered too. - * @param assetIds An array of asset ids. Note, this should be the same size and order as amounts. - * @param amounts An array of amounts. Note, this should be the same size and order as assetIds. - * @param destChainId The id of the destination chain. This will be zero for a relay chain. - * @param xcmVersion Supported XCM version. - * @param specName The specName for the current chain - * @param registry Registry + * @param baseArgs The base args needed to construct this call. * @param opts CreateXcmCallOpts */ export const reserveTransferAssets = async ( - api: ApiPromise, - direction: XcmDirection, - destAddr: string, - assetIds: string[], - amounts: string[], - destChainId: string, - xcmVersion: number, - specName: string, - registry: Registry, + baseArgs: PolkadotXcmBaseArgs, opts: CreateXcmCallOpts ): Promise> => { + const { api, direction, destAddr, assetIds, amounts, destChainId, xcmVersion, specName, registry } = baseArgs; const { paysWithFeeDest, isLiquidTokenTransfer, isForeignAssetsTransfer } = opts; const pallet = establishXcmPallet(api); const ext = api.tx[pallet].reserveTransferAssets; diff --git a/src/createXcmCalls/polkadotXcm/teleportAssets.spec.ts b/src/createXcmCalls/polkadotXcm/teleportAssets.spec.ts index ea175536..655dcb0e 100644 --- a/src/createXcmCalls/polkadotXcm/teleportAssets.spec.ts +++ b/src/createXcmCalls/polkadotXcm/teleportAssets.spec.ts @@ -2,33 +2,33 @@ import type { ApiPromise } from '@polkadot/api'; import { Registry } from '../../registry'; import { mockSystemApi } from '../../testHelpers/mockSystemApi'; -import { Direction } from '../../types'; +import { Direction, XcmDirection } from '../../types'; import { teleportAssets } from './teleportAssets'; describe('teleportAssets', () => { const registry = new Registry('statemine', {}); describe('SystemToPara', () => { const isLiquidTokenTransfer = false; + const baseArgs = { + api: mockSystemApi, + direction: Direction.SystemToPara as XcmDirection, + destAddr: '0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b', + assetIds: ['1'], + amounts: ['100'], + destChainId: '2004', + xcmVersion: 2, + specName: 'statemine', + registry, + }; it('Should correctly construct a tx for a system parachain with V2', async () => { const paysWithFeeDest = undefined; const isForeignAssetsTransfer = false; - const ext = await teleportAssets( - mockSystemApi, - Direction.SystemToPara, - '0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b', - ['1'], - ['100'], - '2004', - 2, - 'statemine', - registry, - { - paysWithFeeDest, - isLiquidTokenTransfer, - isForeignAssetsTransfer, - } - ); + const ext = await teleportAssets(baseArgs, { + paysWithFeeDest, + isLiquidTokenTransfer, + isForeignAssetsTransfer, + }); expect(ext.toHex()).toBe( '0xf8041f0101010100511f0100010100f5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b01040000020432050400910100000000' @@ -36,26 +36,16 @@ describe('teleportAssets', () => { }); it('Should error when a api does not support the required pallets', async () => { const mockApi = { tx: {} } as unknown as ApiPromise; + const mockApiBaseArgs = { ...baseArgs, api: mockApi }; const paysWithFeeDest = undefined; const isForeignAssetsTransfer = true; await expect(async () => { - await teleportAssets( - mockApi, - Direction.SystemToPara, - '0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b', - ['1'], - ['100'], - '1000', - 2, - 'statemine', - registry, - { - paysWithFeeDest, - isLiquidTokenTransfer, - isForeignAssetsTransfer, - } - ); + await teleportAssets(mockApiBaseArgs, { + paysWithFeeDest, + isLiquidTokenTransfer, + isForeignAssetsTransfer, + }); }).rejects.toThrowError("Can't find the `polkadotXcm` or `xcmPallet` pallet with the given API"); }); }); diff --git a/src/createXcmCalls/polkadotXcm/teleportAssets.ts b/src/createXcmCalls/polkadotXcm/teleportAssets.ts index cf5220e6..c868bd21 100644 --- a/src/createXcmCalls/polkadotXcm/teleportAssets.ts +++ b/src/createXcmCalls/polkadotXcm/teleportAssets.ts @@ -1,42 +1,25 @@ // Copyright 2023 Parity Technologies (UK) Ltd. -import type { ApiPromise } from '@polkadot/api'; import type { SubmittableExtrinsic } from '@polkadot/api/submittable/types'; import type { ISubmittableResult } from '@polkadot/types/types'; import { createXcmTypes } from '../../createXcmTypes'; -import { Registry } from '../../registry'; -import { XcmDirection } from '../../types'; import { normalizeArrToStr } from '../../util/normalizeArrToStr'; import type { CreateXcmCallOpts } from '../types'; import { establishXcmPallet } from '../util/establishXcmPallet'; +import type { PolkadotXcmBaseArgs } from './types'; /** * Build a Polkadot-js SubmittableExtrinsic for a `teleportAssets` call. * - * @param api ApiPromise - * @param direction Denotes the xcm direction of the call. - * @param destAddr The address the funds will be transfered too. - * @param assetIds An array of asset ids. Note, this should be the same size and order as amounts. - * @param amounts An array of amounts. Note, this should be the same size and order as assetIds. - * @param destChainId The id of the destination chain. This will be zero for a relay chain. - * @param xcmVersion Supported XCM version. - * @param specName The specName for the current chain - * @param registry Registry + * @param baseArgs The base args needed to construct this call. * @param opts CreateXcmCallOpts */ export const teleportAssets = async ( - api: ApiPromise, - direction: XcmDirection, - destAddr: string, - assetIds: string[], - amounts: string[], - destChainId: string, - xcmVersion: number, - specName: string, - registry: Registry, + baseArgs: PolkadotXcmBaseArgs, opts: CreateXcmCallOpts ): Promise> => { + const { api, direction, destAddr, assetIds, amounts, destChainId, xcmVersion, specName, registry } = baseArgs; const { paysWithFeeDest, isForeignAssetsTransfer } = opts; const pallet = establishXcmPallet(api); const ext = api.tx[pallet].teleportAssets; diff --git a/src/createXcmCalls/polkadotXcm/types.ts b/src/createXcmCalls/polkadotXcm/types.ts new file mode 100644 index 00000000..cc01fccf --- /dev/null +++ b/src/createXcmCalls/polkadotXcm/types.ts @@ -0,0 +1,18 @@ +// Copyright 2023 Parity Technologies (UK) Ltd. + +import type { ApiPromise } from '@polkadot/api'; + +import type { Registry } from '../../registry'; +import type { XcmDirection } from '../../types'; + +export type PolkadotXcmBaseArgs = { + api: ApiPromise; + direction: XcmDirection; + destAddr: string; + assetIds: string[]; + amounts: string[]; + destChainId: string; + xcmVersion: number; + specName: string; + registry: Registry; +};