Skip to content

Commit e22194d

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

23 files changed

+1471
-277
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. This is a "normal" transfer_asset call.
37+
> - [ ] Polimec to Ethereum: WETH transfer. This is a bit more complex, example extrinsic: https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Fhydration.ibp.network#/extrinsics/decode/0x6b0d04010100a10f040801000007464a69c7e002020907040300c02aaa39b223fe8d0a0e5c4f27ead9083c756cc200130000e8890423c78a0204010002040816040d01000001010088ca48e3e1d0f1c50bd6b504e1312d21f5bd45ed147e3c30c77eb5e4d63bdc6310010102020907040300c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000201090704081300010300c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20004000d010204000103001501c1413e4178c38567ada8945a80351f7b849600

integration-tests/chopsticks/bun.lock

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

integration-tests/chopsticks/overrides/polimec.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,8 @@ 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
],
81+
// Note: We can remove Asset and Metadata from the storage override as soon we set them on-chain.
8782
Asset: [
8883
[
8984
[weth_location],

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

+11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { INITIAL_BALANCES } from '@/constants';
22
import { Accounts, Asset } from '@/types';
3+
import { weth_location } from './polimec';
34

45
export const polkadot_hub_storage = {
56
System: {
@@ -31,4 +32,14 @@ export const polkadot_hub_storage = {
3132
],
3233
],
3334
},
35+
ForeignAssets: {
36+
Account: [
37+
[
38+
[weth_location, Accounts.POLIMEC],
39+
{
40+
balance: INITIAL_BALANCES.WETH,
41+
},
42+
],
43+
],
44+
},
3445
} as const;

integration-tests/chopsticks/package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44
"type": "module",
55
"scripts": {
66
"lint": "biome check --write src overrides",
7-
"test": "env LOG_LEVEL=error bun test --timeout 25000"
7+
"test": "env LOG_LEVEL=error bun test"
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.9.0"
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;
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
});

0 commit comments

Comments
 (0)