Skip to content

Commit

Permalink
Merge pull request #4 from niklasad1/add-subxt-example
Browse files Browse the repository at this point in the history
add subxt example
  • Loading branch information
niklasad1 authored Dec 13, 2023
2 parents ac40803 + ba8edec commit 0fb9e87
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
9 changes: 8 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,11 @@ subxt = { version = "0.33", optional = true }
[dev-dependencies]
jsonrpsee = { version = "0.20.3", features = ["server"] }
anyhow = "1"
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
subxt = "0.33"
subxt-signer = "0.33"

[[example]]
name = "hello_subxt"
path = "examples/hello_subxt.rs"
required-features = ["subxt"]
48 changes: 48 additions & 0 deletions examples/hello_subxt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use reconnecting_jsonrpsee_ws_client::Client;
use subxt::backend::rpc::RpcClient;
use subxt::{OnlineClient, PolkadotConfig};
use subxt_signer::sr25519::dev;
use tracing_subscriber::util::SubscriberInitExt;

// Generate an interface that we can use from the node's metadata.
#[subxt::subxt(runtime_metadata_path = "examples/metadata.scale")]
pub mod runtime {}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let filter = tracing_subscriber::EnvFilter::from_default_env();
tracing_subscriber::fmt()
.with_env_filter(filter)
.finish()
.init();

let rpc = Client::builder()
.build("ws://localhost:9944".to_string())
.await?;

// Create a new API client, configured to talk to Polkadot nodes.
let api: OnlineClient<PolkadotConfig> =
OnlineClient::from_rpc_client(RpcClient::new(rpc)).await?;

// Build a balance transfer extrinsic.
let dest = dev::bob().public_key().into();
let balance_transfer_tx = runtime::tx().balances().transfer_allow_death(dest, 10_000);

// Submit the balance transfer extrinsic from Alice, and wait for it to be successful
// and in a finalized block. We get back the extrinsic events if all is well.
let from = dev::alice();
let events = api
.tx()
.sign_and_submit_then_watch_default(&balance_transfer_tx, &from)
.await?
.wait_for_finalized_success()
.await?;

// Find a Transfer event and print it.
let transfer_event = events.find_first::<runtime::balances::events::Transfer>()?;
if let Some(event) = transfer_event {
println!("Balance transfer success: {event:?}");
}

Ok(())
}
Binary file added examples/metadata.scale
Binary file not shown.

0 comments on commit 0fb9e87

Please sign in to comment.