Skip to content

Commit

Permalink
allow getting account nonce at arbitrary blocks, too (#1182)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsdw authored Oct 2, 2023
1 parent 958ff56 commit e91d0c7
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 24 deletions.
3 changes: 0 additions & 3 deletions subxt/examples/runtime_apis_dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
38 changes: 37 additions & 1 deletion subxt/src/blocks/block_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 changes: 3 additions & 0 deletions subxt/src/blocks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
21 changes: 1 addition & 20 deletions subxt/src/tx/tx_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use std::borrow::Cow;

use crate::error::DecodeError;
use crate::{
backend::{BackendExt, BlockRef, TransactionStatus},
client::{OfflineClientT, OnlineClientT},
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit e91d0c7

Please sign in to comment.