Skip to content

Commit e2c1bf2

Browse files
Update deposit-reserve-asset-with-set-topic.ts
1 parent 14c7ab5 commit e2c1bf2

File tree

3 files changed

+104
-80
lines changed

3 files changed

+104
-80
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<div class="termynal" data-termynal>
22
<span data-ty="input">npx tsx limited-reserve-transfer-assets.ts</span>
33
<span data-ty>βœ… Local dry run successful.</span>
4-
<span data-ty>πŸ“¦ Finalised on Polkadot Asset Hub in block #9437981: 0xf3c33536a2687f20947fb25cf2b3d70c4917ff84e7a8f205f5a58a188bda7a3d</span>
4+
<span data-ty>πŸ“¦ Finalised on Polkadot Asset Hub in block #9471830: 0x98bd858739b3b5dd558def60cbd85d5e7fb2f4e33b0c00e1895e316541d727d9</span>
55
<span data-ty>πŸ“£ Last message Sent on Polkadot Asset Hub: 0xd60225f721599cb7c6e23cdf4fab26f205e30cd7eb6b5ccf6637cdc80b2339b2</span>
6-
<span data-ty>βœ… Sent message ID matched.</span>
7-
<span data-ty>πŸ“¦ Finalised on Hydration in block #8676869: 0x55e3e0eeec7396d00ad776d85ea60f935d6e90a99cfde97b25a550845e084f92</span>
6+
<span data-ty>βœ… Sent Message ID on Polkadot Asset Hub matched.</span>
7+
<span data-ty>πŸ“¦ Finalised on Hydration in block #8749233: 0xe1413c5126698d7189d6f55a38e62d07ea4915078c2b1f3914d70f670e79e162</span>
88
<span data-ty>πŸ“£ Last message Processed on Hydration: 0xd60225f721599cb7c6e23cdf4fab26f205e30cd7eb6b5ccf6637cdc80b2339b2</span>
9-
<span data-ty>βœ… Processed Message ID matched.</span>
9+
<span data-ty>βœ… Processed Message ID on Hydration matched.</span>
1010
</div>

β€Ž.snippets/code/tutorials/interoperability/xcm-observability/deposit-reserve-asset-with-set-topic.tsβ€Ž

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Binary, createClient, Enum} from "polkadot-api";
1+
import {Binary, BlockInfo, createClient, Enum, PolkadotClient, TypedApi} from "polkadot-api";
22
import {withPolkadotSdkCompat} from "polkadot-api/polkadot-sdk-compat";
33
import {getPolkadotSigner} from "polkadot-api/signer";
44
import {getWsProvider} from "polkadot-api/ws-provider/web";
@@ -36,6 +36,38 @@ const toHuman = (_key: any, value: any) => {
3636
return value;
3737
};
3838

