mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-22 14:58:04 +00:00
Use RPC call to get account nonce (#476)
* remove code related to getting nonce from storage and use RPC call instead * cargo fmt * move nonce fetching into Rpc, since it's just an RPC call * clippy
This commit is contained in:
+10
-95
@@ -49,10 +49,8 @@ use crate::{
|
||||
use codec::Decode;
|
||||
use frame_metadata::{
|
||||
v14::RuntimeMetadataV14,
|
||||
PalletMetadata,
|
||||
RuntimeMetadata,
|
||||
RuntimeMetadataPrefixed,
|
||||
StorageEntryType,
|
||||
};
|
||||
use heck::ToSnakeCase as _;
|
||||
use proc_macro2::TokenStream as TokenStream2;
|
||||
@@ -61,7 +59,6 @@ use quote::{
|
||||
format_ident,
|
||||
quote,
|
||||
};
|
||||
use scale_info::form::PortableForm;
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
fs,
|
||||
@@ -268,16 +265,6 @@ impl RuntimeGenerator {
|
||||
let has_module_error_impl =
|
||||
errors::generate_has_module_error_impl(&self.metadata, types_mod_ident);
|
||||
|
||||
let default_account_data_ident = format_ident!("DefaultAccountData");
|
||||
let default_account_data_impl = generate_default_account_data_impl(
|
||||
&pallets_with_mod_names,
|
||||
&default_account_data_ident,
|
||||
&type_gen,
|
||||
);
|
||||
let type_parameter_default_impl = default_account_data_impl
|
||||
.as_ref()
|
||||
.map(|_| quote!( = #default_account_data_ident ));
|
||||
|
||||
quote! {
|
||||
#[allow(dead_code, unused_imports, non_camel_case_types)]
|
||||
pub mod #mod_ident {
|
||||
@@ -293,29 +280,25 @@ impl RuntimeGenerator {
|
||||
// Impl HasModuleError on DispatchError so we can pluck out module error details.
|
||||
#has_module_error_impl
|
||||
|
||||
#default_account_data_impl
|
||||
|
||||
pub struct RuntimeApi<T: ::subxt::Config, X, A #type_parameter_default_impl> {
|
||||
pub struct RuntimeApi<T: ::subxt::Config, X> {
|
||||
pub client: ::subxt::Client<T>,
|
||||
marker: ::core::marker::PhantomData<(X, A)>,
|
||||
marker: ::core::marker::PhantomData<X>,
|
||||
}
|
||||
|
||||
impl<T, X, A> ::core::convert::From<::subxt::Client<T>> for RuntimeApi<T, X, A>
|
||||
impl<T, X> ::core::convert::From<::subxt::Client<T>> for RuntimeApi<T, X>
|
||||
where
|
||||
T: ::subxt::Config,
|
||||
X: ::subxt::SignedExtra<T>,
|
||||
A: ::subxt::AccountData,
|
||||
X: ::subxt::SignedExtra<T>
|
||||
{
|
||||
fn from(client: ::subxt::Client<T>) -> Self {
|
||||
Self { client, marker: ::core::marker::PhantomData }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T, X, A> RuntimeApi<T, X, A>
|
||||
impl<'a, T, X> RuntimeApi<T, X>
|
||||
where
|
||||
T: ::subxt::Config,
|
||||
X: ::subxt::SignedExtra<T>,
|
||||
A: ::subxt::AccountData,
|
||||
{
|
||||
pub fn constants(&'a self) -> ConstantsApi {
|
||||
ConstantsApi
|
||||
@@ -325,7 +308,7 @@ impl RuntimeGenerator {
|
||||
StorageApi { client: &self.client }
|
||||
}
|
||||
|
||||
pub fn tx(&'a self) -> TransactionApi<'a, T, X, A> {
|
||||
pub fn tx(&'a self) -> TransactionApi<'a, T, X> {
|
||||
TransactionApi { client: &self.client, marker: ::core::marker::PhantomData }
|
||||
}
|
||||
|
||||
@@ -377,19 +360,18 @@ impl RuntimeGenerator {
|
||||
)*
|
||||
}
|
||||
|
||||
pub struct TransactionApi<'a, T: ::subxt::Config, X, A> {
|
||||
pub struct TransactionApi<'a, T: ::subxt::Config, X> {
|
||||
client: &'a ::subxt::Client<T>,
|
||||
marker: ::core::marker::PhantomData<(X, A)>,
|
||||
marker: ::core::marker::PhantomData<X>,
|
||||
}
|
||||
|
||||
impl<'a, T, X, A> TransactionApi<'a, T, X, A>
|
||||
impl<'a, T, X> TransactionApi<'a, T, X>
|
||||
where
|
||||
T: ::subxt::Config,
|
||||
X: ::subxt::SignedExtra<T>,
|
||||
A: ::subxt::AccountData,
|
||||
{
|
||||
#(
|
||||
pub fn #pallets_with_calls(&self) -> #pallets_with_calls::calls::TransactionApi<'a, T, X, A> {
|
||||
pub fn #pallets_with_calls(&self) -> #pallets_with_calls::calls::TransactionApi<'a, T, X> {
|
||||
#pallets_with_calls::calls::TransactionApi::new(self.client)
|
||||
}
|
||||
)*
|
||||
@@ -399,73 +381,6 @@ impl RuntimeGenerator {
|
||||
}
|
||||
}
|
||||
|
||||
/// Most chains require a valid account nonce as part of the extrinsic, so the default behaviour of
|
||||
/// the client is to fetch the nonce for the current account.
|
||||
///
|
||||
/// The account index (aka nonce) is commonly stored in the `System` pallet's `Account` storage item.
|
||||
/// This function attempts to find that storage item, and if it is present will implement the
|
||||
/// `subxt::AccountData` trait for it. This allows the client to construct the appropriate
|
||||
/// storage key from the account id, and then retrieve the `nonce` from the resulting storage item.
|
||||
fn generate_default_account_data_impl(
|
||||
pallets_with_mod_names: &[(&PalletMetadata<PortableForm>, syn::Ident)],
|
||||
default_impl_name: &syn::Ident,
|
||||
type_gen: &TypeGenerator,
|
||||
) -> Option<TokenStream2> {
|
||||
let storage = pallets_with_mod_names
|
||||
.iter()
|
||||
.find(|(pallet, _)| pallet.name == "System")
|
||||
.and_then(|(pallet, _)| pallet.storage.as_ref())?;
|
||||
let storage_entry = storage
|
||||
.entries
|
||||
.iter()
|
||||
.find(|entry| entry.name == "Account")?;
|
||||
|
||||
// resolve the concrete types for `AccountId` (to build the key) and `Index` to extract the
|
||||
// account index (nonce) value from the result.
|
||||
let (account_id_ty, account_nonce_ty) =
|
||||
if let StorageEntryType::Map { key, value, .. } = &storage_entry.ty {
|
||||
let account_id_ty = type_gen.resolve_type_path(key.id(), &[]);
|
||||
let account_data_ty = type_gen.resolve_type(value.id());
|
||||
let nonce_field = if let scale_info::TypeDef::Composite(composite) =
|
||||
account_data_ty.type_def()
|
||||
{
|
||||
composite
|
||||
.fields()
|
||||
.iter()
|
||||
.find(|f| f.name() == Some(&"nonce".to_string()))?
|
||||
} else {
|
||||
abort_call_site!("Expected a `nonce` field in the account info struct")
|
||||
};
|
||||
let account_nonce_ty = type_gen.resolve_type_path(nonce_field.ty().id(), &[]);
|
||||
(account_id_ty, account_nonce_ty)
|
||||
} else {
|
||||
abort_call_site!("System::Account should be a `StorageEntryType::Map`")
|
||||
};
|
||||
|
||||
// this path to the storage entry depends on storage codegen.
|
||||
// AccountOwned contains the same data as Account does, but without references.
|
||||
let storage_entry_path = quote!(self::system::storage::AccountOwned);
|
||||
|
||||
Some(quote! {
|
||||
/// The default storage entry from which to fetch an account nonce, required for
|
||||
/// constructing a transaction.
|
||||
pub enum #default_impl_name {}
|
||||
|
||||
impl ::subxt::AccountData for #default_impl_name {
|
||||
type StorageEntry = #storage_entry_path;
|
||||
type AccountId = #account_id_ty;
|
||||
type Index = #account_nonce_ty;
|
||||
|
||||
fn storage_entry(account_id: Self::AccountId) -> Self::StorageEntry {
|
||||
#storage_entry_path(account_id)
|
||||
}
|
||||
fn nonce(result: &<Self::StorageEntry as ::subxt::StorageEntry>::Value) -> Self::Index {
|
||||
result.nonce
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn generate_structs_from_variants<'a, F>(
|
||||
type_gen: &'a TypeGenerator,
|
||||
type_id: u32,
|
||||
|
||||
Reference in New Issue
Block a user