Merge branch 'master' into tadeohepperle/pull-in-sp-arithmetic-for-perbill-permill-etc

This commit is contained in:
Tadeo hepperle
2023-10-05 15:50:11 +02:00
14 changed files with 128 additions and 58 deletions
-3
View File
@@ -2,9 +2,6 @@ use subxt::dynamic::Value;
use subxt::{config::PolkadotConfig, OnlineClient};
use subxt_signer::sr25519::dev;
#[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata_small.scale")]
pub mod polkadot {}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a client to use:
+37 -1
View File
@@ -7,12 +7,13 @@ use crate::{
blocks::{extrinsic_types::ExtrinsicPartTypeIds, Extrinsics},
client::{OfflineClientT, OnlineClientT},
config::{Config, Header},
error::{BlockError, Error},
error::{BlockError, DecodeError, Error},
events,
runtime_api::RuntimeApi,
storage::Storage,
};
use codec::{Decode, Encode};
use futures::lock::Mutex as AsyncMutex;
use std::sync::Arc;
@@ -102,6 +103,11 @@ where
pub async fn runtime_api(&self) -> Result<RuntimeApi<T, C>, Error> {
Ok(RuntimeApi::new(self.client.clone(), self.block_ref.clone()))
}
/// Get the account nonce for a given account ID at this block.
pub async fn account_nonce(&self, account_id: &T::AccountId) -> Result<u64, Error> {
get_account_nonce(&self.client, account_id, self.hash()).await
}
}
// Return Events from the cache, or fetch from the node if needed.
@@ -129,3 +135,33 @@ where
Ok(events)
}
// Return the account nonce at some block hash for an account ID.
pub(crate) async fn get_account_nonce<C, T>(
client: &C,
account_id: &T::AccountId,
block_hash: T::Hash,
) -> Result<u64, Error>
where
C: OnlineClientT<T>,
T: Config,
{
let account_nonce_bytes = client
.backend()
.call(
"AccountNonceApi_account_nonce",
Some(&account_id.encode()),
block_hash,
)
.await?;
// custom decoding from a u16/u32/u64 into a u64, based on the number of bytes we got back.
let cursor = &mut &account_nonce_bytes[..];
let account_nonce: u64 = match account_nonce_bytes.len() {
2 => u16::decode(cursor)?.into(),
4 => u32::decode(cursor)?.into(),
8 => u64::decode(cursor)?,
_ => return Err(Error::Decode(DecodeError::custom_string(format!("state call AccountNonceApi_account_nonce returned an unexpected number of bytes: {} (expected 2, 4 or 8)", account_nonce_bytes.len()))))
};
Ok(account_nonce)
}
+3
View File
@@ -14,3 +14,6 @@ pub use crate::backend::BlockRef;
pub use block_types::Block;
pub use blocks_client::BlocksClient;
pub use extrinsic_types::{ExtrinsicDetails, ExtrinsicEvents, Extrinsics, StaticExtrinsic};
// We get account nonce info in tx_client, too, so re-use the logic:
pub(crate) use block_types::get_account_nonce;
+6 -6
View File
@@ -42,13 +42,13 @@ pub enum Error {
#[error("Serde json error: {0}")]
Serialization(#[from] serde_json::error::Error),
/// Error working with metadata.
#[error("Metadata: {0}")]
#[error("Metadata error: {0}")]
Metadata(#[from] MetadataError),
/// Error decoding metadata.
#[error("Metadata: {0}")]
#[error("Metadata Decoding error: {0}")]
MetadataDecoding(#[from] MetadataTryFromError),
/// Runtime error.
#[error("Runtime error: {0:?}")]
#[error("Runtime error: {0}")]
Runtime(#[from] DispatchError),
/// Error decoding to a [`crate::dynamic::Value`].
#[error("Error decoding into dynamic value: {0}")]
@@ -60,7 +60,7 @@ pub enum Error {
#[error("Transaction error: {0}")]
Transaction(#[from] TransactionError),
/// Error constructing the appropriate extrinsic params.
#[error("{0}")]
#[error("Extrinsic params error: {0}")]
ExtrinsicParams(#[from] ExtrinsicParamsError),
/// Block related error.
#[error("Block error: {0}")]
@@ -73,7 +73,7 @@ pub enum Error {
Unknown(Vec<u8>),
/// Light client error.
#[cfg(feature = "unstable-light-client")]
#[error("An error occurred but it could not be decoded: {0:?}")]
#[error("An error occurred but it could not be decoded: {0}")]
LightClient(#[from] LightClientError),
/// Other error.
#[error("Other error: {0}")]
@@ -110,7 +110,7 @@ pub enum RpcError {
ClientError(Box<dyn std::error::Error + Send + Sync + 'static>),
/// This error signals that the request was rejected for some reason.
/// The specific reason is provided.
#[error("RPC error: request rejected")]
#[error("RPC error: request rejected: {0}")]
RequestRejected(String),
/// The RPC subscription dropped.
#[error("RPC error: subscription dropped.")]
+1 -20
View File
@@ -4,7 +4,6 @@
use std::borrow::Cow;
use crate::error::DecodeError;
use crate::{
backend::{BackendExt, BlockRef, TransactionStatus},
client::{OfflineClientT, OnlineClientT},
@@ -170,25 +169,7 @@ where
/// Get the account nonce for a given account ID.
pub async fn account_nonce(&self, account_id: &T::AccountId) -> Result<u64, Error> {
let block_ref = self.client.backend().latest_finalized_block_ref().await?;
let account_nonce_bytes = self
.client
.backend()
.call(
"AccountNonceApi_account_nonce",
Some(&account_id.encode()),
block_ref.hash(),
)
.await?;
// custom decoding from a u16/u32/u64 into a u64, based on the number of bytes we got back.
let cursor = &mut &account_nonce_bytes[..];
let account_nonce: u64 = match account_nonce_bytes.len() {
2 => u16::decode(cursor)?.into(),
4 => u32::decode(cursor)?.into(),
8 => u64::decode(cursor)?,
_ => return Err(Error::Decode(DecodeError::custom_string(format!("state call AccountNonceApi_account_nonce returned an unexpected number of bytes: {} (expected 2, 4 or 8)", account_nonce_bytes.len()))))
};
Ok(account_nonce)
crate::blocks::get_account_nonce(&self.client, account_id, block_ref.hash()).await
}
/// Creates a partial signed extrinsic, without submitting it.