mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-09 21:21:11 +00:00
fa3e274020
Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
102 lines
3.7 KiB
Rust
102 lines
3.7 KiB
Rust
// Copyright (c) 2019-2022 Parity Technologies Limited
|
|
// This file is part of subxt.
|
|
//
|
|
// Permission is hereby granted, free of charge, to any
|
|
// person obtaining a copy of this software and associated
|
|
// documentation files (the "Software"), to deal in the
|
|
// Software without restriction, including without
|
|
// limitation the rights to use, copy, modify, merge,
|
|
// publish, distribute, sublicense, and/or sell copies of
|
|
// the Software, and to permit persons to whom the Software
|
|
// is furnished to do so, subject to the following
|
|
// conditions:
|
|
//
|
|
// The above copyright notice and this permission notice
|
|
// shall be included in all copies or substantial portions
|
|
// of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
|
|
// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
|
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
|
|
// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
|
|
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
//! To run this example, a local polkadot node should be running. Example verified against polkadot 0.9.18-4542a603cc-aarch64-macos.
|
|
//!
|
|
//! E.g.
|
|
//! ```bash
|
|
//! curl "https://github.com/paritytech/polkadot/releases/download/v0.9.18/polkadot" --output /usr/local/bin/polkadot --location
|
|
//! polkadot --dev --tmp
|
|
//! ```
|
|
|
|
use futures::StreamExt;
|
|
use sp_keyring::AccountKeyring;
|
|
use std::time::Duration;
|
|
use subxt::{
|
|
ClientBuilder,
|
|
DefaultConfig,
|
|
PairSigner,
|
|
PolkadotExtrinsicParams,
|
|
};
|
|
|
|
#[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata.scale")]
|
|
pub mod polkadot {}
|
|
|
|
/// Subscribe to all events, and then manually look through them and
|
|
/// pluck out the events that we care about.
|
|
#[tokio::main]
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
tracing_subscriber::fmt::init();
|
|
|
|
// Subscribe to any events that occur:
|
|
let api = ClientBuilder::new()
|
|
.build()
|
|
.await?
|
|
.to_runtime_api::<polkadot::RuntimeApi<DefaultConfig, PolkadotExtrinsicParams<DefaultConfig>>>();
|
|
|
|
// Subscribe to just balance transfer events, making use of `filter_events`
|
|
// to select a single event type (note the 1-tuple) to filter out and return.
|
|
let mut transfer_events = api
|
|
.events()
|
|
.subscribe()
|
|
.await?
|
|
.filter_events::<(polkadot::balances::events::Transfer,)>();
|
|
|
|
// While this subscription is active, we imagine some balance transfers are made somewhere else:
|
|
tokio::task::spawn(async {
|
|
let signer = PairSigner::new(AccountKeyring::Alice.pair());
|
|
let api =
|
|
ClientBuilder::new()
|
|
.build()
|
|
.await
|
|
.unwrap()
|
|
.to_runtime_api::<polkadot::RuntimeApi<
|
|
DefaultConfig,
|
|
PolkadotExtrinsicParams<DefaultConfig>,
|
|
>>();
|
|
|
|
// Make small balance transfers from Alice to Bob in a loop:
|
|
loop {
|
|
api.tx()
|
|
.balances()
|
|
.transfer(AccountKeyring::Bob.to_account_id().into(), 1_000_000_000)
|
|
.expect("compatible transfer call on runtime node")
|
|
.sign_and_submit_default(&signer)
|
|
.await
|
|
.unwrap();
|
|
tokio::time::sleep(Duration::from_secs(10)).await;
|
|
}
|
|
});
|
|
|
|
// Our subscription will see all of the transfer events emitted as a result of this:
|
|
while let Some(transfer_event) = transfer_events.next().await {
|
|
println!("Balance transfer event: {transfer_event:?}");
|
|
}
|
|
|
|
Ok(())
|
|
}
|