Skip to content

Commit

Permalink
dev: save
Browse files Browse the repository at this point in the history
  • Loading branch information
lrazovic committed Jan 21, 2025
1 parent 8d39a01 commit a64e4e9
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 30 deletions.
Binary file modified integration-tests/chopsticks/bun.lockb
Binary file not shown.
4 changes: 0 additions & 4 deletions integration-tests/chopsticks/overrides/polkadot-hub.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
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 @@ -32,7 +31,4 @@ export const polkadot_hub_storage = {
],
],
},
ForeignAssets: {
Account: [[[weth_location, Accounts.ALICE], { balance: INITIAL_BALANCES.WETH }]],
},
} as const;
12 changes: 10 additions & 2 deletions integration-tests/chopsticks/src/managers/BaseManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
} 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 type { PolkadotClient, PolkadotSigner, TypedApi } from 'polkadot-api';
import { getPolkadotSigner } from 'polkadot-api/signer';
import { filter, firstValueFrom, take } from 'rxjs';

Expand Down Expand Up @@ -41,6 +41,12 @@ export abstract class BaseChainManager {
return client.api as TypedApi<ChainToDefinition[T]>;
}

getClient<T extends Chains>(chain: T): PolkadotClient {
const client = this.clients.get(chain);
if (!client) throw new Error(`Chain ${chain} not initialized`);
return client.client;
}

getSigner(account: Accounts) {
const signer = this.signers.get(account);
if (!signer) throw new Error(`Signer for ${account} not found`);
Expand All @@ -57,9 +63,11 @@ export abstract class BaseChainManager {
);
}

