From 8f4acd20b54595105a605062f03428a76a48d3d0 Mon Sep 17 00:00:00 2001 From: tarikgul Date: Fri, 20 Oct 2023 14:31:35 -0400 Subject: [PATCH] Refactor ParaToPara --- src/createXcmTypes/ParaToPara.spec.ts | 31 +++++++++++---------------- src/createXcmTypes/ParaToPara.ts | 28 +++++++++++------------- 2 files changed, 26 insertions(+), 33 deletions(-) diff --git a/src/createXcmTypes/ParaToPara.spec.ts b/src/createXcmTypes/ParaToPara.spec.ts index 3a6c055a..108c241f 100644 --- a/src/createXcmTypes/ParaToPara.spec.ts +++ b/src/createXcmTypes/ParaToPara.spec.ts @@ -9,85 +9,80 @@ describe('ParaToPara', () => { describe('Beneficiary', () => { it('Should work for V2', () => { const beneficiary = ParaToPara.createBeneficiary( - mockParachainApi, '0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b', 2 ); const expectedRes = { - v2: { + V2: { parents: 0, interior: { X1: { AccountId32: { id: '0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b', - network: { - any: null, - }, + network: 'Any', }, }, }, }, }; - expect(beneficiary.toJSON()?.toString()).toStrictEqual(expectedRes.toString()); + expect(beneficiary).toStrictEqual(expectedRes); }); it('Should work for V3', () => { const beneficiary = ParaToPara.createBeneficiary( - mockParachainApi, '0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b', 3 ); const expectedRes = { - v3: { + V3: { parents: 0, interior: { X1: { AccountId32: { id: '0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b', - network: null, }, }, }, }, }; - expect(beneficiary.toJSON()?.toString()).toStrictEqual(expectedRes.toString()); + expect(beneficiary).toStrictEqual(expectedRes); }); }); describe('Destination', () => { it('Should work for V2', () => { - const destination = ParaToPara.createDest(mockParachainApi, '100', 2); + const destination = ParaToPara.createDest('100', 2); const expectedRes = { - v2: { + V2: { parents: 1, interior: { X1: { - Parachain: 100, + Parachain: '100', }, }, }, }; - expect(destination.toJSON()?.toString()).toStrictEqual(expectedRes.toString()); + expect(destination).toStrictEqual(expectedRes); }); it('Should work for V3', () => { - const destination = ParaToPara.createDest(mockParachainApi, '100', 3); + const destination = ParaToPara.createDest('100', 3); const expectedRes = { - v3: { + V3: { parents: 1, interior: { X1: { - Parachain: 100, + Parachain: '100', }, }, }, }; - expect(destination.toJSON()?.toString()).toStrictEqual(expectedRes.toString()); + expect(destination).toStrictEqual(expectedRes); }); }); describe('Assets', () => { diff --git a/src/createXcmTypes/ParaToPara.ts b/src/createXcmTypes/ParaToPara.ts index 05fcbb32..25af862d 100644 --- a/src/createXcmTypes/ParaToPara.ts +++ b/src/createXcmTypes/ParaToPara.ts @@ -5,7 +5,6 @@ import type { u32 } from '@polkadot/types'; import type { MultiAssetsV2, VersionedMultiAssets, - VersionedMultiLocation, WeightLimitV2, } from '@polkadot/types/interfaces'; import type { XcmV3MultiassetMultiAssets } from '@polkadot/types/lookup'; @@ -32,6 +31,7 @@ import type { CreateWeightLimitOpts, ICreateXcmType, IWeightLimit, + XcmBase, } from './types'; import { constructForeignAssetMultiLocationFromAssetId } from './util/constructForeignAssetMultiLocationFromAssetId'; import { dedupeMultiAssets } from './util/dedupeMultiAssets'; @@ -45,66 +45,64 @@ export const ParaToPara: ICreateXcmType = { /** * Create a XcmVersionedMultiLocation type for a beneficiary. * - * @param api ApiPromise * @param accountId The accountId of the beneficiary * @param xcmVersion The accepted xcm version */ - createBeneficiary: (api: ApiPromise, accountId: string, xcmVersion?: number): VersionedMultiLocation => { + createBeneficiary: (accountId: string, xcmVersion?: number): XcmBase => { if (xcmVersion == 2) { - return api.registry.createType('XcmVersionedMultiLocation', { + return { V2: { parents: 0, interior: { X1: { AccountId32: { network: 'Any', id: accountId } }, }, }, - }); + }; } - return api.registry.createType('XcmVersionedMultiLocation', { + return { V3: { parents: 0, interior: { X1: { AccountId32: { id: accountId } }, }, }, - }); + }; }, /** * Create a XcmVersionedMultiLocation type for a destination. * - * @param api ApiPromise * @param destId The parachain Id of the destination * @param xcmVersion The accepted xcm version */ - createDest: (api: ApiPromise, destId: string, xcmVersion?: number) => { + createDest: (destId: string, xcmVersion?: number): XcmBase => { if (xcmVersion === 2) { - return api.registry.createType('XcmVersionedMultiLocation', { + return { V2: { parents: 1, interior: { X1: { - parachain: destId, + Parachain: destId, }, }, }, - }); + }; } /** * Ensure that the `parents` field is a `1` when sending a destination MultiLocation * from a system parachain to a sovereign parachain. */ - return api.registry.createType('XcmVersionedMultiLocation', { + return { V3: { parents: 1, interior: { X1: { - parachain: destId, + Parachain: destId, }, }, }, - }); + }; }, /** * Create a VersionedMultiAsset type.