Skip to content

Commit

Permalink
setup test with chopsticks
Browse files Browse the repository at this point in the history
  • Loading branch information
ermalkaleci committed Jan 24, 2023
1 parent 3fc5f0d commit f6367e0
Show file tree
Hide file tree
Showing 21 changed files with 7,684 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
root = true
[*]
indent_style=tab
indent_size=tab
tab_width=4
end_of_line=lf
charset=utf-8
trim_trailing_whitespace=true
max_line_length=120
insert_final_newline=true

[*.{yml,yaml}]
indent_style=space
indent_size=2
tab_width=8
end_of_line=lf

[*.{js,ts}]
indent_style=space
tab_width=2
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vitest.config.ts
.eslintrc.js
node_modules/
27 changes: 27 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module.exports = {
root: true,
parser: "@typescript-eslint/parser",
plugins: ["@typescript-eslint", "import", "sort-imports-es6-autofix"],
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"prettier",
"plugin:import/recommended",
"plugin:import/typescript",
],
rules: {
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"sort-imports-es6-autofix/sort-imports-es6": "error",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/no-unused-vars": [
"warn",
{
argsIgnorePattern: "^_",
varsIgnorePattern: "^_",
caughtErrorsIgnorePattern: "^_",
},
],
},
};
11 changes: 11 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
- package-ecosystem: "npm" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "monthly"
23 changes: 23 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
submodules: recursive
- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: 18.x
cache: 'yarn'
- run: yarn --immutable
- run: yarn lint
- run: yarn test
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,9 @@ dist

# TernJS port file
.tern-port

