Skip to content

Commit f8610ce

Browse files
committed
ran prettier
1 parent 6c440be commit f8610ce

File tree

1 file changed

+95
-54
lines changed
  • .snippets/code/develop/interoperability/best-practices-for-teleporting-assets

1 file changed

+95
-54
lines changed

.snippets/code/develop/interoperability/best-practices-for-teleporting-assets/dry-run-example.ts

Lines changed: 95 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,33 @@ import {
1010
XcmV4Instruction,
1111
XcmVersionedAssets,
1212
XcmVersionedLocation,
13-
XcmVersionedXcm
14-
} from "@polkadot-api/descriptors";
15-
import { createClient, Enum, FixedSizeBinary, type Transaction } from "polkadot-api";
16-
import { getWsProvider } from "polkadot-api/ws-provider/web";
17-
import { withPolkadotSdkCompat } from "polkadot-api/polkadot-sdk-compat";
13+
XcmVersionedXcm,
14+
} from '@polkadot-api/descriptors';
15+
import {
16+
createClient,
17+
Enum,
18+
FixedSizeBinary,
19+
type Transaction,
20+
} from 'polkadot-api';
21+
import { getWsProvider } from 'polkadot-api/ws-provider/web';
22+
import { withPolkadotSdkCompat } from 'polkadot-api/polkadot-sdk-compat';
1823

1924
// Asset Hub constants.
20-
const ASSET_HUB_WS_URL = "ws://localhost:8000";
25+
const ASSET_HUB_WS_URL = 'ws://localhost:8000';
2126
const ASSET_HUB_PARA_ID = 1000;
22-
const ASSET_HUB_ACCOUNT = "15oF4uVJwmo4TdGW7VfQxNLavjCXviqxT9S1MgbjMNHr6Sp5";
27+
const ASSET_HUB_ACCOUNT = '15oF4uVJwmo4TdGW7VfQxNLavjCXviqxT9S1MgbjMNHr6Sp5';
2328
const DOT_UNITS = 10_000_000_000n; // 10 decimals.
2429
const DOT_CENTS = DOT_UNITS / 100n;
2530

2631
// Mythos constants.
27-
const MYTHOS_WS_URL = "ws://localhost:8001";
32+
const MYTHOS_WS_URL = 'ws://localhost:8001';
2833
const MYTHOS_PARA_ID = 3369;
29-
const MYTHOS_ACCOUNT = "0x69CF15A9393A0869A7fE535eCFe2C3CbaC127A40";
34+
const MYTHOS_ACCOUNT = '0x69CF15A9393A0869A7fE535eCFe2C3CbaC127A40';
3035
const MYTH_UNITS = 1_000_000_000_000_000_000n; // 18 decimals.
31-
36+
3237
// Connect to Mythos.
3338
const mythosClient = createClient(
34-
withPolkadotSdkCompat(
35-
getWsProvider(MYTHOS_WS_URL)
36-
)
39+
withPolkadotSdkCompat(getWsProvider(MYTHOS_WS_URL))
3740
);
3841

