Skip to content

Commit

Permalink
add example for pjs
Browse files Browse the repository at this point in the history
  • Loading branch information
pepoviola committed Nov 24, 2023
1 parent 4f2fa90 commit ea0c2ea
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ tokio = { workspace = true }
futures = { workspace = true }
subxt = { workspace = true }
tracing-subscriber = "0.3"
serde_json = { workspace = true }
52 changes: 52 additions & 0 deletions crates/examples/examples/pjs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use serde_json::json;

use futures::stream::StreamExt;
use zombienet_sdk::{NetworkConfigBuilder, NetworkConfigExt};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt::init();
let network = NetworkConfigBuilder::new()
.with_relaychain(|r| {
r.with_chain("rococo-local")
.with_default_command("polkadot")
.with_node(|node| node.with_name("alice"))
.with_node(|node| node.with_name("bob"))
})
.with_parachain(|p| {
p.with_id(100)
.cumulus_based(true)
.with_collator(|n| n.with_name("collator").with_command("polkadot-parachain"))
})
.build()
.unwrap()
.spawn_native()
.await?;

println!("🚀🚀🚀🚀 network deployed");

let alice = network.get_node("alice")?;
let client = alice.client::<subxt::PolkadotConfig>().await?;

// wait 2 blocks
let mut blocks = client.blocks().subscribe_finalized().await?.take(2);

while let Some(block) = blocks.next().await {
println!("Block #{}", block?.header().number);
}

// run pjs with code
let query_paras = r#"
const parachains: number[] = (await api.query.paras.parachains()) || [];
return parachains.toJSON()
"#;

let paras = alice.pjs(query_paras, vec![]).await??;

println!("parachains registered: {:?}", paras);

// run pjs with file
let _ = alice.pjs_file("./examples/pjs_transfer.js", vec![json!("//Alice")]).await?;

Ok(())
}
32 changes: 32 additions & 0 deletions crates/examples/examples/pjs_transfer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const seed = arguments[0];

await utilCrypto.cryptoWaitReady();
const k = new keyring.Keyring({ type: "sr25519" });
const signer = k.addFromUri(seed);

// Make a transfer from Alice to Bob and listen to system events.
// You need to be connected to a development chain for this example to work.
const ALICE = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY';
const BOB = '5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty';

// Get a random number between 1 and 100000
const randomAmount = Math.floor((Math.random() * 100000) + 1);

// Create a extrinsic, transferring randomAmount units to Bob.
const transferAllowDeath = api.tx.balances.transferAllowDeath(BOB, randomAmount);

return new Promise(async (resolve, _reject) => {
// Sign and Send the transaction
const unsub = await transferAllowDeath.signAndSend(signer, ({ events = [], status }) => {
if (status.isInBlock) {
console.log('Successful transfer of ' + randomAmount + ' with hash ' + status.asInBlock.toHex());
return resolve();
} else {
console.log('Status of transfer: ' + status.type);
}

events.forEach(({ phase, event: { data, method, section } }) => {
console.log(phase.toString() + ' : ' + section + '.' + method + ' ' + data.toString());
});
});
});

0 comments on commit ea0c2ea

Please sign in to comment.