Skip to content

Commit a37d455

Browse files
committed
refactor: cleanup bridge code
1 parent a64e4e9 commit a37d455

23 files changed

+1462
-276
lines changed

integration-tests/chopsticks/.papi/descriptors/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "0.1.0-autogenerated.697579702651170142",
2+
"version": "0.1.0-autogenerated.14907376514540113357",
33
"name": "@polkadot-api/descriptors",
44
"files": [
55
"dist"
Binary file not shown.

integration-tests/chopsticks/.papi/polkadot-api.json

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"descriptorPath": ".papi/descriptors",
44
"entries": {
55
"polimec": {
6-
"wsUrl": "ws://127.0.0.1:8000",
76
"metadata": ".papi/metadata/polimec.scale"
87
},
98
"polkadot": {

integration-tests/chopsticks/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ bun install
1010
bun papi
1111
```
1212

13+
> [!NOTE]
14+
> Sometimes you need to regenerate the Polimec descriptors. To do that, run:
15+
>
16+
> ```bash
17+
> bun papi add polimec --wasm ../../target/release/wbuild/polimec-runtime/polimec_runtime.compact.compressed.wasm
18+
> ```
19+
1320
To start the chains:
1421
1522
```bash
@@ -22,3 +29,9 @@ To run the tests:
2229
bun run test
2330
```
2431

32+
33+
> [!IMPORTANT]
34+
> TODO: Add:
35+
> - [ ] Polimec SA on AH: Add WETH balance to it in the Chopstick ovveride
36+
> - [ ] Polimec to Asset Hub: WETH transfer
37+
> - [ ] Polimec to Ethereum: WETH transfer

integration-tests/chopsticks/bun.lock

+1,078
Large diffs are not rendered by default.
-195 KB
Binary file not shown.

integration-tests/chopsticks/overrides/polimec.ts

-6
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,6 @@ export const polimec_storage = {
7777
balance: INITIAL_BALANCES.DOT,
7878
},
7979
],
80-
[
81-
[weth_location, Accounts.BOB],
82-
{
83-
balance: INITIAL_BALANCES.WETH,
84-
},
85-
],
8680
],
8781
Asset: [
8882
[

integration-tests/chopsticks/overrides/polkadot-hub.ts

+10
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,14 @@ export const polkadot_hub_storage = {
3131
],
3232
],
3333
},
34+
ForeignAssets: {
35+
Account: [
36+
[
37+
[Asset.WETH, Accounts.POLIMEC],
38+
{
39+
balance: INITIAL_BALANCES.DOT,
40+
},
41+
],
42+
],
43+
},
3444
} as const;

integration-tests/chopsticks/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
"test": "env LOG_LEVEL=error bun test --timeout 25000"
88
},
99
"devDependencies": {
10-
"@acala-network/chopsticks": "1.0.1",
10+
"@acala-network/chopsticks": "1.0.2",
1111
"@biomejs/biome": "1.9.4",
12-
"@polkadot-labs/hdkd": "0.0.10",
12+
"@polkadot-labs/hdkd": "0.0.11",
1313
"@types/bun": "latest"
1414
},
1515
"peerDependencies": {
1616
"typescript": "^5.7.3"
1717
},
1818
"dependencies": {
1919
"@polkadot-api/descriptors": "file:.papi/descriptors",
20-
"polkadot-api": "^1.8.2"
20+
"polkadot-api": "^1.8.4"
2121
}
2222
}

integration-tests/chopsticks/src/constants.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Accounts } from '@/types';
2+
import { FixedSizeBinary } from 'polkadot-api';
23

34
export const INITIAL_BALANCES = {
45
USDT: 52000n * 10n ** 6n,
@@ -17,6 +18,9 @@ export const TRANSFER_AMOUNTS = {
1718
export const DERIVE_PATHS = {
1819
[Accounts.ALICE]: '//Alice',
1920
[Accounts.BOB]: '//Bob',
20-
};
21+
} as const;
2122

2223
export const WETH_ADDRESS = '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2';
24+
export const DEFAULT_TOPIC = FixedSizeBinary.fromArray(Array(32).fill(1));
25+
export const FEE_AMOUNT = 40_000_000_000n;
26+
export const WETH_AMOUNT = 15_000_000_000_000n;

integration-tests/chopsticks/src/managers/BridgeHubManager.ts

+14-22
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,36 @@
1-
import { type Accounts, Asset, AssetLocation, AssetSourceRelation, Chains } from '@/types';
2-
import { flatObject } from '@/utils.ts';
1+
import { type Accounts, Asset, AssetSourceRelation, Chains } from '@/types';
32
import { bridge } from '@polkadot-api/descriptors';
43
import { createClient } from 'polkadot-api';
54
import { withPolkadotSdkCompat } from 'polkadot-api/polkadot-sdk-compat';
65
import { getWsProvider } from 'polkadot-api/ws-provider/web';
76
import { BaseChainManager } from './BaseManager';
87

98
export class BridgerHubManagaer extends BaseChainManager {
9+
private chain = Chains.BridgeHub;
10+
1011
connect() {
11-
const client = createClient(withPolkadotSdkCompat(getWsProvider(this.getChainType())));
12-
const api = client.getTypedApi(bridge);
12+
const provider = withPolkadotSdkCompat(getWsProvider(this.chain));
13+
const client = createClient(provider);
1314

1415
// Verify connection
15-
if (!client || !api) {
16-
throw new Error(`Failed to connect to ${this.getChainType()}`);
16+
if (!client) {
17+
throw new Error(`Failed to connect to ${this.chain}`);
1718
}
1819

19-
this.clients.set(this.getChainType(), { client, api });
20+
const api = client.getTypedApi(bridge);
21+
this.clients.set(this.chain, { client, api });
2022
}
2123

2224
disconnect() {
23-
this.clients.get(Chains.BridgeHub)?.client.destroy();
25+
this.clients.get(this.chain)?.client.destroy();
2426
}
2527

2628
getChainType() {
27-
return Chains.BridgeHub;
29+
return this.chain;
2830
}
2931

3032
getXcmPallet() {
31-
const api = this.getApi(Chains.BridgeHub);
33+
const api = this.getApi(this.chain);
3234
return api.tx.PolkadotXcm;
3335
}
3436

@@ -50,19 +52,9 @@ export class BridgerHubManagaer extends BaseChainManager {
5052
}
5153
}
5254

55+
// Note: On BridgeHub, there should be no balance for any asset.
56+
// There is DOT, but we are not tracking it.
5357
async getAssetBalanceOf(account: Accounts, asset: Asset): Promise<bigint> {
54-
const api = this.getApi(Chains.BridgeHub);
55-
// const asset_source_relation = this.getAssetSourceRelation(asset);
56-
// const asset_location = AssetLocation(asset, asset_source_relation).value;
57-
// const account_balances_result = await api.apis.FungiblesApi.query_account_balances(account);
58-
// if (account_balances_result.success === true && account_balances_result.value.type === 'V4') {
59-
// const assets = account_balances_result.value.value;
60-
// for (const asset of assets) {
61-
// if (Bun.deepEquals(flatObject(asset.id), flatObject(asset_location))) {
62-
// return asset.fun.value as bigint;
63-
// }
64-
// }
65-
// }
6658
return 0n;
6759
}
6860

integration-tests/chopsticks/src/managers/PolimecManager.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,31 @@ import { getWsProvider } from 'polkadot-api/ws-provider/web';
77
import { BaseChainManager } from './BaseManager';
88

99
export class PolimecManager extends BaseChainManager {
10+
private chain = Chains.Polimec;
11+
1012
connect() {
11-
const client = createClient(withPolkadotSdkCompat(getWsProvider(this.getChainType())));
12-
const api = client.getTypedApi(polimec);
13+
const provider = withPolkadotSdkCompat(getWsProvider(this.chain));
14+
const client = createClient(provider);
1315

1416
// Verify connection
15-
if (!client || !api) {
16-
throw new Error(`Failed to connect to ${this.getChainType()}`);
17+
if (!client) {
18+
throw new Error(`Failed to connect to ${this.chain}`);
1719
}
1820

19-
this.clients.set(this.getChainType(), { client, api });
21+
const api = client.getTypedApi(polimec);
22+
this.clients.set(this.chain, { client, api });
2023
}
2124

2225
disconnect() {
23-
this.clients.get(Chains.Polimec)?.client.destroy();
26+
this.clients.get(this.chain)?.client.destroy();
2427
}
2528

2629
getChainType() {
27-
return Chains.Polimec;
30+
return this.chain;
2831
}
2932

3033
getXcmPallet() {
31-
const api = this.getApi(Chains.Polimec);
34+
const api = this.getApi(this.chain);
3235
return api.tx.PolkadotXcm;
3336
}
3437

integration-tests/chopsticks/src/managers/PolkadotHubManager.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,31 @@ import { getWsProvider } from 'polkadot-api/ws-provider/web';
77
import { BaseChainManager } from './BaseManager';
88

99
export class PolkadotHubManager extends BaseChainManager {
10+
private chain = Chains.PolkadotHub;
11+
1012
connect() {
11-
const client = createClient(withPolkadotSdkCompat(getWsProvider(this.getChainType())));
12-
const api = client.getTypedApi(pah);
13+
const provider = withPolkadotSdkCompat(getWsProvider(this.chain));
14+
const client = createClient(provider);
1315

1416
// Verify connection
15-
if (!client || !api) {
16-
throw new Error(`Failed to connect to ${this.getChainType()}`);
17+
if (!client) {
18+
throw new Error(`Failed to connect to ${this.chain}`);
1719
}
1820

19-
this.clients.set(this.getChainType(), { client, api });
21+
const api = client.getTypedApi(pah);
22+
this.clients.set(this.chain, { client, api });
2023
}
2124

2225
disconnect() {
23-
this.clients.get(Chains.PolkadotHub)?.client.destroy();
26+
this.clients.get(this.chain)?.client.destroy();
2427
}
2528

2629
getChainType() {
27-
return Chains.PolkadotHub;
30+
return this.chain;
2831
}
2932

3033
getXcmPallet() {
31-
const api = this.getApi(Chains.PolkadotHub);
34+
const api = this.getApi(this.chain);
3235
return api.tx.PolkadotXcm;
3336
}
3437

integration-tests/chopsticks/src/managers/PolkadotManager.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,31 @@ import { getWsProvider } from 'polkadot-api/ws-provider/web';
66
import { BaseChainManager } from './BaseManager';
77

88
export class PolkadotManager extends BaseChainManager {
9+
private chain = Chains.Polkadot as const;
10+
911
connect() {
10-
const client = createClient(withPolkadotSdkCompat(getWsProvider(this.getChainType())));
11-
const api = client.getTypedApi(polkadot);
12+
const provider = withPolkadotSdkCompat(getWsProvider(this.chain));
13+
const client = createClient(provider);
1214

1315
// Verify connection
14-
if (!client || !api) {
15-
throw new Error(`Failed to connect to ${this.getChainType()}`);
16+
if (!client) {
17+
throw new Error(`Failed to connect to ${this.chain}`);
1618
}
1719

18-
this.clients.set(this.getChainType(), { client, api });
20+
const api = client.getTypedApi(polkadot);
21+
this.clients.set(this.chain, { client, api });
1922
}
2023

2124
disconnect() {
22-
this.clients.get(Chains.Polkadot)?.client.destroy();
25+
this.clients.get(this.chain)?.client.destroy();
2326
}
2427

2528
getChainType() {
26-
return Chains.Polkadot;
29+
return this.chain;
2730
}
2831

2932
getXcmPallet() {
30-
const api = this.getApi(Chains.Polkadot);
33+
const api = this.getApi(this.chain);
3134
return api.tx.XcmPallet;
3235
}
3336

integration-tests/chopsticks/src/setup.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,11 @@ export class ChainSetup {
4444
this.relaychain = relaychainSetup.chain;
4545
this.bridgeHub = bridgeHubSetup.chain;
4646

47-
await Promise.all([
48-
connectVertical(this.relaychain, this.polimec),
49-
connectVertical(this.relaychain, this.assetHub),
50-
connectVertical(this.relaychain, this.bridgeHub),
51-
connectParachains([this.polimec, this.assetHub]),
52-
connectParachains([this.bridgeHub, this.assetHub]),
53-
]);
47+
const parachains = [this.polimec, this.assetHub, this.bridgeHub];
48+
for (const parachain of parachains) {
49+
await connectVertical(this.relaychain, parachain);
50+
}
51+
await connectParachains(parachains);
5452

5553
console.log('✅ HRMP channels created');
5654

integration-tests/chopsticks/src/tests/bridge.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { afterAll, beforeAll, beforeEach, describe, test } from 'bun:test';
22
import { TRANSFER_AMOUNTS } from '@/constants';
33
import { createChainManager } from '@/managers/Factory';
4+
import { polimec_storage } from '@/polimec';
45
import { ChainSetup } from '@/setup';
56
import { BridgeToPolimecTransfer } from '@/transfers/BridgeToPolimec';
6-
import { HubToPolimecTransfer } from '@/transfers/HubToPolimec';
77
import { Accounts, Asset, AssetSourceRelation, Chains } from '@/types';
88

99
describe('Bridge Hub -> Polimec Transfer Tests', () => {
@@ -13,7 +13,7 @@ describe('Bridge Hub -> Polimec Transfer Tests', () => {
1313
const transferTest = new BridgeToPolimecTransfer(sourceManager, hopManager, destManager);
1414
const chainSetup = new ChainSetup();
1515

16-
beforeAll(async () => await chainSetup.initialize());
16+
beforeAll(async () => await chainSetup.initialize(polimec_storage));
1717
beforeEach(() => {
1818
sourceManager.connect();
1919
hopManager.connect();

integration-tests/chopsticks/src/tests/hub.test.ts

-10
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,4 @@ describe('Polkadot Hub -> Polimec Transfer Tests', () => {
4747
}),
4848
{ timeout: 25000 },
4949
);
50-
51-
// test(
52-
// 'Send WETH to Polimec',
53-
// () =>
54-
// transferTest.testTransfer({
55-
// account: Accounts.ALICE,
56-
// assets: [[Asset.WETH, TRANSFER_AMOUNTS.BRIDGED, AssetSourceRelation.Self]],
57-
// }),
58-
// { timeout: 25000 },
59-
// );
6050
});

integration-tests/chopsticks/src/transfers/BaseTransfer.ts

+28-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import { expect } from 'bun:test';
22
import type { BaseChainManager } from '@/managers/BaseManager';
3-
import type { Accounts, Asset, AssetSourceRelation, BalanceCheck, TransferResult } from '@/types';
3+
import {
4+
type Accounts,
5+
type Asset,
6+
type AssetSourceRelation,
7+
type BalanceCheck,
8+
Chains,
9+
type TransferResult,
10+
} from '@/types';
411
import { sleep } from 'bun';
512

613
export interface TransferOptions {
@@ -26,15 +33,26 @@ export abstract class BaseTransferTest {
2633
): void;
2734

2835
async testTransfer(options: TransferOptions) {
29-
// const { asset_balances: initialBalances } = await this.getBalances(options);
30-
// if (options.assets[0][1] > initialBalances[0].source) {
31-
// throw new Error(`Insufficient balance on Source chain for asset: ${options.assets[0][0]}`);
32-
// }
33-
const blockNumbers = await this.executeTransfer(options);
34-
// await this.waitForBlocks();
35-
// await this.verifyExecution();
36-
// const { asset_balances: finalBalances } = await this.getBalances(options);
37-
// this.verifyFinalBalances(initialBalances, finalBalances, options);
36+
// Note: For the bridged tests we use the dry-run Runtime API, so we don't write any data to the chain.
37+
const isBridged = this.sourceManager.getChainType() === Chains.BridgeHub;
38+
39+
let initialBalances: BalanceCheck[] = [];
40+
if (!isBridged) {
41+
const { asset_balances } = await this.getBalances(options);
42+
initialBalances = asset_balances; // Assign within the block
43+
if (options.assets[0][1] > asset_balances[0].source) {
44+
throw new Error(`Insufficient balance on Source chain for asset: ${options.assets[0][0]}`);
45+
}
46+
}
47+
48+
await this.executeTransfer(options);
49+
50+
if (!isBridged) {
51+
await this.waitForBlocks();
52+
await this.verifyExecution();
53+
const { asset_balances: finalBalances } = await this.getBalances(options);
54+
this.verifyFinalBalances(initialBalances, finalBalances, options);
55+
}
3856
}
3957

4058
// TODO: Wait for the next block to be produced.

0 commit comments

Comments
 (0)