Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add subxt example #4

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Loading