1use crate::metadata::{DecodeWithMetadata, Metadata};
9use alloc::vec::Vec;
10use scale_decode::DecodeAsType;
11pub use scale_value::{At, Value};
12
13pub type DecodedValue = scale_value::Value<u32>;
18
19pub use crate::tx::payload::dynamic as tx;
21
22pub use crate::constants::address::dynamic as constant;
24
25pub use crate::storage::address::dynamic as storage;
27
28pub use crate::runtime_api::payload::dynamic as runtime_api_call;
30
31pub use crate::view_functions::payload::dynamic as view_function_call;
33
34pub struct DecodedValueThunk {
38 type_id: u32,
39 metadata: Metadata,
40 scale_bytes: Vec<u8>,
41}
42
43impl DecodeWithMetadata for DecodedValueThunk {
44 fn decode_with_metadata(
45 bytes: &mut &[u8],
46 type_id: u32,
47 metadata: &Metadata,
48 ) -> Result<Self, scale_decode::Error> {
49 let mut v = Vec::with_capacity(bytes.len());
50 v.extend_from_slice(bytes);
51 *bytes = &[];
52 Ok(DecodedValueThunk {
53 type_id,
54 metadata: metadata.clone(),
55 scale_bytes: v,
56 })
57 }
58}
59
60impl DecodedValueThunk {
61 pub fn into_encoded(self) -> Vec<u8> {
63 self.scale_bytes
64 }
65 pub fn encoded(&self) -> &[u8] {
67 &self.scale_bytes
68 }
69 pub fn to_value(&self) -> Result<DecodedValue, scale_decode::Error> {
71 let val = scale_value::scale::decode_as_type(
72 &mut &*self.scale_bytes,
73 self.type_id,
74 self.metadata.types(),
75 )?;
76 Ok(val)
77 }
78 pub fn as_type<T: DecodeAsType>(&self) -> Result<T, scale_decode::Error> {
80 T::decode_as_type(
81 &mut &self.scale_bytes[..],
82 self.type_id,
83 self.metadata.types(),
84 )
85 }
86}