This repository has been archived by the owner on Sep 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add xcm_teleport_assets.ts (#432)
Co-authored-by: Harry Solovay <[email protected]>
- Loading branch information
1 parent
eb34d72
commit dcb4b46
Showing
10 changed files
with
324 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
name: "Setup Binary" | ||
description: "Setup Binary" | ||
inputs: | ||
binary-name: | ||
description: "Binary name" | ||
required: true | ||
binary-version: | ||
description: "Binary version" | ||
required: true | ||
binary-github: | ||
description: "Binary github repository url" | ||
required: true | ||
runs: | ||
using: "composite" | ||
steps: | ||
- name: Cache Binary | ||
uses: actions/cache@v3 | ||
id: cache-binary | ||
with: | ||
path: | | ||
/usr/local/bin/${{ inputs.binary-name }} | ||
key: binary-${{ inputs.binary-name }}-${{ inputs.binary-version }} | ||
- if: ${{ steps.cache-binary.outputs.cache-hit != 'true' }} | ||
name: Download Binary | ||
run: | | ||
curl -L -o /usr/local/bin/${{ inputs.binary-name }} ${{ inputs.binary-github }}/releases/download/${{ inputs.binary-version }}/${{ inputs.binary-name }} | ||
chmod +x /usr/local/bin/${{ inputs.binary-name }} | ||
shell: bash |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
mod.ts | ||
multisig_transfer.ts | ||
multisig_transfer.ts | ||
xcm_teleport_assets.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
[relaychain] | ||
default_image = "docker.io/paritypr/polkadot-debug:master" | ||
default_command = "polkadot" | ||
default_args = ["-lparachain=debug"] | ||
chain = "rococo-local" | ||
|
||
[[relaychain.nodes]] | ||
name = "alice" | ||
validator = true | ||
|
||
[[relaychain.nodes]] | ||
name = "bob" | ||
validator = true | ||
|
||
[[parachains]] | ||
id = 1000 | ||
cumulus_based = true | ||
chain = "statemine-local" | ||
|
||
[parachains.collator] | ||
name = "collator01" | ||
image = "docker.io/parity/polkadot-parachain:latest" | ||
command = "polkadot-parachain" | ||
args = ["-lparachain=debug"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
// This example requires zombienet-macos/zombienet-linux, polkadot and polkadot-parachain binaries in the PATH | ||
|
||
import * as path from "http://localhost:5646/@local/deps/std/path.ts" | ||
import * as C from "http://localhost:5646/@local/mod.ts" | ||
import * as T from "http://localhost:5646/@local/test_util/mod.ts" | ||
import * as U from "http://localhost:5646/@local/util/mod.ts" | ||
|
||
const configFile = path.join( | ||
path.dirname(path.fromFileUrl(import.meta.url)), | ||
"xcm_teleport_assets.toml", | ||
) | ||
const zombienet = await T.zombienet.start(configFile) | ||
|
||
Deno.addSignalListener("SIGINT", async () => { | ||
try { | ||
await zombienet.close() | ||
} finally { | ||
Deno.exit() | ||
} | ||
}) | ||
|
||
// see xcm_teleport_asset.toml for node names | ||
const relaychainClient = zombienet.clients.byName["alice"]! | ||
const parachainClient = zombienet.clients.byName["collator01"]! | ||
|
||
const teleportAssetsTx = C.extrinsic(relaychainClient)({ | ||
sender: T.alice.address, | ||
call: { | ||
type: "XcmPallet", | ||
value: { | ||
type: "limitedTeleportAssets", | ||
dest: { | ||
type: "V1", | ||
value: { | ||
parents: 0, | ||
interior: { | ||
type: "X1", | ||
value: { | ||
type: "Parachain", | ||
value: 1000, | ||
}, | ||
}, | ||
}, | ||
}, | ||
beneficiary: { | ||
type: "V1", | ||
value: { | ||
parents: 0, | ||
interior: { | ||
type: "X1", | ||
value: { | ||
type: "AccountId32", | ||
id: T.alice.address.value, | ||
network: { | ||
type: "Any", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
assets: { | ||
type: "V1", | ||
value: [{ | ||
id: { | ||
type: "Concrete", | ||
value: { | ||
parents: 0, | ||
interior: { | ||
type: "Here", | ||
}, | ||
}, | ||
}, | ||
fun: { | ||
type: "Fungible", | ||
value: 500_000_000_000_000n, | ||
}, | ||
}], | ||
}, | ||
feeAssetItem: 0, | ||
weightLimit: { | ||
type: "Unlimited", | ||
}, | ||
}, | ||
}, | ||
}) | ||
.signed(T.alice.sign) | ||
.watch(({ end }) => { | ||
return (status) => { | ||
console.log(status) | ||
if (typeof status !== "string" && status.finalized) { | ||
return end(status.finalized) | ||
} else if (C.rpc.known.TransactionStatus.isTerminal(status)) { | ||
return end(new Error()) | ||
} | ||
return | ||
} | ||
}) | ||
|
||
const aliceParachainBalance = () => | ||
C.entryRead(parachainClient)("System", "Account", [T.alice.publicKey]) | ||
.access("value") | ||
.access("data") | ||
.access("free") | ||
|
||
const watchForDownwardMessagesProcessed = C.entryWatch(parachainClient)( | ||
"System", | ||
"Events", | ||
[], | ||
({ end }) => { | ||
return (entry: any) => { | ||
for (const [_hash, events] of entry) { | ||
if (!events) return | ||
for (const { event } of events) { | ||
if ( | ||
event.type === "ParachainSystem" && event.value.type === "DownwardMessagesProcessed" | ||
) { | ||
return end(event) | ||
} | ||
} | ||
} | ||
return | ||
} | ||
}, | ||
) | ||
|
||
const watchForDownwardMessagesProcessedPending = watchForDownwardMessagesProcessed.run() | ||
|
||
const watchForParachainBlocksPending = C.blockWatch(parachainClient)( | ||
({ end }) => { | ||
let i = 0 | ||
return () => { | ||
if (i === 1) { | ||
return end() | ||
} | ||
i++ | ||
return | ||
} | ||
}, | ||
).run() | ||
|
||
console.log("Alice parachain balance before XcmPallet.limitedTeleportAssets") | ||
console.log(U.throwIfError(await aliceParachainBalance().run())) | ||
U.throwIfError(await teleportAssetsTx.run()) | ||
console.log("waiting for parachain ParachainSystem.DownwardMessagesProcessed event") | ||
console.log(U.throwIfError(await watchForDownwardMessagesProcessedPending)) | ||
console.log("waiting for parachain to start generating blocks") | ||
U.throwIfError(await watchForParachainBlocksPending) | ||
console.log("Alice parachain balance after XcmPallet.limitedTeleportAssets") | ||
console.log(U.throwIfError(await aliceParachainBalance().run())) | ||
|
||
await zombienet.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import * as Z from "../deps/zones.ts" | ||
import * as C from "../mod.ts" | ||
|
||
export const start = async (configFile: string, env?: Record<string, string>) => { | ||
const networkFilesPath = await Deno.makeTempDir({ prefix: "capi_zombienet" }) | ||
const process = Deno.run({ | ||
cmd: [ | ||
zombienetBinary(), | ||
"-d", | ||
networkFilesPath, | ||
"--provider", | ||
"native", | ||
"--force", | ||
"spawn", | ||
configFile, | ||
], | ||
stdout: "piped", | ||
env, | ||
}) | ||
// TODO: improve Network launched detection | ||
// Deno.watchFs on `${networkFilesPath}/zombie.json` could be an alternative | ||
const buffer = new Uint8Array(1024) | ||
while (true) { | ||
await process.stdout?.read(buffer) | ||
const text = new TextDecoder().decode(buffer) | ||
if (text.includes("Network launched")) { | ||
process.stdout.close() | ||
break | ||
} | ||
} | ||
const close = async () => { | ||
process.kill("SIGINT") | ||
await process.status() | ||
process.close() | ||
Deno.remove(networkFilesPath, { recursive: true }) | ||
} | ||
const config = JSON.parse(await Deno.readTextFile(`${networkFilesPath}/zombie.json`)) | ||
const clients = { | ||
relay: config.relay.map((node: any) => new NodeClientEffect(node.wsUri)) as NodeClientEffect[], | ||
paras: Object.entries(config.paras) | ||
.reduce( | ||
(acc, [name, { nodes }]: any) => { | ||
acc[name] = nodes.map((node: any) => new NodeClientEffect(node.wsUri)) | ||
return acc | ||
}, | ||
{} as Record<string, NodeClientEffect[]>, | ||
), | ||
byName: Object.entries(config.nodesByName) | ||
.reduce( | ||
(acc, [name, node]: any) => { | ||
acc[name] = new NodeClientEffect(node.wsUri) | ||
return acc | ||
}, | ||
{} as Record<string, NodeClientEffect>, | ||
), | ||
} | ||
return { close, config, clients } | ||
} | ||
|
||
export class NodeClientEffect extends Z.Effect<C.rpc.Client<string, Event, Event, Event>, Error> { | ||
constructor(readonly url: string) { | ||
super({ | ||
kind: "Client", | ||
impl: Z | ||
.call(() => { | ||
try { | ||
return new C.rpc.Client(C.rpc.proxyProvider, url) | ||
} catch (e) { | ||
return e | ||
} | ||
}) | ||
.impl, | ||
items: [url], | ||
memoize: true, | ||
}) | ||
} | ||
} | ||
|
||
function zombienetBinary() { | ||
switch (Deno.build.os) { | ||
case "darwin": | ||
return "zombienet-macos" | ||
case "linux": | ||
return "zombienet-linux" | ||
default: | ||
throw new Error(`zombienet does not support ${Deno.build.os} OS`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -205,4 +205,7 @@ xxhash | |
xxnetwork | ||
zio | ||
zombienet | ||
relaychain | ||
paritypr | ||
lparachain | ||
unioned |