WIP starting to update subxt from subxt-core changes

This commit is contained in:
James Wilson
2025-10-03 10:39:12 +01:00
parent e7d41c13a2
commit 40952df6cb
13 changed files with 37 additions and 54 deletions
+7 -17
View File
@@ -11,8 +11,7 @@ use crate::{
use alloc::sync::Arc; use alloc::sync::Arc;
use alloc::vec::Vec; use alloc::vec::Vec;
use frame_decode::extrinsics::Extrinsic; use frame_decode::extrinsics::Extrinsic;
use scale_decode::DecodeAsType; use scale_decode::{DecodeAsFields, DecodeAsType};
use subxt_metadata::PalletMetadata;
pub use crate::blocks::StaticExtrinsic; pub use crate::blocks::StaticExtrinsic;
@@ -278,7 +277,7 @@ where
/// Decode and provide the extrinsic fields back in the form of a [`scale_value::Composite`] /// Decode and provide the extrinsic fields back in the form of a [`scale_value::Composite`]
/// type which represents the named or unnamed fields that were present in the extrinsic. /// type which represents the named or unnamed fields that were present in the extrinsic.
pub fn field_values(&self) -> Result<scale_value::Composite<u32>, ExtrinsicError> { pub fn decode_as_fields<E: DecodeAsFields>(&self) -> Result<E, ExtrinsicError> {
let bytes = &mut self.field_bytes(); let bytes = &mut self.field_bytes();
let mut fields = self.decoded_info().call_data().map(|d| { let mut fields = self.decoded_info().call_data().map(|d| {
let name = if d.name().is_empty() { let name = if d.name().is_empty() {
@@ -288,12 +287,11 @@ where
}; };
scale_decode::Field::new(*d.ty(), name) scale_decode::Field::new(*d.ty(), name)
}); });
let decoded = let decoded = E::decode_as_fields(bytes, &mut fields, self.metadata.types())
scale_value::scale::decode_as_fields(bytes, &mut fields, self.metadata.types()) .map_err(|e| ExtrinsicError::CannotDecodeFields {
.map_err(|e| ExtrinsicError::CannotDecodeFields { extrinsic_index: self.index as usize,
extrinsic_index: self.index as usize, error: e.into()
error: e.into() })?;
})?;
Ok(decoded) Ok(decoded)
} }
@@ -353,14 +351,6 @@ pub struct FoundExtrinsic<T: Config, E> {
pub value: E, pub value: E,
} }
/// Details for the given extrinsic plucked from the metadata.
pub struct ExtrinsicMetadataDetails<'a> {
/// Metadata for the pallet that the extrinsic belongs to.
pub pallet: PalletMetadata<'a>,
/// Metadata for the variant which describes the pallet extrinsics.
pub variant: &'a scale_info::Variant<scale_info::form::PortableForm>,
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
+1 -1
View File
@@ -76,7 +76,7 @@ pub use crate::error::ExtrinsicError;
pub use extrinsic_transaction_extensions::{ pub use extrinsic_transaction_extensions::{
ExtrinsicTransactionExtension, ExtrinsicTransactionExtensions, ExtrinsicTransactionExtension, ExtrinsicTransactionExtensions,
}; };
pub use extrinsics::{ExtrinsicDetails, ExtrinsicMetadataDetails, Extrinsics, FoundExtrinsic}; pub use extrinsics::{ExtrinsicDetails, Extrinsics, FoundExtrinsic};
pub use static_extrinsic::StaticExtrinsic; pub use static_extrinsic::StaticExtrinsic;
/// Instantiate a new [`Extrinsics`] object, given a vector containing each extrinsic hash (in the /// Instantiate a new [`Extrinsics`] object, given a vector containing each extrinsic hash (in the
+11 -17
View File
@@ -11,13 +11,12 @@ use crate::{
}; };
use derive_where::derive_where; use derive_where::derive_where;
use scale_decode::DecodeAsType; use scale_decode::{DecodeAsFields, DecodeAsType};
use subxt_core::blocks::{ExtrinsicDetails as CoreExtrinsicDetails, Extrinsics as CoreExtrinsics}; use subxt_core::blocks::{ExtrinsicDetails as CoreExtrinsicDetails, Extrinsics as CoreExtrinsics};
// Re-export anything that's directly returned/used in the APIs below. // Re-export anything that's directly returned/used in the APIs below.
pub use subxt_core::blocks::{ pub use subxt_core::blocks::{
ExtrinsicMetadataDetails, ExtrinsicTransactionExtension, ExtrinsicParams, ExtrinsicTransactionExtension, ExtrinsicTransactionExtensions, StaticExtrinsic,
StaticExtrinsic,
}; };
/// The body of a block. /// The body of a block.
@@ -198,7 +197,7 @@ where
} }
/// See [`subxt_core::blocks::ExtrinsicDetails::transaction_extensions()`]. /// See [`subxt_core::blocks::ExtrinsicDetails::transaction_extensions()`].
pub fn transaction_extensions(&self) -> Option<ExtrinsicExtrinsicParams<'_, T>> { pub fn transaction_extensions(&self) -> Option<ExtrinsicTransactionExtensions<'_, T>> {
self.inner.transaction_extensions() self.inner.transaction_extensions()
} }
@@ -213,23 +212,18 @@ where
} }
/// See [`subxt_core::blocks::ExtrinsicDetails::pallet_name()`]. /// See [`subxt_core::blocks::ExtrinsicDetails::pallet_name()`].
pub fn pallet_name(&self) -> Result<&str, Error> { pub fn pallet_name(&self) -> &str {
self.inner.pallet_name().map_err(Into::into) self.inner.pallet_name()
} }
/// See [`subxt_core::blocks::ExtrinsicDetails::variant_name()`]. /// See [`subxt_core::blocks::ExtrinsicDetails::call_name()`].
pub fn variant_name(&self) -> Result<&str, Error> { pub fn call_name(&self) -> &str {
self.inner.variant_name().map_err(Into::into) self.inner.call_name()
} }
/// See [`subxt_core::blocks::ExtrinsicDetails::extrinsic_metadata()`]. /// See [`subxt_core::blocks::ExtrinsicDetails::decode_as_fields()`].
pub fn extrinsic_metadata(&self) -> Result<ExtrinsicMetadataDetails<'_>, Error> { pub fn decode_as_fields<E: DecodeAsFields>(&self) -> Result<E, Error> {
self.inner.extrinsic_metadata().map_err(Into::into) self.inner.decode_as_fields().map_err(Into::into)
}
/// See [`subxt_core::blocks::ExtrinsicDetails::field_values()`].
pub fn field_values(&self) -> Result<scale_value::Composite<u32>, Error> {
self.inner.field_values().map_err(Into::into)
} }
/// See [`subxt_core::blocks::ExtrinsicDetails::as_extrinsic()`]. /// See [`subxt_core::blocks::ExtrinsicDetails::as_extrinsic()`].
+1 -1
View File
@@ -15,7 +15,7 @@ pub use block_types::Block;
pub use blocks_client::BlocksClient; pub use blocks_client::BlocksClient;
pub use extrinsic_types::{ pub use extrinsic_types::{
ExtrinsicDetails, ExtrinsicEvents, ExtrinsicTransactionExtension, ExtrinsicDetails, ExtrinsicEvents, ExtrinsicTransactionExtension,
ExtrinsicExtrinsicParams, Extrinsics, FoundExtrinsic, StaticExtrinsic, ExtrinsicTransactionExtensions, Extrinsics, FoundExtrinsic, StaticExtrinsic,
}; };
// We get account nonce info in tx_client, too, so re-use the logic: // We get account nonce info in tx_client, too, so re-use the logic:
+1 -1
View File
@@ -8,5 +8,5 @@ mod constants_client;
pub use constants_client::ConstantsClient; pub use constants_client::ConstantsClient;
pub use subxt_core::constants::address::{ pub use subxt_core::constants::address::{
Address, DefaultAddress, DynamicAddress, StaticAddress, dynamic, Address, DynamicAddress, StaticAddress, dynamic,
}; };
@@ -2,7 +2,7 @@ use crate::client::OfflineClientT;
use crate::{Config, Error}; use crate::{Config, Error};
use derive_where::derive_where; use derive_where::derive_where;
use subxt_core::custom_values::address::{Address, Yes}; use subxt_core::custom_values::address::{Address, Maybe};
/// A client for accessing custom values stored in the metadata. /// A client for accessing custom values stored in the metadata.
#[derive_where(Clone; Client)] #[derive_where(Clone; Client)]
@@ -24,7 +24,7 @@ impl<T, Client> CustomValuesClient<T, Client> {
impl<T: Config, Client: OfflineClientT<T>> CustomValuesClient<T, Client> { impl<T: Config, Client: OfflineClientT<T>> CustomValuesClient<T, Client> {
/// Access a custom value by the address it is registered under. This can be just a [str] to get back a dynamic value, /// Access a custom value by the address it is registered under. This can be just a [str] to get back a dynamic value,
/// or a static address from the generated static interface to get a value of a static type returned. /// or a static address from the generated static interface to get a value of a static type returned.
pub fn at<Addr: Address<IsDecodable = Yes> + ?Sized>( pub fn at<Addr: Address<IsDecodable = Maybe> + ?Sized>(
&self, &self,
address: &Addr, address: &Addr,
) -> Result<Addr::Target, Error> { ) -> Result<Addr::Target, Error> {
@@ -46,7 +46,7 @@ impl<T: Config, Client: OfflineClientT<T>> CustomValuesClient<T, Client> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::custom_values::CustomValuesClient; use crate::custom_values::{self, CustomValuesClient};
use crate::{Metadata, OfflineClient, SubstrateConfig}; use crate::{Metadata, OfflineClient, SubstrateConfig};
use codec::Encode; use codec::Encode;
use scale_decode::DecodeAsType; use scale_decode::DecodeAsType;
@@ -117,10 +117,12 @@ mod tests {
}, },
mock_metadata(), mock_metadata(),
); );
let custom_value_client = CustomValuesClient::new(client); let custom_value_client = CustomValuesClient::new(client);
assert!(custom_value_client.at("No one").is_err()); assert!(custom_value_client.at("No one").is_err());
let person_decoded_value_thunk = custom_value_client.at("Person").unwrap();
let person: Person = person_decoded_value_thunk.as_type().unwrap(); let person_addr = custom_values::dynamic::<Person>("Person");
let person = custom_value_client.at(&person_addr).unwrap();
assert_eq!( assert_eq!(
person, person,
Person { Person {
+1 -1
View File
@@ -7,4 +7,4 @@
mod custom_values_client; mod custom_values_client;
pub use custom_values_client::CustomValuesClient; pub use custom_values_client::CustomValuesClient;
pub use subxt_core::custom_values::address::{Address, StaticAddress, Yes}; pub use subxt_core::custom_values::address::{Address, StaticAddress, DynamicAddress, dynamic};
+1 -1
View File
@@ -5,7 +5,7 @@
//! A representation of the dispatch error; an error returned when //! A representation of the dispatch error; an error returned when
//! something fails in trying to submit/execute a transaction. //! something fails in trying to submit/execute a transaction.
use crate::metadata::{DecodeWithMetadata, Metadata}; use crate::metadata::Metadata;
use core::fmt::Debug; use core::fmt::Debug;
use scale_decode::{DecodeAsType, TypeResolver, visitor::DecodeAsTypeResult}; use scale_decode::{DecodeAsType, TypeResolver, visitor::DecodeAsTypeResult};
+3 -3
View File
@@ -3,7 +3,7 @@ use crate::{
config::{Config, HashFor}, config::{Config, HashFor},
}; };
use derive_where::derive_where; use derive_where::derive_where;
use scale_decode::DecodeAsType; use scale_decode::{DecodeAsFields, DecodeAsType};
use subxt_core::events::{EventDetails as CoreEventDetails, Events as CoreEvents}; use subxt_core::events::{EventDetails as CoreEventDetails, Events as CoreEvents};
pub use subxt_core::events::{EventMetadataDetails, Phase, StaticEvent}; pub use subxt_core::events::{EventMetadataDetails, Phase, StaticEvent};
@@ -138,8 +138,8 @@ impl<T: Config> EventDetails<T> {
/// Decode and provide the event fields back in the form of a [`scale_value::Composite`] /// Decode and provide the event fields back in the form of a [`scale_value::Composite`]
/// type which represents the named or unnamed fields that were present in the event. /// type which represents the named or unnamed fields that were present in the event.
pub fn field_values(&self) -> Result<scale_value::Composite<u32>, Error> { pub fn decode_fields_as<E: DecodeAsFields>(&self) -> Result<E, Error> {
self.inner.field_values().map_err(Into::into) self.inner.decode_fields_as().map_err(Into::into)
} }
/// Attempt to decode these [`EventDetails`] into a type representing the event fields. /// Attempt to decode these [`EventDetails`] into a type representing the event fields.
+2 -4
View File
@@ -68,15 +68,13 @@ pub mod config {
/// Types representing the metadata obtained from a node. /// Types representing the metadata obtained from a node.
pub mod metadata { pub mod metadata {
pub use subxt_core::metadata::{DecodeWithMetadata, Metadata}; pub use subxt_metadata::*;
// Expose metadata types under a sub module in case somebody needs to reference them:
pub use subxt_metadata as types;
} }
/// Submit dynamic transactions. /// Submit dynamic transactions.
pub mod dynamic { pub mod dynamic {
pub use subxt_core::dynamic::{ pub use subxt_core::dynamic::{
At, DecodedValue, DecodedValueThunk, Value, constant, runtime_api_call, tx, storage, At, DecodedValue, Value, constant, runtime_api_call, tx, storage,
view_function_call, view_function_call,
}; };
} }
+1 -1
View File
@@ -10,5 +10,5 @@ mod runtime_types;
pub use runtime_client::RuntimeApiClient; pub use runtime_client::RuntimeApiClient;
pub use runtime_types::RuntimeApi; pub use runtime_types::RuntimeApi;
pub use subxt_core::runtime_api::payload::{ pub use subxt_core::runtime_api::payload::{
DefaultPayload, DynamicPayload, Payload, StaticPayload, dynamic, DynamicPayload, Payload, StaticPayload, dynamic,
}; };
-1
View File
@@ -7,7 +7,6 @@ use crate::{
client::{OfflineClientT, OnlineClientT}, client::{OfflineClientT, OnlineClientT},
config::{Config, HashFor}, config::{Config, HashFor},
error::{Error, MetadataError, StorageError}, error::{Error, MetadataError, StorageError},
metadata::DecodeWithMetadata,
storage::storage_value::StorageValue, storage::storage_value::StorageValue,
}; };
use codec::Decode; use codec::Decode;
+1 -1
View File
@@ -8,7 +8,7 @@ mod view_function_types;
mod view_functions_client; mod view_functions_client;
pub use subxt_core::view_functions::payload::{ pub use subxt_core::view_functions::payload::{
DefaultPayload, DynamicPayload, Payload, StaticPayload, dynamic, DynamicPayload, Payload, StaticPayload, dynamic,
}; };
pub use view_function_types::ViewFunctionsApi; pub use view_function_types::ViewFunctionsApi;
pub use view_functions_client::ViewFunctionsClient; pub use view_functions_client::ViewFunctionsClient;