39+
async function getProcessedMessageId(client: PolkadotClient, api: TypedApi<any>, name: String, blockBefore: BlockInfo): Promise<String> {
40+
let processedMessageId = undefined;
41+
const maxRetries = 8;
42+
for (let i = 0; i < maxRetries; i++) {
43+
const blockAfter = await client.getFinalizedBlock();
44+
if (blockAfter.number == blockBefore.number) {
45+
const waiting = 1_000 * (2 ** i);
46+
console.log(`⏳ Waiting ${waiting / 1_000}s for ${name} block to be finalised (${i + 1}/${maxRetries})...`);
47+
await new Promise((resolve) => setTimeout(resolve, waiting));
48+
continue;
49+
}
50+
51+
console.log(`πŸ“¦ Finalised on ${name} in block #${blockAfter.number}: ${blockAfter.hash}`);
52+
const processedEvents = await api.event.MessageQueue.Processed.pull();
53+
const processingFailedEvents = await api.event.MessageQueue.ProcessingFailed.pull();
54+
if (processedEvents.length > 0) {
55+
processedMessageId = processedEvents[0].payload.id.asHex();
56+
console.log(`πŸ“£ Last message Processed on ${name}: ${processedMessageId}`);
57+
break;
58+
} else if (processingFailedEvents.length > 0) {
59+
processedMessageId = processingFailedEvents[0].payload.id.asHex();
60+
console.log(`πŸ“£ Last message ProcessingFailed on ${name}: ${processedMessageId}`);
61+
break;
62+
} else {
63+
console.log(`πŸ“£ No Processed events on ${name} found.`);
64+
blockBefore = blockAfter; // Update the block before to the latest one
65+
}
66+
}
67+
68+
return processedMessageId;
69+
}
70+
3971
async function main() {
4072
const para1Name = "Polkadot Asset Hub";
4173
const para1Client = createClient(
@@ -76,7 +108,9 @@ async function main() {
76108
fun: XcmV3MultiassetFungibility.Fungible(1_000_000_000n),
77109
},
78110
]),
111+
79112
XcmV5Instruction.ClearOrigin(),
113+
80114
XcmV5Instruction.BuyExecution({
81115
fees: {
82116
id: {
@@ -87,6 +121,7 @@ async function main() {
87121
},
88122
weight_limit: XcmV3WeightLimit.Unlimited(),
89123
}),
124+
90125
XcmV5Instruction.DepositReserveAsset({
91126
assets: XcmV5AssetFilter.Wild(XcmV5WildAsset.All()),
92127
dest: {
@@ -104,12 +139,16 @@ async function main() {
104139
},
105140
weight_limit: XcmV3WeightLimit.Unlimited(),
106141
}),
142+
107143
XcmV5Instruction.DepositAsset({
108144
assets: XcmV5AssetFilter.Wild(XcmV5WildAsset.All()),
109145
beneficiary,
110146
}),
147+
148+
XcmV5Instruction.SetTopic(Binary.fromHex(expectedMessageId)),
111149
],
112150
}),
151+
113152
XcmV5Instruction.SetTopic(Binary.fromHex(expectedMessageId)),
114153
]);
115154

