Skip to content

Commit

Permalink
refactor: cleanup bridge code
Browse files Browse the repository at this point in the history
  • Loading branch information
lrazovic committed Feb 11, 2025
1 parent a64e4e9 commit e22194d
Show file tree
Hide file tree
Showing 23 changed files with 1,471 additions and 277 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.1.0-autogenerated.697579702651170142",
"version": "0.1.0-autogenerated.14907376514540113357",
"name": "@polkadot-api/descriptors",
"files": [
"dist"
Expand Down
Binary file modified integration-tests/chopsticks/.papi/metadata/polimec.scale
Binary file not shown.
1 change: 0 additions & 1 deletion integration-tests/chopsticks/.papi/polkadot-api.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"descriptorPath": ".papi/descriptors",
"entries": {
"polimec": {
"wsUrl": "ws://127.0.0.1:8000",
"metadata": ".papi/metadata/polimec.scale"
},
"polkadot": {
Expand Down
13 changes: 13 additions & 0 deletions integration-tests/chopsticks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ bun install
bun papi
```

> [!NOTE]
> Sometimes you need to regenerate the Polimec descriptors. To do that, run:
>
> ```bash
> bun papi add polimec --wasm ../../target/release/wbuild/polimec-runtime/polimec_runtime.compact.compressed.wasm
> ```
To start the chains:
```bash
Expand All @@ -22,3 +29,9 @@ To run the tests:
bun run test
```


> [!IMPORTANT]
> TODO: Add:
> - [ ] Polimec SA on AH: Add WETH balance to it in the Chopstick ovveride
> - [ ] Polimec to Asset Hub: WETH transfer. This is a "normal" transfer_asset call.
> - [ ] 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
1,084 changes: 1,084 additions & 0 deletions integration-tests/chopsticks/bun.lock

Large diffs are not rendered by default.

Binary file removed integration-tests/chopsticks/bun.lockb
Binary file not shown.
7 changes: 1 addition & 6 deletions integration-tests/chopsticks/overrides/polimec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,8 @@ export const polimec_storage = {
balance: INITIAL_BALANCES.DOT,
},
],
[
[weth_location, Accounts.BOB],
{
balance: INITIAL_BALANCES.WETH,
},
],
],
// Note: We can remove Asset and Metadata from the storage override as soon we set them on-chain.
Asset: [
[
[weth_location],
Expand Down
11 changes: 11 additions & 0 deletions integration-tests/chopsticks/overrides/polkadot-hub.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { INITIAL_BALANCES } from '@/constants';
import { Accounts, Asset } from '@/types';
import { weth_location } from './polimec';

export const polkadot_hub_storage = {
System: {
Expand Down Expand Up @@ -31,4 +32,14 @@ export const polkadot_hub_storage = {
],
],
},
ForeignAssets: {
Account: [
[
[weth_location, Accounts.POLIMEC],
{
balance: INITIAL_BALANCES.WETH,
},
],
],
},
} as const;
8 changes: 4 additions & 4 deletions integration-tests/chopsticks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
"type": "module",
"scripts": {
"lint": "biome check --write src overrides",
"test": "env LOG_LEVEL=error bun test --timeout 25000"
"test": "env LOG_LEVEL=error bun test"
},
"devDependencies": {
"@acala-network/chopsticks": "1.0.1",
"@acala-network/chopsticks": "1.0.2",
"@biomejs/biome": "1.9.4",
"@polkadot-labs/hdkd": "0.0.10",
"@polkadot-labs/hdkd": "0.0.11",
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5.7.3"
},
"dependencies": {
"@polkadot-api/descriptors": "file:.papi/descriptors",
"polkadot-api": "^1.8.2"
"polkadot-api": "^1.9.0"
}
}
6 changes: 5 additions & 1 deletion integration-tests/chopsticks/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Accounts } from '@/types';
import { FixedSizeBinary } from 'polkadot-api';

export const INITIAL_BALANCES = {
USDT: 52000n * 10n ** 6n,
Expand All @@ -17,6 +18,9 @@ export const TRANSFER_AMOUNTS = {
export const DERIVE_PATHS = {
[Accounts.ALICE]: '//Alice',
[Accounts.BOB]: '//Bob',
};
} as const;

export const WETH_ADDRESS = '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2';
export const DEFAULT_TOPIC = FixedSizeBinary.fromArray(Array(32).fill(1));
export const FEE_AMOUNT = 40_000_000_000n;
export const WETH_AMOUNT = 15_000_000_000_000n;
36 changes: 14 additions & 22 deletions integration-tests/chopsticks/src/managers/BridgeHubManager.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,36 @@
import { type Accounts, Asset, AssetLocation, AssetSourceRelation, Chains } from '@/types';
import { flatObject } from '@/utils.ts';
import { type Accounts, Asset, AssetSourceRelation, Chains } from '@/types';
import { bridge } from '@polkadot-api/descriptors';
import { createClient } from 'polkadot-api';
import { withPolkadotSdkCompat } from 'polkadot-api/polkadot-sdk-compat';
import { getWsProvider } from 'polkadot-api/ws-provider/web';
import { BaseChainManager } from './BaseManager';

export class BridgerHubManagaer extends BaseChainManager {
private chain = Chains.BridgeHub;

connect() {
const client = createClient(withPolkadotSdkCompat(getWsProvider(this.getChainType())));
const api = client.getTypedApi(bridge);
const provider = withPolkadotSdkCompat(getWsProvider(this.chain));
const client = createClient(provider);

// Verify connection
if (!client || !api) {
throw new Error(`Failed to connect to ${this.getChainType()}`);
if (!client) {
throw new Error(`Failed to connect to ${this.chain}`);
}

this.clients.set(this.getChainType(), { client, api });
const api = client.getTypedApi(bridge);
this.clients.set(this.chain, { client, api });
}

disconnect() {
this.clients.get(Chains.BridgeHub)?.client.destroy();
this.clients.get(this.chain)?.client.destroy();
}

getChainType() {
return Chains.BridgeHub;
return this.chain;
}

getXcmPallet() {
const api = this.getApi(Chains.BridgeHub);
const api = this.getApi(this.chain);
return api.tx.PolkadotXcm;
}

Expand All @@ -50,19 +52,9 @@ export class BridgerHubManagaer extends BaseChainManager {
}
}

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

Expand Down
19 changes: 11 additions & 8 deletions integration-tests/chopsticks/src/managers/PolimecManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,31 @@ import { getWsProvider } from 'polkadot-api/ws-provider/web';
import { BaseChainManager } from './BaseManager';

export class PolimecManager extends BaseChainManager {
private chain = Chains.Polimec;

connect() {
const client = createClient(withPolkadotSdkCompat(getWsProvider(this.getChainType())));
const api = client.getTypedApi(polimec);
const provider = withPolkadotSdkCompat(getWsProvider(this.chain));
const client = createClient(provider);

// Verify connection
if (!client || !api) {
throw new Error(`Failed to connect to ${this.getChainType()}`);
if (!client) {
throw new Error(`Failed to connect to ${this.chain}`);
}

this.clients.set(this.getChainType(), { client, api });
const api = client.getTypedApi(polimec);
this.clients.set(this.chain, { client, api });
}

disconnect() {
this.clients.get(Chains.Polimec)?.client.destroy();
this.clients.get(this.chain)?.client.destroy();
}

getChainType() {
return Chains.Polimec;
return this.chain;
}

getXcmPallet() {
const api = this.getApi(Chains.Polimec);
const api = this.getApi(this.chain);
return api.tx.PolkadotXcm;
}

Expand Down
19 changes: 11 additions & 8 deletions integration-tests/chopsticks/src/managers/PolkadotHubManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,31 @@ import { getWsProvider } from 'polkadot-api/ws-provider/web';
import { BaseChainManager } from './BaseManager';

export class PolkadotHubManager extends BaseChainManager {
private chain = Chains.PolkadotHub;

connect() {
const client = createClient(withPolkadotSdkCompat(getWsProvider(this.getChainType())));
const api = client.getTypedApi(pah);
const provider = withPolkadotSdkCompat(getWsProvider(this.chain));
const client = createClient(provider);

// Verify connection
if (!client || !api) {
throw new Error(`Failed to connect to ${this.getChainType()}`);
if (!client) {
throw new Error(`Failed to connect to ${this.chain}`);
}

this.clients.set(this.getChainType(), { client, api });
const api = client.getTypedApi(pah);
this.clients.set(this.chain, { client, api });
}

disconnect() {
this.clients.get(Chains.PolkadotHub)?.client.destroy();
this.clients.get(this.chain)?.client.destroy();
}

getChainType() {
return Chains.PolkadotHub;
return this.chain;
}

getXcmPallet() {
const api = this.getApi(Chains.PolkadotHub);
const api = this.getApi(this.chain);
return api.tx.PolkadotXcm;
}

Expand Down
19 changes: 11 additions & 8 deletions integration-tests/chopsticks/src/managers/PolkadotManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,31 @@ import { getWsProvider } from 'polkadot-api/ws-provider/web';
import { BaseChainManager } from './BaseManager';

export class PolkadotManager extends BaseChainManager {
private chain = Chains.Polkadot;

connect() {
const client = createClient(withPolkadotSdkCompat(getWsProvider(this.getChainType())));
const api = client.getTypedApi(polkadot);
const provider = withPolkadotSdkCompat(getWsProvider(this.chain));
const client = createClient(provider);

// Verify connection
if (!client || !api) {
throw new Error(`Failed to connect to ${this.getChainType()}`);
if (!client) {
throw new Error(`Failed to connect to ${this.chain}`);
}

this.clients.set(this.getChainType(), { client, api });
const api = client.getTypedApi(polkadot);
this.clients.set(this.chain, { client, api });
}

disconnect() {
this.clients.get(Chains.Polkadot)?.client.destroy();
this.clients.get(this.chain)?.client.destroy();
}

getChainType() {
return Chains.Polkadot;
return this.chain;
}

getXcmPallet() {
const api = this.getApi(Chains.Polkadot);
const api = this.getApi(this.chain);
return api.tx.XcmPallet;
}

Expand Down
12 changes: 5 additions & 7 deletions integration-tests/chopsticks/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,11 @@ export class ChainSetup {
this.relaychain = relaychainSetup.chain;
this.bridgeHub = bridgeHubSetup.chain;

await Promise.all([
connectVertical(this.relaychain, this.polimec),
connectVertical(this.relaychain, this.assetHub),
connectVertical(this.relaychain, this.bridgeHub),
connectParachains([this.polimec, this.assetHub]),
connectParachains([this.bridgeHub, this.assetHub]),
]);
const parachains = [this.polimec, this.assetHub, this.bridgeHub];
for (const parachain of parachains) {
await connectVertical(this.relaychain, parachain);
}
await connectParachains(parachains);

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

Expand Down
4 changes: 2 additions & 2 deletions integration-tests/chopsticks/src/tests/bridge.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { afterAll, beforeAll, beforeEach, describe, test } from 'bun:test';
import { TRANSFER_AMOUNTS } from '@/constants';
import { createChainManager } from '@/managers/Factory';
import { polimec_storage } from '@/polimec';
import { ChainSetup } from '@/setup';
import { BridgeToPolimecTransfer } from '@/transfers/BridgeToPolimec';
import { HubToPolimecTransfer } from '@/transfers/HubToPolimec';
import { Accounts, Asset, AssetSourceRelation, Chains } from '@/types';

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

beforeAll(async () => await chainSetup.initialize());
beforeAll(async () => await chainSetup.initialize(polimec_storage));
beforeEach(() => {
sourceManager.connect();
hopManager.connect();
Expand Down
10 changes: 0 additions & 10 deletions integration-tests/chopsticks/src/tests/hub.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,4 @@ describe('Polkadot Hub -> Polimec Transfer Tests', () => {
}),
{ timeout: 25000 },
);

// test(
// 'Send WETH to Polimec',
// () =>
// transferTest.testTransfer({
// account: Accounts.ALICE,
// assets: [[Asset.WETH, TRANSFER_AMOUNTS.BRIDGED, AssetSourceRelation.Self]],
// }),
// { timeout: 25000 },
// );
});
Loading

0 comments on commit e22194d

Please sign in to comment.