.yarn/*
!.yarn/releases
!.yarn/patches
!.yarn/plugins
!.yarn/sdks
1 change: 1 addition & 0 deletions .husky/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

yarn lint-staged
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.yarn
*.yml
*.md
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
}
541 changes: 541 additions & 0 deletions .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions .yarn/plugins/@yarnpkg/plugin-typescript.cjs

Large diffs are not rendered by default.

801 changes: 801 additions & 0 deletions .yarn/releases/yarn-3.2.4.cjs

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
nodeLinker: node-modules

plugins:
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
spec: "@yarnpkg/plugin-interactive-tools"
- path: .yarn/plugins/@yarnpkg/plugin-typescript.cjs
spec: "@yarnpkg/plugin-typescript"

yarnPath: .yarn/releases/yarn-3.2.4.cjs
36 changes: 36 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "e2e-tests",
"version": "0.1.0",
"author": "Ermal Kaleci <[email protected]>",
"license": "MIT",
"private": true,
"packageManager": "[email protected]",
"scripts": {
"lint": "tsc --noEmit && eslint . --ext .js,.ts && prettier --check .",
"fix": "eslint . --ext .js,.ts --fix && prettier -w .",
"prepare": "husky install",
"test": "LOG_LEVEL=error vitest --silent",
"test:ui": "yarn test --ui",
"test:dev": "vitest --inspect"
},
"dependencies": {
"@acala-network/chopsticks": "^0.3.6",
"@types/node": "^18.11.18",
"@typescript-eslint/eslint-plugin": "^5.48.2",
"@typescript-eslint/parser": "^5.48.2",
"@vitest/ui": "^0.28.1",
"eslint": "^8.32.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-sort-imports-es6-autofix": "^0.6.0",
"husky": "^8.0.3",
"lint-staged": "^13.1.0",
"prettier": "^2.8.3",
"ts-node": "^10.9.1",
"typescript": "^4.9.4",
"vitest": "^0.28.1"
},
"engines": {
"node": ">=v14"
}
}
98 changes: 98 additions & 0 deletions tests/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { ApiPromise, WsProvider } from "@polkadot/api";
import { BuildBlockMode, setupWithServer } from "@acala-network/chopsticks";
import { Codec } from "@polkadot/types/types";
import { HexString } from "@polkadot/util/types";
import { Keyring } from "@polkadot/keyring";
import { StorageValues } from "@acala-network/chopsticks/dist/utils/set-storage";
import { expect } from "vitest";

export type SetupOption = {
endpoint: string;
blockNumber?: number;
blockHash?: HexString;
mockSignatureHost?: boolean;
};

export const setupContext = async ({ endpoint, blockNumber, blockHash, mockSignatureHost }: SetupOption) => {
const port = 8000;
const config = {
endpoint,
port,
block: blockNumber || blockHash,
mockSignatureHost,
"build-block-mode": BuildBlockMode.Manual,
};
const { chain, listenPort, close } = await setupWithServer(config);

const ws = new WsProvider(`ws://localhost:${listenPort}`);
const api = await ApiPromise.create({
provider: ws,
signedExtensions: {
SetEvmOrigin: {
extrinsic: {},
payload: {},
},
},
});

await api.isReady;

return {
chain,
ws,
api,
dev: {
newBlock: (param?: { count?: number; to?: number }): Promise<string> => {
return ws.send("dev_newBlock", [param]);
},
setStorage: (values: StorageValues, blockHash?: string) => {
return ws.send("dev_setStorage", [values, blockHash]);
},
timeTravel: (date: string | number) => {
return ws.send<number>("dev_timeTravel", [date]);
},
setHead: (hashOrNumber: string | number) => {
return ws.send("dev_setHead", [hashOrNumber]);
},
},
async teardown() {
await api.disconnect();
await close();
},
};
};

type CodecOrArray = Codec | Codec[];

export const matchSnapshot = (codec: CodecOrArray | Promise<CodecOrArray>) => {
return expect(
Promise.resolve(codec).then((x) => (Array.isArray(x) ? x.map((x) => x.toHuman()) : x.toHuman()))
).resolves.toMatchSnapshot();
};

export const expectEvent = async (codec: Codec | Promise<Codec>, event: any) => {
return expect(await Promise.resolve(codec).then((x) => x.toHuman())).toEqual(
expect.arrayContaining([expect.objectContaining(event)])
);
};

export const testingPairs = (ss58Format?: number) => {
const keyring = new Keyring({ type: "ed25519", ss58Format }); // cannot use sr25519 as it is non determinstic
const alice = keyring.addFromUri("//Alice");
const bob = keyring.addFromUri("//Bob");
const charlie = keyring.addFromUri("//Charlie");
const dave = keyring.addFromUri("//Dave");
const eve = keyring.addFromUri("//Eve");
const test1 = keyring.addFromUri("//test1");
const test2 = keyring.addFromUri("//test2");
return {
alice,
bob,
charlie,
dave,
eve,
test1,
test2,
keyring,
};
};
47 changes: 47 additions & 0 deletions tests/polkadot-acala/__snapshots__/transfer-reserve.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Vitest Snapshot v1

exports[`Polkadot <-> Acala > Polkadot transfer assets to Acala 1`] = `
{
"consumers": "0",
"data": {
"feeFrozen": "0",
"free": "10,000,000,000,000",
"miscFrozen": "0",
"reserved": "0",
},
"nonce": "0",
"providers": "0",
"sufficients": "0",
}
`;

exports[`Polkadot <-> Acala > Polkadot transfer assets to Acala 2`] = `
{
"free": "0",
"frozen": "0",
"reserved": "0",
}
`;

exports[`Polkadot <-> Acala > Polkadot transfer assets to Acala 3`] = `
{
"consumers": "0",
"data": {
"feeFrozen": "0",
"free": "8,999,815,091,021",
"miscFrozen": "0",
"reserved": "0",
},
"nonce": "1",
"providers": "0",
"sufficients": "0",
}
`;

exports[`Polkadot <-> Acala > Polkadot transfer assets to Acala 4`] = `
{
"free": "999,998,266,092",
"frozen": "0",
"reserved": "0",
}
`;
81 changes: 81 additions & 0 deletions tests/polkadot-acala/transfer-reserve.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { afterAll, describe, expect, it } from "vitest";
import { connectVertical } from "@acala-network/chopsticks";

import { expectEvent, matchSnapshot, setupContext, testingPairs } from "../helper";

describe("Polkadot <-> Acala", async () => {
const polkadot = await setupContext({
endpoint: "wss://rpc.polkadot.io",
});
const acala = await setupContext({
endpoint: "wss://acala-rpc-1.aca-api.network",
});
await connectVertical(polkadot.chain, acala.chain);

const { alice } = testingPairs();

afterAll(async () => {
await polkadot.teardown();
await acala.teardown();
});

it("Polkadot transfer assets to Acala", async () => {
await polkadot.dev.setStorage({
System: {
Account: [[[alice.address], { data: { free: 1000 * 1e10 } }]],
},
});

await matchSnapshot(polkadot.api.query.system.account(alice.address));
await matchSnapshot(acala.api.query.tokens.accounts(alice.address, { token: "DOT" }));

await polkadot.api.tx.xcmPallet
.reserveTransferAssets(
{
V0: {
X1: {
Parachain: 2000,
},
},
},
{
V0: {
X1: {
AccountId32: {
network: "Any",
id: alice.addressRaw,
},
},
},
},
{
V0: [
{
ConcreteFungible: { id: "Null", amount: 100e10 },
},
],
},
0
)
.signAndSend(alice);

await polkadot.chain.newBlock();
await acala.chain.upcomingBlock();

await matchSnapshot(polkadot.api.query.system.account(alice.address));
await expectEvent(polkadot.api.query.system.events(), {
event: expect.objectContaining({
section: "xcmPallet",
method: "Attempted",
}),
});

await matchSnapshot(acala.api.query.tokens.accounts(alice.address, { token: "DOT" }));
await expectEvent(acala.api.query.system.events(), {
event: expect.objectContaining({
section: "parachainSystem",
method: "DownwardMessagesReceived",
}),
});
});
});
Loading

0 comments on commit f6367e0

Please sign in to comment.