pub enum DynSolValue {
Bool(bool),
Int(I256, usize),
Uint(U256, usize),
FixedBytes(Word, usize),
Address(Address),
Function(Function),
Bytes(Vec<u8>),
String(String),
Array(Vec<DynSolValue>),
FixedArray(Vec<DynSolValue>),
Tuple(Vec<DynSolValue>),
CustomStruct {
name: String,
prop_names: Vec<String>,
tuple: Vec<DynSolValue>,
},
}
Expand description
A dynamic Solidity value.
It is broadly similar to serde_json::Value
in that it is an enum of
possible types, and the user must inspect and disambiguate.
§Examples
Basic usage:
use alloy_dyn_abi::{DynSolType, DynSolValue};
let ty: DynSolType = "uint64".parse()?;
let value: DynSolValue = 183u64.into();
let encoded: Vec<u8> = value.abi_encode();
let decoded: DynSolValue = ty.abi_decode(&encoded)?;
assert_eq!(decoded, value);
Coerce a string using DynSolType
:
use alloy_dyn_abi::{DynSolType, DynSolValue};
use alloy_primitives::U256;
let ty: DynSolType = "(string, uint256)".parse()?;
let value = ty.coerce_str("(foo bar, 2.5 gwei)")?;
assert_eq!(
value,
DynSolValue::Tuple(vec![
DynSolValue::String(String::from("foo bar")),
DynSolValue::Uint(U256::from(2_500_000_000u64), 256)
]),
);
Variants§
Bool(bool)
A boolean.
Int(I256, usize)
A signed integer. The second parameter is the number of bits, not bytes.
Uint(U256, usize)
An unsigned integer. The second parameter is the number of bits, not bytes.
FixedBytes(Word, usize)
A fixed-length byte array. The second parameter is the number of bytes.
Address(Address)
An address.
Function(Function)
A function pointer.
Bytes(Vec<u8>)
A dynamic-length byte array.
String(String)
A string.
Array(Vec<DynSolValue>)
A dynamically-sized array of values.
FixedArray(Vec<DynSolValue>)
A fixed-size array of values.
Tuple(Vec<DynSolValue>)
A tuple of values.
CustomStruct
eip712
only.A named struct, treated as a tuple with a name parameter.
Implementations§
Source§impl DynSolValue
impl DynSolValue
Sourcepub fn arbitrary_from_type(
ty: &DynSolType,
u: &mut Unstructured<'_>,
) -> Result<Self>
Available on crate feature arbitrary
only.
pub fn arbitrary_from_type( ty: &DynSolType, u: &mut Unstructured<'_>, ) -> Result<Self>
arbitrary
only.Generate an arbitrary DynSolValue
from the given DynSolType
.
Sourcepub fn type_strategy(ty: &DynSolType) -> SBoxedStrategy<Self>
Available on crate feature arbitrary
only.
pub fn type_strategy(ty: &DynSolType) -> SBoxedStrategy<Self>
arbitrary
only.Create a proptest strategy to generate DynSolValue
s from
the given type.
Sourcepub fn value_strategy(&self) -> SBoxedStrategy<Self>
Available on crate feature arbitrary
only.
pub fn value_strategy(&self) -> SBoxedStrategy<Self>
arbitrary
only.Create a proptest strategy to generate DynSolValue
s from
the given value’s type.
Source§impl DynSolValue
impl DynSolValue
Sourcepub fn as_type(&self) -> Option<DynSolType>
pub fn as_type(&self) -> Option<DynSolType>
The Solidity type. 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 sol_type_name(&self) -> Option<Cow<'static, str>>
pub fn sol_type_name(&self) -> Option<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 const fn is_word(&self) -> bool
pub const fn is_word(&self) -> bool
Trust if this value is encoded as a single word. False otherwise.
Sourcepub fn as_word(&self) -> Option<Word>
pub fn as_word(&self) -> Option<Word>
Fallible cast to a single word. Will succeed for any single-word type.
Sourcepub const fn as_address(&self) -> Option<Address>
pub const fn as_address(&self) -> Option<Address>
Fallible cast to the contents of a variant DynSolValue {.
Sourcepub const fn as_fixed_bytes(&self) -> Option<(&[u8], usize)>
pub const fn as_fixed_bytes(&self) -> Option<(&[u8], usize)>
Fallible cast to the contents of a variant.
Sourcepub const fn as_int(&self) -> Option<(I256, usize)>
pub const fn as_int(&self) -> Option<(I256, usize)>
Fallible cast to the contents of a variant.
Sourcepub const fn as_uint(&self) -> Option<(U256, usize)>
pub const fn as_uint(&self) -> Option<(U256, usize)>
Fallible cast to the contents of a variant.
Sourcepub fn as_fixed_array(&self) -> Option<&[Self]>
pub fn as_fixed_array(&self) -> Option<&[Self]>
Fallible cast to the contents of a variant.
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 const fn is_sequence(&self) -> bool
pub const fn is_sequence(&self) -> bool
Returns true if the value is a sequence type.
Sourcepub fn as_fixed_seq(&self) -> Option<&[Self]>
pub fn as_fixed_seq(&self) -> Option<&[Self]>
Fallible cast to a fixed-size array. Any of a FixedArray
, a Tuple
,
or a CustomStruct
.
Sourcepub fn as_packed_seq(&self) -> Option<&[u8]>
pub fn as_packed_seq(&self) -> Option<&[u8]>
Fallible cast to a packed sequence. Any of a String, or a Bytes.
Sourcepub fn is_dynamic(&self) -> bool
pub fn is_dynamic(&self) -> bool
Returns true
if the value is an instance of a dynamically sized type.
Sourcepub fn matches_many(values: &[Self], types: &[DynSolType]) -> bool
pub fn matches_many(values: &[Self], types: &[DynSolType]) -> bool
Check that these values have the same type as the given DynSolType
s.
See DynSolType::matches
for more information.
Sourcepub fn matches(&self, ty: &DynSolType) -> bool
pub fn matches(&self, ty: &DynSolType) -> bool
Check that this value has the same type as the given DynSolType
.
See DynSolType::matches
for more information.
Sourcepub fn head_append(&self, enc: &mut Encoder)
pub fn head_append(&self, enc: &mut Encoder)
Append this data to the head of an in-progress blob via the encoder.
Sourcepub fn tail_append(&self, enc: &mut Encoder)
pub fn tail_append(&self, enc: &mut Encoder)
Append this data to the tail of an in-progress blob via the encoder.
Sourcepub fn abi_encode_packed(&self) -> Vec<u8>
pub fn abi_encode_packed(&self) -> Vec<u8>
Non-standard Packed Mode ABI encoding.
Note that invalid value sizes will saturate to the maximum size, e.g. Uint(x, 300)
will
behave the same as Uint(x, 256)
.
See SolType::abi_encode_packed
for more
details.
Sourcepub fn abi_encode_packed_to(&self, buf: &mut Vec<u8>)
pub fn abi_encode_packed_to(&self, buf: &mut Vec<u8>)
Non-standard Packed Mode ABI encoding.
See abi_encode_packed
for more details.
Sourcepub fn abi_packed_encoded_size(&self) -> usize
pub fn abi_packed_encoded_size(&self) -> usize
Returns the length of this value when ABI-encoded in Non-standard Packed Mode.
See abi_encode_packed
for more details.
Sourcepub fn abi_encode(&self) -> Vec<u8>
pub fn abi_encode(&self) -> Vec<u8>
Encode this value into a byte array by wrapping it into a 1-element sequence.
Sourcepub fn abi_encode_params(&self) -> Vec<u8>
pub fn abi_encode_params(&self) -> Vec<u8>
Encode this value into a byte array suitable for passing to a function. If this value is a tuple, it is encoded as is. Otherwise, it is wrapped into a 1-element sequence.
§Examples
// Encoding for function foo(address)
DynSolValue::Address(_).abi_encode_params();
// Encoding for function foo(address, uint256)
DynSolValue::Tuple(vec![
DynSolValue::Address(_),
DynSolValue::Uint(_, 256),
]).abi_encode_params();
Sourcepub fn abi_encode_sequence(&self) -> Option<Vec<u8>>
pub fn abi_encode_sequence(&self) -> Option<Vec<u8>>
If this value is a fixed sequence, encode it into a byte array. If this
value is not a fixed sequence, return None
.
Trait Implementations§
Source§impl<'a> Arbitrary<'a> for DynSolValue
Available on crate feature arbitrary
only.
impl<'a> Arbitrary<'a> for DynSolValue
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 DynSolValue
Available on crate feature arbitrary
only.
impl Arbitrary for DynSolValue
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<DynSolValue, fn(_: BoxedStrategy<DynSolValue>) -> TupleUnion<((u32, Arc<BoxedStrategy<DynSolValue>>), (u32, Arc<Map<Flatten<Map<BoxedStrategy<DynSolValue>, fn(_: <BoxedStrategy<DynSolValue> as Strategy>::Value) -> VecStrategy<SBoxedStrategy<DynSolValue>>>>, fn(_: <Flatten<Map<BoxedStrategy<DynSolValue>, fn(_: <BoxedStrategy<DynSolValue> as Strategy>::Value) -> VecStrategy<SBoxedStrategy<DynSolValue>>>> as Strategy>::Value) -> DynSolValue>>), (u32, Arc<Map<Flatten<Map<BoxedStrategy<DynSolValue>, fn(_: <BoxedStrategy<DynSolValue> as Strategy>::Value) -> VecStrategy<SBoxedStrategy<DynSolValue>>>>, fn(_: <Flatten<Map<BoxedStrategy<DynSolValue>, fn(_: <BoxedStrategy<DynSolValue> as Strategy>::Value) -> VecStrategy<SBoxedStrategy<DynSolValue>>>> as Strategy>::Value) -> DynSolValue>>), (u32, Arc<Map<VecStrategy<BoxedStrategy<DynSolValue>>, fn(_: <VecStrategy<BoxedStrategy<DynSolValue>> as Strategy>::Value) -> DynSolValue>>), (u32, Arc<BoxedStrategy<DynSolValue>>))>>
type Strategy = Recursive<DynSolValue, fn(_: BoxedStrategy<DynSolValue>) -> TupleUnion<((u32, Arc<BoxedStrategy<DynSolValue>>), (u32, Arc<Map<Flatten<Map<BoxedStrategy<DynSolValue>, fn(_: <BoxedStrategy<DynSolValue> as Strategy>::Value) -> VecStrategy<SBoxedStrategy<DynSolValue>>>>, fn(_: <Flatten<Map<BoxedStrategy<DynSolValue>, fn(_: <BoxedStrategy<DynSolValue> as Strategy>::Value) -> VecStrategy<SBoxedStrategy<DynSolValue>>>> as Strategy>::Value) -> DynSolValue>>), (u32, Arc<Map<Flatten<Map<BoxedStrategy<DynSolValue>, fn(_: <BoxedStrategy<DynSolValue> as Strategy>::Value) -> VecStrategy<SBoxedStrategy<DynSolValue>>>>, fn(_: <Flatten<Map<BoxedStrategy<DynSolValue>, fn(_: <BoxedStrategy<DynSolValue> as Strategy>::Value) -> VecStrategy<SBoxedStrategy<DynSolValue>>>> as Strategy>::Value) -> DynSolValue>>), (u32, Arc<Map<VecStrategy<BoxedStrategy<DynSolValue>>, fn(_: <VecStrategy<BoxedStrategy<DynSolValue>> as Strategy>::Value) -> DynSolValue>>), (u32, Arc<BoxedStrategy<DynSolValue>>))>>
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 DynSolValue
impl Clone for DynSolValue
Source§fn clone(&self) -> DynSolValue
fn clone(&self) -> DynSolValue
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for DynSolValue
impl Debug for DynSolValue
Source§impl<const N: usize> From<[DynSolValue; N]> for DynSolValue
impl<const N: usize> From<[DynSolValue; N]> for DynSolValue
Source§impl From<Address> for DynSolValue
impl From<Address> for DynSolValue
Source§impl From<Signed<256, 4>> for DynSolValue
impl From<Signed<256, 4>> for DynSolValue
Source§impl From<String> for DynSolValue
impl From<String> for DynSolValue
Source§impl From<Uint<256, 4>> for DynSolValue
impl From<Uint<256, 4>> for DynSolValue
Source§impl From<Vec<DynSolValue>> for DynSolValue
impl From<Vec<DynSolValue>> for DynSolValue
Source§impl From<bool> for DynSolValue
impl From<bool> for DynSolValue
Source§impl From<i128> for DynSolValue
impl From<i128> for DynSolValue
Source§impl From<i16> for DynSolValue
impl From<i16> for DynSolValue
Source§impl From<i32> for DynSolValue
impl From<i32> for DynSolValue
Source§impl From<i64> for DynSolValue
impl From<i64> for DynSolValue
Source§impl From<i8> for DynSolValue
impl From<i8> for DynSolValue
Source§impl From<isize> for DynSolValue
impl From<isize> for DynSolValue
Source§impl From<u128> for DynSolValue
impl From<u128> for DynSolValue
Source§impl From<u16> for DynSolValue
impl From<u16> for DynSolValue
Source§impl From<u32> for DynSolValue
impl From<u32> for DynSolValue
Source§impl From<u64> for DynSolValue
impl From<u64> for DynSolValue
Source§impl From<u8> for DynSolValue
impl From<u8> for DynSolValue
Source§impl From<usize> for DynSolValue
impl From<usize> for DynSolValue
Source§impl PartialEq for DynSolValue
impl PartialEq for DynSolValue
impl StructuralPartialEq for DynSolValue
Auto Trait Implementations§
impl Freeze for DynSolValue
impl RefUnwindSafe for DynSolValue
impl Send for DynSolValue
impl Sync for DynSolValue
impl Unpin for DynSolValue
impl UnwindSafe for DynSolValue
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
)