-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extrinsics: Decode extrinsics from blocks (#929)
* Update polkadot.scale Signed-off-by: Alexandru Vasile <[email protected]> * extrinsics: Add extrinsics client Signed-off-by: Alexandru Vasile <[email protected]> * extrinsics: Decode extrinsics Signed-off-by: Alexandru Vasile <[email protected]> * subxt: Add extrinsic error Signed-off-by: Alexandru Vasile <[email protected]> * blocks: Expose extrinsics Signed-off-by: Alexandru Vasile <[email protected]> * examples: Fetch and decode block extrinsics Signed-off-by: Alexandru Vasile <[email protected]> * Fix clippy Signed-off-by: Alexandru Vasile <[email protected]> * extrinsics: Fetch pallet and variant index Signed-off-by: Alexandru Vasile <[email protected]> * subxt: Move extrinsics on the subxt::blocks Signed-off-by: Alexandru Vasile <[email protected]> * example: Adjust example Signed-off-by: Alexandru Vasile <[email protected]> * metadata: Collect ExtrinsicMetadata Signed-off-by: Alexandru Vasile <[email protected]> * subxt: Implement StaticExtrinsic for the calls Signed-off-by: Alexandru Vasile <[email protected]> * Adjust examples Signed-off-by: Alexandru Vasile <[email protected]> * codegen: Add root level Call enum Signed-off-by: Alexandru Vasile <[email protected]> * Adjust testing Signed-off-by: Alexandru Vasile <[email protected]> * subxt: Add new decode interface Signed-off-by: Alexandru Vasile <[email protected]> * subxt: Merge ExtrinsicError with BlockError Signed-off-by: Alexandru Vasile <[email protected]> * examples: Find first extrinsic Signed-off-by: Alexandru Vasile <[email protected]> * Move code to extrinsic_types Signed-off-by: Alexandru Vasile <[email protected]> * Add Extrinsic struct Signed-off-by: Alexandru Vasile <[email protected]> * Adjust examples Signed-off-by: Alexandru Vasile <[email protected]> * test: Decode extinsics Signed-off-by: Alexandru Vasile <[email protected]> * extrinsics/test: Add fake metadata for static decoding Signed-off-by: Alexandru Vasile <[email protected]> * extrinsics/test: Decode from insufficient bytes Signed-off-by: Alexandru Vasile <[email protected]> * extrinsics/test: Check unsupported versions Signed-off-by: Alexandru Vasile <[email protected]> * extrinsics/test: Statically decode to root and pallet enums Signed-off-by: Alexandru Vasile <[email protected]> * extrinsics/tests: Remove clones Signed-off-by: Alexandru Vasile <[email protected]> * blocks: Fetch block body inline Signed-off-by: Alexandru Vasile <[email protected]> * blocks: Rename ExtrinsicIds to ExtrinsicPartTypeIds Signed-off-by: Alexandru Vasile <[email protected]> * extrinsics/test: Check decode as_extrinsic Signed-off-by: Alexandru Vasile <[email protected]> * blocks: Remove InsufficientData error Signed-off-by: Alexandru Vasile <[email protected]> * blocks: Return error from extrinsic_metadata Signed-off-by: Alexandru Vasile <[email protected]> * extrinsics: Postpone decoding of call bytes Signed-off-by: Alexandru Vasile <[email protected]> * metadata_type: Rename variables Signed-off-by: Alexandru Vasile <[email protected]> * Adjust calls path for example and tests Signed-off-by: Alexandru Vasile <[email protected]> * examples: Remove traces Signed-off-by: Alexandru Vasile <[email protected]> * book: Add extrinsics documentation Signed-off-by: Alexandru Vasile <[email protected]> * book: Improve extrinsics docs Signed-off-by: Alexandru Vasile <[email protected]> --------- Signed-off-by: Alexandru Vasile <[email protected]> Co-authored-by: James Wilson <[email protected]>
- Loading branch information
1 parent
0f963f2
commit 3b0022b
Showing
19 changed files
with
3,281 additions
and
163 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,99 @@ | ||
// Copyright 2019-2023 Parity Technologies (UK) Ltd. | ||
// This file is dual-licensed as Apache-2.0 or GPL-3.0. | ||
// see LICENSE for license details. | ||
|
||
//! To run this example, a local polkadot node should be running. Example verified against polkadot v0.9.28-9ffe6e9e3da. | ||
//! | ||
//! E.g. | ||
//! ```bash | ||
//! curl "https://github.com/paritytech/polkadot/releases/download/v0.9.28/polkadot" --output /usr/local/bin/polkadot --location | ||
//! polkadot --dev --tmp | ||
//! ``` | ||
use futures::StreamExt; | ||
use sp_keyring::AccountKeyring; | ||
use std::time::Duration; | ||
use subxt::{tx::PairSigner, OnlineClient, PolkadotConfig}; | ||
|
||
#[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>> { | ||
// Create a client to use: | ||
let api = OnlineClient::<PolkadotConfig>::new().await?; | ||
|
||
// Subscribe to (in this case, finalized) blocks. | ||
let mut block_sub = api.blocks().subscribe_finalized().await?; | ||
|
||
// While this subscription is active, balance transfers are made somewhere: | ||
tokio::task::spawn({ | ||
let api = api.clone(); | ||
async move { | ||
let signer = PairSigner::new(AccountKeyring::Alice.pair()); | ||
let mut transfer_amount = 1_000_000_000; | ||
|
||
// Make small balance transfers from Alice to Bob in a loop: | ||
loop { | ||
let transfer_tx = polkadot::tx() | ||
.balances() | ||
.transfer(AccountKeyring::Bob.to_account_id().into(), transfer_amount); | ||
api.tx() | ||
.sign_and_submit_default(&transfer_tx, &signer) | ||
.await | ||
.unwrap(); | ||
|
||
tokio::time::sleep(Duration::from_secs(10)).await; | ||
transfer_amount += 100_000_000; | ||
} | ||
} | ||
}); | ||
|
||
// Get each finalized block as it arrives. | ||
while let Some(block) = block_sub.next().await { | ||
let block = block?; | ||
|
||
let block_hash = block.hash(); | ||
println!(" Block {:?}", block_hash); | ||
|
||
// Ask for the extrinsics for this block. | ||
let extrinsics = block.body().await?.extrinsics(); | ||
|
||
let transfer_tx = extrinsics.find_first::<polkadot::balances::calls::types::Transfer>(); | ||
println!(" Transfer tx: {:?}", transfer_tx); | ||
|
||
// Ask for the extrinsics for this block. | ||
for extrinsic in extrinsics.iter() { | ||
let extrinsic = extrinsic?; | ||
|
||
println!( | ||
" Extrinsic block index {:?}, pallet index {:?}, variant index {:?}", | ||
extrinsic.index(), | ||
extrinsic.pallet_index(), | ||
extrinsic.variant_index() | ||
); | ||
|
||
let root_extrinsic = extrinsic.as_root_extrinsic::<polkadot::Call>(); | ||
println!(" As root extrinsic {:?}\n", root_extrinsic); | ||
|
||
let pallet_extrinsic = extrinsic | ||
.as_pallet_extrinsic::<polkadot::runtime_types::pallet_balances::pallet::Call>(); | ||
println!( | ||
" Extrinsic as Balances Pallet call: {:?}\n", | ||
pallet_extrinsic | ||
); | ||
|
||
let call = extrinsic.as_extrinsic::<polkadot::balances::calls::types::Transfer>()?; | ||
println!( | ||
" Extrinsic as polkadot::balances::calls::Transfer: {:?}\n\n", | ||
call | ||
); | ||
} | ||
|
||
println!("\n"); | ||
} | ||
|
||
Ok(()) | ||
} |
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
Oops, something went wrong.