@@ -149,10 +188,10 @@ async function main() {
149188
if (polkadotXcmSentEvent === undefined) {
150189
console.log(`⚠️ PolkadotXcm.Sent is available in runtimes built from stable2503-5 or later.`);
151190
} else {
152-
let parachainBlockBefore = await para2Client.getFinalizedBlock();
191+
let para2BlockBefore = await para2Client.getFinalizedBlock();
153192
const extrinsic = await tx.signAndSubmit(aliceSigner);
154-
const block = extrinsic.block;
155-
console.log(`πŸ“¦ Finalised on ${para1Name} in block #${block.number}: ${block.hash}`);
193+
const para1BlockBefore = extrinsic.block;
194+
console.log(`πŸ“¦ Finalised on ${para1Name} in block #${para1BlockBefore.number}: ${para1BlockBefore.hash}`);
156195

157196
if (!extrinsic.ok) {
158197
const dispatchError = extrinsic.dispatchError;
@@ -169,43 +208,16 @@ async function main() {
169208
const sentMessageId = sentEvents[0].payload.message_id.asHex();
170209
console.log(`πŸ“£ Last message Sent on ${para1Name}: ${sentMessageId}`);
171210
if (sentMessageId === expectedMessageId) {
172-
console.log("βœ… Sent message ID matched.");
211+
console.log(`βœ… Sent Message ID on ${para1Name} matched.`);
173212
} else {
174-
console.error("❌ Sent message ID does not match expexted message ID.");
175-
}
176-
177-
let processedMessageId = undefined;
178-
const maxRetries = 8;
179-
for (let i = 0; i < maxRetries; i++) {
180-
const parachainBlockAfter = await para2Client.getFinalizedBlock();
181-
if (parachainBlockAfter.number == parachainBlockBefore.number) {
182-
const waiting = 1_000 * (2 ** i);
183-
console.log(`⏳ Waiting ${waiting}ms for ${para2Name} block to be finalised (${i + 1}/${maxRetries})...`);
184-
await new Promise((resolve) => setTimeout(resolve, waiting));
185-
continue;
186-
}
187-
188-
console.log(`πŸ“¦ Finalised on ${para2Name} in block #${parachainBlockAfter.number}: ${parachainBlockAfter.hash}`);
189-
const processedEvents = await para2Api.event.MessageQueue.Processed.pull();
190-
const processingFailedEvents = await para2Api.event.MessageQueue.ProcessingFailed.pull();
191-
if (processedEvents.length > 0) {
192-
processedMessageId = processedEvents[0].payload.id.asHex();
193-
console.log(`πŸ“£ Last message Processed on ${para2Name}: ${processedMessageId}`);
194-
break;
195-
} else if (processingFailedEvents.length > 0) {
196-
processedMessageId = processingFailedEvents[0].payload.id.asHex();
197-
console.log(`πŸ“£ Last message ProcessingFailed on ${para2Name}: ${processedMessageId}`);
198-
break;
199-
} else {
200-
console.log(`πŸ“£ No Processed events on ${para2Name} found.`);
201-
parachainBlockBefore = parachainBlockAfter; // Update the block before to the latest one
202-
}
213+
console.error(`❌ Sent Message ID [${sentMessageId}] on ${para1Name} doesn't match expexted Message ID [${expectedMessageId}].`);
203214
}
204215

216+
let processedMessageId = await getProcessedMessageId(para2Client, para2Api, para2Name, para2BlockBefore);
205217
if (processedMessageId === expectedMessageId) {
206-
console.log("βœ… Processed Message ID matched.");
218+
console.log(`βœ… Processed Message ID on ${para2Name} matched.`);
207219
} else {
208-
console.error("❌ Processed Message ID does not match expected Message ID.");
220+
console.error(`❌ Processed Message ID [${processedMessageId}] on ${para2Name} doesn't match expected Message ID [${expectedMessageId}].`);
209221
}
210222
} else {
211223
console.log(`πŸ“£ No Sent events on ${para1Name} found.`);

β€Žllms.txtβ€Ž

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -28209,7 +28209,7 @@ In complex XCM flows, such as transfers that span multiple parachains, you may w
2820928209
Create a new script, `deposit-reserve-asset-with-set-topic.ts`
2821028210

2821128211
```ts
28212-
import {Binary, createClient, Enum} from "polkadot-api";
28212+
import {Binary, BlockInfo, createClient, Enum, PolkadotClient, TypedApi} from "polkadot-api";
2821328213
import {withPolkadotSdkCompat} from "polkadot-api/polkadot-sdk-compat";
2821428214
import {getPolkadotSigner} from "polkadot-api/signer";
2821528215
import {getWsProvider} from "polkadot-api/ws-provider/web";
@@ -28247,6 +28247,38 @@ const toHuman = (_key: any, value: any) => {
2824728247
return value;
2824828248
};
2824928249

28250+
async function getProcessedMessageId(client: PolkadotClient, api: TypedApi<any>, name: String, blockBefore: BlockInfo): Promise<String> {
28251+
let processedMessageId = undefined;
28252+
const maxRetries = 8;
28253+
for (let i = 0; i < maxRetries; i++) {
28254+
const blockAfter = await client.getFinalizedBlock();
28255+
if (blockAfter.number == blockBefore.number) {
28256+
const waiting = 1_000 * (2 ** i);
28257+
console.log(`⏳ Waiting ${waiting / 1_000}s for ${name} block to be finalised (${i + 1}/${maxRetries})...`);
28258+
await new Promise((resolve) => setTimeout(resolve, waiting));
28259+
continue;
28260+
}
28261+
28262+
console.log(`πŸ“¦ Finalised on ${name} in block #${blockAfter.number}: ${blockAfter.hash}`);
28263+
const processedEvents = await api.event.MessageQueue.Processed.pull();
28264+
const processingFailedEvents = await api.event.MessageQueue.ProcessingFailed.pull();
28265+
if (processedEvents.length > 0) {
28266+
processedMessageId = processedEvents[0].payload.id.asHex();
28267+
console.log(`πŸ“£ Last message Processed on ${name}: ${processedMessageId}`);
28268+
break;
28269+
} else if (processingFailedEvents.length > 0) {
28270+
processedMessageId = processingFailedEvents[0].payload.id.asHex();
28271+
console.log(`πŸ“£ Last message ProcessingFailed on ${name}: ${processedMessageId}`);
28272+
break;
28273+
} else {
28274+
console.log(`πŸ“£ No Processed events on ${name} found.`);
28275+
blockBefore = blockAfter; // Update the block before to the latest one
28276+
}
28277+
}
28278+
28279+
return processedMessageId;
28280+
}
28281+
2825028282
async function main() {
2825128283
const para1Name = "Polkadot Asset Hub";
2825228284
const para1Client = createClient(
@@ -28287,7 +28319,9 @@ async function main() {
2828728319
fun: XcmV3MultiassetFungibility.Fungible(1_000_000_000n),
2828828320
},
2828928321
]),
28322+
2829028323
XcmV5Instruction.ClearOrigin(),
28324+
2829128325
XcmV5Instruction.BuyExecution({
2829228326
fees: {
2829328327
id: {
@@ -28298,6 +28332,7 @@ async function main() {
2829828332
},
2829928333
weight_limit: XcmV3WeightLimit.Unlimited(),
2830028334
}),
28335+
2830128336
XcmV5Instruction.DepositReserveAsset({
2830228337
assets: XcmV5AssetFilter.Wild(XcmV5WildAsset.All()),
2830328338
dest: {
@@ -28315,12 +28350,16 @@ async function main() {
2831528350
},
2831628351
weight_limit: XcmV3WeightLimit.Unlimited(),
2831728352
}),
28353+
2831828354
XcmV5Instruction.DepositAsset({
2831928355
assets: XcmV5AssetFilter.Wild(XcmV5WildAsset.All()),
2832028356
beneficiary,
2832128357
}),
28358+
28359+
XcmV5Instruction.SetTopic(Binary.fromHex(expectedMessageId)),
2832228360
],
2832328361
}),
28362+
2832428363
XcmV5Instruction.SetTopic(Binary.fromHex(expectedMessageId)),
2832528364
]);
2832628365

