-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
33 changed files
with
1,221 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
* | ||
!.gitignore | ||
!package.json |
24 changes: 24 additions & 0 deletions
24
integration-tests/chopsticks/.papi/descriptors/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]>; | ||
} |
Oops, something went wrong.