From 2c8650f340cc2163b2b3f75f8de408dbfc6eebe6 Mon Sep 17 00:00:00 2001 From: Alexandru Vasile Date: Tue, 13 Dec 2022 11:53:25 +0000 Subject: [PATCH] example: Submit raw extrinsic from polkadot.js Signed-off-by: Alexandru Vasile --- examples/examples/submit_raw_extrinsic.rs | 86 +++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 examples/examples/submit_raw_extrinsic.rs diff --git a/examples/examples/submit_raw_extrinsic.rs b/examples/examples/submit_raw_extrinsic.rs new file mode 100644 index 0000000000..431392f127 --- /dev/null +++ b/examples/examples/submit_raw_extrinsic.rs @@ -0,0 +1,86 @@ +// Copyright 2019-2022 Parity Technologies (UK) Ltd. +// This file is dual-licensed as Apache-2.0 or GPL-3.0. +// see LICENSE for license details. + +//! To run this example, a local polkadot node should be running. Example verified against polkadot v0.9.28-9ffe6e9e3da. +//! +//! E.g. +//! ```bash +//! curl "https://github.com/paritytech/polkadot/releases/download/v0.9.28/polkadot" --output /usr/local/bin/polkadot --location +//! polkadot --dev --tmp +//! ``` + +use subxt::{ + tx::SubmittableExtrinsic, + OnlineClient, + PolkadotConfig, +}; + +use sp_keyring::AccountKeyring; +use subxt::tx::PairSigner; + +#[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata.scale")] +pub mod polkadot {} + +#[tokio::main] +async fn main() -> Result<(), Box> { + tracing_subscriber::fmt::init(); + + // Create a client to communicate with the chain. + let api = OnlineClient::::new().await?; + + // Note: This can be replaced with the extrinsic hex from polkadot.js directly. + let extrinsic_hex = { + let signer = PairSigner::new(AccountKeyring::Alice.pair()); + let dest = AccountKeyring::Bob.to_account_id().into(); + + let api = OnlineClient::::new().await?; + + let tx = polkadot::tx() + .balances() + .transfer(dest, 123_456_789_012_345); + + let signed_tx = api + .tx() + .create_signed(&tx, &signer, Default::default()) + .await?; + let bytes = signed_tx.encoded(); + let extrinsic_hex = hex::encode(bytes); + println!("Extrinsic hex: {:?}", extrinsic_hex); + + // TODO: Comment this. + extrinsic_hex + // TODO: Place your extrinsic here + // "0x..".to_string() + }; + + println!("Decoding the extrinsic bytes..."); + // Obtain the raw bytes from the extrinsic hexadecimal representation. + let extrinsic_bytes = hex::decode(extrinsic_hex.trim_start_matches("0x"))?; + + println!("Creating submittable extrinsic from raw bytes..."); + // The raw bytes are wrapped into `Encoded` to ensure that `scale::encode` calls + // will have no effect upon the raw bytes. + let extrinsic = SubmittableExtrinsic::from_bytes(api.clone(), extrinsic_bytes); + + println!("Submit extrinsic and wait for success..."); + let extrinic_events = extrinsic + .submit_and_watch() + .await? + .wait_for_finalized_success() + .await?; + + println!("Finding extrinsic event..."); + + // This presumes the extrinsic is a transfer one. + let transfer_event = + extrinic_events.find_first::()?; + + if let Some(event) = transfer_event { + println!("Balance transfer success: {event:?}"); + } else { + println!("Failed to find Balances::Transfer Event"); + } + + Ok(()) +}