-
Notifications
You must be signed in to change notification settings - Fork 262
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
Filter one or multiple events by type from an EventSubscription #461
Merged
Merged
Changes from 16 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
75e615a
Split events.rs into multiple files and start work on FilterEvents
jsdw 9e187fe
First pass filtering event(s)
jsdw a3383da
Tweak event examples to show filter_events
jsdw fc25bc4
cargo clippy + fmt
jsdw c6702f9
consistify and tidy
jsdw f6cc1bd
cargo fmt
jsdw 7ff6ce6
Tweak a couple of comments
jsdw 35c98ad
Expose phase and block_hash of filtered events, too
jsdw f9d535d
cargo fmt
jsdw b2ddf6b
expose FilteredEventDetails
jsdw 8489394
Add docs
jsdw 329e785
cargo clippy
jsdw 9da2438
remove FilterEvents knowledge of EventSubscription so it's easier to …
jsdw fdfb9f7
unit test filter_events
jsdw 0df05ec
tweak an integration test to use filter_events
jsdw 8584078
cargo fmt
jsdw 333101c
cargo clippy
jsdw 486f8aa
Tweak a comment
jsdw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright 2019-2022 Parity Technologies (UK) Ltd. | ||
// This file is part of subxt. | ||
// | ||
// subxt is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// subxt is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with subxt. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
//! To run this example, a local polkadot node should be running. Example verified against polkadot 0.9.13-82616422d0-aarch64-macos. | ||
//! | ||
//! E.g. | ||
//! ```bash | ||
//! curl "https://github.com/paritytech/polkadot/releases/download/v0.9.13/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, | ||
DefaultExtra, | ||
PairSigner, | ||
}; | ||
|
||
#[subxt::subxt(runtime_metadata_path = "examples/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. | ||
#[async_std::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
env_logger::init(); | ||
|
||
// Subscribe to any events that occur: | ||
let api = ClientBuilder::new() | ||
.build() | ||
.await? | ||
.to_runtime_api::<polkadot::RuntimeApi<DefaultConfig, DefaultExtra<DefaultConfig>>>(); | ||
|
||
// Subscribe to several balance related events. If we ask for more than one event, | ||
// we'll be given a correpsonding tuple of `Option`'s, with exactly one | ||
// variant populated each time. | ||
let mut balance_events = api.events().subscribe().await?.filter_events::<( | ||
polkadot::balances::events::Withdraw, | ||
polkadot::balances::events::Transfer, | ||
polkadot::balances::events::Deposit, | ||
)>(); | ||
|
||
// While this subscription is active, we imagine some balance transfers are made somewhere else: | ||
async_std::task::spawn(async { | ||
let signer = PairSigner::new(AccountKeyring::Alice.pair()); | ||
let api = ClientBuilder::new() | ||
.build() | ||
.await | ||
.unwrap() | ||
.to_runtime_api::<polkadot::RuntimeApi<DefaultConfig, DefaultExtra<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) | ||
.sign_and_submit(&signer) | ||
.await | ||
.unwrap(); | ||
async_std::task::sleep(Duration::from_secs(10)).await; | ||
} | ||
}); | ||
|
||
// Our subscription will see all of the balance events we're filtering on: | ||
while let Some(ev) = balance_events.next().await { | ||
let event_details = ev?; | ||
|
||
let block_hash = event_details.block_hash; | ||
let event = event_details.event; | ||
println!("Event at {:?}:", block_hash); | ||
|
||
if let (Some(withdraw), _, _) = &event { | ||
println!(" Withdraw event: {withdraw:?}"); | ||
} | ||
if let (_, Some(transfer), _) = &event { | ||
println!(" Transfer event: {transfer:?}"); | ||
} | ||
if let (_, _, Some(deposit)) = &event { | ||
println!(" Deposit event: {deposit:?}"); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added this new example to show off the "filter multiple events" case.