@@ -28360,10 +28399,10 @@ async function main() {
2836028399
if (polkadotXcmSentEvent === undefined) {
2836128400
console.log(`⚠️ PolkadotXcm.Sent is available in runtimes built from stable2503-5 or later.`);
2836228401
} else {
28363-
let parachainBlockBefore = await para2Client.getFinalizedBlock();
28402+
let para2BlockBefore = await para2Client.getFinalizedBlock();
2836428403
const extrinsic = await tx.signAndSubmit(aliceSigner);
28365-
const block = extrinsic.block;
28366-
console.log(`πŸ“¦ Finalised on ${para1Name} in block #${block.number}: ${block.hash}`);
28404+
const para1BlockBefore = extrinsic.block;
28405+
console.log(`πŸ“¦ Finalised on ${para1Name} in block #${para1BlockBefore.number}: ${para1BlockBefore.hash}`);
2836728406

2836828407
if (!extrinsic.ok) {
2836928408
const dispatchError = extrinsic.dispatchError;
@@ -28380,43 +28419,16 @@ async function main() {
2838028419
const sentMessageId = sentEvents[0].payload.message_id.asHex();
2838128420
console.log(`πŸ“£ Last message Sent on ${para1Name}: ${sentMessageId}`);
2838228421
if (sentMessageId === expectedMessageId) {
28383-
console.log("βœ… Sent message ID matched.");
28422+
console.log(`βœ… Sent Message ID on ${para1Name} matched.`);
2838428423
} else {
28385-
console.error("❌ Sent message ID does not match expexted message ID.");
28386-
}
28387-
28388-
let processedMessageId = undefined;
28389-
const maxRetries = 8;
28390-
for (let i = 0; i < maxRetries; i++) {
28391-
const parachainBlockAfter = await para2Client.getFinalizedBlock();
28392-
if (parachainBlockAfter.number == parachainBlockBefore.number) {
28393-
const waiting = 1_000 * (2 ** i);
28394-
console.log(`⏳ Waiting ${waiting}ms for ${para2Name} block to be finalised (${i + 1}/${maxRetries})...`);
28395-
await new Promise((resolve) => setTimeout(resolve, waiting));
28396-
continue;
28397-
}
28398-
28399-
console.log(`πŸ“¦ Finalised on ${para2Name} in block #${parachainBlockAfter.number}: ${parachainBlockAfter.hash}`);
28400-
const processedEvents = await para2Api.event.MessageQueue.Processed.pull();
28401-
const processingFailedEvents = await para2Api.event.MessageQueue.ProcessingFailed.pull();
28402-
if (processedEvents.length > 0) {
28403-
processedMessageId = processedEvents[0].payload.id.asHex();
28404-
console.log(`πŸ“£ Last message Processed on ${para2Name}: ${processedMessageId}`);
28405-
break;
28406-
} else if (processingFailedEvents.length > 0) {
28407-
processedMessageId = processingFailedEvents[0].payload.id.asHex();
28408-
console.log(`πŸ“£ Last message ProcessingFailed on ${para2Name}: ${processedMessageId}`);
28409-
break;
28410-
} else {
28411-
console.log(`πŸ“£ No Processed events on ${para2Name} found.`);
28412-
parachainBlockBefore = parachainBlockAfter; // Update the block before to the latest one
28413-
}
28424+
console.error(`❌ Sent Message ID [${sentMessageId}] on ${para1Name} doesn't match expexted Message ID [${expectedMessageId}].`);
2841428425
}
2841528426

28427+
let processedMessageId = await getProcessedMessageId(para2Client, para2Api, para2Name, para2BlockBefore);
2841628428
if (processedMessageId === expectedMessageId) {
28417-
console.log("βœ… Processed Message ID matched.");
28429+
console.log(`βœ… Processed Message ID on ${para2Name} matched.`);
2841828430
} else {
28419-
console.error("❌ Processed Message ID does not match expected Message ID.");
28431+
console.error(`❌ Processed Message ID [${processedMessageId}] on ${para2Name} doesn't match expected Message ID [${expectedMessageId}].`);
2842028432
}
2842128433
} else {
2842228434
console.log(`πŸ“£ No Sent events on ${para1Name} found.`);
@@ -28449,12 +28461,12 @@ code/tutorials/interoperability/xcm-observability/forwarded-xcm-custom.html
2844928461
<div class="termynal" data-termynal>
2845028462
<span data-ty="input">npx tsx limited-reserve-transfer-assets.ts</span>
2845128463
<span data-ty>βœ… Local dry run successful.</span>
28452-
<span data-ty>πŸ“¦ Finalised on Polkadot Asset Hub in block #9437981: 0xf3c33536a2687f20947fb25cf2b3d70c4917ff84e7a8f205f5a58a188bda7a3d</span>
28464+
<span data-ty>πŸ“¦ Finalised on Polkadot Asset Hub in block #9471830: 0x98bd858739b3b5dd558def60cbd85d5e7fb2f4e33b0c00e1895e316541d727d9</span>
2845328465
<span data-ty>πŸ“£ Last message Sent on Polkadot Asset Hub: 0xd60225f721599cb7c6e23cdf4fab26f205e30cd7eb6b5ccf6637cdc80b2339b2</span>
28454-
<span data-ty>βœ… Sent message ID matched.</span>
28455-
<span data-ty>πŸ“¦ Finalised on Hydration in block #8676869: 0x55e3e0eeec7396d00ad776d85ea60f935d6e90a99cfde97b25a550845e084f92</span>
28466+
<span data-ty>βœ… Sent Message ID on Polkadot Asset Hub matched.</span>
28467+
<span data-ty>πŸ“¦ Finalised on Hydration in block #8749233: 0xe1413c5126698d7189d6f55a38e62d07ea4915078c2b1f3914d70f670e79e162</span>
2845628468
<span data-ty>πŸ“£ Last message Processed on Hydration: 0xd60225f721599cb7c6e23cdf4fab26f205e30cd7eb6b5ccf6637cdc80b2339b2</span>
28457-
<span data-ty>βœ… Processed Message ID matched.</span>
28469+
<span data-ty>βœ… Processed Message ID on Hydration matched.</span>
2845828470
</div>
2845928471

2846028472
## Workaround for Older Runtimes

0 commit comments

Comments
Β (0)