From f0122b96cb97c54287355d43df31a8c1692864f7 Mon Sep 17 00:00:00 2001 From: Harry Solovay Date: Tue, 28 Mar 2023 14:48:23 -0400 Subject: [PATCH 1/2] clean up asset teleportation example --- examples/xcm/asset_teleportation.ts | 70 +++++++++++++++++++++++ examples/xcm_asset_teleportation.ts | 86 ----------------------------- 2 files changed, 70 insertions(+), 86 deletions(-) create mode 100644 examples/xcm/asset_teleportation.ts delete mode 100644 examples/xcm_asset_teleportation.ts diff --git a/examples/xcm/asset_teleportation.ts b/examples/xcm/asset_teleportation.ts new file mode 100644 index 000000000..67bf6ccbe --- /dev/null +++ b/examples/xcm/asset_teleportation.ts @@ -0,0 +1,70 @@ +import { alice, Rune } from "capi" +import { signature } from "capi/patterns/signature/polkadot.ts" +import { types, XcmPallet } from "zombienet/statemine.toml/alice/@latest/mod.js" +import { chain as parachain, System } from "zombienet/statemine.toml/collator/@latest/mod.js" +import { Event as ParachainEvent } from "zombienet/statemine.toml/collator/@latest/types/cumulus_pallet_parachain_system/pallet.js" +import { RuntimeEvent as ParachainRuntimeEvent } from "zombienet/statemine.toml/collator/@latest/types/statemine_runtime.js" + +const { + VersionedMultiAssets, + VersionedMultiLocation, + v2: { + NetworkId, + WeightLimit, + junction: { Junction }, + multilocation: { Junctions }, + multiasset: { AssetId, Fungibility }, + }, +} = types.xcm + +const aliceBalance = System.Account + .value(alice.publicKey) + .unhandle(undefined) + .access("data", "free") + +console.log("Alice balance before:", await aliceBalance.run()) + +XcmPallet + .limitedTeleportAssets({ + dest: VersionedMultiLocation.V2(Rune.rec({ + parents: 0, + interior: Junctions.X1(Junction.Parachain(1000)), + })), + beneficiary: VersionedMultiLocation.V2(Rune.rec({ + parents: 0, + interior: Junctions.X1(Junction.AccountId32({ + id: alice.publicKey, + network: NetworkId.Any(), + })), + })), + assets: VersionedMultiAssets.V2(Rune.array([Rune.rec({ + id: AssetId.Concrete(Rune.rec({ + parents: 0, + interior: Junctions.Here(), + })), + fun: Fungibility.Fungible(500_000_000_000_000n), + })])), + feeAssetItem: 0, + weightLimit: WeightLimit.Unlimited(), + }) + .signed(signature({ sender: alice })) + .sent() + .dbgStatus() + .finalized() + .run() + +// TODO: have the recipient associate the downward message with the sender +outer: +for await (const e of System.Events.value(undefined, parachain.latestBlock.hash).iter()) { + if (e) { + for (const { event } of e) { + if (ParachainRuntimeEvent.isParachainSystem(event)) { + const { value } = event + if (ParachainEvent.isDownwardMessagesProcessed(value)) { + console.log("Alice balance after:", await aliceBalance.run()) + break outer + } + } + } + } +} diff --git a/examples/xcm_asset_teleportation.ts b/examples/xcm_asset_teleportation.ts deleted file mode 100644 index d05fbafb7..000000000 --- a/examples/xcm_asset_teleportation.ts +++ /dev/null @@ -1,86 +0,0 @@ -import { alice, Rune, ValueRune } from "capi" -import { types, XcmPallet } from "zombienet/statemine.toml/alice/@latest/mod.js" -import { Event as XcmPalletEvent } from "zombienet/statemine.toml/alice/@latest/types/pallet_xcm/pallet.js" -import { RuntimeEvent as AliceRuntimeEvent } from "zombienet/statemine.toml/alice/@latest/types/rococo_runtime/mod.js" -import { chain as collator, System } from "zombienet/statemine.toml/collator/@latest/mod.js" -import { Event as ParachainSystemEvent } from "zombienet/statemine.toml/collator/@latest/types/cumulus_pallet_parachain_system/pallet.js" -import { RuntimeEvent as CollatorRuntimeEvent } from "zombienet/statemine.toml/collator/@latest/types/statemine_runtime.js" -import { signature } from "../patterns/signature/polkadot.ts" - -const { - VersionedMultiAssets, - VersionedMultiLocation, - v2: { - NetworkId, - WeightLimit, - junction: { Junction }, - multilocation: { Junctions }, - multiasset: { AssetId, Fungibility }, - }, -} = types.xcm - -class CannotFindAttemptError extends Error { - override readonly name = "CannotFindAttemptError" -} - -const initiatedEvent = XcmPallet - .limitedTeleportAssets({ - dest: VersionedMultiLocation.V2(Rune.rec({ - parents: 0, - interior: Junctions.X1(Junction.Parachain(1000)), - })), - beneficiary: VersionedMultiLocation.V2(Rune.rec({ - parents: 0, - interior: Junctions.X1(Junction.AccountId32({ - id: alice.publicKey, - network: NetworkId.Any(), - })), - })), - assets: VersionedMultiAssets.V2(Rune.array([Rune.rec({ - id: AssetId.Concrete(Rune.rec({ - parents: 0, - interior: Junctions.Here(), - })), - fun: Fungibility.Fungible(500_000_000_000_000n), - })])), - feeAssetItem: 0, - weightLimit: WeightLimit.Unlimited(), - }) - .signed(signature({ sender: alice })) - .sent() - .dbgStatus("Teleportation status:") - .finalizedEvents() - .into(ValueRune) - .map((events) => - events.find((e) => - AliceRuntimeEvent.isXcmPallet(e.event) - && XcmPalletEvent.isAttempted(e.event.value) - ) ?? new CannotFindAttemptError() - ) - .unhandle(CannotFindAttemptError) - .dbg("Initiated event:") - -const processedEvent = System.Events - .value(undefined, collator.latestBlock.hash) - .map((events) => - events?.find((e) => - CollatorRuntimeEvent.isParachainSystem(e.event) - && ParachainSystemEvent.isDownwardMessagesProcessed(e.event.value) - ) - ) - .filter((event) => !!event) - .dbg("Processed events:") - -// TODO: have the recipient associate the downward message with the sender -await logAliceBalance() - .chain(() => Rune.tuple([initiatedEvent, processedEvent])) - .chain(() => logAliceBalance()) - .run() - -function logAliceBalance() { - return System.Account - .value(alice.publicKey) - .unhandle(undefined) - .access("data", "free") - .dbg("Alice balance:") -} From 76a3b47a37cd081822a7ad4dbd94fb083be00c68 Mon Sep 17 00:00:00 2001 From: Harry Solovay Date: Tue, 28 Mar 2023 14:52:20 -0400 Subject: [PATCH 2/2] cleanup --- examples/xcm/asset_teleportation.ts | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/examples/xcm/asset_teleportation.ts b/examples/xcm/asset_teleportation.ts index 67bf6ccbe..a897cc193 100644 --- a/examples/xcm/asset_teleportation.ts +++ b/examples/xcm/asset_teleportation.ts @@ -2,8 +2,8 @@ import { alice, Rune } from "capi" import { signature } from "capi/patterns/signature/polkadot.ts" import { types, XcmPallet } from "zombienet/statemine.toml/alice/@latest/mod.js" import { chain as parachain, System } from "zombienet/statemine.toml/collator/@latest/mod.js" -import { Event as ParachainEvent } from "zombienet/statemine.toml/collator/@latest/types/cumulus_pallet_parachain_system/pallet.js" -import { RuntimeEvent as ParachainRuntimeEvent } from "zombienet/statemine.toml/collator/@latest/types/statemine_runtime.js" +import { Event } from "zombienet/statemine.toml/collator/@latest/types/cumulus_pallet_parachain_system/pallet.js" +import { RuntimeEvent } from "zombienet/statemine.toml/collator/@latest/types/statemine_runtime.js" const { VersionedMultiAssets, @@ -58,12 +58,9 @@ outer: for await (const e of System.Events.value(undefined, parachain.latestBlock.hash).iter()) { if (e) { for (const { event } of e) { - if (ParachainRuntimeEvent.isParachainSystem(event)) { - const { value } = event - if (ParachainEvent.isDownwardMessagesProcessed(value)) { - console.log("Alice balance after:", await aliceBalance.run()) - break outer - } + if (RuntimeEvent.isParachainSystem(event) && Event.isDownwardMessagesProcessed(event.value)) { + console.log("Alice balance after:", await aliceBalance.run()) + break outer } } }