Skip to content

Commit

Permalink
Migrate XCM TS tests to Moonwall (#2441)
Browse files Browse the repository at this point in the history
* progress on PoV tests

* started precompile migration

* finished author precompile tests

* bn and callpermit precompiles done

* collective precompile done

* beginning conviction voting tests

* Updated to moonwall4

* fix

* added erc20 precompile tests

* progress on local asset precompile

* Added Local assets tests

* modexp tests done

* Added proxy tests

* progress on proxy tests

* finished proxy tests

* updated precompile tests

* added more precompile tests

* progress

* added more precompile tests

* refactored wormhole tests

* added XCM transactor tests

* started xcm utils tests

* Precompile tests done

* done proxy tests

* randomness tests

* randomness tests

* randomness tests

* finished randomness tests

* added receipt tests

* staking tests

* more staking tests

* more staking

* progress

* completed staking tests

* added subscription tests

* added sudo tests

* added treasury tests

* update moonwall config

* added missing conviction tests

* txpool progress

* tx pool tests done

* CI fix

* updated prettier CI

* editorconfig

* pnpm cache

* changed CI

* fix CI

* changes

* fix CI

* fixed some tests

* more test fixes

* fixed tests

* lint

* editor config

* pkg fix

* updated lockfile

* pkg updates

* fix import

* fix import

* archived old-style dev-tests

* lint

* PR comments

* Port ERC20-XCM tests. Add excess gas tests (#2410)

* ERC20XCM tests

* Add tsx

* Moved expect out of xcm helper

* Removed unused imports

* removed old tests

* editorconfig grr

* begin xcm migration

* migrate more xcm tests to moonwall using xcmv3

* removed smoke tests

* Upgraded randomness smoketest

* updated randomness comments

* Fix conviction MaxVotes (#2401)

* Reduce the ConvictionVoting MaxVotes from 512 to 30

* Set MaxVote to 20

* Run toml-sort and update repo reference (#2426)

* [MOON-2434] remove deprecated leave delegator functionality (#2349)

* remove leave delegator functionality

* remove precompile methods

* return error for removed call indices

* removed scheduleLeaveDelegators calls

* de-bun 🫓 the CI

* fix

* migrate hrmp-asset-transfer tests

* apply some fixes

* migrate test-mock-hrmp-transact-ethereum

* small fixes

* migrate test-mock-hrmp-transact

* migrate more tests to moonwall

* migrate test-xcmv3-new-instructions and max weight

* create separate folders for v2 and v3

* migrate test-mock-hrmp-transact tests to v3

* migrate mock-hrmp-transact-ethereum tests to v3

* fix network "Any" in tests

* fix order in xcmv2 tests

* fix order in xcmv3 tests

* remove replaceNetworkAny

* prettier

* fmt

* fix orders

* remove "Any" type from test-xcm-erc20-v3-filter

* use proper GLMR constant

* remove cast to any in xcmv2 tests

* remove cast to any in xcmv3 tests and re-org

* small fixes

* add expect messages

* refactor registerForeignAsset

* fixes in test-mock-dmp-queue

* remove BN from AssetMetadata

* use proper readContract function

* use proper deployContract function

* remove BN from requireWeightAtMost

* use sovereignAccountOfSibling helper function

* remove expectOk

---------

Co-authored-by: timbrinded <79199034+timbrinded@users.noreply.github.com>
Co-authored-by: Francisco Gamundi <52399794+fgamundi@users.noreply.github.com>
Co-authored-by: Alan Sapede <alan.sapede@gmail.com>
  • Loading branch information
4 people authored and grw-ms committed Aug 28, 2023
1 parent 80d226b commit 534b387
Show file tree
Hide file tree
Showing 76 changed files with 8,736 additions and 56 deletions.
4 changes: 2 additions & 2 deletions test/helpers/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ export const PARA_1001_SOURCE_LOCATION = {
export interface AssetMetadata {
name: string;
symbol: string;
decimals: BN;
decimals: bigint;
isFrozen: boolean;
}

export const relayAssetMetadata: AssetMetadata = {
name: "DOT",
symbol: "DOT",
decimals: new BN(12),
decimals: 12n,
isFrozen: false,
};

Expand Down
48 changes: 17 additions & 31 deletions test/helpers/xcm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ export async function registerForeignAsset(
context
.polkadotJs()
.tx.assetManager.setAssetUnitsPerSecond(asset, unitsPerSecond, numAssetsWeightHint!)
)
),
{
expectEvents: [context.polkadotJs().events.assetManager.UnitsPerSecondChanged],
allowFailures: false,
}
);
// check asset in storage
const registeredAsset = (
Expand Down Expand Up @@ -202,9 +206,12 @@ export async function injectHrmpMessageAndSeal(

interface Junction {
Parachain?: number;
AccountId32?: { network: "Any" | XcmV3JunctionNetworkId["type"]; id: Uint8Array | string };
AccountIndex64?: { network: "Any" | XcmV3JunctionNetworkId["type"]; index: number };
AccountKey20?: { network: "Any" | XcmV3JunctionNetworkId["type"]; key: Uint8Array | string };
AccountId32?: { network: "Any" | XcmV3JunctionNetworkId["type"] | null; id: Uint8Array | string };
AccountIndex64?: { network: "Any" | XcmV3JunctionNetworkId["type"] | null; index: number };
AccountKey20?: {
network: "Any" | XcmV3JunctionNetworkId["type"] | null;
key: Uint8Array | string;
};
PalletInstance?: number;
GeneralIndex?: bigint;
GeneralKey?: { length: number; data: Uint8Array };
Expand Down Expand Up @@ -344,13 +351,13 @@ export class XcmFragment {
}

// Add a `DescendOrigin` instruction
descend_origin(): this {
descend_origin(network: "Any" | XcmV3JunctionNetworkId["type"] | null = null): this {
if (this.config.descend_origin != null) {
this.instructions.push({
DescendOrigin: {
X1: {
AccountKey20: {
network: "Any",
network,
key: this.config.descend_origin,
},
},
Expand Down Expand Up @@ -460,7 +467,7 @@ export class XcmFragment {
/// XCM V3 calls
as_v3(): any {
return {
V3: replaceNetworkAny(this.instructions),
V3: this.instructions,
};
}

Expand Down Expand Up @@ -554,7 +561,7 @@ export class XcmFragment {
): this {
this.instructions.push({
QueryPallet: {
module_name: stringToU8a(module_name),
module_name,
response_info: {
destination,
query_id,
Expand All @@ -576,8 +583,8 @@ export class XcmFragment {
this.instructions.push({
ExpectPallet: {
index,
name: stringToU8a(name),
module_name: stringToU8a(module_name),
name,
module_name,
crate_major,
min_crate_minor,
},
Expand Down Expand Up @@ -807,27 +814,6 @@ export class XcmFragment {
}
}

function replaceNetworkAny(obj: AnyObject | Array<AnyObject>): any {
if (Array.isArray(obj)) {
return obj.map((item) => replaceNetworkAny(item));
} else if (typeof obj === "object" && obj !== null) {
const newObj: AnyObject = {};
for (const key in obj) {
if (key === "network" && obj[key] === "Any") {
newObj[key] = null;
} else {
newObj[key] = replaceNetworkAny(obj[key]);
}
}
return newObj;
}
return obj;
}

type AnyObject = {
[key: string]: any;
};

export const registerXcmTransactorAndContract = async (context: DevModeContext) => {
await context.createBlock(
context
Expand Down
80 changes: 80 additions & 0 deletions test/suites/dev/test-xcm-v2/test-mock-dmp-error-and-appendix-1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import "@moonbeam-network/api-augment";
import { beforeAll, describeSuite, expect, customDevRpcRequest } from "@moonwall/cli";

import { alith } from "@moonwall/util";
import { RELAY_SOURCE_LOCATION, relayAssetMetadata } from "../../../helpers/assets.js";
import { registerForeignAsset, XcmFragment } from "../../../helpers/xcm.js";
import type { XcmVersionedXcm } from "@polkadot/types/lookup";

// Twelve decimal places in the moonbase relay chain's token
const RELAY_TOKEN = 1_000_000_000_000n;

const palletId = "0x6D6f646c617373746d6E67720000000000000000";

describeSuite({
id: "D3401",
title: "Mock XCM V3 - downward transfer with non-triggered error handler",
foundationMethods: "dev",
testCases: ({ context, it, log }) => {
let assetId: string;

beforeAll(async () => {
// registerForeignAsset
const { registeredAssetId, registeredAsset } = await registerForeignAsset(
context,
RELAY_SOURCE_LOCATION,
relayAssetMetadata
);
assetId = registeredAssetId;
expect(registeredAsset.owner.toHex()).to.eq(palletId.toLowerCase());
});

it({
id: "T01",
title: "Should make sure that Alith does not receive 10 dot without error",
test: async function () {
const xcmMessage = new XcmFragment({
assets: [
{
multilocation: {
parents: 1,
interior: {
Here: null,
},
},
fungible: 10n * RELAY_TOKEN,
},
],
beneficiary: alith.address,
})
.reserve_asset_deposited()
.buy_execution()
// BuyExecution does not charge for fees because we registered it for not doing so
// But since there is no error, and the deposit is on the error handler, the assets
// will be trapped
.with(function () {
return this.set_error_handler_with([this.deposit_asset]);
})
.clear_origin()
.as_v2();

const receivedMessage: XcmVersionedXcm = context
.polkadotJs()
.createType("XcmVersionedXcm", xcmMessage);

const totalMessage = [...receivedMessage.toU8a()];
// Send RPC call to inject XCM message
await customDevRpcRequest("xcm_injectDownwardMessage", [totalMessage]);

// Create a block in which the XCM will be executed
await context.createBlock();
// Make sure ALITH did not reveive anything
const alith_dot_balance = await context
.polkadotJs()
.query.localAssets.account(assetId, alith.address);

expect(alith_dot_balance.isNone, "Alith's DOT balance is not empty").to.be.true;
},
});
},
});
82 changes: 82 additions & 0 deletions test/suites/dev/test-xcm-v2/test-mock-dmp-error-and-appendix-2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import "@moonbeam-network/api-augment";
import { beforeAll, describeSuite, expect, customDevRpcRequest } from "@moonwall/cli";

import { alith } from "@moonwall/util";
import { RELAY_SOURCE_LOCATION, relayAssetMetadata } from "../../../helpers/assets.js";
import { registerForeignAsset, XcmFragment } from "../../../helpers/xcm.js";
import type { XcmVersionedXcm } from "@polkadot/types/lookup";

// Twelve decimal places in the moonbase relay chain's token
const RELAY_TOKEN = 1_000_000_000_000n;

const palletId = "0x6D6f646c617373746d6E67720000000000000000";

describeSuite({
id: "D3402",
title: "Mock XCM V3 - downward transfer with triggered error handler",
foundationMethods: "dev",
testCases: ({ context, it, log }) => {
let assetId: string;

beforeAll(async () => {
// registerForeignAsset
const { registeredAssetId, registeredAsset } = await registerForeignAsset(
context,
RELAY_SOURCE_LOCATION,
relayAssetMetadata
);
assetId = registeredAssetId;
expect(registeredAsset.owner.toHex()).to.eq(palletId.toLowerCase());
});

it({
id: "T01",
title: "Should make sure that Alith does receive 10 dot because there is error",
test: async function () {
const xcmMessage = new XcmFragment({
assets: [
{
multilocation: {
parents: 1,
interior: {
Here: null,
},
},
fungible: 10n * RELAY_TOKEN,
},
],
beneficiary: alith.address,
})
.reserve_asset_deposited()
.buy_execution()
// BuyExecution does not charge for fees because we registered it for not doing so
// As a consequence the trapped assets will be entirely credited
.with(function () {
return this.set_error_handler_with([this.deposit_asset]);
})
.trap()
.as_v2();

const receivedMessage: XcmVersionedXcm = context
.polkadotJs()
.createType("XcmVersionedXcm", xcmMessage);

const totalMessage = [...receivedMessage.toU8a()];

// Send RPC call to inject XCM message
await customDevRpcRequest("xcm_injectDownwardMessage", [totalMessage]);

// Create a block in which the XCM will be executed
await context.createBlock();
// Make sure the state has ALITH's to DOT tokens
const alith_dot_balance = (
await context.polkadotJs().query.assets.account(assetId, alith.address)
)
.unwrap()
.balance.toBigInt();

expect(alith_dot_balance, "Alith's DOT balance is empty").to.eq(10n * RELAY_TOKEN);
},
});
},
});
79 changes: 79 additions & 0 deletions test/suites/dev/test-xcm-v2/test-mock-dmp-error-and-appendix-3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import "@moonbeam-network/api-augment";
import { beforeAll, describeSuite, expect, customDevRpcRequest } from "@moonwall/cli";

import { alith } from "@moonwall/util";
import { RELAY_SOURCE_LOCATION, relayAssetMetadata } from "../../../helpers/assets.js";
import { registerForeignAsset, XcmFragment } from "../../../helpers/xcm.js";
import type { XcmVersionedXcm } from "@polkadot/types/lookup";

// Twelve decimal places in the moonbase relay chain's token
const RELAY_TOKEN = 1_000_000_000_000n;

const palletId = "0x6D6f646c617373746d6E67720000000000000000";

describeSuite({
id: "D3403",
title: "Mock XCM V3 - downward transfer with always triggered appendix",
foundationMethods: "dev",
testCases: ({ context, it, log }) => {
let assetId: string;

beforeAll(async () => {
// registerForeignAsset
const { registeredAssetId, registeredAsset } = await registerForeignAsset(
context,
RELAY_SOURCE_LOCATION,
relayAssetMetadata
);
assetId = registeredAssetId;
expect(registeredAsset.owner.toHex()).to.eq(palletId.toLowerCase());
});

it({
id: "T01",
title: "Should make sure Alith receives 10 dot with appendix and without error",
test: async function () {
const xcmMessage = new XcmFragment({
assets: [
{
multilocation: {
parents: 1,
interior: {
Here: null,
},
},
fungible: 10n * RELAY_TOKEN,
},
],
beneficiary: alith.address,
})
.reserve_asset_deposited()
.buy_execution()
// Set an appendix to be executed after the XCM message is executed. No matter if errors
.with(function () {
return this.set_appendix_with([this.deposit_asset]);
})
.as_v2();

const receivedMessage: XcmVersionedXcm = context
.polkadotJs()
.createType("XcmVersionedXcm", xcmMessage);

const totalMessage = [...receivedMessage.toU8a()];
// Send RPC call to inject XCM message
await customDevRpcRequest("xcm_injectDownwardMessage", [totalMessage]);

// Create a block in which the XCM will be executed
await context.createBlock();
// Make sure the state has ALITH's to DOT tokens
const alith_dot_balance = (
await context.polkadotJs().query.assets.account(assetId, alith.address)
)
.unwrap()
.balance.toBigInt();

expect(alith_dot_balance, "Alith's DOT balance is empty").to.eq(10n * RELAY_TOKEN);
},
});
},
});

0 comments on commit 534b387

Please sign in to comment.