Skip to content
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

Provide get_account_nonce method #508

Merged
merged 5 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion examples/examples/get_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use sp_keyring::AccountKeyring;
use substrate_api_client::{
ac_primitives::{ExtrinsicSigner, PlainTipExtrinsicParams},
rpc::JsonrpseeClient,
Api, GetStorage,
Api, GetAccountInformation, GetStorage,
};

type IndexFor<T> = <T as frame_system::Config>::Index;
Expand Down Expand Up @@ -61,6 +61,11 @@ async fn main() {
api.set_signer(ExtrinsicSigner::<_, Signature, Runtime>::new(signer));
println!("[+] Alice's Account Nonce is {}", api.get_nonce().unwrap());

println!(
"[+] Bob's Account Nonce is {}",
api.get_account_nonce(&AccountKeyring::Bob.to_account_id()).unwrap()
);

// Get an vector of storage keys, numbering up to the given max keys and that start with the (optionally) given storage key prefix.
let storage_key_prefix = api.get_storage_map_key_prefix("System", "Account").unwrap();
let max_keys = 3;
Expand Down
2 changes: 1 addition & 1 deletion primitives/src/pallet_traits/frame_system_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use sp_runtime::traits::{
SimpleBitOps, StaticLookup,
};

/// Simplifed Frame system Config trait. Needed because substrate pallets compile to wasm
/// Simplified Frame system Config trait. Needed because substrate pallets compile to wasm
/// in no_std mode.
pub trait FrameSystemConfig {
type BaseCallFilter;
Expand Down
2 changes: 1 addition & 1 deletion primitives/src/pallet_traits/pallet_assets_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use scale_info::TypeInfo;
use serde::{de::DeserializeOwned, Serialize};
use sp_runtime::traits::{AtLeast32BitUnsigned, Get, Member};

/// Simplifed pallet assets Config trait. Needed because substrate pallets compile to wasm
/// Simplified pallet assets Config trait. Needed because substrate pallets compile to wasm
/// in no_std mode.
pub trait AssetsConfig: crate::FrameSystemConfig {
type RuntimeEvent;
Expand Down
2 changes: 1 addition & 1 deletion primitives/src/pallet_traits/pallet_balances_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use sp_runtime::{
FixedPointOperand,
};

/// Simplifed pallet balances Config trait. Needed because substrate pallets compile to wasm
/// Simplified pallet balances Config trait. Needed because substrate pallets compile to wasm
/// in no_std mode.
pub trait BalancesConfig: crate::FrameSystemConfig {
/// This type enforces the (de)serialization implementation
Expand Down
2 changes: 1 addition & 1 deletion primitives/src/pallet_traits/pallet_contracts_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use crate::FrameSystemConfig;
use sp_runtime::traits::Get;

/// Simplifed pallet contract Config trait. Needed because substrate pallets compile to wasm
/// Simplified pallet contract Config trait. Needed because substrate pallets compile to wasm
/// in no_std mode.
pub trait ContractsConfig: FrameSystemConfig {
type Time;
Expand Down
2 changes: 1 addition & 1 deletion primitives/src/pallet_traits/pallet_staking_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use sp_staking::{EraIndex, SessionIndex};
/// The balance type of this pallet.
pub type BalanceOf<T> = <T as StakingConfig>::CurrencyBalance;

/// Simplifed pallet staking Config trait. Needed because substrate pallets compile to wasm
/// Simplified pallet staking Config trait. Needed because substrate pallets compile to wasm
haerdib marked this conversation as resolved.
Show resolved Hide resolved
/// in no_std mode.
pub trait StakingConfig: FrameSystemConfig {
type Currency;
Expand Down
3 changes: 1 addition & 2 deletions src/api/api_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,7 @@ where
/// Get nonce of self signer account.
pub fn get_nonce(&self) -> Result<Runtime::Index> {
let account = self.signer_account().ok_or(Error::NoSigner)?;
self.get_account_info(account)
.map(|acc_opt| acc_opt.map_or_else(|| 0u32.into(), |acc| acc.nonce))
self.get_account_nonce(account)
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/api/rpc_api/frame_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ pub trait GetAccountInformation<AccountId> {
) -> Result<Option<AccountInfo<Self::Index, Self::AccountData>>>;

fn get_account_data(&self, address: &AccountId) -> Result<Option<Self::AccountData>>;

/// Get nonce of an account.
fn get_account_nonce(&self, account: &AccountId) -> Result<Self::Index>;
}

impl<Signer, Client, Params, Runtime> GetAccountInformation<Runtime::AccountId>
Expand Down Expand Up @@ -64,6 +67,11 @@ where
) -> Result<Option<Runtime::AccountData>> {
self.get_account_info(address).map(|info| info.map(|i| i.data))
}

fn get_account_nonce(&self, account: &Runtime::AccountId) -> Result<Runtime::Index> {
self.get_account_info(account)
.map(|acc_opt| acc_opt.map_or_else(|| 0u32.into(), |acc| acc.nonce))
}
}

/// Helper functions for some common SystemApi function.
Expand Down