From d6aca26d24ff44aae675f40f17dfc48d94ef21bf Mon Sep 17 00:00:00 2001 From: Francisco Gamundi <52399794+fxgamundi@users.noreply.github.com> Date: Thu, 9 Mar 2023 17:02:48 +0100 Subject: [PATCH] Autodetect ML version available for polkdotXcm extrinsics (#64) * Autodetect ML version available for polkdotXcm extrinsics * Update snapshots * Removed origin * Removed origin from tokens deposits definitions * Updated tests --- .../config/src/config/moonbase/assets/tt1.ts | 3 +- .../config/src/config/moonbase/assets/unit.ts | 4 +- .../config/src/config/moonbeam/assets/astr.ts | 3 +- .../config/src/config/moonbeam/assets/dot.ts | 4 +- .../config/src/config/moonbeam/assets/glmr.ts | 3 +- .../config/src/config/moonbeam/assets/ring.ts | 3 +- .../config/src/config/moonbeam/assets/usdt.ts | 3 +- .../src/config/moonriver/assets/crab.ts | 3 +- .../config/src/config/moonriver/assets/csm.ts | 4 +- .../config/src/config/moonriver/assets/ksm.ts | 4 +- .../src/config/moonriver/assets/movr.ts | 3 +- .../src/config/moonriver/assets/rmrk.ts | 3 +- .../config/src/config/moonriver/assets/sdn.ts | 3 +- .../src/config/moonriver/assets/usdt.ts | 3 +- .../config/src/config/moonriver/assets/xrt.ts | 3 +- packages/config/src/extrinsic/extrinsic.ts | 4 +- .../__snapshots__/polkadotXcm.test.ts.snap | 83 +----- .../polkadotXcm/polkadotXcm.interfaces.ts | 138 ++-------- .../extrinsic/polkadotXcm/polkadotXcm.test.ts | 46 +--- .../src/extrinsic/polkadotXcm/polkadotXcm.ts | 246 +++++++----------- .../extrinsic/polkadotXcm/polkadotXcm.util.ts | 73 +++--- .../__snapshots__/xcmPallet.test.ts.snap | 57 +--- .../src/extrinsic/xcmPallet/xcmPallet.test.ts | 49 +--- .../src/extrinsic/xcmPallet/xcmPallet.ts | 89 +++---- 24 files changed, 233 insertions(+), 601 deletions(-) diff --git a/packages/config/src/config/moonbase/assets/tt1.ts b/packages/config/src/config/moonbase/assets/tt1.ts index 162dcc0c..202b9ec4 100644 --- a/packages/config/src/config/moonbase/assets/tt1.ts +++ b/packages/config/src/config/moonbase/assets/tt1.ts @@ -27,8 +27,7 @@ export const TT1: MoonbaseXcmConfig = { .polkadotXcm() .limitedReserveTransferAssets() .successEvent(PolkadotXcmExtrinsicSuccessEvent.Attempted) - .origin(origin) - .V1() + .V1V2() .X2(getPalletInstance(origin), originAssetId), }, }, diff --git a/packages/config/src/config/moonbase/assets/unit.ts b/packages/config/src/config/moonbase/assets/unit.ts index b9ede792..cd9caae6 100644 --- a/packages/config/src/config/moonbase/assets/unit.ts +++ b/packages/config/src/config/moonbase/assets/unit.ts @@ -22,9 +22,7 @@ export const UNIT: MoonbaseXcmConfig = { extrinsic: extrinsic .xcmPallet() .limitedReserveTransferAssets() - .successEvent(PolkadotXcmExtrinsicSuccessEvent.Attempted) - .origin(origin) - .V2(), + .successEvent(PolkadotXcmExtrinsicSuccessEvent.Attempted), }, }, withdraw: { diff --git a/packages/config/src/config/moonbeam/assets/astr.ts b/packages/config/src/config/moonbeam/assets/astr.ts index 68e56f29..bcbedbbf 100644 --- a/packages/config/src/config/moonbeam/assets/astr.ts +++ b/packages/config/src/config/moonbeam/assets/astr.ts @@ -23,8 +23,7 @@ export const ASTR: MoonbeamXcmConfig = { .polkadotXcm() .limitedReserveTransferAssets() .successEvent(PolkadotXcmExtrinsicSuccessEvent.Attempted) - .origin(origin) - .V1() + .V1V2() .here(), }, }, diff --git a/packages/config/src/config/moonbeam/assets/dot.ts b/packages/config/src/config/moonbeam/assets/dot.ts index 70c04617..13390508 100644 --- a/packages/config/src/config/moonbeam/assets/dot.ts +++ b/packages/config/src/config/moonbeam/assets/dot.ts @@ -22,9 +22,7 @@ export const DOT: MoonbeamXcmConfig = { extrinsic: extrinsic .xcmPallet() .limitedReserveTransferAssets() - .successEvent(PolkadotXcmExtrinsicSuccessEvent.Attempted) - .origin(origin) - .V0(), + .successEvent(PolkadotXcmExtrinsicSuccessEvent.Attempted), }, }, withdraw: { diff --git a/packages/config/src/config/moonbeam/assets/glmr.ts b/packages/config/src/config/moonbeam/assets/glmr.ts index ecf97369..c1adb9c0 100644 --- a/packages/config/src/config/moonbeam/assets/glmr.ts +++ b/packages/config/src/config/moonbeam/assets/glmr.ts @@ -54,8 +54,7 @@ export const GLMR: MoonbeamXcmConfig = { .polkadotXcm() .limitedReserveWithdrawAssets() .successEvent(PolkadotXcmExtrinsicSuccessEvent.Attempted) - .origin(astar) - .V1() + .V1V2() .X2(getPalletInstance(astar)), }, [bifrost.key]: { diff --git a/packages/config/src/config/moonbeam/assets/ring.ts b/packages/config/src/config/moonbeam/assets/ring.ts index df851e26..f5f05ceb 100644 --- a/packages/config/src/config/moonbeam/assets/ring.ts +++ b/packages/config/src/config/moonbeam/assets/ring.ts @@ -23,8 +23,7 @@ export const RING: MoonbeamXcmConfig = { .polkadotXcm() .limitedReserveTransferAssets() .successEvent(PolkadotXcmExtrinsicSuccessEvent.Attempted) - .origin(origin) - .V1() + .V1V2() .X1(), }, }, diff --git a/packages/config/src/config/moonbeam/assets/usdt.ts b/packages/config/src/config/moonbeam/assets/usdt.ts index dcdc5e6f..62c7bb93 100644 --- a/packages/config/src/config/moonbeam/assets/usdt.ts +++ b/packages/config/src/config/moonbeam/assets/usdt.ts @@ -27,8 +27,7 @@ export const USDT: MoonbeamXcmConfig = { .polkadotXcm() .limitedReserveTransferAssets() .successEvent(PolkadotXcmExtrinsicSuccessEvent.Attempted) - .origin(origin) - .V1() + .V1V2() .X2(getPalletInstance(origin), originAssetId), }, }, diff --git a/packages/config/src/config/moonriver/assets/crab.ts b/packages/config/src/config/moonriver/assets/crab.ts index 4f46ccbb..223f326a 100644 --- a/packages/config/src/config/moonriver/assets/crab.ts +++ b/packages/config/src/config/moonriver/assets/crab.ts @@ -23,8 +23,7 @@ export const CRAB: MoonriverXcmConfig = { .polkadotXcm() .limitedReserveTransferAssets() .successEvent(PolkadotXcmExtrinsicSuccessEvent.Attempted) - .origin(origin) - .V1() + .V1V2() .X1(), }, }, diff --git a/packages/config/src/config/moonriver/assets/csm.ts b/packages/config/src/config/moonriver/assets/csm.ts index 2b9b555a..d61b296e 100644 --- a/packages/config/src/config/moonriver/assets/csm.ts +++ b/packages/config/src/config/moonriver/assets/csm.ts @@ -23,8 +23,8 @@ export const CSM: MoonriverXcmConfig = { .polkadotXcm() .limitedReserveTransferAssets() .successEvent(PolkadotXcmExtrinsicSuccessEvent.Attempted) - .origin(origin) - .V0(), + .V1V2() + .here(), }, }, withdraw: { diff --git a/packages/config/src/config/moonriver/assets/ksm.ts b/packages/config/src/config/moonriver/assets/ksm.ts index 9d27952a..43ce19e1 100644 --- a/packages/config/src/config/moonriver/assets/ksm.ts +++ b/packages/config/src/config/moonriver/assets/ksm.ts @@ -22,9 +22,7 @@ export const KSM: MoonriverXcmConfig = { extrinsic: extrinsic .xcmPallet() .limitedReserveTransferAssets() - .successEvent(PolkadotXcmExtrinsicSuccessEvent.Attempted) - .origin(origin) - .V0(), + .successEvent(PolkadotXcmExtrinsicSuccessEvent.Attempted), }, }, withdraw: { diff --git a/packages/config/src/config/moonriver/assets/movr.ts b/packages/config/src/config/moonriver/assets/movr.ts index b7e73f84..34319da2 100644 --- a/packages/config/src/config/moonriver/assets/movr.ts +++ b/packages/config/src/config/moonriver/assets/movr.ts @@ -118,8 +118,7 @@ export const MOVR: MoonriverXcmConfig = { .polkadotXcm() .limitedReserveWithdrawAssets() .successEvent(PolkadotXcmExtrinsicSuccessEvent.Attempted) - .origin(shiden) - .V1() + .V1V2() .X2(getPalletInstance(shiden)), }, }, diff --git a/packages/config/src/config/moonriver/assets/rmrk.ts b/packages/config/src/config/moonriver/assets/rmrk.ts index 2b46a9ee..3bd36ef5 100644 --- a/packages/config/src/config/moonriver/assets/rmrk.ts +++ b/packages/config/src/config/moonriver/assets/rmrk.ts @@ -27,8 +27,7 @@ export const RMRK: MoonriverXcmConfig = { .polkadotXcm() .limitedReserveTransferAssets() .successEvent(PolkadotXcmExtrinsicSuccessEvent.Attempted) - .origin(origin) - .V1() + .V1V2() .X2(getPalletInstance(origin), originAssetId), }, }, diff --git a/packages/config/src/config/moonriver/assets/sdn.ts b/packages/config/src/config/moonriver/assets/sdn.ts index 4d970f5f..f987f4e3 100644 --- a/packages/config/src/config/moonriver/assets/sdn.ts +++ b/packages/config/src/config/moonriver/assets/sdn.ts @@ -23,8 +23,7 @@ export const SDN: MoonriverXcmConfig = { .polkadotXcm() .limitedReserveTransferAssets() .successEvent(PolkadotXcmExtrinsicSuccessEvent.Attempted) - .origin(origin) - .V1() + .V1V2() .here(), }, }, diff --git a/packages/config/src/config/moonriver/assets/usdt.ts b/packages/config/src/config/moonriver/assets/usdt.ts index 69844ebc..e4333a54 100644 --- a/packages/config/src/config/moonriver/assets/usdt.ts +++ b/packages/config/src/config/moonriver/assets/usdt.ts @@ -27,8 +27,7 @@ export const USDT: MoonriverXcmConfig = { .polkadotXcm() .limitedReserveTransferAssets() .successEvent(PolkadotXcmExtrinsicSuccessEvent.Attempted) - .origin(origin) - .V1() + .V1V2() .X2(getPalletInstance(origin), originAssetId), }, }, diff --git a/packages/config/src/config/moonriver/assets/xrt.ts b/packages/config/src/config/moonriver/assets/xrt.ts index d9aa9921..ded7bb45 100644 --- a/packages/config/src/config/moonriver/assets/xrt.ts +++ b/packages/config/src/config/moonriver/assets/xrt.ts @@ -23,8 +23,7 @@ export const XRT: MoonriverXcmConfig = { .polkadotXcm() .limitedReserveTransferAssets() .successEvent(PolkadotXcmExtrinsicSuccessEvent.Attempted) - .origin(origin) - .V1() + .V1V2() .here(), }, }, diff --git a/packages/config/src/extrinsic/extrinsic.ts b/packages/config/src/extrinsic/extrinsic.ts index 85d283a7..29f69b24 100644 --- a/packages/config/src/extrinsic/extrinsic.ts +++ b/packages/config/src/extrinsic/extrinsic.ts @@ -10,8 +10,8 @@ export function createExtrinsicBuilder< ChainKeys extends ChainKey, >(chain: MoonChain) { return { - polkadotXcm: () => polkadotXcm(chain), - xcmPallet: () => xcmPallet(chain), + polkadotXcm: () => polkadotXcm(chain), + xcmPallet: () => xcmPallet(chain), xTokens: () => xTokens(chain), xTransfer: () => xTransfer(chain), }; diff --git a/packages/config/src/extrinsic/polkadotXcm/__snapshots__/polkadotXcm.test.ts.snap b/packages/config/src/extrinsic/polkadotXcm/__snapshots__/polkadotXcm.test.ts.snap index 6691ef76..dd5f998c 100644 --- a/packages/config/src/extrinsic/polkadotXcm/__snapshots__/polkadotXcm.test.ts.snap +++ b/packages/config/src/extrinsic/polkadotXcm/__snapshots__/polkadotXcm.test.ts.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`polkadotXcm limitedReserveTransferAssets v0 should be correct config 1`] = ` +exports[`polkadotXcm limitedReserveTransferAssets v1v2 here should be correct config 1`] = ` { "extrinsic": "limitedReserveTransferAssets", "getParams": [Function], @@ -9,58 +9,7 @@ exports[`polkadotXcm limitedReserveTransferAssets v0 should be correct config 1` } `; -exports[`polkadotXcm limitedReserveTransferAssets v0 should get correct params 1`] = ` -[ - { - "V1": { - "interior": { - "X1": { - "Parachain": 1000, - }, - }, - "parents": 1, - }, - }, - { - "V1": { - "interior": { - "X1": { - "AccountKey20": { - "key": "0xeF46c7649270C912704fB09B75097f6E32208b85", - "network": "Any", - }, - }, - }, - "parents": 0, - }, - }, - { - "V0": [ - { - "ConcreteFungible": { - "amount": 1000n, - "id": "Null", - }, - }, - ], - }, - 0, - { - "Limited": 1000000000, - }, -] -`; - -exports[`polkadotXcm limitedReserveTransferAssets v1 here should be correct config 1`] = ` -{ - "extrinsic": "limitedReserveTransferAssets", - "getParams": [Function], - "pallet": "polkadotXcm", - "successEvent": "Attempted", -} -`; - -exports[`polkadotXcm limitedReserveTransferAssets v1 here should get correct params 1`] = ` +exports[`polkadotXcm limitedReserveTransferAssets v1v2 here should get correct params 1`] = ` [ { "V1": { @@ -101,13 +50,11 @@ exports[`polkadotXcm limitedReserveTransferAssets v1 here should get correct par ], }, 0, - { - "Limited": 1000000000, - }, + "Unlimited", ] `; -exports[`polkadotXcm limitedReserveTransferAssets v1 x1 should be correct config 1`] = ` +exports[`polkadotXcm limitedReserveTransferAssets v1v2 x1 should be correct config 1`] = ` { "extrinsic": "limitedReserveTransferAssets", "getParams": [Function], @@ -116,7 +63,7 @@ exports[`polkadotXcm limitedReserveTransferAssets v1 x1 should be correct config } `; -exports[`polkadotXcm limitedReserveTransferAssets v1 x1 should get correct params 1`] = ` +exports[`polkadotXcm limitedReserveTransferAssets v1v2 x1 should get correct params 1`] = ` [ { "V1": { @@ -161,13 +108,11 @@ exports[`polkadotXcm limitedReserveTransferAssets v1 x1 should get correct param ], }, 0, - { - "Limited": 1000000000, - }, + "Unlimited", ] `; -exports[`polkadotXcm limitedReserveTransferAssets v1 x2 should be correct config 1`] = ` +exports[`polkadotXcm limitedReserveTransferAssets v1v2 x2 should be correct config 1`] = ` { "extrinsic": "limitedReserveTransferAssets", "getParams": [Function], @@ -176,7 +121,7 @@ exports[`polkadotXcm limitedReserveTransferAssets v1 x2 should be correct config } `; -exports[`polkadotXcm limitedReserveTransferAssets v1 x2 should get correct params 1`] = ` +exports[`polkadotXcm limitedReserveTransferAssets v1v2 x2 should get correct params 1`] = ` [ { "V1": { @@ -226,13 +171,11 @@ exports[`polkadotXcm limitedReserveTransferAssets v1 x2 should get correct param ], }, 0, - { - "Limited": 1000000000, - }, + "Unlimited", ] `; -exports[`polkadotXcm limitedReserveWithdrawAssets v1 x2 should be correct config 1`] = ` +exports[`polkadotXcm limitedReserveWithdrawAssets v1v2 x2 should be correct config 1`] = ` { "extrinsic": "limitedReserveWithdrawAssets", "getParams": [Function], @@ -241,7 +184,7 @@ exports[`polkadotXcm limitedReserveWithdrawAssets v1 x2 should be correct config } `; -exports[`polkadotXcm limitedReserveWithdrawAssets v1 x2 should get correct params 1`] = ` +exports[`polkadotXcm limitedReserveWithdrawAssets v1v2 x2 should get correct params 1`] = ` [ { "V1": { @@ -291,8 +234,6 @@ exports[`polkadotXcm limitedReserveWithdrawAssets v1 x2 should get correct param ], }, 0, - { - "Limited": 1000000000, - }, + "Unlimited", ] `; diff --git a/packages/config/src/extrinsic/polkadotXcm/polkadotXcm.interfaces.ts b/packages/config/src/extrinsic/polkadotXcm/polkadotXcm.interfaces.ts index 84abf1be..f7db274b 100644 --- a/packages/config/src/extrinsic/polkadotXcm/polkadotXcm.interfaces.ts +++ b/packages/config/src/extrinsic/polkadotXcm/polkadotXcm.interfaces.ts @@ -13,12 +13,12 @@ export interface PolkadotXcmPallet { getParams: (params: XcmExtrinsicGetParams) => PolkadotXcmPalletParams; } -export type PolkadotXcmPalletParamsV1 = [ +export type PolkadotXcmPalletParams = [ /** * destination */ { - V1: { + [v in XcmMLVersion]?: { parents: Parents; interior: { X1: { @@ -31,7 +31,7 @@ export type PolkadotXcmPalletParamsV1 = [ * beneficiary */ { - V1: { + [v in XcmMLVersion]?: { parents: 0; interior: { X1: { @@ -49,56 +49,9 @@ export type PolkadotXcmPalletParamsV1 = [ /** * asset */ - PolkadotXcmAssetParam, - /** - * fee - */ - 0, - /** - * weight - */ { - Limited: number; - }, -]; - -export type PolkadotXcmPalletParamsV2 = [ - /** - * destination - */ - { - V2: { - parents: Parents; - interior: { - X1: { - Parachain: number; - }; - }; - }; + [v in XcmMLVersion]?: PolkadotXcmAssetParam[]; }, - /** - * beneficiary - */ - { - V2: { - parents: 0; - interior: { - X1: { - AccountKey20: { - network: 'Any'; - /** - * account - */ - key: string; - }; - }; - }; - }; - }, - /** - * asset - */ - PolkadotXcmAssetParam, /** * fee */ @@ -109,52 +62,32 @@ export type PolkadotXcmPalletParamsV2 = [ 'Unlimited', ]; -export type PolkadotXcmPalletParams = - | PolkadotXcmPalletParamsV1 - | PolkadotXcmPalletParamsV2; - -export type PolkadotXcmAssetParam = - | PolkadotXcmAssetParamV0 - | PolkadotXcmAssetParamV1 - | PolkadotXcmAssetParamV2; - -export interface PolkadotXcmAssetParamV0 { - V0: [ - { - ConcreteFungible: { - id: 'Null'; - amount: bigint; - }; - }, - ]; -} - -export interface PolkadotXcmAssetParamV1 { - V1: [ - { - id: { - Concrete: { - parents: Parents; - interior: - | 'Here' - | PolkadotXcmAssetParamV1InteriorX1 - | PolkadotXcmAssetParamV1InteriorX2; - }; - }; - fun: { - Fungible: bigint; - }; - }, - ]; +export enum XcmMLVersion { + v1 = 'V1', + v2 = 'V2', } +export type PolkadotXcmAssetParam = { + id: { + Concrete: { + parents: Parents; + interior: + | 'Here' + | PolkadotXcmAssetParamInteriorX1 + | PolkadotXcmAssetParamInteriorX2; + }; + }; + fun: { + Fungible: bigint; + }; +}; -export interface PolkadotXcmAssetParamV1InteriorX1 { +export interface PolkadotXcmAssetParamInteriorX1 { X1: { PalletInstance: number; }; } -export interface PolkadotXcmAssetParamV1InteriorX2 { +export interface PolkadotXcmAssetParamInteriorX2 { X2: | [ { @@ -173,28 +106,3 @@ export interface PolkadotXcmAssetParamV1InteriorX2 { }, ]; } - -export interface PolkadotXcmAssetParamV2 { - V2: [ - { - id: { - Concrete: { - parents: Parents; - interior: - | 'Here' - | PolkadotXcmAssetParamV1InteriorX1 - | PolkadotXcmAssetParamV1InteriorX2; - }; - }; - fun: { - Fungible: bigint; - }; - }, - ]; -} - -export enum XcmMLVersion { - v0 = 'V0', - v1 = 'V1', - v2 = 'V2', -} diff --git a/packages/config/src/extrinsic/polkadotXcm/polkadotXcm.test.ts b/packages/config/src/extrinsic/polkadotXcm/polkadotXcm.test.ts index b1b3b15c..e3a3598b 100644 --- a/packages/config/src/extrinsic/polkadotXcm/polkadotXcm.test.ts +++ b/packages/config/src/extrinsic/polkadotXcm/polkadotXcm.test.ts @@ -1,5 +1,5 @@ -import { ChainKey, MoonChainKey } from '../../constants'; -import { Chain, MoonChain } from '../../interfaces'; +import { MoonChainKey } from '../../constants'; +import { MoonChain } from '../../interfaces'; import { polkadotXcm } from './polkadotXcm'; import { PolkadotXcmExtrinsicSuccessEvent } from './polkadotXcm.constants'; @@ -15,42 +15,15 @@ describe('polkadotXcm', () => { chainId: 1287, unitsPerSecond: 100n, }; - const origin: Chain = { - key: ChainKey.AlphanetRelay, - name: 'Alphanet Relay', - ws: 'wss://frag-moonbase-relay-rpc-ws.g.moonbase.moonbeam.network', - weight: 1_000_000_000, - parachainId: 0, - ss58Format: 42, - genesisHash: - '0xe1ea3ab1d46ba8f4898b6b4b9c54ffc05282d299f89e84bd0fd08067758c9443', - }; const extrinsic = polkadotXcm(chain); describe('limitedReserveTransferAssets', () => { - describe('v0', () => { - const cfg = extrinsic - .limitedReserveTransferAssets() - .successEvent(PolkadotXcmExtrinsicSuccessEvent.Attempted) - .origin(origin) - .V0(); - - it('should be correct config', () => { - expect(cfg).toMatchSnapshot(); - }); - - it('should get correct params', () => { - expect(cfg.getParams({ account, amount })).toMatchSnapshot(); - }); - }); - - describe('v1', () => { + describe('v1v2', () => { describe('here', () => { const cfg = extrinsic .limitedReserveTransferAssets() .successEvent(PolkadotXcmExtrinsicSuccessEvent.Attempted) - .origin(origin) - .V1() + .V1V2() .here(); it('should be correct config', () => { @@ -66,8 +39,7 @@ describe('polkadotXcm', () => { const cfg = extrinsic .limitedReserveTransferAssets() .successEvent(PolkadotXcmExtrinsicSuccessEvent.Attempted) - .origin(origin) - .V1() + .V1V2() .X1(); it('should be correct config', () => { @@ -83,8 +55,7 @@ describe('polkadotXcm', () => { const cfg = extrinsic .limitedReserveTransferAssets() .successEvent(PolkadotXcmExtrinsicSuccessEvent.Attempted) - .origin(origin) - .V1() + .V1V2() .X2(10, 50); it('should be correct config', () => { @@ -99,13 +70,12 @@ describe('polkadotXcm', () => { }); describe('limitedReserveWithdrawAssets', () => { - describe('v1', () => { + describe('v1v2', () => { describe('x2', () => { const cfg = extrinsic .limitedReserveWithdrawAssets() .successEvent(PolkadotXcmExtrinsicSuccessEvent.Attempted) - .origin(origin) - .V1() + .V1V2() .X2(10); it('should be correct config', () => { diff --git a/packages/config/src/extrinsic/polkadotXcm/polkadotXcm.ts b/packages/config/src/extrinsic/polkadotXcm/polkadotXcm.ts index ca452346..42152ce5 100644 --- a/packages/config/src/extrinsic/polkadotXcm/polkadotXcm.ts +++ b/packages/config/src/extrinsic/polkadotXcm/polkadotXcm.ts @@ -1,174 +1,126 @@ -import { ChainKey } from '../../constants'; -import { AssetId, Chain, MoonChain } from '../../interfaces'; +import { AssetId, MoonChain } from '../../interfaces'; import { PolkadotXcmExtrinsic, PolkadotXcmExtrinsicSuccessEvent, } from './polkadotXcm.constants'; -import { XcmMLVersion } from './polkadotXcm.interfaces'; -import { getCreateExtrinsic } from './polkadotXcm.util'; +import { getCreateV1V2Extrinsic } from './polkadotXcm.util'; /* eslint-disable @typescript-eslint/no-use-before-define */ -export function polkadotXcm(chain: MoonChain) { +export function polkadotXcm(chain: MoonChain) { return { - limitedReserveTransferAssets: () => - limitedReserveTransferAssets(chain), - limitedReserveWithdrawAssets: () => - limitedReserveWithdrawAssets(chain), + limitedReserveTransferAssets: () => limitedReserveTransferAssets(chain), + limitedReserveWithdrawAssets: () => limitedReserveWithdrawAssets(chain), }; } -function limitedReserveTransferAssets( - chain: MoonChain, -) { +function limitedReserveTransferAssets(chain: MoonChain) { return { - successEvent: (event: PolkadotXcmExtrinsicSuccessEvent) => ({ - origin: (origin: Chain) => { - const createExtrinsic = getCreateExtrinsic( - PolkadotXcmExtrinsic.LimitedReserveTransferAssets, - event, - chain, - origin, - ); + successEvent: (event: PolkadotXcmExtrinsicSuccessEvent) => { + const createExtrinsic = getCreateV1V2Extrinsic( + PolkadotXcmExtrinsic.LimitedReserveTransferAssets, + event, + chain, + ); - return { - V0: () => - createExtrinsic( - (amount) => ({ - V0: [ - { - ConcreteFungible: { - id: 'Null', - amount, - }, + return { + V1V2: () => ({ + here: () => + createExtrinsic((amount) => [ + { + id: { + Concrete: { + parents: 0, + interior: 'Here', }, - ], - }), - XcmMLVersion.v0, - ), - V1: () => ({ - here: () => - createExtrinsic( - (amount) => ({ - V1: [ - { - id: { - Concrete: { - parents: 0, - interior: 'Here', - }, - }, - fun: { - Fungible: amount, + }, + fun: { + Fungible: amount, + }, + }, + ]), + X1: () => + createExtrinsic((amount) => [ + { + id: { + Concrete: { + parents: 0, + interior: { + X1: { + PalletInstance: 5, }, }, - ], - }), - XcmMLVersion.v1, - ), - X1: () => - createExtrinsic( - (amount) => ({ - V1: [ - { - id: { - Concrete: { - parents: 0, - interior: { - X1: { - PalletInstance: 5, - }, - }, + }, + }, + fun: { + Fungible: amount, + }, + }, + ]), + X2: (palletInstance: number, assetId: AssetId) => + createExtrinsic((amount) => [ + { + id: { + Concrete: { + parents: 0, + interior: { + X2: [ + { + PalletInstance: palletInstance, }, - }, - fun: { - Fungible: amount, - }, - }, - ], - }), - XcmMLVersion.v1, - ), - X2: (palletInstance: number, assetId: AssetId) => - createExtrinsic( - (amount) => ({ - V1: [ - { - id: { - Concrete: { - parents: 0, - interior: { - X2: [ - { - PalletInstance: palletInstance, - }, - { - GeneralIndex: assetId, - }, - ], - }, + { + GeneralIndex: assetId, }, - }, - fun: { - Fungible: amount, - }, + ], }, - ], - }), - XcmMLVersion.v1, - ), - }), - }; - }, - }), + }, + }, + fun: { + Fungible: amount, + }, + }, + ]), + }), + }; + }, }; } -function limitedReserveWithdrawAssets( - chain: MoonChain, -) { +function limitedReserveWithdrawAssets(chain: MoonChain) { return { - successEvent: (event: PolkadotXcmExtrinsicSuccessEvent) => ({ - origin: (origin: Chain) => { - const createExtrinsic = getCreateExtrinsic( - PolkadotXcmExtrinsic.LimitedReserveWithdrawAssets, - event, - chain, - origin, - ); + successEvent: (event: PolkadotXcmExtrinsicSuccessEvent) => { + const createExtrinsic = getCreateV1V2Extrinsic( + PolkadotXcmExtrinsic.LimitedReserveWithdrawAssets, + event, + chain, + ); - return { - V1: () => ({ - X2: (palletInstance: number) => - createExtrinsic( - (amount) => ({ - V1: [ - { - id: { - Concrete: { - parents: 1, - interior: { - X2: [ - { - Parachain: chain.parachainId, - }, - { - PalletInstance: palletInstance, - }, - ], - }, + return { + V1V2: () => ({ + X2: (palletInstance: number) => + createExtrinsic((amount) => [ + { + id: { + Concrete: { + parents: 1, + interior: { + X2: [ + { + Parachain: chain.parachainId, }, - }, - fun: { - Fungible: amount, - }, + { + PalletInstance: palletInstance, + }, + ], }, - ], - }), - XcmMLVersion.v1, - ), - }), - }; - }, - }), + }, + }, + fun: { + Fungible: amount, + }, + }, + ]), + }), + }; + }, }; } diff --git a/packages/config/src/extrinsic/polkadotXcm/polkadotXcm.util.ts b/packages/config/src/extrinsic/polkadotXcm/polkadotXcm.util.ts index 232f37ad..4ded34f4 100644 --- a/packages/config/src/extrinsic/polkadotXcm/polkadotXcm.util.ts +++ b/packages/config/src/extrinsic/polkadotXcm/polkadotXcm.util.ts @@ -1,5 +1,6 @@ -import { ChainKey } from '../../constants'; -import { Chain, MoonChain } from '../../interfaces'; +import { SubmittableExtrinsicFunction } from '@polkadot/api/types'; +import { getTypeDef } from '@polkadot/types/create'; +import { MoonChain } from '../../interfaces'; import { Parents } from '../common.interfaces'; import { ExtrinsicPallet } from '../extrinsic.constants'; import { @@ -13,54 +14,42 @@ import { XcmMLVersion, } from './polkadotXcm.interfaces'; -export function getCreateExtrinsic( +function getAvailableVersion( + extrinsicCall?: SubmittableExtrinsicFunction<'promise'>, +): XcmMLVersion { + if (!extrinsicCall) return XcmMLVersion.v1; + + const { type } = extrinsicCall.meta.args[0]; + const instance = extrinsicCall.meta.registry.createType(type.toString()); + const raw = getTypeDef(instance?.toRawType()); + + if (Array.isArray(raw.sub) && raw.sub.find((x) => x.name === 'V2')) + return XcmMLVersion.v2; + + return XcmMLVersion.v1; +} + +export function getCreateV1V2Extrinsic( extrinsic: PolkadotXcmExtrinsic, event: PolkadotXcmExtrinsicSuccessEvent, config: MoonChain, - origin: Chain, parents: Parents = 1, ) { return ( - getAsset: (amount: bigint) => PolkadotXcmAssetParam, - xcmMLVersion: XcmMLVersion, + getAsset: (amount: bigint) => PolkadotXcmAssetParam[], ): PolkadotXcmPallet => ({ pallet: ExtrinsicPallet.PolkadotXcm, extrinsic, successEvent: event, - getParams: ({ account, amount }): PolkadotXcmPalletParams => { - if (xcmMLVersion === XcmMLVersion.v2) - return [ - { - V2: { - parents, - interior: { - X1: { - Parachain: config.parachainId, - }, - }, - }, - }, - { - V2: { - parents: 0, - interior: { - X1: { - AccountKey20: { - network: 'Any', - key: account, - }, - }, - }, - }, - }, - getAsset(amount), - 0, - 'Unlimited', - ]; - + getParams: ({ + account, + amount, + extrinsicCall, + }): PolkadotXcmPalletParams => { + const versionKey = getAvailableVersion(extrinsicCall); return [ { - V1: { + [versionKey]: { parents, interior: { X1: { @@ -70,7 +59,7 @@ export function getCreateExtrinsic( }, }, { - V1: { + [versionKey]: { parents: 0, interior: { X1: { @@ -82,11 +71,11 @@ export function getCreateExtrinsic( }, }, }, - getAsset(amount), - 0, { - Limited: origin.weight, + [versionKey]: getAsset(amount), }, + 0, + 'Unlimited', ]; }, }); diff --git a/packages/config/src/extrinsic/xcmPallet/__snapshots__/xcmPallet.test.ts.snap b/packages/config/src/extrinsic/xcmPallet/__snapshots__/xcmPallet.test.ts.snap index e7bbdec5..46fb43cb 100644 --- a/packages/config/src/extrinsic/xcmPallet/__snapshots__/xcmPallet.test.ts.snap +++ b/packages/config/src/extrinsic/xcmPallet/__snapshots__/xcmPallet.test.ts.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`xcmPallet limitedReserveTransferAssets v0 should be correct config 1`] = ` +exports[`xcmPallet limitedReserveTransferAssets should be correct config 1`] = ` { "extrinsic": "limitedReserveTransferAssets", "getParams": [Function], @@ -9,7 +9,7 @@ exports[`xcmPallet limitedReserveTransferAssets v0 should be correct config 1`] } `; -exports[`xcmPallet limitedReserveTransferAssets v0 should get correct params 1`] = ` +exports[`xcmPallet limitedReserveTransferAssets should get correct params 1`] = ` [ { "V1": { @@ -35,58 +35,7 @@ exports[`xcmPallet limitedReserveTransferAssets v0 should get correct params 1`] }, }, { - "V0": [ - { - "ConcreteFungible": { - "amount": 1000n, - "id": "Null", - }, - }, - ], - }, - 0, - { - "Limited": 1000000000, - }, -] -`; - -exports[`xcmPallet limitedReserveTransferAssets v2 should be correct config 1`] = ` -{ - "extrinsic": "limitedReserveTransferAssets", - "getParams": [Function], - "pallet": "xcmPallet", - "successEvent": "Attempted", -} -`; - -exports[`xcmPallet limitedReserveTransferAssets v2 should get correct params 1`] = ` -[ - { - "V2": { - "interior": { - "X1": { - "Parachain": 1000, - }, - }, - "parents": 0, - }, - }, - { - "V2": { - "interior": { - "X1": { - "AccountKey20": { - "key": "0xeF46c7649270C912704fB09B75097f6E32208b85", - "network": "Any", - }, - }, - }, - "parents": 0, - }, - }, - { - "V2": [ + "V1": [ { "fun": { "Fungible": 1000n, diff --git a/packages/config/src/extrinsic/xcmPallet/xcmPallet.test.ts b/packages/config/src/extrinsic/xcmPallet/xcmPallet.test.ts index 4fb7d910..856bbe75 100644 --- a/packages/config/src/extrinsic/xcmPallet/xcmPallet.test.ts +++ b/packages/config/src/extrinsic/xcmPallet/xcmPallet.test.ts @@ -1,5 +1,5 @@ -import { ChainKey, MoonChainKey } from '../../constants'; -import { Chain, MoonChain } from '../../interfaces'; +import { MoonChainKey } from '../../constants'; +import { MoonChain } from '../../interfaces'; import { PolkadotXcmExtrinsicSuccessEvent } from '../polkadotXcm'; import { xcmPallet } from './xcmPallet'; @@ -15,49 +15,20 @@ describe('xcmPallet', () => { chainId: 1287, unitsPerSecond: 100n, }; - const origin: Chain = { - key: ChainKey.AlphanetRelay, - name: 'Alphanet Relay', - ws: 'wss://frag-moonbase-relay-rpc-ws.g.moonbase.moonbeam.network', - weight: 1_000_000_000, - parachainId: 0, - ss58Format: 42, - genesisHash: - '0xe1ea3ab1d46ba8f4898b6b4b9c54ffc05282d299f89e84bd0fd08067758c9443', - }; + const extrinsic = xcmPallet(chain); describe('limitedReserveTransferAssets', () => { - describe('v0', () => { - const cfg = extrinsic - .limitedReserveTransferAssets() - .successEvent(PolkadotXcmExtrinsicSuccessEvent.Attempted) - .origin(origin) - .V0(); - - it('should be correct config', () => { - expect(cfg).toMatchSnapshot(); - }); + const cfg = extrinsic + .limitedReserveTransferAssets() + .successEvent(PolkadotXcmExtrinsicSuccessEvent.Attempted); - it('should get correct params', () => { - expect(cfg.getParams({ account, amount })).toMatchSnapshot(); - }); + it('should be correct config', () => { + expect(cfg).toMatchSnapshot(); }); - describe('v2', () => { - const cfg = extrinsic - .limitedReserveTransferAssets() - .successEvent(PolkadotXcmExtrinsicSuccessEvent.Attempted) - .origin(origin) - .V2(); - - it('should be correct config', () => { - expect(cfg).toMatchSnapshot(); - }); - - it('should get correct params', () => { - expect(cfg.getParams({ account, amount })).toMatchSnapshot(); - }); + it('should get correct params', () => { + expect(cfg.getParams({ account, amount })).toMatchSnapshot(); }); }); }); diff --git a/packages/config/src/extrinsic/xcmPallet/xcmPallet.ts b/packages/config/src/extrinsic/xcmPallet/xcmPallet.ts index 2b6a9d61..d91607b7 100644 --- a/packages/config/src/extrinsic/xcmPallet/xcmPallet.ts +++ b/packages/config/src/extrinsic/xcmPallet/xcmPallet.ts @@ -1,75 +1,44 @@ -import { ChainKey } from '../../constants'; -import { Chain, MoonChain } from '../../interfaces'; +import { MoonChain } from '../../interfaces'; import { ExtrinsicPallet } from '../extrinsic.constants'; import { - getCreateExtrinsic, + getCreateV1V2Extrinsic, PolkadotXcmExtrinsic, PolkadotXcmExtrinsicSuccessEvent, - XcmMLVersion, } from '../polkadotXcm'; /* eslint-disable @typescript-eslint/no-use-before-define */ -export function xcmPallet(chain: MoonChain) { +export function xcmPallet(chain: MoonChain) { return { - limitedReserveTransferAssets: () => - limitedReserveTransferAssets(chain), + limitedReserveTransferAssets: () => limitedReserveTransferAssets(chain), }; } -function limitedReserveTransferAssets( - chain: MoonChain, -) { +function limitedReserveTransferAssets(chain: MoonChain) { return { - successEvent: (event: PolkadotXcmExtrinsicSuccessEvent) => ({ - origin: (origin: Chain) => { - const createExtrinsic = getCreateExtrinsic( - PolkadotXcmExtrinsic.LimitedReserveTransferAssets, - event, - chain, - origin, - 0, - ); + successEvent: (event: PolkadotXcmExtrinsicSuccessEvent) => { + const createExtrinsic = getCreateV1V2Extrinsic( + PolkadotXcmExtrinsic.LimitedReserveTransferAssets, + event, + chain, + 0, + ); - return { - V0: () => ({ - ...createExtrinsic( - (amount) => ({ - V0: [ - { - ConcreteFungible: { - id: 'Null', - amount, - }, - }, - ], - }), - XcmMLVersion.v0, - ), - pallet: ExtrinsicPallet.XcmPallet, - }), - V2: () => ({ - ...createExtrinsic( - (amount) => ({ - V2: [ - { - id: { - Concrete: { - parents: 0, - interior: 'Here', - }, - }, - fun: { - Fungible: amount, - }, - }, - ], - }), - XcmMLVersion.v2, - ), - pallet: ExtrinsicPallet.XcmPallet, - }), - }; - }, - }), + return { + ...createExtrinsic((amount) => [ + { + id: { + Concrete: { + parents: 0, + interior: 'Here', + }, + }, + fun: { + Fungible: amount, + }, + }, + ]), + pallet: ExtrinsicPallet.XcmPallet, + }; + }, }; }