mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 11:41:04 +00:00
start porting dynamic value stuff
This commit is contained in:
+4
-4
@@ -31,16 +31,16 @@ frame-metadata = { workspace = true, default-features = false }
|
||||
subxt-metadata = { workspace = true, default-features = false }
|
||||
derivative = { workspace = true, features = ["use_core"] }
|
||||
derive_more = { workspace = true }
|
||||
hex = { workspace = true }
|
||||
serde = { workspace = true, features = ["derive"] }
|
||||
serde_json = { workspace = true, features = ["raw_value"] }
|
||||
hex = { workspace = true, default-features = false, features = ["alloc"] }
|
||||
serde = { workspace = true, default-features = false, features = ["derive"] }
|
||||
serde_json = { workspace = true, default-features = false, features = ["raw_value", "alloc"] }
|
||||
|
||||
# For ss58 encoding AccountId32 to serialize them properly:
|
||||
base58 = { workspace = true }
|
||||
blake2 = { workspace = true }
|
||||
|
||||
# Provides some deserialization, types like U256/H256 and hashing impls like twox/blake256:
|
||||
impl-serde = { workspace = true }
|
||||
impl-serde = { workspace = true, default-features = false }
|
||||
primitive-types = { workspace = true, default-features = false, features = ["codec", "serde_no_std", "scale-info"] }
|
||||
sp-core-hashing = { workspace = true }
|
||||
|
||||
|
||||
+66
-65
@@ -1,20 +1,21 @@
|
||||
// // Copyright 2019-2023 Parity Technologies (UK) Ltd.
|
||||
// // This file is dual-licensed as Apache-2.0 or GPL-3.0.
|
||||
// // see LICENSE for license details.
|
||||
// Copyright 2019-2023 Parity Technologies (UK) Ltd.
|
||||
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
|
||||
// see LICENSE for license details.
|
||||
|
||||
// //! This module provides the entry points to create dynamic
|
||||
// //! transactions, storage and constant lookups.
|
||||
//! This module provides the entry points to create dynamic
|
||||
//! transactions, storage and constant lookups.
|
||||
|
||||
// use crate::metadata::{DecodeWithMetadata, Metadata};
|
||||
// use scale_decode::DecodeAsType;
|
||||
use crate::metadata::{DecodeWithMetadata, Metadata};
|
||||
use crate::prelude::*;
|
||||
use scale_decode::DecodeAsType;
|
||||
pub use scale_value::{At, Value};
|
||||
use vec::Vec;
|
||||
|
||||
// pub use scale_value::{At, Value};
|
||||
|
||||
// /// A [`scale_value::Value`] type endowed with contextual information
|
||||
// /// regarding what type was used to decode each part of it. This implements
|
||||
// /// [`crate::metadata::DecodeWithMetadata`], and is used as a return type
|
||||
// /// for dynamic requests.
|
||||
// pub type DecodedValue = scale_value::Value<scale_value::scale::TypeId>;
|
||||
/// A [`scale_value::Value`] type endowed with contextual information
|
||||
/// regarding what type was used to decode each part of it. This implements
|
||||
/// [`subxt_core::metadata::DecodeWithMetadata`], and is used as a return type
|
||||
/// for dynamic requests.
|
||||
pub type DecodedValue = scale_value::Value<scale_value::scale::TypeId>;
|
||||
|
||||
// // Submit dynamic transactions.
|
||||
// pub use crate::tx::dynamic as tx;
|
||||
@@ -28,56 +29,56 @@
|
||||
// // Execute runtime API function call dynamically.
|
||||
// pub use crate::runtime_api::dynamic as runtime_api_call;
|
||||
|
||||
// /// This is the result of making a dynamic request to a node. From this,
|
||||
// /// we can return the raw SCALE bytes that we were handed back, or we can
|
||||
// /// complete the decoding of the bytes into a [`DecodedValue`] type.
|
||||
// pub struct DecodedValueThunk {
|
||||
// type_id: u32,
|
||||
// metadata: Metadata,
|
||||
// scale_bytes: Vec<u8>,
|
||||
// }
|
||||
/// This is the result of making a dynamic request to a node. From this,
|
||||
/// we can return the raw SCALE bytes that we were handed back, or we can
|
||||
/// complete the decoding of the bytes into a [`DecodedValue`] type.
|
||||
pub struct DecodedValueThunk {
|
||||
type_id: u32,
|
||||
metadata: Metadata,
|
||||
scale_bytes: Vec<u8>,
|
||||
}
|
||||
|
||||
// impl DecodeWithMetadata for DecodedValueThunk {
|
||||
// fn decode_with_metadata(
|
||||
// bytes: &mut &[u8],
|
||||
// type_id: u32,
|
||||
// metadata: &Metadata,
|
||||
// ) -> Result<Self, Error> {
|
||||
// let mut v = Vec::with_capacity(bytes.len());
|
||||
// v.extend_from_slice(bytes);
|
||||
// *bytes = &[];
|
||||
// Ok(DecodedValueThunk {
|
||||
// type_id,
|
||||
// metadata: metadata.clone(),
|
||||
// scale_bytes: v,
|
||||
// })
|
||||
// }
|
||||
// }
|
||||
impl DecodeWithMetadata for DecodedValueThunk {
|
||||
fn decode_with_metadata(
|
||||
bytes: &mut &[u8],
|
||||
type_id: u32,
|
||||
metadata: &Metadata,
|
||||
) -> Result<Self, scale_decode::Error> {
|
||||
let mut v = Vec::with_capacity(bytes.len());
|
||||
v.extend_from_slice(bytes);
|
||||
*bytes = &[];
|
||||
Ok(DecodedValueThunk {
|
||||
type_id,
|
||||
metadata: metadata.clone(),
|
||||
scale_bytes: v,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// impl DecodedValueThunk {
|
||||
// /// Return the SCALE encoded bytes handed back from the node.
|
||||
// pub fn into_encoded(self) -> Vec<u8> {
|
||||
// self.scale_bytes
|
||||
// }
|
||||
// /// Return the SCALE encoded bytes handed back from the node without taking ownership of them.
|
||||
// pub fn encoded(&self) -> &[u8] {
|
||||
// &self.scale_bytes
|
||||
// }
|
||||
// /// Decode the SCALE encoded storage entry into a dynamic [`DecodedValue`] type.
|
||||
// pub fn to_value(&self) -> Result<DecodedValue, Error> {
|
||||
// let val = DecodedValue::decode_as_type(
|
||||
// &mut &*self.scale_bytes,
|
||||
// self.type_id,
|
||||
// self.metadata.types(),
|
||||
// )?;
|
||||
// Ok(val)
|
||||
// }
|
||||
// /// decode the `DecodedValueThunk` into a concrete type.
|
||||
// pub fn as_type<T: DecodeAsType>(&self) -> Result<T, scale_decode::Error> {
|
||||
// T::decode_as_type(
|
||||
// &mut &self.scale_bytes[..],
|
||||
// self.type_id,
|
||||
// self.metadata.types(),
|
||||
// )
|
||||
// }
|
||||
// }
|
||||
impl DecodedValueThunk {
|
||||
/// Return the SCALE encoded bytes handed back from the node.
|
||||
pub fn into_encoded(self) -> Vec<u8> {
|
||||
self.scale_bytes
|
||||
}
|
||||
/// Return the SCALE encoded bytes handed back from the node without taking ownership of them.
|
||||
pub fn encoded(&self) -> &[u8] {
|
||||
&self.scale_bytes
|
||||
}
|
||||
/// Decode the SCALE encoded storage entry into a dynamic [`DecodedValue`] type.
|
||||
pub fn to_value(&self) -> Result<DecodedValue, scale_decode::Error> {
|
||||
let val = DecodedValue::decode_as_type(
|
||||
&mut &*self.scale_bytes,
|
||||
self.type_id,
|
||||
self.metadata.types(),
|
||||
)?;
|
||||
Ok(val)
|
||||
}
|
||||
/// decode the `DecodedValueThunk` into a concrete type.
|
||||
pub fn as_type<T: DecodeAsType>(&self) -> Result<T, scale_decode::Error> {
|
||||
T::decode_as_type(
|
||||
&mut &self.scale_bytes[..],
|
||||
self.type_id,
|
||||
self.metadata.types(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,5 +21,7 @@ pub use config::{
|
||||
PolkadotExtrinsicParams, SubstrateConfig, SubstrateExtrinsicParams,
|
||||
};
|
||||
|
||||
pub use metadata::Metadata;
|
||||
|
||||
#[macro_use]
|
||||
mod macros;
|
||||
|
||||
@@ -22,7 +22,7 @@ impl core::ops::Deref for Metadata {
|
||||
}
|
||||
|
||||
impl Metadata {
|
||||
pub(crate) fn new(md: subxt_metadata::Metadata) -> Self {
|
||||
pub fn new(md: subxt_metadata::Metadata) -> Self {
|
||||
Metadata {
|
||||
inner: Arc::new(md),
|
||||
}
|
||||
@@ -125,3 +125,6 @@ pub enum MetadataError {
|
||||
#[display(fmt = "Custom value with name {_0} not found")]
|
||||
CustomValueNameNotFound(String),
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl std::error::Error for MetadataError {}
|
||||
|
||||
@@ -8,7 +8,7 @@ mod decode_encode_traits;
|
||||
mod metadata_type;
|
||||
|
||||
pub use decode_encode_traits::{DecodeWithMetadata, EncodeWithMetadata};
|
||||
pub use metadata_type::Metadata;
|
||||
pub use metadata_type::{Metadata, MetadataError};
|
||||
|
||||
// Expose metadata types under a sub module in case somebody needs to reference them:
|
||||
pub use subxt_metadata as types;
|
||||
|
||||
Reference in New Issue
Block a user