-
Notifications
You must be signed in to change notification settings - Fork 281
New Event Subscription API #442
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
Merged
+11,598
−5,287
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
18b95a4
Add reworked event types
jsdw 18482d8
first pass implementing event subscriptions
jsdw 644b638
make clear that some methods are private
jsdw 3443e6c
comment tidy
jsdw 589d1a6
use Events in transaction stuff
jsdw 8627117
align transaction and event APIs
jsdw 9c409a6
remove __private_ prefixes; they are ugly
jsdw 72e66a9
fix examples and remove old events and subscription code
jsdw 1273c7e
better comments on hidden event functions
jsdw 63a4e1a
re-add find_first_event; it's used a bunch in tests and examples
jsdw 9ac234f
cargo check --all-targets now passes
jsdw 9a40790
Fix up existing event tests
jsdw d9bc620
cargo fmt
jsdw 457cd6e
change todo to note
jsdw b85acf1
clippy and doc niggles
jsdw 9d99ddb
revert to find_first_event
jsdw ea71f74
Add specific subscription related tests
jsdw 5b1ed7b
cargo fmt
jsdw 221135c
Update tests and add/fix examples
jsdw 4d0085d
cargo fmt
jsdw 1d202b3
add a little to subscribe_all_events example
jsdw a74c8ed
cargo fmt
jsdw e20d171
move an example comment
jsdw aa96d8d
easy access to root mod for more clarity
jsdw 18ca3be
add a couple of tests to ensure that events properly decoded until na…
jsdw 4056f29
Simplify EventSubscription Stream impl a little
jsdw ef96407
Address some PR feedback
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Binary file not shown.
This file contains hidden or 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 hidden or 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 hidden or 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,116 @@ | ||
| // 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(); | ||
|
|
||
| let api = ClientBuilder::new() | ||
| .build() | ||
| .await? | ||
| .to_runtime_api::<polkadot::RuntimeApi<DefaultConfig, DefaultExtra<DefaultConfig>>>(); | ||
|
|
||
| // Subscribe to any events that occur: | ||
| let mut event_sub = api.events().subscribe().await?; | ||
dvdplm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // While this subscription is active, balance transfers are made somewhere: | ||
| 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>>>(); | ||
|
|
||
| let mut transfer_amount = 1_000_000_000; | ||
|
|
||
| // Make small balance transfers from Alice to Bob in a loop: | ||
| loop { | ||
| api.tx() | ||
| .balances() | ||
| .transfer(AccountKeyring::Bob.to_account_id().into(), transfer_amount) | ||
| .sign_and_submit(&signer) | ||
| .await | ||
| .unwrap(); | ||
|
|
||
| async_std::task::sleep(Duration::from_secs(10)).await; | ||
| transfer_amount += 100_000_000; | ||
| } | ||
| }); | ||
|
|
||
| // Our subscription will see the events emitted as a result of this: | ||
| while let Some(events) = event_sub.next().await { | ||
| let events = events?; | ||
dvdplm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| let block_hash = events.block_hash(); | ||
|
|
||
| // We can iterate, statically decoding all events if we want: | ||
| println!("All events in block {block_hash:?}:"); | ||
| println!(" Static event details:"); | ||
| for event in events.iter() { | ||
| let event = event?; | ||
| println!(" {event:?}"); | ||
| } | ||
|
|
||
| // Or we can dynamically decode events: | ||
| println!(" Dynamic event details: {block_hash:?}:"); | ||
dvdplm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| for event in events.iter_raw() { | ||
| let event = event?; | ||
| let is_balance_transfer = event | ||
| .as_event::<polkadot::balances::events::Transfer>()? | ||
| .is_some(); | ||
| let pallet = event.pallet; | ||
| let variant = event.variant; | ||
| println!( | ||
| " {pallet}::{variant} (is balance transfer? {is_balance_transfer})" | ||
| ); | ||
| } | ||
|
|
||
| // Or we can dynamically find the first transfer event, ignoring any others: | ||
| let transfer_event = | ||
| events.find_first_event::<polkadot::balances::events::Transfer>()?; | ||
|
|
||
| if let Some(ev) = transfer_event { | ||
| println!(" - Balance transfer success: value: {:?}", ev.amount); | ||
| } else { | ||
| println!(" - No balance transfer event found in this block"); | ||
| } | ||
| } | ||
|
|
||
| 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.
Uh oh!
There was an error while loading. Please reload this page.