3942
// Get the typed API which lets us use descriptors.
@@ -47,15 +50,17 @@ const tx = mythApi.tx.PolkadotXcm.limited_teleport_assets({
4750
}),
4851
beneficiary: XcmVersionedLocation.V4({
4952
parents: 0,
50-
interior: XcmV3Junctions.X1(XcmV3Junction.AccountId32({
51-
id: FixedSizeBinary.fromAccountId32(ASSET_HUB_ACCOUNT),
52-
network: undefined,
53-
})),
53+
interior: XcmV3Junctions.X1(
54+
XcmV3Junction.AccountId32({
55+
id: FixedSizeBinary.fromAccountId32(ASSET_HUB_ACCOUNT),
56+
network: undefined,
57+
})
58+
),
5459
}),
5560
assets: XcmVersionedAssets.V4([
5661
{
5762
id: { parents: 0, interior: XcmV3Junctions.Here() },
58-
fun: XcmV3MultiassetFungibility.Fungible(1n * MYTH_UNITS)
63+
fun: XcmV3MultiassetFungibility.Fungible(1n * MYTH_UNITS),
5964
},
6065
]),
6166
fee_asset_item: 0,
@@ -67,28 +72,30 @@ await dryRun(tx.decodedCall);
6772
async function dryRun(call: Transaction<any, any, any, any>['decodedCall']) {
6873
// Dry run the teleport locally so you know you can for example withdraw
6974
// the necessary funds and pay for delivery fees.
70-
console.log('Dry running teleport on Mythos...')
75+
console.log('Dry running teleport on Mythos...');
7176
const localDryRunResult = await mythApi.apis.DryRunApi.dry_run_call(
7277
Enum('system', Enum('Signed', MYTHOS_ACCOUNT)),
73-
call,
78+
call
7479
);
7580

7681
// Only continue if the local dry run works.
7782
// The first condition is whether or not the runtime API was successful,
7883
// the second is whether or not the underlying dry-run was successful.
79-
if (localDryRunResult.success && localDryRunResult.value.execution_result.success) {
84+
if (
85+
localDryRunResult.success &&
86+
localDryRunResult.value.execution_result.success
87+
) {
8088
// You are interested in the message to Asset Hub that results from your call.
8189
// You filter for it here.
82-
const [_, messages] =
83-
localDryRunResult.value.forwarded_xcms.find(
84-
([location, _]) =>
85-
// You happen to know the latest version in Mythos is V5 because of the descriptors.
86-
location.type === 'V5' &&
87-
location.value.parents === 1 &&
88-
location.value.interior.type === 'X1' &&
89-
location.value.interior.value.type === 'Parachain' &&
90-
location.value.interior.value.value === ASSET_HUB_PARA_ID
91-
)!;
90+
const [_, messages] = localDryRunResult.value.forwarded_xcms.find(
91+
([location, _]) =>
92+
// You happen to know the latest version in Mythos is V5 because of the descriptors.
93+
location.type === 'V5' &&
94+
location.value.parents === 1 &&
95+
location.value.interior.type === 'X1' &&
96+
location.value.interior.value.type === 'Parachain' &&
97+
location.value.interior.value.value === ASSET_HUB_PARA_ID
98+
)!;
9299
// There could be multiple messages to Asset Hub, you know it's only one
93100
// so you take the first one.
94101
const messageToAh = messages[0];
@@ -101,16 +108,18 @@ async function dryRun(call: Transaction<any, any, any, any>['decodedCall']) {
101108
// You get the supported versions directly from the descriptors.
102109
if (messageToAh.type === 'V4') {
103110
console.log('Dry running on Asset Hub...');
104-
const remoteDryRunResult =
105-
await ahApi.apis.DryRunApi.dry_run_xcm(
106-
XcmVersionedLocation.V4({
107-
parents: 1,
108-
interior: XcmV3Junctions.X1(XcmV3Junction.Parachain(MYTHOS_PARA_ID))
109-
}),
110-
messageToAh
111-
);
111+
const remoteDryRunResult = await ahApi.apis.DryRunApi.dry_run_xcm(
112+
XcmVersionedLocation.V4({
113+
parents: 1,
114+
interior: XcmV3Junctions.X1(XcmV3Junction.Parachain(MYTHOS_PARA_ID)),
115+
}),
116+
messageToAh
117+
);
112118

113-
if (remoteDryRunResult.success && remoteDryRunResult.value.execution_result.type === 'Complete') {
119+
if (
120+
remoteDryRunResult.success &&
121+
remoteDryRunResult.value.execution_result.type === 'Complete'
122+
) {
114123
// Success! Let's go ahead with the teleport.
115124
console.log('Success!');
116125
} else {
@@ -127,39 +136,71 @@ async function dryRun(call: Transaction<any, any, any, any>['decodedCall']) {
127136
const xcm = XcmVersionedXcm.V4([
128137
// You withdraw some MYTH from your account on Mythos.
129138
XcmV4Instruction.WithdrawAsset([
130-
{ id: { parents: 0, interior: XcmV3Junctions.Here() }, fun: XcmV3MultiassetFungibility.Fungible(100n * MYTH_UNITS) },
139+
{
140+
id: { parents: 0, interior: XcmV3Junctions.Here() },
141+
fun: XcmV3MultiassetFungibility.Fungible(100n * MYTH_UNITS),
142+
},
131143
]),
132144
// Use them to pay fees.
133145
XcmV4Instruction.BuyExecution({
134-
fees: { id: { parents: 0, interior: XcmV3Junctions.Here() }, fun: XcmV3MultiassetFungibility.Fungible(100n * MYTH_UNITS) },
146+
fees: {
147+
id: { parents: 0, interior: XcmV3Junctions.Here() },
148+
fun: XcmV3MultiassetFungibility.Fungible(100n * MYTH_UNITS),
149+
},
135150
weight_limit: XcmV3WeightLimit.Unlimited(),
136151
}),
137152
// Teleport the MYTH to Asset Hub.
138153
XcmV4Instruction.InitiateTeleport({
139-
assets: XcmV4AssetAssetFilter.Wild(XcmV4AssetWildAsset.AllCounted(1)),
140-
dest: { parents: 1, interior: XcmV3Junctions.X1(XcmV3Junction.Parachain(ASSET_HUB_PARA_ID)) },
154+
assets: XcmV4AssetAssetFilter.Wild(
155+
XcmV4AssetWildAsset.AllCounted(1)
156+
),
157+
dest: {
158+
parents: 1,
159+
interior: XcmV3Junctions.X1(
160+
XcmV3Junction.Parachain(ASSET_HUB_PARA_ID)
161+
),
162+
},
141163
xcm: [
142164
// You pay fees with MYTH on Asset Hub.
143165
// This is possible because Asset Hub allows paying fees with any asset that has a liquidity pool.
144166
XcmV4Instruction.BuyExecution({
145-
fees: { id: { parents: 1, interior: XcmV3Junctions.X1(XcmV3Junction.Parachain(MYTHOS_PARA_ID)) }, fun: XcmV3MultiassetFungibility.Fungible(50n * MYTH_UNITS) },
167+
fees: {
168+
id: {
169+
parents: 1,
170+
interior: XcmV3Junctions.X1(
171+
XcmV3Junction.Parachain(MYTHOS_PARA_ID)
172+
),
173+
},
174+
fun: XcmV3MultiassetFungibility.Fungible(50n * MYTH_UNITS),
175+
},
146176
weight_limit: XcmV3WeightLimit.Unlimited(),
147177
}),
148178
// You explicitly swap your MYTH for 0.01 DOT to cover ED.
149179
XcmV4Instruction.ExchangeAsset({
150-
give: XcmV4AssetAssetFilter.Wild(XcmV4AssetWildAsset.AllCounted(1)),
151-
want: [{ id: { parents: 1, interior: XcmV3Junctions.Here() }, fun: XcmV3MultiassetFungibility.Fungible(1n * DOT_CENTS) }],
180+
give: XcmV4AssetAssetFilter.Wild(
181+
XcmV4AssetWildAsset.AllCounted(1)
182+
),
183+
want: [
184+
{
185+
id: { parents: 1, interior: XcmV3Junctions.Here() },
186+
fun: XcmV3MultiassetFungibility.Fungible(1n * DOT_CENTS),
187+
},
188+
],
152189
maximal: false,
153190
}),
154191
// You deposit all your MYTH and your 0.01 DOT into the beneficiary account.
155192
XcmV4Instruction.DepositAsset({
156-
assets: XcmV4AssetAssetFilter.Wild(XcmV4AssetWildAsset.AllCounted(2)),
193+
assets: XcmV4AssetAssetFilter.Wild(
194+
XcmV4AssetWildAsset.AllCounted(2)
195+
),
157196
beneficiary: {
158197
parents: 0,
159-
interior: XcmV3Junctions.X1(XcmV3Junction.AccountId32({
160-
id: FixedSizeBinary.fromAccountId32(ASSET_HUB_ACCOUNT),
161-
network: undefined,
162-
})),
198+
interior: XcmV3Junctions.X1(
199+
XcmV3Junction.AccountId32({
200+
id: FixedSizeBinary.fromAccountId32(ASSET_HUB_ACCOUNT),
201+
network: undefined,
202+
})
203+
),
163204
},
164205
}),
165206
],
@@ -168,12 +209,12 @@ async function dryRun(call: Transaction<any, any, any, any>['decodedCall']) {
168209

169210
const tx = mythApi.tx.PolkadotXcm.execute({
170211
message: xcm,
171-
max_weight: { ref_time: 4_000_000_000n, proof_size: 300_000n }
212+
max_weight: { ref_time: 4_000_000_000n, proof_size: 300_000n },
172213
});
173214

174215
// You try the dry run again.
175216
dryRun(tx.decodedCall);
176217
}
177218
}
178219
}
179-
}
220+
}

0 commit comments

Comments
 (0)