mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-22 09:05:41 +00:00
Rework Subxt API to support offline and dynamic transactions (#593)
* WIP API changes * debug impls * Get main crate compiling with first round of changes * Some tidy up * Add WithExtrinsicParams, and have SubstrateConfig + PolkadotConfig, not DefaultConfig * move transaction into extrinsic folder * Add runtime updates back to OnlineClient * rework to be 'client first' to fit better with storage + events * add support for events to Client * tidy dupe trait bound * Wire storage into client, but need to remove static reliance * various tidy up and start stripping codegen to remove bits we dont need now * First pass updating calls and constants codegen * WIP storage client updates * First pass migrated runtime storage over to new format * pass over codegen to generate StorageAddresses and throw other stuff out * don't need a Call trait any more * shuffle things around a bit * Various proc_macro fixes to get 'cargo check' working * organise what's exposed from subxt * Get first example working; balance_transfer_with_params * get balance_transfer example compiling * get concurrent_storage_requests.rs example compiling * get fetch_all_accounts example compiling * get a bunch more of the examples compiling * almost get final example working; type mismatch to look into * wee tweaks * move StorageAddress to separate file * pass Defaultable/Iterable info to StorageAddress in codegen * fix storage validation ne, and partial run through example code * Remove static iteration and strip a generic param from everything * fix doc tests in subxt crate * update test utils and start fixing frame tests * fix frame staking tests * fix the rest of the test compile issues, Borrow on storage values * cargo fmt * remove extra logging during tests * Appease clippy and no more need for into_iter on events * cargo fmt * fix dryRun tests by waiting for blocks * wait for blocks instead of sleeping or other test hacks * cargo fmt * Fix doc links * Traitify StorageAddress * remove out-of-date doc comments * optimise decoding storage a little * cleanup tx stuff, trait for TxPayload, remove Err type param and decode at runtime * clippy fixes * fix doc links * fix doc example * constant address trait for consistency * fix a typo and remove EncodeWithMetadata stuff * Put EventDetails behind a proper interface and allow decoding into top level event, too * fix docs * tweak StorageAddress docs * re-export StorageAddress at root for consistency * fix clippy things * Add support for dynamic values * fix double encoding of storage map key after refactor * clippy fix * Fixes and add a dynamic usage example (needs new scale_value release) * bump scale_value version * cargo fmt * Tweak event bits * cargo fmt * Add a test and bump scale-value to 0.4.0 to support this * remove unnecessary vec from dynamic example * Various typo/grammar fixes Co-authored-by: Alexandru Vasile <60601340+lexnv@users.noreply.github.com> * Address PR nits * Undo accidental rename in changelog * Small PR nits/tidyups * fix tests; codegen change against latest substrate * tweak storage address util names * move error decoding to DecodeError and expose * impl some basic traits on the extrinsic param builder Co-authored-by: Alexandru Vasile <60601340+lexnv@users.noreply.github.com>
This commit is contained in:
+75
-338
@@ -10,164 +10,80 @@
|
||||
//! - [Query constants](https://docs.substrate.io/how-to-guides/v3/basics/configurable-constants/) (Constants)
|
||||
//! - [Subscribe to events](https://docs.substrate.io/v3/runtime/events-and-errors/) (Events)
|
||||
//!
|
||||
//! # Initializing the API client
|
||||
//!
|
||||
//! # Generate the runtime API
|
||||
//! To interact with a node, you'll need to construct a client.
|
||||
//!
|
||||
//! Subxt generates a runtime API from downloaded static metadata. The metadata can be downloaded using the
|
||||
//! ```no_run
|
||||
//! use subxt::{OnlineClient, PolkadotConfig};
|
||||
//!
|
||||
//! # #[tokio::main]
|
||||
//! # async fn main() {
|
||||
//! let api = OnlineClient::<PolkadotConfig>::new().await.unwrap();
|
||||
//! # }
|
||||
//! ```
|
||||
//!
|
||||
//! This default client connects to a locally running node, but can be configured to point anywhere.
|
||||
//! Additionally, an [`crate::OfflineClient`] is available to perform operations that don't require a
|
||||
//! network connection to a node.
|
||||
//!
|
||||
//! The client takes a type parameter, here [`crate::PolkadotConfig`], which bakes in assumptions about
|
||||
//! the structure of extrinsics and the underlying types used by the node for things like block numbers.
|
||||
//! If the node you'd like to interact with deviates from Polkadot or the default Substrate node in these
|
||||
//! areas, you'll need to configure them by implementing the [`crate::config::Config`] type yourself.
|
||||
//!
|
||||
//! # Generating runtime types
|
||||
//!
|
||||
//! Subxt can generate types at compile time to help you interact with a node. The metadata can be downloaded using the
|
||||
//! [subxt-cli](https://crates.io/crates/subxt-cli) tool.
|
||||
//!
|
||||
//! To generate the runtime API, use the `subxt` attribute which points at downloaded static metadata.
|
||||
//! To generate the types, use the `subxt` attribute which points at downloaded static metadata.
|
||||
//!
|
||||
//! ```ignore
|
||||
//! #[subxt::subxt(runtime_metadata_path = "metadata.scale")]
|
||||
//! pub mod node_runtime { }
|
||||
//! ```
|
||||
//!
|
||||
//! The `node_runtime` has the following hierarchy:
|
||||
//! For more information, please visit the [subxt-codegen](https://docs.rs/subxt-codegen/latest/subxt_codegen/)
|
||||
//! documentation.
|
||||
//!
|
||||
//! ```rust
|
||||
//! pub mod node_runtime {
|
||||
//! pub mod PalletName {
|
||||
//! pub mod calls { }
|
||||
//! pub mod storage { }
|
||||
//! pub mod constants { }
|
||||
//! pub mod events { }
|
||||
//! }
|
||||
//! }
|
||||
//! ```
|
||||
//! # Interacting with the API
|
||||
//!
|
||||
//! For more information regarding the `node_runtime` hierarchy, please visit the
|
||||
//! [subxt-codegen](https://docs.rs/subxt-codegen/latest/subxt_codegen/) documentation.
|
||||
//! Once instantiated, a client, exposes four functions:
|
||||
//! - `.tx()` for submitting extrinsics/transactions. See [`crate::tx::TxClient`] for more details, or see
|
||||
//! the [balance_transfer](../examples/examples/balance_transfer.rs) example.
|
||||
//! - `.storage()` for fetching and iterating over storage entries. See [`crate::storage::StorageClient`] for more details, or see
|
||||
//! the [fetch_staking_details](../examples/examples/fetch_staking_details.rs) example.
|
||||
//! - `.constants()` for getting hold of constants. See [`crate::constants::ConstantsClient`] for more details, or see
|
||||
//! the [fetch_constants](../examples/examples/fetch_constants.rs) example.
|
||||
//! - `.events()` for subscribing/obtaining events. See [`crate::events::EventsClient`] for more details, or see:
|
||||
//! - [subscribe_all_events](../examples/examples/subscribe_all_events.rs): Subscribe to events emitted from blocks.
|
||||
//! - [subscribe_one_event](../examples/examples/subscribe_one_event.rs): Subscribe and filter by one event.
|
||||
//! - [subscribe_some_events](../examples/examples/subscribe_some_events.rs): Subscribe and filter event.
|
||||
//!
|
||||
//! # Static Metadata Validation
|
||||
//!
|
||||
//! # Initializing the API client
|
||||
//! If you use types generated by the [`crate::subxt`] macro, there is a chance that they will fall out of sync
|
||||
//! with the actual state of the node you're trying to interact with.
|
||||
//!
|
||||
//! When you attempt to use any of these static types to interact with a node, Subxt will validate that they are
|
||||
//! still compatible and issue an error if they have deviated.
|
||||
//!
|
||||
//! Additionally, you can validate that the entirety of the statically generated code aligns with a node like so:
|
||||
//!
|
||||
//! ```no_run
|
||||
//! use subxt::{ClientBuilder, DefaultConfig, PolkadotExtrinsicParams};
|
||||
//! use subxt::{OnlineClient, PolkadotConfig};
|
||||
//!
|
||||
//! #[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata.scale")]
|
||||
//! pub mod polkadot {}
|
||||
//!
|
||||
//! # #[tokio::main]
|
||||
//! # async fn main() {
|
||||
//! let api = ClientBuilder::new()
|
||||
//! .set_url("wss://rpc.polkadot.io:443")
|
||||
//! .build()
|
||||
//! .await
|
||||
//! .unwrap()
|
||||
//! .to_runtime_api::<polkadot::RuntimeApi<DefaultConfig, PolkadotExtrinsicParams<DefaultConfig>>>();
|
||||
//! # }
|
||||
//! ```
|
||||
//! let api = OnlineClient::<PolkadotConfig>::new().await.unwrap();
|
||||
//!
|
||||
//! The `RuntimeApi` type is generated by the `subxt` macro from the supplied metadata. This can be parameterized with user
|
||||
//! supplied implementations for the `Config` and `Extra` types, if the default implementation differs from the target
|
||||
//! chain.
|
||||
//!
|
||||
//! To ensure that extrinsics are properly submitted, during the build phase of the Client the
|
||||
//! runtime metadata of the node is downloaded. If the URL is not specified (`set_url`), the local host is used instead.
|
||||
//!
|
||||
//!
|
||||
//! # Submit Extrinsics
|
||||
//!
|
||||
//! Extrinsics are obtained using the API's `RuntimeApi::tx()` method, followed by `pallet_name()` and then the
|
||||
//! `call_item_name()`.
|
||||
//!
|
||||
//! Submit an extrinsic, returning success once the transaction is validated and accepted into the pool:
|
||||
//!
|
||||
//! Please visit the [balance_transfer](../examples/examples/balance_transfer.rs) example for more details.
|
||||
//!
|
||||
//!
|
||||
//! # Querying Storage
|
||||
//!
|
||||
//! The runtime storage is queried via the generated `RuntimeApi::storage()` method, followed by the `pallet_name()` and
|
||||
//! then the `storage_item_name()`.
|
||||
//!
|
||||
//! Please visit the [fetch_staking_details](../examples/examples/fetch_staking_details.rs) example for more details.
|
||||
//!
|
||||
//! # Query Constants
|
||||
//!
|
||||
//! Constants are embedded into the node's metadata.
|
||||
//!
|
||||
//! The subxt offers the ability to query constants from the runtime metadata (metadata downloaded when constructing
|
||||
//! the client, *not* the one provided for API generation).
|
||||
//!
|
||||
//! To query constants use the generated `RuntimeApi::constants()` method, followed by the `pallet_name()` and then the
|
||||
//! `constant_item_name()`.
|
||||
//!
|
||||
//! Please visit the [fetch_constants](../examples/examples/fetch_constants.rs) example for more details.
|
||||
//!
|
||||
//! # Subscribe to Events
|
||||
//!
|
||||
//! To subscribe to events, use the generated `RuntimeApi::events()` method which exposes:
|
||||
//! - `subscribe()` - Subscribe to events emitted from blocks. These blocks haven't necessarily been finalised.
|
||||
//! - `subscribe_finalized()` - Subscribe to events from finalized blocks.
|
||||
//! - `at()` - Obtain events at a given block hash.
|
||||
//!
|
||||
//!
|
||||
//! *Examples*
|
||||
//! - [subscribe_all_events](../examples/examples/subscribe_all_events.rs): Subscribe to events emitted from blocks.
|
||||
//! - [subscribe_one_event](../examples/examples/subscribe_one_event.rs): Subscribe and filter by one event.
|
||||
//! - [subscribe_some_events](../examples/examples/subscribe_some_events.rs): Subscribe and filter event.
|
||||
//!
|
||||
//! # Static Metadata Validation
|
||||
//!
|
||||
//! There are two types of metadata that the subxt is aware of:
|
||||
//! - static metadata: Metadata used for generating the API.
|
||||
//! - runtime metadata: Metadata downloaded from the target node when a subxt client is created.
|
||||
//!
|
||||
//! There are cases when the static metadata is different from the runtime metadata of a node.
|
||||
//! Such is the case when the node performs a runtime update.
|
||||
//!
|
||||
//! To ensure that subxt can properly communicate with the target node the static metadata is validated
|
||||
//! against the runtime metadata of the node.
|
||||
//!
|
||||
//! This validation is performed at the Call, Constant, and Storage levels, as well for the entire metadata.
|
||||
//! The level of granularity ensures that the users can still submit a given call, even if another
|
||||
//! call suffered changes.
|
||||
//!
|
||||
//! Full metadata validation:
|
||||
//!
|
||||
//! ```no_run
|
||||
//! # use subxt::{ClientBuilder, DefaultConfig, PolkadotExtrinsicParams};
|
||||
//! # #[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata.scale")]
|
||||
//! # pub mod polkadot {}
|
||||
//! # #[tokio::main]
|
||||
//! # async fn main() {
|
||||
//! # let api = ClientBuilder::new()
|
||||
//! # .build()
|
||||
//! # .await
|
||||
//! # .unwrap()
|
||||
//! # .to_runtime_api::<polkadot::RuntimeApi<DefaultConfig, PolkadotExtrinsicParams<DefaultConfig>>>();
|
||||
//! // To make sure that all of our statically generated pallets are compatible with the
|
||||
//! // runtime node, we can run this check:
|
||||
//! api.validate_metadata().unwrap();
|
||||
//! # }
|
||||
//! ```
|
||||
//!
|
||||
//! Call level validation:
|
||||
//!
|
||||
//! ```ignore
|
||||
//! # use sp_keyring::AccountKeyring;
|
||||
//! # use subxt::{ClientBuilder, DefaultConfig, PolkadotExtrinsicParams};
|
||||
//! # #[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata.scale")]
|
||||
//! # pub mod polkadot {}
|
||||
//! # #[tokio::main]
|
||||
//! # async fn main() {
|
||||
//! # let api = ClientBuilder::new()
|
||||
//! # .build()
|
||||
//! # .await
|
||||
//! # .unwrap()
|
||||
//! # .to_runtime_api::<polkadot::RuntimeApi<DefaultConfig, PolkadotExtrinsicParams<DefaultConfig>>>();
|
||||
//! // Submit the `transfer` extrinsic from Alice's account to Bob's.
|
||||
//! let dest = AccountKeyring::Bob.to_account_id().into();
|
||||
//!
|
||||
//! let extrinsic = api
|
||||
//! .tx()
|
||||
//! .balances()
|
||||
//! // Constructing an extrinsic will fail if the metadata
|
||||
//! // is not in sync with the generated API.
|
||||
//! .transfer(dest, 123_456_789_012_345)
|
||||
//! .unwrap();
|
||||
//! if let Err(_e) = polkadot::validate_codegen(&api) {
|
||||
//! println!("Generated code is not up to date with node we're connected to");
|
||||
//! }
|
||||
//! # }
|
||||
//! ```
|
||||
//!
|
||||
@@ -204,221 +120,42 @@
|
||||
)]
|
||||
#![allow(clippy::type_complexity)]
|
||||
|
||||
pub use frame_metadata::StorageHasher;
|
||||
pub use subxt_macro::subxt;
|
||||
|
||||
pub use bitvec;
|
||||
pub use codec;
|
||||
pub use sp_core;
|
||||
pub use sp_runtime;
|
||||
|
||||
use codec::{
|
||||
Decode,
|
||||
DecodeAll,
|
||||
Encode,
|
||||
};
|
||||
use core::fmt::Debug;
|
||||
use derivative::Derivative;
|
||||
|
||||
mod client;
|
||||
mod config;
|
||||
mod error;
|
||||
pub mod client;
|
||||
pub mod config;
|
||||
pub mod constants;
|
||||
pub mod dynamic;
|
||||
pub mod error;
|
||||
pub mod events;
|
||||
pub mod extrinsic;
|
||||
mod metadata;
|
||||
pub mod metadata;
|
||||
pub mod rpc;
|
||||
pub mod storage;
|
||||
mod transaction;
|
||||
pub mod updates;
|
||||
pub mod tx;
|
||||
pub mod utils;
|
||||
|
||||
// Expose a few of the most common types at root,
|
||||
// but leave most types behind their respoctive modules.
|
||||
pub use crate::{
|
||||
client::{
|
||||
Client,
|
||||
ClientBuilder,
|
||||
SubmittableExtrinsic,
|
||||
OfflineClient,
|
||||
OnlineClient,
|
||||
},
|
||||
config::{
|
||||
Config,
|
||||
DefaultConfig,
|
||||
},
|
||||
error::{
|
||||
BasicError,
|
||||
Error,
|
||||
GenericError,
|
||||
HasModuleError,
|
||||
ModuleError,
|
||||
ModuleErrorData,
|
||||
RuntimeError,
|
||||
TransactionError,
|
||||
},
|
||||
events::{
|
||||
EventDetails,
|
||||
Events,
|
||||
RawEventDetails,
|
||||
},
|
||||
extrinsic::{
|
||||
PairSigner,
|
||||
PolkadotExtrinsicParams,
|
||||
PolkadotExtrinsicParamsBuilder,
|
||||
SubstrateExtrinsicParams,
|
||||
SubstrateExtrinsicParamsBuilder,
|
||||
},
|
||||
metadata::{
|
||||
ErrorMetadata,
|
||||
Metadata,
|
||||
MetadataError,
|
||||
PalletMetadata,
|
||||
},
|
||||
rpc::{
|
||||
BlockNumber,
|
||||
ReadProof,
|
||||
RpcClient,
|
||||
SystemProperties,
|
||||
},
|
||||
storage::{
|
||||
KeyIter,
|
||||
StorageEntry,
|
||||
StorageEntryKey,
|
||||
StorageMapKey,
|
||||
},
|
||||
transaction::{
|
||||
TransactionEvents,
|
||||
TransactionInBlock,
|
||||
TransactionProgress,
|
||||
TransactionStatus,
|
||||
PolkadotConfig,
|
||||
SubstrateConfig,
|
||||
},
|
||||
error::Error,
|
||||
metadata::Metadata,
|
||||
};
|
||||
|
||||
/// Trait to uniquely identify the call (extrinsic)'s identity from the runtime metadata.
|
||||
///
|
||||
/// Generated API structures that represent each of the different possible
|
||||
/// calls to a node each implement this trait.
|
||||
///
|
||||
/// When encoding an extrinsic, we use this information to know how to map
|
||||
/// the call to the specific pallet and call index needed by a particular node.
|
||||
pub trait Call: Encode {
|
||||
/// Pallet name.
|
||||
const PALLET: &'static str;
|
||||
/// Function name.
|
||||
const FUNCTION: &'static str;
|
||||
|
||||
/// Returns true if the given pallet and function names match this call.
|
||||
fn is_call(pallet: &str, function: &str) -> bool {
|
||||
Self::PALLET == pallet && Self::FUNCTION == function
|
||||
}
|
||||
/// Re-export external crates that are made use of in the subxt API.
|
||||
pub mod ext {
|
||||
pub use bitvec;
|
||||
pub use codec;
|
||||
pub use frame_metadata;
|
||||
pub use scale_value;
|
||||
pub use sp_core;
|
||||
pub use sp_runtime;
|
||||
}
|
||||
|
||||
/// Trait to uniquely identify the events's identity from the runtime metadata.
|
||||
///
|
||||
/// Generated API structures that represent an event implement this trait.
|
||||
///
|
||||
/// The trait is utilized to decode emitted events from a block, via obtaining the
|
||||
/// form of the `Event` from the metadata.
|
||||
pub trait Event: Decode {
|
||||
/// Pallet name.
|
||||
const PALLET: &'static str;
|
||||
/// Event name.
|
||||
const EVENT: &'static str;
|
||||
|
||||
/// Returns true if the given pallet and event names match this event.
|
||||
fn is_event(pallet: &str, event: &str) -> bool {
|
||||
Self::PALLET == pallet && Self::EVENT == event
|
||||
}
|
||||
}
|
||||
|
||||
/// Wraps an already encoded byte vector, prevents being encoded as a raw byte vector as part of
|
||||
/// the transaction payload
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub struct Encoded(pub Vec<u8>);
|
||||
|
||||
impl codec::Encode for Encoded {
|
||||
fn encode(&self) -> Vec<u8> {
|
||||
self.0.to_owned()
|
||||
}
|
||||
}
|
||||
|
||||
/// A phase of a block's execution.
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Decode, Encode)]
|
||||
pub enum Phase {
|
||||
/// Applying an extrinsic.
|
||||
ApplyExtrinsic(u32),
|
||||
/// Finalizing the block.
|
||||
Finalization,
|
||||
/// Initializing the block.
|
||||
Initialization,
|
||||
}
|
||||
|
||||
/// A wrapper for any type `T` which implement encode/decode in a way compatible with `Vec<u8>`.
|
||||
///
|
||||
/// [`WrapperKeepOpaque`] stores the type only in its opaque format, aka as a `Vec<u8>`. To
|
||||
/// access the real type `T` [`Self::try_decode`] needs to be used.
|
||||
#[derive(Derivative, Encode, Decode)]
|
||||
#[derivative(
|
||||
Debug(bound = ""),
|
||||
Clone(bound = ""),
|
||||
PartialEq(bound = ""),
|
||||
Eq(bound = ""),
|
||||
Default(bound = ""),
|
||||
Hash(bound = "")
|
||||
)]
|
||||
pub struct WrapperKeepOpaque<T> {
|
||||
data: Vec<u8>,
|
||||
_phantom: PhantomDataSendSync<T>,
|
||||
}
|
||||
|
||||
impl<T: Decode> WrapperKeepOpaque<T> {
|
||||
/// Try to decode the wrapped type from the inner `data`.
|
||||
///
|
||||
/// Returns `None` if the decoding failed.
|
||||
pub fn try_decode(&self) -> Option<T> {
|
||||
T::decode_all(&mut &self.data[..]).ok()
|
||||
}
|
||||
|
||||
/// Returns the length of the encoded `T`.
|
||||
pub fn encoded_len(&self) -> usize {
|
||||
self.data.len()
|
||||
}
|
||||
|
||||
/// Returns the encoded data.
|
||||
pub fn encoded(&self) -> &[u8] {
|
||||
&self.data
|
||||
}
|
||||
|
||||
/// Create from the given encoded `data`.
|
||||
pub fn from_encoded(data: Vec<u8>) -> Self {
|
||||
Self {
|
||||
data,
|
||||
_phantom: PhantomDataSendSync::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A version of [`std::marker::PhantomData`] that is also Send and Sync (which is fine
|
||||
/// because regardless of the generic param, it is always possible to Send + Sync this
|
||||
/// 0 size type).
|
||||
#[derive(Derivative, Encode, Decode, scale_info::TypeInfo)]
|
||||
#[derivative(
|
||||
Clone(bound = ""),
|
||||
PartialEq(bound = ""),
|
||||
Debug(bound = ""),
|
||||
Eq(bound = ""),
|
||||
Default(bound = ""),
|
||||
Hash(bound = "")
|
||||
)]
|
||||
#[scale_info(skip_type_params(T))]
|
||||
#[doc(hidden)]
|
||||
pub struct PhantomDataSendSync<T>(core::marker::PhantomData<T>);
|
||||
|
||||
impl<T> PhantomDataSendSync<T> {
|
||||
pub(crate) fn new() -> Self {
|
||||
Self(core::marker::PhantomData)
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl<T> Send for PhantomDataSendSync<T> {}
|
||||
unsafe impl<T> Sync for PhantomDataSendSync<T> {}
|
||||
|
||||
/// This represents a key-value collection and is SCALE compatible
|
||||
/// with collections like BTreeMap. This has the same type params
|
||||
/// as `BTreeMap` which allows us to easily swap the two during codegen.
|
||||
pub type KeyedVec<K, V> = Vec<(K, V)>;
|
||||
|
||||
Reference in New Issue
Block a user