getBlockNumber() {
async getBlockNumber() {
const chain = this.getChainType();
const api = this.getApi(chain);
// Note: Not sure why this is needed, but without it we cannot retrieve the block number.
await api.compatibilityToken;
return api.query.System.Number.getValue();
}

Expand Down
197 changes: 174 additions & 23 deletions integration-tests/chopsticks/src/transfers/BridgeToPolimec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,31 @@ import type { BridgerHubManagaer } from '@/managers/BridgeHubManager';
import type { PolimecManager } from '@/managers/PolimecManager';
import type { PolkadotHubManager } from '@/managers/PolkadotHubManager';
import {
Accounts,
Asset,
AssetSourceRelation,
Chains,
ParaId,
type PolimecBalanceCheck,
getVersionedAssets,
} from '@/types';
import { createTransferData, unwrap } from '@/utils';
import { createTransferData, flatObject, unwrap } from '@/utils';
import {
DispatchRawOrigin,
XcmVersionedAssetId,
type XcmVersionedLocation,
type XcmVersionedXcm,
XcmV3Instruction,
XcmV3Junction,
XcmV3JunctionNetworkId,
XcmV3Junctions,
XcmV3MultiassetAssetId,
XcmV3MultiassetFungibility,
XcmV3MultiassetMultiAssetFilter,
XcmV3MultiassetWildMultiAsset,
XcmV3WeightLimit,
XcmVersionedLocation,
XcmVersionedXcm,
} from '@polkadot-api/descriptors';

import { FixedSizeBinary } from 'polkadot-api';
import { BaseTransferTest, type TransferOptions } from './BaseTransfer';

export class BridgeToPolimecTransfer extends BaseTransferTest {
Expand All @@ -32,33 +42,174 @@ export class BridgeToPolimecTransfer extends BaseTransferTest {

async executeTransfer({ account, assets }: TransferOptions) {
console.log('BridgeToPolimecTransfer executeTransfer');
const [sourceBlock, hopManagerBlock, destBlock] = await Promise.all([
this.sourceManager.getBlockNumber(),
this.hopManager.getBlockNumber(),
this.destManager.getBlockNumber(),
]);

const sourceBlock = await this.sourceManager.getBlockNumber();
console.log('sourceBlock', sourceBlock);
const hopManagerBlock = await this.hopManager.getBlockNumber();
console.log('hopManagerBlock', hopManagerBlock);
const destBlock = await this.destManager.getBlockNumber();
console.log('destBlock', destBlock);
return { sourceBlock, destBlock };

// const versioned_assets = getVersionedAssets(assets);
const sourceApi = this.sourceManager.getApi<Chains.BridgeHub>(Chains.BridgeHub);

const dest: XcmVersionedLocation = XcmVersionedLocation.V4({
parents: 1,
interior: XcmV3Junctions.X1(XcmV3Junction.Parachain(ParaId[Chains.PolkadotHub])),
});

const message: XcmVersionedXcm = XcmVersionedXcm.V3([
// 1. Receive Teleported Assets
XcmV3Instruction.ReceiveTeleportedAsset([
{
id: XcmV3MultiassetAssetId.Concrete({
parents: 1,
interior: XcmV3Junctions.Here(),
}),
fun: XcmV3MultiassetFungibility.Fungible(80000000000n),
},
]),

// 2. Buy Execution
XcmV3Instruction.BuyExecution({
fees: {
id: XcmV3MultiassetAssetId.Concrete({
parents: 1,
interior: XcmV3Junctions.Here(),
}),
fun: XcmV3MultiassetFungibility.Fungible(40000000000n),
},
weight_limit: XcmV3WeightLimit.Unlimited(),
}),

// 3. Descend Origin
XcmV3Instruction.DescendOrigin(XcmV3Junctions.X1(XcmV3Junction.PalletInstance(80))),

// 4. Universal Origin
XcmV3Instruction.UniversalOrigin(
XcmV3Junction.GlobalConsensus(XcmV3JunctionNetworkId.Ethereum({ chain_id: 1n })),
),

// const data = createTransferData({
// toChain: Chains.Polimec,
// assets: versioned_assets,
// recv: account,
// });
// 5. Reserve Asset Deposited
XcmV3Instruction.ReserveAssetDeposited([
{
id: XcmV3MultiassetAssetId.Concrete({
parents: 2,
interior: XcmV3Junctions.X2([
XcmV3Junction.GlobalConsensus(XcmV3JunctionNetworkId.Ethereum({ chain_id: 1n })),
XcmV3Junction.AccountKey20({
network: undefined,
key: FixedSizeBinary.fromHex('0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2'),
}),
]),
}),
fun: XcmV3MultiassetFungibility.Fungible(15000000000000n),
},
]),

// const api = this.sourceManager.getApi(Chains.PolkadotHub);
// const transfer = api.tx.PolkadotXcm.transfer_assets(data);
// const res = await transfer.signAndSubmit(this.sourceManager.getSigner(account));
// 6. Clear Origin
XcmV3Instruction.ClearOrigin(),

// console.log('Extrinsic result: ', res.ok);
// 7. Set Appendix
XcmV3Instruction.SetAppendix([
XcmV3Instruction.DepositAsset({
assets: XcmV3MultiassetMultiAssetFilter.Wild(XcmV3MultiassetWildMultiAsset.AllCounted(2)),
beneficiary: {
parents: 2,
interior: XcmV3Junctions.X1(
XcmV3Junction.GlobalConsensus(XcmV3JunctionNetworkId.Ethereum({ chain_id: 1n })),
),
},
}),
]),

// expect(res.ok).toBeTrue();
// return { sourceBlock, destBlock };
// 8. Deposit Reserve Asset
XcmV3Instruction.DepositReserveAsset({
assets: XcmV3MultiassetMultiAssetFilter.Definite([
{
id: XcmV3MultiassetAssetId.Concrete({
parents: 1,
interior: XcmV3Junctions.Here(),
}),
fun: XcmV3MultiassetFungibility.Fungible(40000000000n),
},
{
id: XcmV3MultiassetAssetId.Concrete({
parents: 2,
interior: XcmV3Junctions.X2([
XcmV3Junction.GlobalConsensus(XcmV3JunctionNetworkId.Ethereum({ chain_id: 1n })),
XcmV3Junction.AccountKey20({
network: undefined,
key: FixedSizeBinary.fromHex('0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2'),
}),
]),
}),
fun: XcmV3MultiassetFungibility.Fungible(15000000000000n),
},
]),
dest: {
parents: 1,
interior: XcmV3Junctions.X1(XcmV3Junction.Parachain(3344)),
},
xcm: [
XcmV3Instruction.BuyExecution({
fees: {
id: XcmV3MultiassetAssetId.Concrete({
parents: 1,
interior: XcmV3Junctions.Here(),
}),
fun: XcmV3MultiassetFungibility.Fungible(40000000000n),
},
weight_limit: XcmV3WeightLimit.Unlimited(),
}),
XcmV3Instruction.DepositAsset({
assets: XcmV3MultiassetMultiAssetFilter.Wild(
XcmV3MultiassetWildMultiAsset.AllCounted(2),
),
beneficiary: {
parents: 0,
interior: XcmV3Junctions.X1(
XcmV3Junction.AccountId32({
network: undefined,
id: FixedSizeBinary.fromAccountId32(Accounts.ALICE),
}),
),
},
}),
XcmV3Instruction.SetTopic(
FixedSizeBinary.fromArray([
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1,
]),
),
],
}),
// 9. Set Topic
XcmV3Instruction.SetTopic(
FixedSizeBinary.fromArray([
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1,
]),
),
]);

const xcm_res = sourceApi.tx.PolkadotXcm.send({ dest, message }).decodedCall;

const dry_run_res = await sourceApi.apis.DryRunApi.dry_run_call(
{ type: 'system', value: DispatchRawOrigin.Root() },
xcm_res,
);
console.log('dryRunCallOnBH SUCCESS?', dry_run_res.success);

const hopApi = this.hopManager.getApi<Chains.PolkadotHub>(Chains.PolkadotHub);

const origin = XcmVersionedLocation.V4({
parents: 1,
interior: XcmV3Junctions.X1(XcmV3Junction.Parachain(ParaId[Chains.BridgeHub])),
});

const dryRunonHop = await hopApi.apis.DryRunApi.dry_run_xcm(origin, message);
console.dir(flatObject(dryRunonHop.value), { depth: null, colors: true });

return { sourceBlock, destBlock };
}

async getBalances({
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/chopsticks/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export type ChainClient<T extends Chains> = {
export const ParaId = {
[Chains.Polimec]: 3344,
[Chains.PolkadotHub]: 1000,
[Chains.BridgeHub]: 1001,
[Chains.BridgeHub]: 1002,
};

export enum AssetSourceRelation {
Expand Down

0 comments on commit a64e4e9

Please sign in to comment.