pub enum DynSolType {
Bool,
Int(usize),
Uint(usize),
FixedBytes(usize),
Address,
Function,
Bytes,
String,
Array(Box<DynSolType>),
FixedArray(Box<DynSolType>, usize),
Tuple(Vec<DynSolType>),
CustomStruct {
name: String,
prop_names: Vec<String>,
tuple: Vec<DynSolType>,
},
}
Expand description
A dynamic Solidity type.
Equivalent to an enum wrapper around all implementers of SolType
.
This is used to represent Solidity types that are not known at compile time.
It is used in conjunction with DynToken
and DynSolValue
to allow for
dynamic ABI encoding and decoding.
§Examples
Parsing Solidity type strings:
use alloy_dyn_abi::DynSolType;
let type_name = "(bool,address)[]";
let ty = DynSolType::parse(type_name)?;
assert_eq!(
ty,
DynSolType::Array(Box::new(DynSolType::Tuple(
vec![DynSolType::Bool, DynSolType::Address,]
)))
);
assert_eq!(ty.sol_type_name(), type_name);
// alternatively, you can use the FromStr impl
let ty2 = type_name.parse::<DynSolType>()?;
assert_eq!(ty, ty2);
Decoding dynamic types:
use alloy_dyn_abi::{DynSolType, DynSolValue};
use alloy_primitives::U256;
let my_type = DynSolType::Uint(256);
let my_data: DynSolValue = U256::from(183u64).into();
let encoded = my_data.abi_encode();
let decoded = my_type.abi_decode(&encoded)?;
assert_eq!(decoded, my_data);
let my_type = DynSolType::Array(Box::new(my_type));
let my_data = DynSolValue::Array(vec![my_data.clone()]);
let encoded = my_data.abi_encode();
let decoded = my_type.abi_decode(&encoded)?;
assert_eq!(decoded, my_data);
Variants§
Bool
Boolean.
Int(usize)
Signed Integer.
Uint(usize)
Unsigned Integer.
FixedBytes(usize)
Fixed-size bytes, up to 32.
Address
Address.
Function
Function.
Bytes
Dynamic bytes.
String
String.
Array(Box<DynSolType>)
Dynamically sized array.
FixedArray(Box<DynSolType>, usize)
Fixed-sized array.
Tuple(Vec<DynSolType>)
Tuple.
CustomStruct
eip712
only.User-defined struct.
Implementations§
Source§impl DynSolType
impl DynSolType
Sourcepub fn arbitrary_value(&self, u: &mut Unstructured<'_>) -> Result<DynSolValue>
Available on crate feature arbitrary
only.
pub fn arbitrary_value(&self, u: &mut Unstructured<'_>) -> Result<DynSolValue>
arbitrary
only.Generate an arbitrary DynSolValue
from this type.
Sourcepub fn value_strategy(&self) -> SBoxedStrategy<DynSolValue>
Available on crate feature arbitrary
only.
pub fn value_strategy(&self) -> SBoxedStrategy<DynSolValue>
arbitrary
only.Create a proptest strategy to generate DynSolValue
s from
this type.
Source§impl DynSolType
impl DynSolType
Sourcepub fn coerce_str(&self, s: &str) -> Result<DynSolValue>
pub fn coerce_str(&self, s: &str) -> Result<DynSolValue>
Coerces a string into a DynSolValue
via this type.
§Syntax
Bool
:true|false
Int
:[+-]?{Uint}
Uint
:{literal}(\.[0-9]+)?(\s*{unit})?
- literal: base 2, 8, 10, or 16 integer literal. If not in base 10, must be prefixed with
0b
,0o
, or0x
respectively. - unit: same as Solidity ether units
- decimals with more digits than the unit’s exponent value are not allowed
- literal: base 2, 8, 10, or 16 integer literal. If not in base 10, must be prefixed with
FixedBytes
:(0x)?[0-9A-Fa-f]{$0*2}
Address
:(0x)?[0-9A-Fa-f]{40}
Function
:(0x)?[0-9A-Fa-f]{48}
Bytes
:(0x)?[0-9A-Fa-f]+
String
:.*
- can be surrounded by a pair of
"
or'
- trims whitespace if not surrounded
- can be surrounded by a pair of
Array
: any number of the inner type delimited by commas (,
) and surrounded by brackets ([]
)FixedArray
: exactly the given number of the inner type delimited by commas (,
) and surrounded by brackets ([]
)Tuple
: the inner types delimited by commas (,
) and surrounded by parentheses (()
)CustomStruct
: the same asTuple
§Examples
use alloy_dyn_abi::{DynSolType, DynSolValue};
use alloy_primitives::U256;
let ty: DynSolType = "(uint256,string)[]".parse()?;
let value = ty.coerce_str("[(0, \"hello\"), (42, \"world\")]")?;
assert_eq!(
value,
DynSolValue::Array(vec![
DynSolValue::Tuple(vec![
DynSolValue::Uint(U256::from(0), 256),
DynSolValue::String(String::from("hello"))
]),
DynSolValue::Tuple(vec![
DynSolValue::Uint(U256::from(42), 256),
DynSolValue::String(String::from("world"))
]),
])
);
assert!(value.matches(&ty));
assert_eq!(value.as_type().unwrap(), ty);
Source§impl DynSolType
impl DynSolType
Sourcepub fn parse(s: &str) -> Result<Self>
pub fn parse(s: &str) -> Result<Self>
Parses a Solidity type name string into a DynSolType
.
§Examples
let type_name = "uint256";
let ty = DynSolType::parse(type_name)?;
assert_eq!(ty, DynSolType::Uint(256));
assert_eq!(ty.sol_type_name(), type_name);
assert_eq!(ty.to_string(), type_name);
// alternatively, you can use the FromStr impl
let ty2 = type_name.parse::<DynSolType>()?;
assert_eq!(ty2, ty);
Sourcepub fn nesting_depth(&self) -> usize
pub fn nesting_depth(&self) -> usize
Calculate the nesting depth of this type. Simple types have a nesting depth of 0, while all other types have a nesting depth of at least 1.
Sourcepub fn as_custom_struct(&self) -> Option<(&str, &[String], &[Self])>
pub fn as_custom_struct(&self) -> Option<(&str, &[String], &[Self])>
Fallible cast to the contents of a variant.
Sourcepub fn has_custom_struct(&self) -> bool
pub fn has_custom_struct(&self) -> bool
Returns whether this type is contains a custom struct.
Sourcepub fn matches_many(types: &[Self], values: &[DynSolValue]) -> bool
pub fn matches_many(types: &[Self], values: &[DynSolValue]) -> bool
Check that the given DynSolValue
s match these types.
See matches
for more information.
Sourcepub fn matches(&self, value: &DynSolValue) -> bool
pub fn matches(&self, value: &DynSolValue) -> bool
Check that the given DynSolValue
matches this type.
Note: this will not check any names, but just the types; e.g for
CustomStruct
, when the “eip712” feature is enabled, this will only
check equality between the lengths and types of the tuple.
Sourcepub fn detokenize(&self, token: DynToken<'_>) -> Result<DynSolValue>
pub fn detokenize(&self, token: DynToken<'_>) -> Result<DynSolValue>
Dynamic detokenization.
Sourcepub fn sol_type_name(&self) -> Cow<'static, str>
pub fn sol_type_name(&self) -> Cow<'static, str>
The Solidity type name. This returns the Solidity type corresponding to
this value, if it is known. A type will not be known if the value
contains an empty sequence, e.g. T[0]
.
Sourcepub fn to_string(&self) -> String
pub fn to_string(&self) -> String
The Solidity type name, as a String
.
Note: this shadows the inherent ToString
implementation, derived
from fmt::Display
, for performance reasons.
Sourcepub fn abi_decode(&self, data: &[u8]) -> Result<DynSolValue>
pub fn abi_decode(&self, data: &[u8]) -> Result<DynSolValue>
Decode a DynSolValue
from a byte slice. Fails if the value does not
match this type.
This method is used for decoding single values. It assumes the data
argument is an encoded single-element sequence wrapping the self
type.
Sourcepub fn abi_decode_params(&self, data: &[u8]) -> Result<DynSolValue>
pub fn abi_decode_params(&self, data: &[u8]) -> Result<DynSolValue>
Decode a DynSolValue
from a byte slice. Fails if the value does not
match this type.
This method is used for decoding function arguments. It tries to
determine whether the user intended to decode a sequence or an
individual value. If the self
type is a tuple, the data
will be
decoded as a sequence, otherwise it will be decoded as a single value.
§Examples
// This function takes a single simple param:
// DynSolType::Uint(256).decode_params(data)
function myFunc(uint256 a) public;
// This function takes 2 params:
// DynSolType::Tuple(vec![DynSolType::Uint(256), DynSolType::Bool])
// .decode_params(data)
function myFunc(uint256 b, bool c) public;
Sourcepub fn abi_decode_sequence(&self, data: &[u8]) -> Result<DynSolValue>
pub fn abi_decode_sequence(&self, data: &[u8]) -> Result<DynSolValue>
Decode a DynSolValue
from a byte slice. Fails if the value does not
match this type.
Sourcepub fn minimum_words(&self) -> usize
pub fn minimum_words(&self) -> usize
Calculate the minimum number of ABI words necessary to encode this type.
Source§impl DynSolType
impl DynSolType
Sourcepub fn coerce_json(&self, value: &Value) -> Result<DynSolValue>
Available on crate feature eip712
only.
pub fn coerce_json(&self, value: &Value) -> Result<DynSolValue>
eip712
only.Coerce a serde_json::Value
to a DynSolValue
via this type.
Trait Implementations§
Source§impl<'a> Arbitrary<'a> for DynSolType
Available on crate feature arbitrary
only.
impl<'a> Arbitrary<'a> for DynSolType
arbitrary
only.Source§fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
Self
from the given unstructured data. Read moreSource§fn size_hint(depth: usize) -> (usize, Option<usize>)
fn size_hint(depth: usize) -> (usize, Option<usize>)
Unstructured
this type
needs to construct itself. Read moreSource§fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
Self
from the entirety of the given
unstructured data. Read moreSource§fn try_size_hint(
depth: usize,
) -> Result<(usize, Option<usize>), MaxRecursionReached>
fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>
Unstructured
this type
needs to construct itself. Read moreSource§impl Arbitrary for DynSolType
Available on crate feature arbitrary
only.
impl Arbitrary for DynSolType
arbitrary
only.Source§type Parameters = (u32, u32, u32)
type Parameters = (u32, u32, u32)
arbitrary_with
accepts for configuration
of the generated Strategy
. Parameters must implement Default
.Source§type Strategy = Recursive<DynSolType, fn(_: BoxedStrategy<DynSolType>) -> TupleUnion<((u32, Arc<BoxedStrategy<DynSolType>>), (u32, Arc<Map<BoxedStrategy<DynSolType>, fn(_: <BoxedStrategy<DynSolType> as Strategy>::Value) -> DynSolType>>), (u32, Arc<Map<(BoxedStrategy<DynSolType>, RangeInclusive<usize>), fn(_: <(BoxedStrategy<DynSolType>, RangeInclusive<usize>) as Strategy>::Value) -> DynSolType>>), (u32, Arc<Map<VecStrategy<BoxedStrategy<DynSolType>>, fn(_: <VecStrategy<BoxedStrategy<DynSolType>> as Strategy>::Value) -> DynSolType>>), (u32, Arc<BoxedStrategy<DynSolType>>))>>
type Strategy = Recursive<DynSolType, fn(_: BoxedStrategy<DynSolType>) -> TupleUnion<((u32, Arc<BoxedStrategy<DynSolType>>), (u32, Arc<Map<BoxedStrategy<DynSolType>, fn(_: <BoxedStrategy<DynSolType> as Strategy>::Value) -> DynSolType>>), (u32, Arc<Map<(BoxedStrategy<DynSolType>, RangeInclusive<usize>), fn(_: <(BoxedStrategy<DynSolType>, RangeInclusive<usize>) as Strategy>::Value) -> DynSolType>>), (u32, Arc<Map<VecStrategy<BoxedStrategy<DynSolType>>, fn(_: <VecStrategy<BoxedStrategy<DynSolType>> as Strategy>::Value) -> DynSolType>>), (u32, Arc<BoxedStrategy<DynSolType>>))>>
Strategy
used to generate values of type Self
.Source§fn arbitrary_with(args: Self::Parameters) -> Self::Strategy
fn arbitrary_with(args: Self::Parameters) -> Self::Strategy
Source§impl Clone for DynSolType
impl Clone for DynSolType
Source§fn clone(&self) -> DynSolType
fn clone(&self) -> DynSolType
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for DynSolType
impl Debug for DynSolType
Source§impl Display for DynSolType
impl Display for DynSolType
Source§impl FromStr for DynSolType
impl FromStr for DynSolType
Source§impl Hash for DynSolType
impl Hash for DynSolType
Source§impl PartialEq for DynSolType
impl PartialEq for DynSolType
Source§impl<T: ?Sized + Specifier<DynSolType>> Specifier<DynSolType> for &T
impl<T: ?Sized + Specifier<DynSolType>> Specifier<DynSolType> for &T
Source§fn resolve(&self) -> Result<DynSolType>
fn resolve(&self) -> Result<DynSolType>
Source§impl<T: ?Sized + Specifier<DynSolType>> Specifier<DynSolType> for &mut T
impl<T: ?Sized + Specifier<DynSolType>> Specifier<DynSolType> for &mut T
Source§fn resolve(&self) -> Result<DynSolType>
fn resolve(&self) -> Result<DynSolType>
Source§impl<T: ?Sized + Specifier<DynSolType>> Specifier<DynSolType> for Arc<T>
impl<T: ?Sized + Specifier<DynSolType>> Specifier<DynSolType> for Arc<T>
Source§fn resolve(&self) -> Result<DynSolType>
fn resolve(&self) -> Result<DynSolType>
Source§impl<T: ?Sized + Specifier<DynSolType>> Specifier<DynSolType> for Box<T>
impl<T: ?Sized + Specifier<DynSolType>> Specifier<DynSolType> for Box<T>
Source§fn resolve(&self) -> Result<DynSolType>
fn resolve(&self) -> Result<DynSolType>
Source§impl<T: ?Sized + ToOwned + Specifier<DynSolType>> Specifier<DynSolType> for Cow<'_, T>
impl<T: ?Sized + ToOwned + Specifier<DynSolType>> Specifier<DynSolType> for Cow<'_, T>
Source§fn resolve(&self) -> Result<DynSolType>
fn resolve(&self) -> Result<DynSolType>
Source§impl Specifier<DynSolType> for EventParam
impl Specifier<DynSolType> for EventParam
Source§fn resolve(&self) -> Result<DynSolType>
fn resolve(&self) -> Result<DynSolType>
Source§impl Specifier<DynSolType> for Param
impl Specifier<DynSolType> for Param
Source§fn resolve(&self) -> Result<DynSolType>
fn resolve(&self) -> Result<DynSolType>
Source§impl Specifier<DynSolType> for ParameterSpecifier<'_>
impl Specifier<DynSolType> for ParameterSpecifier<'_>
Source§fn resolve(&self) -> Result<DynSolType>
fn resolve(&self) -> Result<DynSolType>
Source§impl Specifier<DynSolType> for Parameters<'_>
impl Specifier<DynSolType> for Parameters<'_>
Source§fn resolve(&self) -> Result<DynSolType>
fn resolve(&self) -> Result<DynSolType>
Source§impl<T: ?Sized + Specifier<DynSolType>> Specifier<DynSolType> for Rc<T>
impl<T: ?Sized + Specifier<DynSolType>> Specifier<DynSolType> for Rc<T>
Source§fn resolve(&self) -> Result<DynSolType>
fn resolve(&self) -> Result<DynSolType>
Source§impl Specifier<DynSolType> for RootType<'_>
impl Specifier<DynSolType> for RootType<'_>
Source§fn resolve(&self) -> Result<DynSolType>
fn resolve(&self) -> Result<DynSolType>
Source§impl Specifier<DynSolType> for String
impl Specifier<DynSolType> for String
Source§fn resolve(&self) -> Result<DynSolType>
fn resolve(&self) -> Result<DynSolType>
Source§impl Specifier<DynSolType> for TupleSpecifier<'_>
impl Specifier<DynSolType> for TupleSpecifier<'_>
Source§fn resolve(&self) -> Result<DynSolType>
fn resolve(&self) -> Result<DynSolType>
Source§impl Specifier<DynSolType> for TypeSpecifier<'_>
impl Specifier<DynSolType> for TypeSpecifier<'_>
Source§fn resolve(&self) -> Result<DynSolType>
fn resolve(&self) -> Result<DynSolType>
Source§impl Specifier<DynSolType> for TypeStem<'_>
impl Specifier<DynSolType> for TypeStem<'_>
Source§fn resolve(&self) -> Result<DynSolType>
fn resolve(&self) -> Result<DynSolType>
Source§impl Specifier<DynSolType> for str
impl Specifier<DynSolType> for str
Source§fn resolve(&self) -> Result<DynSolType>
fn resolve(&self) -> Result<DynSolType>
impl Eq for DynSolType
impl StructuralPartialEq for DynSolType
Auto Trait Implementations§
impl Freeze for DynSolType
impl RefUnwindSafe for DynSolType
impl Send for DynSolType
impl Sync for DynSolType
impl Unpin for DynSolType
impl UnwindSafe for DynSolType
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)