From 7e24392a67733b91c1fc7cc23543c74a88f36ed2 Mon Sep 17 00:00:00 2001 From: mmaurello <93129175+mmaurello@users.noreply.github.com> Date: Wed, 13 Mar 2024 05:03:40 +1300 Subject: [PATCH] PINK integration with Moonbeam (#212) --- .changeset/two-roses-destroy.md | 5 + .../builder/fixtures/builderParamsMock.ts | 11 ++ .../__snapshots__/polkadotXcm.test.ts.snap | 108 +++++++++++++++++- .../pallets/polkadotXcm/polkadotXcm.test.ts | 36 ++++-- .../pallets/polkadotXcm/polkadotXcm.ts | 75 ++++++++---- .../pallets/polkadotXcm/polkadotXcm.util.ts | 4 +- packages/config/src/assets.ts | 6 + packages/config/src/chains.ts | 10 ++ packages/config/src/configs/moonbeam.ts | 16 +++ .../config/src/configs/polkadotAssetHub.ts | 22 +++- 10 files changed, 256 insertions(+), 37 deletions(-) create mode 100644 .changeset/two-roses-destroy.md diff --git a/.changeset/two-roses-destroy.md b/.changeset/two-roses-destroy.md new file mode 100644 index 00000000..2cee001c --- /dev/null +++ b/.changeset/two-roses-destroy.md @@ -0,0 +1,5 @@ +--- +'@moonbeam-network/xcm-config': patch +--- + +PINK asset configuration diff --git a/packages/builder/fixtures/builderParamsMock.ts b/packages/builder/fixtures/builderParamsMock.ts index adcdaa22..8c408b18 100644 --- a/packages/builder/fixtures/builderParamsMock.ts +++ b/packages/builder/fixtures/builderParamsMock.ts @@ -54,6 +54,17 @@ export const buildParamsMock: ExtrinsicConfigBuilderPrams = { source: alphanetAssetHubMock, }; +export const buildParamsSameAssetMock: ExtrinsicConfigBuilderPrams = { + address: '0xeF46c7649270C912704fB09B75097f6E32208b85', + amount: 99_000_000_000n, + asset: 'USDT', + destination: moonbaseAlphaMock, + fee: 5_000_000_000n, + feeAsset: 'USDT', + palletInstance: 10, + source: alphanetAssetHubMock, +}; + export const buildParachainParamsMock: ExtrinsicConfigBuilderPrams = { address: 'wd84XqsQ4LVzhmTBVd4s5ApGt9sBnnk8K7Q5PhBwwhxwqgm1u', amount: 99_000_000_000n, diff --git a/packages/builder/src/extrinsic/pallets/polkadotXcm/__snapshots__/polkadotXcm.test.ts.snap b/packages/builder/src/extrinsic/pallets/polkadotXcm/__snapshots__/polkadotXcm.test.ts.snap index 28783af1..7b45c1e8 100644 --- a/packages/builder/src/extrinsic/pallets/polkadotXcm/__snapshots__/polkadotXcm.test.ts.snap +++ b/packages/builder/src/extrinsic/pallets/polkadotXcm/__snapshots__/polkadotXcm.test.ts.snap @@ -109,9 +109,29 @@ exports[`polkadotXcm limitedReserveTransferAssets parachain should get correct a }, }, }, + { + "fun": { + "Fungible": 5000000000n, + }, + "id": { + "Concrete": { + "interior": { + "X2": [ + { + "PalletInstance": 10, + }, + { + "GeneralIndex": "RMRK", + }, + ], + }, + "parents": 0, + }, + }, + }, ], }, - 0, + 1, "Unlimited", ] `; @@ -173,7 +193,89 @@ exports[`polkadotXcm limitedReserveTransferAssets x1 should get correct argument ] `; -exports[`polkadotXcm limitedReserveTransferAssets x2 should be correct config 1`] = ` +exports[`polkadotXcm limitedReserveTransferAssets x2 different assets should be correct config 1`] = ` +ExtrinsicConfig { + "func": "limitedReserveTransferAssets", + "getArgs": [Function], + "module": "polkadotXcm", + "type": "Substrate", +} +`; + +exports[`polkadotXcm limitedReserveTransferAssets x2 different assets should get correct arguments 1`] = ` +[ + { + "V1": { + "interior": { + "X1": { + "Parachain": 1000, + }, + }, + "parents": 1, + }, + }, + { + "V1": { + "interior": { + "X1": { + "AccountKey20": { + "key": "0xeF46c7649270C912704fB09B75097f6E32208b85", + }, + }, + }, + "parents": 0, + }, + }, + { + "V1": [ + { + "fun": { + "Fungible": 99000000000n, + }, + "id": { + "Concrete": { + "interior": { + "X2": [ + { + "PalletInstance": 10, + }, + { + "GeneralIndex": "USDT", + }, + ], + }, + "parents": 0, + }, + }, + }, + { + "fun": { + "Fungible": 5000000000n, + }, + "id": { + "Concrete": { + "interior": { + "X2": [ + { + "PalletInstance": 10, + }, + { + "GeneralIndex": "RMRK", + }, + ], + }, + "parents": 0, + }, + }, + }, + ], + }, + 1, + "Unlimited", +] +`; + +exports[`polkadotXcm limitedReserveTransferAssets x2 the same asset should be correct config 1`] = ` ExtrinsicConfig { "func": "limitedReserveTransferAssets", "getArgs": [Function], @@ -182,7 +284,7 @@ ExtrinsicConfig { } `; -exports[`polkadotXcm limitedReserveTransferAssets x2 should get correct arguments 1`] = ` +exports[`polkadotXcm limitedReserveTransferAssets x2 the same asset should get correct arguments 1`] = ` [ { "V1": { diff --git a/packages/builder/src/extrinsic/pallets/polkadotXcm/polkadotXcm.test.ts b/packages/builder/src/extrinsic/pallets/polkadotXcm/polkadotXcm.test.ts index 62dab60e..4ad917b3 100644 --- a/packages/builder/src/extrinsic/pallets/polkadotXcm/polkadotXcm.test.ts +++ b/packages/builder/src/extrinsic/pallets/polkadotXcm/polkadotXcm.test.ts @@ -1,6 +1,7 @@ import { buildParachainParamsMock, buildParamsMock, + buildParamsSameAssetMock, } from '../../../../fixtures'; import { polkadotXcm } from './polkadotXcm'; @@ -37,17 +38,34 @@ describe('polkadotXcm', () => { }); describe('x2', () => { - const extrinsic = polkadotXcm() - .limitedReserveTransferAssets() - .X2() - .build(buildParamsMock); - - it('should be correct config', () => { - expect(extrinsic).toMatchSnapshot(); + describe('the same asset', () => { + const extrinsic = polkadotXcm() + .limitedReserveTransferAssets() + .X2() + .build(buildParamsSameAssetMock); + + it('should be correct config', () => { + expect(extrinsic).toMatchSnapshot(); + }); + + it('should get correct arguments', () => { + expect(extrinsic.getArgs()).toMatchSnapshot(); + }); }); - it('should get correct arguments', () => { - expect(extrinsic.getArgs()).toMatchSnapshot(); + describe('different assets', () => { + const extrinsic = polkadotXcm() + .limitedReserveTransferAssets() + .X2() + .build(buildParamsMock); + + it('should be correct config', () => { + expect(extrinsic).toMatchSnapshot(); + }); + + it('should get correct arguments', () => { + expect(extrinsic.getArgs()).toMatchSnapshot(); + }); }); }); diff --git a/packages/builder/src/extrinsic/pallets/polkadotXcm/polkadotXcm.ts b/packages/builder/src/extrinsic/pallets/polkadotXcm/polkadotXcm.ts index 052c15f4..34b4d36e 100644 --- a/packages/builder/src/extrinsic/pallets/polkadotXcm/polkadotXcm.ts +++ b/packages/builder/src/extrinsic/pallets/polkadotXcm/polkadotXcm.ts @@ -70,33 +70,62 @@ export function polkadotXcm() { new ExtrinsicConfig({ module: pallet, func, - getArgs: (extrinsicFunction) => - getPolkadotXcmExtrinsicArgs({ - ...params, - func: extrinsicFunction, - asset: [ - { - id: { - Concrete: { - parents: 0, - interior: { - X2: [ - { - PalletInstance: params.palletInstance, - }, - { - GeneralIndex: params.asset, - }, - ], - }, + getArgs: (extrinsicFunction) => { + const isAssetDifferent = + !!params.feeAsset && params.asset !== params.feeAsset; + const asset = [ + { + id: { + Concrete: { + parents: 0, + interior: { + X2: [ + { + PalletInstance: params.palletInstance, + }, + { + GeneralIndex: params.asset, + }, + ], }, }, - fun: { - Fungible: params.amount, + }, + fun: { + Fungible: params.amount, + }, + }, + ]; + + if (isAssetDifferent) { + asset.push({ + id: { + Concrete: { + parents: 0, + interior: { + X2: [ + { + PalletInstance: params.palletInstance, + }, + { + GeneralIndex: params.feeAsset, + }, + ], + }, }, }, - ], - }), + fun: { + Fungible: params.fee, + }, + }); + } + + return getPolkadotXcmExtrinsicArgs({ + ...params, + func: extrinsicFunction, + asset, + feeIndex: isAssetDifferent ? 1 : 0, + }); + }, }), }), }; diff --git a/packages/builder/src/extrinsic/pallets/polkadotXcm/polkadotXcm.util.ts b/packages/builder/src/extrinsic/pallets/polkadotXcm/polkadotXcm.util.ts index e73107d1..2cf597ef 100644 --- a/packages/builder/src/extrinsic/pallets/polkadotXcm/polkadotXcm.util.ts +++ b/packages/builder/src/extrinsic/pallets/polkadotXcm/polkadotXcm.util.ts @@ -13,6 +13,7 @@ export interface GetExtrinsicParams extends ExtrinsicConfigBuilderPrams { asset: any; func?: SubmittableExtrinsicFunction<'promise'>; parents?: Parents; + feeIndex?: number; } export function getPolkadotXcmExtrinsicArgs({ @@ -21,6 +22,7 @@ export function getPolkadotXcmExtrinsicArgs({ destination, func, parents = 1, + feeIndex = 0, }: GetExtrinsicParams): any[] { // eslint-disable-next-line @typescript-eslint/no-use-before-define const version = getExtrinsicArgumentVersion(func); @@ -47,7 +49,7 @@ export function getPolkadotXcmExtrinsicArgs({ { [version]: asset, }, - 0, + feeIndex, 'Unlimited', ]; } diff --git a/packages/config/src/assets.ts b/packages/config/src/assets.ts index 375ea2b0..63c77261 100644 --- a/packages/config/src/assets.ts +++ b/packages/config/src/assets.ts @@ -200,6 +200,11 @@ export const pica = new Asset({ originSymbol: 'PICA', }); +export const pink = new Asset({ + key: 'pink', + originSymbol: 'PINK', +}); + export const ring = new Asset({ key: 'ring', originSymbol: 'RING', @@ -370,6 +375,7 @@ export const assetsList: Asset[] = [ pen, pha, pica, + pink, ring, rmrk, sdn, diff --git a/packages/config/src/chains.ts b/packages/config/src/chains.ts index d758345c..fa22819e 100644 --- a/packages/config/src/chains.ts +++ b/packages/config/src/chains.ts @@ -45,6 +45,7 @@ import { pen, pha, pica, + pink, ring, rmrk, sdn, @@ -919,6 +920,10 @@ export const moonbeam = new EvmParachain({ asset: ztg, id: '150874409661081770150564009349448205842', }, + { + asset: pink, + id: '64174511183114006009298114091987195453', + }, ], ecosystem: Ecosystem.Polkadot, genesisHash: @@ -1291,6 +1296,11 @@ export const polkadotAssetHub = new Parachain({ id: 1337, palletInstance: 50, }, + { + asset: pink, + id: 23, + palletInstance: 50, + }, ], ecosystem: Ecosystem.Polkadot, genesisHash: diff --git a/packages/config/src/configs/moonbeam.ts b/packages/config/src/configs/moonbeam.ts index 79996b95..357c92f1 100644 --- a/packages/config/src/configs/moonbeam.ts +++ b/packages/config/src/configs/moonbeam.ts @@ -21,6 +21,7 @@ import { para, pen, pha, + pink, ring, sub, usdc, @@ -483,6 +484,21 @@ export const moonbeamConfig = new ChainConfig({ balance: BalanceBuilder().substrate().system().account(), }, }), + new AssetConfig({ + asset: pink, + balance: BalanceBuilder().substrate().assets().account(), + contract: ContractBuilder().Xtokens().transferMultiCurrencies(), + destination: polkadotAssetHub, + destinationFee: { + amount: 0.7, + asset: usdt, + balance: BalanceBuilder().substrate().assets().account(), + }, + fee: { + asset: glmr, + balance: BalanceBuilder().substrate().system().account(), + }, + }), new AssetConfig({ asset: hdx, balance: BalanceBuilder().substrate().assets().account(), diff --git a/packages/config/src/configs/polkadotAssetHub.ts b/packages/config/src/configs/polkadotAssetHub.ts index 9d9be9ef..e4f34457 100644 --- a/packages/config/src/configs/polkadotAssetHub.ts +++ b/packages/config/src/configs/polkadotAssetHub.ts @@ -4,7 +4,7 @@ import { ExtrinsicBuilder, FeeBuilder, } from '@moonbeam-network/xcm-builder'; -import { dot, usdc, usdt } from '../assets'; +import { dot, pink, usdc, usdt } from '../assets'; import { moonbeam, polkadotAssetHub } from '../chains'; import { AssetConfig } from '../types/AssetConfig'; import { ChainConfig } from '../types/ChainConfig'; @@ -53,6 +53,26 @@ export const polkadotAssetHubConfig = new ChainConfig({ }, min: AssetMinBuilder().assets().asset(), }), + new AssetConfig({ + asset: pink, + balance: BalanceBuilder().substrate().assets().account(), + destination: moonbeam, + destinationFee: { + amount: FeeBuilder().assetManager().assetTypeUnitsPerSecond(), + asset: usdt, + balance: BalanceBuilder().substrate().assets().account(), + }, + extrinsic: ExtrinsicBuilder() + .polkadotXcm() + .limitedReserveTransferAssets() + .X2(), + fee: { + asset: dot, + balance: BalanceBuilder().substrate().system().account(), + xcmDeliveryFeeAmount, + }, + min: AssetMinBuilder().assets().asset(), + }), ], chain: polkadotAssetHub, });