From 939b1f4c0fa94cdb7befedb59d1dceb66c0f2a86 Mon Sep 17 00:00:00 2001 From: cuteolaf Date: Mon, 6 May 2024 18:26:49 +0000 Subject: [PATCH] use typescript for e2e tests --- .gitignore | 1 + e2e_tests/{common.js => common.ts} | 12 ++- e2e_tests/fee-payment/custom.js | 73 ------------------- e2e_tests/fee-payment/custom.ts | 73 +++++++++++++++++++ e2e_tests/fee-payment/native.js | 18 ----- e2e_tests/fee-payment/native.ts | 18 +++++ e2e_tests/governance/delegated.js | 52 ------------- e2e_tests/governance/delegated.ts | 52 +++++++++++++ e2e_tests/governance/native.js | 35 --------- e2e_tests/governance/native.ts | 35 +++++++++ e2e_tests/package.json | 6 +- e2e_tests/tsconfig.json | 15 ++++ zombienet_tests/0002-native-fee-payment.zndsl | 2 +- zombienet_tests/0003-custom-fee-payment.zndsl | 2 +- .../0004-delegated-governance.zndsl | 2 +- zombienet_tests/0005-native-governance.zndsl | 2 +- 16 files changed, 210 insertions(+), 188 deletions(-) rename e2e_tests/{common.js => common.ts} (72%) delete mode 100644 e2e_tests/fee-payment/custom.js create mode 100644 e2e_tests/fee-payment/custom.ts delete mode 100644 e2e_tests/fee-payment/native.js create mode 100644 e2e_tests/fee-payment/native.ts delete mode 100644 e2e_tests/governance/delegated.js create mode 100644 e2e_tests/governance/delegated.ts delete mode 100644 e2e_tests/governance/native.js create mode 100644 e2e_tests/governance/native.ts create mode 100644 e2e_tests/tsconfig.json diff --git a/.gitignore b/.gitignore index ca01b4c0..6bacf47e 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,4 @@ bin/ node_modules **/package-lock.json +**/build \ No newline at end of file diff --git a/e2e_tests/common.js b/e2e_tests/common.ts similarity index 72% rename from e2e_tests/common.js rename to e2e_tests/common.ts index 0e5df279..04391000 100644 --- a/e2e_tests/common.js +++ b/e2e_tests/common.ts @@ -1,6 +1,10 @@ -const RELAY_ASSET_ID = 1; +import { ApiPromise } from "@polkadot/api"; +import { SubmittableExtrinsic, SignerOptions } from "@polkadot/api/types"; +import { KeyringPair } from "@polkadot/keyring/types"; -async function submitExtrinsic(signer, call, options) { +const RELAY_ASSET_ID = 1; + +async function submitExtrinsic(signer: KeyringPair, call: SubmittableExtrinsic<"promise">, options: Partial): Promise { return new Promise(async (resolve, reject) => { const unsub = await call.signAndSend(signer, options, (result) => { console.log(`Current status is ${result.status}`); @@ -23,7 +27,7 @@ async function submitExtrinsic(signer, call, options) { }); } -async function setupRelayAsset(api, signer) { +async function setupRelayAsset(api: ApiPromise, signer: KeyringPair) { const assetMetadata = { decimals: 12, name: "ROC", @@ -50,4 +54,4 @@ async function setupRelayAsset(api, signer) { await submitExtrinsic(signer, sudoCall, {}); } -module.exports = { submitExtrinsic, setupRelayAsset, RELAY_ASSET_ID } +export { submitExtrinsic, setupRelayAsset, RELAY_ASSET_ID } diff --git a/e2e_tests/fee-payment/custom.js b/e2e_tests/fee-payment/custom.js deleted file mode 100644 index 6f4d90f9..00000000 --- a/e2e_tests/fee-payment/custom.js +++ /dev/null @@ -1,73 +0,0 @@ -const { ApiPromise, WsProvider, Keyring } = require("@polkadot/api"); -const { submitExtrinsic, setupRelayAsset, RELAY_ASSET_ID } = require("../common"); - -async function run(nodeName, networkInfo, _jsArgs) { - const { wsUri: regionXUri } = networkInfo.nodesByName[nodeName]; - const { wsUri: rococoUri } = networkInfo.nodesByName["rococo-validator01"]; - - const rococoApi = await ApiPromise.create({provider: new WsProvider(rococoUri)}); - const regionXApi = await ApiPromise.create({ - provider: new WsProvider(regionXUri), - signedExtensions: { - ChargeAssetTxPayment: { - extrinsic: { - tip: "Compact", - assetId: "Option", - }, - payload: {}, - }, - }, - }); - - // account to submit tx - const keyring = new zombie.Keyring({ type: "sr25519" }); - const alice = keyring.addFromUri("//Alice"); - - const setXcmVersion = rococoApi.tx.xcmPallet.forceDefaultXcmVersion([3]); - await submitExtrinsic(alice, rococoApi.tx.sudo.sudo(setXcmVersion), {}); - - await setupRelayAsset(regionXApi, alice); - - const receiverKeypair = new Keyring(); - receiverKeypair.addFromAddress(alice.address); - - const feeAssetItem = 0; - const weightLimit = "Unlimited"; - const reserveTransfer = rococoApi.tx.xcmPallet.limitedReserveTransferAssets( - { V3: { parents: 0, interior: { X1: { Parachain: 2000 } } } }, //dest - { - V3: { - parents: 0, - interior: { - X1: { - AccountId32: { - chain: "Any", - id: receiverKeypair.pairs[0].publicKey, - }, - }, - }, - }, - }, //beneficiary - { - V3: [ - { - id: { - Concrete: { parents: 0, interior: "Here" }, - }, - fun: { - Fungible: 10n ** 9n, - }, - }, - ], - }, //asset - feeAssetItem, - weightLimit, - ); - await submitExtrinsic(alice, reserveTransfer, {}); - - // Try to pay for fees with relay chain asset. - const remarkCall = regionXApi.tx.system.remark("0x44"); - await submitExtrinsic(alice, remarkCall, { assetId: RELAY_ASSET_ID }); -} - -module.exports = { run }; diff --git a/e2e_tests/fee-payment/custom.ts b/e2e_tests/fee-payment/custom.ts new file mode 100644 index 00000000..78c59e13 --- /dev/null +++ b/e2e_tests/fee-payment/custom.ts @@ -0,0 +1,73 @@ +import { ApiPromise, WsProvider, Keyring } from "@polkadot/api"; +import { submitExtrinsic, setupRelayAsset, RELAY_ASSET_ID } from "../common"; + +async function run(nodeName: string, networkInfo: any, _jsArgs: any) { + const { wsUri: regionXUri } = networkInfo.nodesByName[nodeName]; + const { wsUri: rococoUri } = networkInfo.nodesByName["rococo-validator01"]; + + const rococoApi = await ApiPromise.create({ provider: new WsProvider(rococoUri) }); + const regionXApi = await ApiPromise.create({ + provider: new WsProvider(regionXUri), + signedExtensions: { + ChargeAssetTxPayment: { + extrinsic: { + tip: "Compact", + assetId: "Option", + }, + payload: {}, + }, + }, + }); + + // account to submit tx + const keyring = new Keyring({ type: "sr25519" }); + const alice = keyring.addFromUri("//Alice"); + + const setXcmVersion = rococoApi.tx.xcmPallet.forceDefaultXcmVersion([3]); + await submitExtrinsic(alice, rococoApi.tx.sudo.sudo(setXcmVersion), {}); + + await setupRelayAsset(regionXApi, alice); + + const receiverKeypair = new Keyring(); + receiverKeypair.addFromAddress(alice.address); + + const feeAssetItem = 0; + const weightLimit = "Unlimited"; + const reserveTransfer = rococoApi.tx.xcmPallet.limitedReserveTransferAssets( + { V3: { parents: 0, interior: { X1: { Parachain: 2000 } } } }, //dest + { + V3: { + parents: 0, + interior: { + X1: { + AccountId32: { + chain: "Any", + id: receiverKeypair.pairs[0].publicKey, + }, + }, + }, + }, + }, //beneficiary + { + V3: [ + { + id: { + Concrete: { parents: 0, interior: "Here" }, + }, + fun: { + Fungible: 10n ** 9n, + }, + }, + ], + }, //asset + feeAssetItem, + weightLimit + ); + await submitExtrinsic(alice, reserveTransfer, {}); + + // Try to pay for fees with relay chain asset. + const remarkCall = regionXApi.tx.system.remark("0x44"); + await submitExtrinsic(alice, remarkCall, { assetId: RELAY_ASSET_ID }); +} + +export { run }; diff --git a/e2e_tests/fee-payment/native.js b/e2e_tests/fee-payment/native.js deleted file mode 100644 index 6598e5c9..00000000 --- a/e2e_tests/fee-payment/native.js +++ /dev/null @@ -1,18 +0,0 @@ -const { ApiPromise, WsProvider } = require("@polkadot/api"); -const { submitExtrinsic } = require("../common"); - -async function run(nodeName, networkInfo, _jsArgs) { - const { wsUri } = networkInfo.nodesByName[nodeName]; - const api = await ApiPromise.create({provider: new WsProvider(wsUri)}); - - // account to submit tx - const keyring = new zombie.Keyring({ type: "sr25519" }); - const alice = keyring.addFromUri("//Alice"); - const bob = keyring.addFromUri("//Bob"); - - const call = api.tx.balances.transferKeepAlive(bob.address, 10n**6n); - const sudo = api.tx.sudo.sudo(call); - await submitExtrinsic(alice, sudo, {}); -} - -module.exports = { run }; diff --git a/e2e_tests/fee-payment/native.ts b/e2e_tests/fee-payment/native.ts new file mode 100644 index 00000000..b4fef856 --- /dev/null +++ b/e2e_tests/fee-payment/native.ts @@ -0,0 +1,18 @@ +import { ApiPromise, Keyring, WsProvider } from "@polkadot/api"; +import { submitExtrinsic } from "../common"; + +async function run(nodeName: string, networkInfo: any, _jsArgs: any) { + const { wsUri } = networkInfo.nodesByName[nodeName]; + const api = await ApiPromise.create({ provider: new WsProvider(wsUri) }); + + // account to submit tx + const keyring = new Keyring({ type: "sr25519" }); + const alice = keyring.addFromUri("//Alice"); + const bob = keyring.addFromUri("//Bob"); + + const call = api.tx.balances.transferKeepAlive(bob.address, 10n ** 6n); + const sudo = api.tx.sudo.sudo(call); + await submitExtrinsic(alice, sudo, {}); +} + +export { run }; diff --git a/e2e_tests/governance/delegated.js b/e2e_tests/governance/delegated.js deleted file mode 100644 index aa073d57..00000000 --- a/e2e_tests/governance/delegated.js +++ /dev/null @@ -1,52 +0,0 @@ -const { ApiPromise, WsProvider, Keyring } = require("@polkadot/api"); -const { submitExtrinsic, setupRelayAsset, RELAY_ASSET_ID } = require("../common"); - -const PREIMAGE_HASH = "0xb8375f7ca0c64a384f2dd643a0d520977f3aae06e64afb8c960891eee5147bd1"; - -async function run(nodeName, networkInfo, _jsArgs) { - const { wsUri } = networkInfo.nodesByName[nodeName]; - const api = await ApiPromise.create({ - provider: new WsProvider(wsUri), - signedExtensions: { - ChargeAssetTxPayment: { - extrinsic: { - tip: "Compact", - assetId: "Option", - }, - payload: {}, - }, - }, - }); - - // account to submit tx - const keyring = new Keyring({ type: "sr25519" }); - const alice = keyring.addFromUri("//Alice"); - const anna = keyring.addFromUri("//Anna"); - - await setupRelayAsset(api, alice); - - const giveBalanceCall = api.tx.tokens.setBalance(anna.address, RELAY_ASSET_ID, 10n ** 18n, 0); - await submitExtrinsic(alice, api.tx.sudo.sudo(giveBalanceCall), {}); - - const remarkCallBytes = api.tx.system.remark("hey").toU8a(); - await submitExtrinsic(alice, api.tx.preimage.notePreimage(remarkCallBytes), {}); - - const submitProposal = api.tx.delegatedReferenda.submit( - { system: "Root" }, - { Lookup: { hash: PREIMAGE_HASH, len: remarkCallBytes.length } }, - { After: 5 }, - ); - await submitExtrinsic(anna, submitProposal, { assetId: RELAY_ASSET_ID }); - - const placeDeposit = api.tx.delegatedReferenda.placeDecisionDeposit(0); - await submitExtrinsic(anna, placeDeposit, { assetId: RELAY_ASSET_ID }); - - const voteCall = api.tx.delegatedConvictionVoting.vote(0, { - // Voting with relay chain tokens. We know this is true; otherwise, this call - // would fail, given that Anna doesn't have 10^16 RegionX tokens. - Standard: { vote: { aye: true, conviction: "None" }, balance: 10n ** 16n }, - }); - await submitExtrinsic(anna, voteCall, { assetId: RELAY_ASSET_ID }); -} - -module.exports = { run }; diff --git a/e2e_tests/governance/delegated.ts b/e2e_tests/governance/delegated.ts new file mode 100644 index 00000000..960428f6 --- /dev/null +++ b/e2e_tests/governance/delegated.ts @@ -0,0 +1,52 @@ +import { ApiPromise, WsProvider, Keyring } from "@polkadot/api"; +import { submitExtrinsic, setupRelayAsset, RELAY_ASSET_ID } from "../common"; + +const PREIMAGE_HASH = "0xb8375f7ca0c64a384f2dd643a0d520977f3aae06e64afb8c960891eee5147bd1"; + +async function run(nodeName: string, networkInfo: any, _jsArgs: any) { + const { wsUri } = networkInfo.nodesByName[nodeName]; + const api = await ApiPromise.create({ + provider: new WsProvider(wsUri), + signedExtensions: { + ChargeAssetTxPayment: { + extrinsic: { + tip: "Compact", + assetId: "Option", + }, + payload: {}, + }, + }, + }); + + // account to submit tx + const keyring = new Keyring({ type: "sr25519" }); + const alice = keyring.addFromUri("//Alice"); + const anna = keyring.addFromUri("//Anna"); + + await setupRelayAsset(api, alice); + + const giveBalanceCall = api.tx.tokens.setBalance(anna.address, RELAY_ASSET_ID, 10n ** 18n, 0); + await submitExtrinsic(alice, api.tx.sudo.sudo(giveBalanceCall), {}); + + const remarkCallBytes = api.tx.system.remark("hey").toU8a(); + await submitExtrinsic(alice, api.tx.preimage.notePreimage(remarkCallBytes), {}); + + const submitProposal = api.tx.delegatedReferenda.submit( + { system: "Root" }, + { Lookup: { hash: PREIMAGE_HASH, len: remarkCallBytes.length } }, + { After: 5 } + ); + await submitExtrinsic(anna, submitProposal, { assetId: RELAY_ASSET_ID }); + + const placeDeposit = api.tx.delegatedReferenda.placeDecisionDeposit(0); + await submitExtrinsic(anna, placeDeposit, { assetId: RELAY_ASSET_ID }); + + const voteCall = api.tx.delegatedConvictionVoting.vote(0, { + // Voting with relay chain tokens. We know this is true; otherwise, this call + // would fail, given that Anna doesn't have 10^16 RegionX tokens. + Standard: { vote: { aye: true, conviction: "None" }, balance: 10n ** 16n }, + }); + await submitExtrinsic(anna, voteCall, { assetId: RELAY_ASSET_ID }); +} + +export { run }; diff --git a/e2e_tests/governance/native.js b/e2e_tests/governance/native.js deleted file mode 100644 index d2b33318..00000000 --- a/e2e_tests/governance/native.js +++ /dev/null @@ -1,35 +0,0 @@ -const { ApiPromise, WsProvider, Keyring } = require("@polkadot/api"); -const { submitExtrinsic, setupRelayAsset } = require("../common"); - -const PREIMAGE_HASH = "0x0ccf4369e9a9f88f035828ba0dd5da645d5c0fa7baa86bdc8d7a80c183ab84c9"; - -async function run(nodeName, networkInfo, _jsArgs) { - const { wsUri } = networkInfo.nodesByName[nodeName]; - const api = await ApiPromise.create({provider: new WsProvider(wsUri)}); - - // account to submit tx - const keyring = new Keyring({ type: "sr25519" }); - const alice = keyring.addFromUri("//Alice"); - - await setupRelayAsset(api, alice); - - const spendCallBytes = api.tx.treasury.spendLocal(10n**6n, alice.address).toU8a(); - await submitExtrinsic(alice, api.tx.preimage.notePreimage(spendCallBytes), {}); - - const submitProposal = api.tx.nativeReferenda.submit( - { Origins: "SmallTipper" }, - { Lookup: { hash: PREIMAGE_HASH, len: spendCallBytes.length } }, - { After: 5 }, - ); - await submitExtrinsic(alice, submitProposal, {}); - - const placeDeposit = api.tx.nativeReferenda.placeDecisionDeposit(0); - await submitExtrinsic(alice, placeDeposit, {}); - - const voteCall = api.tx.nativeConvictionVoting.vote(0, { - Standard: { vote: { aye: true, conviction: "None" }, balance: 10n ** 16n }, - }); - await submitExtrinsic(alice, voteCall, {}); -} - -module.exports = { run }; diff --git a/e2e_tests/governance/native.ts b/e2e_tests/governance/native.ts new file mode 100644 index 00000000..40c78e7d --- /dev/null +++ b/e2e_tests/governance/native.ts @@ -0,0 +1,35 @@ +import { ApiPromise, WsProvider, Keyring } from "@polkadot/api"; +import { submitExtrinsic, setupRelayAsset } from "../common"; + +const PREIMAGE_HASH = "0x0ccf4369e9a9f88f035828ba0dd5da645d5c0fa7baa86bdc8d7a80c183ab84c9"; + +async function run(nodeName: string, networkInfo: any, _jsArgs: any) { + const { wsUri } = networkInfo.nodesByName[nodeName]; + const api = await ApiPromise.create({ provider: new WsProvider(wsUri) }); + + // account to submit tx + const keyring = new Keyring({ type: "sr25519" }); + const alice = keyring.addFromUri("//Alice"); + + await setupRelayAsset(api, alice); + + const spendCallBytes = api.tx.treasury.spendLocal(10n ** 6n, alice.address).toU8a(); + await submitExtrinsic(alice, api.tx.preimage.notePreimage(spendCallBytes), {}); + + const submitProposal = api.tx.nativeReferenda.submit( + { Origins: "SmallTipper" }, + { Lookup: { hash: PREIMAGE_HASH, len: spendCallBytes.length } }, + { After: 5 } + ); + await submitExtrinsic(alice, submitProposal, {}); + + const placeDeposit = api.tx.nativeReferenda.placeDecisionDeposit(0); + await submitExtrinsic(alice, placeDeposit, {}); + + const voteCall = api.tx.nativeConvictionVoting.vote(0, { + Standard: { vote: { aye: true, conviction: "None" }, balance: 10n ** 16n }, + }); + await submitExtrinsic(alice, voteCall, {}); +} + +export { run }; diff --git a/e2e_tests/package.json b/e2e_tests/package.json index 48b819dc..7ddd3a57 100644 --- a/e2e_tests/package.json +++ b/e2e_tests/package.json @@ -4,12 +4,14 @@ "description": "", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "build": "npx tsc --outDir build > /dev/null" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { - "@polkadot/api": "^11.0.1" + "@polkadot/api": "^11.0.2", + "@polkadot/keyring": "^12.6.2", + "typescript": "^5.4.5" } } diff --git a/e2e_tests/tsconfig.json b/e2e_tests/tsconfig.json new file mode 100644 index 00000000..e399350e --- /dev/null +++ b/e2e_tests/tsconfig.json @@ -0,0 +1,15 @@ +{ + "compilerOptions": { + "module": "commonjs", + "outDir": "build", + "target": "ES2020", + "strict": true, + "esModuleInterop": true + }, + "include": [ + "**/*.ts" + ], + "exclude": [ + "node_modules" + ] + } \ No newline at end of file diff --git a/zombienet_tests/0002-native-fee-payment.zndsl b/zombienet_tests/0002-native-fee-payment.zndsl index e87c365e..7eaae633 100644 --- a/zombienet_tests/0002-native-fee-payment.zndsl +++ b/zombienet_tests/0002-native-fee-payment.zndsl @@ -7,4 +7,4 @@ bob: is up alice: parachain 2000 is registered within 225 seconds -regionx-collator01: js-script ../e2e_tests/fee-payment/native.js return is 0 within 200 seconds +regionx-collator01: js-script ../e2e_tests/build/fee-payment/native.js return is 0 within 200 seconds diff --git a/zombienet_tests/0003-custom-fee-payment.zndsl b/zombienet_tests/0003-custom-fee-payment.zndsl index 3ed50752..75ec1d28 100644 --- a/zombienet_tests/0003-custom-fee-payment.zndsl +++ b/zombienet_tests/0003-custom-fee-payment.zndsl @@ -7,4 +7,4 @@ rococo-validator02: is up rococo-validator01: parachain 2000 is registered within 225 seconds -regionx-collator01: js-script ../e2e_tests/fee-payment/custom.js return is 0 within 400 seconds +regionx-collator01: js-script ../e2e_tests/build/fee-payment/custom.js return is 0 within 400 seconds diff --git a/zombienet_tests/0004-delegated-governance.zndsl b/zombienet_tests/0004-delegated-governance.zndsl index f81632ce..4c63880c 100644 --- a/zombienet_tests/0004-delegated-governance.zndsl +++ b/zombienet_tests/0004-delegated-governance.zndsl @@ -7,4 +7,4 @@ rococo-validator02: is up rococo-validator01: parachain 2000 is registered within 225 seconds -regionx-collator01: js-script ../e2e_tests/governance/delegated.js return is 0 within 400 seconds +regionx-collator01: js-script ../e2e_tests/build/governance/delegated.js return is 0 within 400 seconds diff --git a/zombienet_tests/0005-native-governance.zndsl b/zombienet_tests/0005-native-governance.zndsl index 896b4767..3830e72c 100644 --- a/zombienet_tests/0005-native-governance.zndsl +++ b/zombienet_tests/0005-native-governance.zndsl @@ -7,4 +7,4 @@ rococo-validator02: is up rococo-validator01: parachain 2000 is registered within 225 seconds -regionx-collator01: js-script ../e2e_tests/governance/native.js return is 0 within 400 seconds +regionx-collator01: js-script ../e2e_tests/build/governance/native.js return is 0 within 400 seconds