Skip to content

Commit

Permalink
Chopsticks integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JuaniRios committed Dec 4, 2024
1 parent aec984b commit cd1666e
Show file tree
Hide file tree
Showing 33 changed files with 1,221 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
# These are backup files generated by rustfmt
**/*.rs.bk

**/node_modules/

# IDE related settings
.vscode/*
.fleet/*
Expand All @@ -18,4 +20,5 @@ genesis-wasm

*.log
fly.toml
bin/
bin/
integration-tests/chopsticks/db/*
3 changes: 3 additions & 0 deletions integration-tests/chopsticks/.papi/descriptors/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*
!.gitignore
!package.json
24 changes: 24 additions & 0 deletions integration-tests/chopsticks/.papi/descriptors/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"version": "0.1.0-autogenerated.575737038775881951",
"name": "@polkadot-api/descriptors",
"files": [
"dist"
],
"exports": {
".": {
"types": "./dist/index.d.ts",
"module": "./dist/index.mjs",
"import": "./dist/index.mjs",
"require": "./dist/index.js"
},
"./package.json": "./package.json"
},
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"browser": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"sideEffects": false,
"peerDependencies": {
"polkadot-api": "*"
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
18 changes: 18 additions & 0 deletions integration-tests/chopsticks/.papi/polkadot-api.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"version": 0,
"descriptorPath": ".papi/descriptors",
"entries": {
"polimec": {
"wsUrl": "ws://127.0.0.1:8000",
"metadata": ".papi/metadata/polimec.scale"
},
"polkadot": {
"wsUrl": "ws://localhost:8002",
"metadata": ".papi/metadata/polkadot.scale"
},
"pah": {
"wsUrl": "ws://localhost:8001",
"metadata": ".papi/metadata/pah.scale"
}
}
}
20 changes: 20 additions & 0 deletions integration-tests/chopsticks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# chopsticks

To install dependencies:

```bash
bun install
```

To start the chains:

```bash
bun run dev
```

To run the tests:

```bash
bun run test
```

37 changes: 37 additions & 0 deletions integration-tests/chopsticks/biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
"vcs": {
"enabled": false,
"clientKind": "git",
"useIgnoreFile": false
},
"files": {
"ignoreUnknown": false,
"ignore": ["node_modules"]
},
"formatter": {
"enabled": true,
"indentStyle": "space",
"lineWidth": 100
},
"organizeImports": {
"enabled": true
},
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
},
"javascript": {
"formatter": {
"quoteStyle": "single"
}
},
"json": {
"formatter": {
"enabled": true
}
}
}

Binary file added integration-tests/chopsticks/bun.lockb
Binary file not shown.
5 changes: 5 additions & 0 deletions integration-tests/chopsticks/overrides/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { POLIMEC_WASM, polimec_storage } from '@/polimec';
import { polkadot_storage } from '@/polkadot';
import { polkadot_hub_storage } from '@/polkadot-hub';

export { polkadot_storage, polkadot_hub_storage, POLIMEC_WASM, polimec_storage };
43 changes: 43 additions & 0 deletions integration-tests/chopsticks/overrides/polimec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { INITIAL_BALANCES } from '@/constants';
import { Accounts, Assets } from '@/types';

export const POLIMEC_WASM =
'../../target/release/wbuild/polimec-runtime/polimec_runtime.compact.compressed.wasm';

export const polimec_storage = {
System: {
Account: [
[
[Accounts.BOB],
{
providers: 1,
data: {
free: INITIAL_BALANCES.PLMC,
},
},
],
],
},
ForeignAssets: {
Account: [
[
[Assets.USDC, Accounts.BOB],
{
balance: INITIAL_BALANCES.USDC,
},
],
[
[Assets.USDT, Accounts.BOB],
{
balance: INITIAL_BALANCES.USDT,
},
],
[
[Assets.DOT, Accounts.BOB],
{
balance: INITIAL_BALANCES.DOT,
},
],
],
},
} as const;
40 changes: 40 additions & 0 deletions integration-tests/chopsticks/overrides/polkadot-hub.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { INITIAL_BALANCES } from '@/constants';
import { Accounts, Assets } from '@/types';

export const polkadot_hub_storage = {
System: {
Account: [
[
[Accounts.ALICE],
{
providers: 1,
data: {
free: INITIAL_BALANCES.DOT,
},
},
],
],
},
Assets: {
Account: [
[
[Assets.USDT, Accounts.ALICE],
{
balance: INITIAL_BALANCES.USDT,
},
],
[
[Assets.USDC, Accounts.ALICE],
{
balance: INITIAL_BALANCES.USDC,
},
],
[
[Assets.UNKNOWN, Accounts.ALICE],
{
balance: INITIAL_BALANCES.USDT,
},
],
],
},
} as const;
21 changes: 21 additions & 0 deletions integration-tests/chopsticks/overrides/polkadot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { INITIAL_BALANCES } from '@/constants';
import { Accounts } from '@/types';

export const polkadot_storage = {
System: {
Account: [
[
[Accounts.ALICE],
{
providers: 1,
data: {
free: INITIAL_BALANCES.DOT,
},
},
],
],
},
ParasDisputes: {
$removePrefix: ['disputes'],
},
} as const;
23 changes: 23 additions & 0 deletions integration-tests/chopsticks/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "chopsticks",
"module": "src/index.ts",
"type": "module",
"scripts": {
"lint": "biome check --write src overrides",
"test": "env LOG_LEVEL=error bun test --timeout 15000"
},
"devDependencies": {
"@acala-network/chopsticks": "1.0.1",
"@biomejs/biome": "1.9.4",
"@polkadot-labs/hdkd": "0.0.10",
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"dependencies": {
"@polkadot-api/descriptors": "file:.papi/descriptors",
"@polkadot/keyring": "13.2.3",
"polkadot-api": "^1.7.7"
}
}
18 changes: 18 additions & 0 deletions integration-tests/chopsticks/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Accounts } from '@/types';

export const INITIAL_BALANCES = {
USDT: 52000000000n,
USDC: 66600000000n,
DOT: 10000000000000000n,
PLMC: 10000000000000000n,
} as const;

export const TRANSFER_AMOUNTS = {
TOKENS: 2000000n,
NATIVE: 20000000000n,
} as const;

export const DERIVE_PATHS = {
[Accounts.ALICE]: '//Alice',
[Accounts.BOB]: '//Bob',
};
86 changes: 86 additions & 0 deletions integration-tests/chopsticks/src/managers/BaseManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { DERIVE_PATHS } from '@/constants';
import type { Accounts, ChainClient, ChainToDefinition, Chains } from '@/types';
import { sr25519CreateDerive } from '@polkadot-labs/hdkd';
import { DEV_PHRASE, entropyToMiniSecret, mnemonicToEntropy } from '@polkadot-labs/hdkd-helpers';
import type { PolkadotSigner, TypedApi } from 'polkadot-api';
import { getPolkadotSigner } from 'polkadot-api/signer';
import { filter, firstValueFrom, take } from 'rxjs';

export abstract class BaseChainManager {
protected clients: Map<Chains, ChainClient<Chains>> = new Map();
protected signers: Map<Accounts, PolkadotSigner> = new Map();

constructor() {
this.initializeSigners();
}

private initializeSigners() {
const entropy = mnemonicToEntropy(DEV_PHRASE);
const miniSecret = entropyToMiniSecret(entropy);
const derive = sr25519CreateDerive(miniSecret);

for (const [account, path] of Object.entries(DERIVE_PATHS)) {
const keyPair = derive(path);
this.signers.set(
account as Accounts,
getPolkadotSigner(keyPair.publicKey, 'Sr25519', keyPair.sign),
);
}
}

getApi<T extends Chains>(chain: T): TypedApi<ChainToDefinition[T]> {
const client = this.clients.get(chain);
if (!client) throw new Error(`Chain ${chain} not initialized`);
return client.api as TypedApi<ChainToDefinition[T]>;
}

getSigner(account: Accounts) {
const signer = this.signers.get(account);
if (!signer) throw new Error(`Signer for ${account} not found`);
return signer;
}

async waitForNextBlock(currentBlock: number) {
const api = this.getApi(this.getChainType());
return firstValueFrom(
api.query.System.Number.watchValue().pipe(
filter((newBlock) => newBlock > currentBlock),
take(1),
),
);
}

async getBlockNumber() {
const chain = this.getChainType();
const api = this.getApi(chain);
return api.query.System.Number.getValue();
}

async getMessageQueueEvents() {
const api = this.getApi(this.getChainType());
return api.event.MessageQueue.Processed.pull();
}

async getExtrinsicFee() {
const api = this.getApi(this.getChainType());
const events = await api.event.TransactionPayment.TransactionFeePaid.pull();
return events[0]?.payload.actual_fee || 0n;
}

async getNativeBalanceOf(account: Accounts) {
const api = this.getApi(this.getChainType());
const balance = await api.query.System.Account.getValue(account);
return balance.data.free;
}

// @ts-expect-error - TODO: Not sure which is the correct type for this
abstract getXcmPallet();

abstract getChainType(): Chains;

abstract getAssetBalanceOf(account: Accounts, asset: number): Promise<bigint>;

abstract connect(): void;

abstract disconnect(): void;
}
17 changes: 17 additions & 0 deletions integration-tests/chopsticks/src/managers/Factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Chains } from '@/types';
import { PolimecManager } from './PolimecManager';
import { PolkadotHubManager } from './PolkadotHubManager';
import { PolkadotManager } from './PolkadotManager';

const chainManagerMap = {
[Chains.PolkadotHub]: PolkadotHubManager,
[Chains.Polimec]: PolimecManager,
[Chains.Polkadot]: PolkadotManager,
} satisfies Record<Chains, new () => PolkadotHubManager | PolimecManager | PolkadotManager>;

export function createChainManager<T extends Chains>(
chain: T,
): InstanceType<(typeof chainManagerMap)[T]> {
const ManagerClass = chainManagerMap[chain];
return new ManagerClass() as InstanceType<(typeof chainManagerMap)[T]>;
}
Loading

0 comments on commit cd1666e

Please sign in to comment.