alloy_sol_types/types/ty.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
use crate::{
abi::{self, Token, TokenSeq},
private::SolTypeValue,
Result, Word,
};
use alloc::{borrow::Cow, vec::Vec};
/// A Solidity type.
///
/// This trait is implemented by types that contain ABI encoding and decoding
/// info for Solidity types. Types may be combined to express arbitrarily
/// complex Solidity types.
///
/// These types are zero cost representations of Solidity types. They do not
/// exist at runtime. They **only** contain information about the type, they do
/// not carry any data.
///
/// # Implementer's Guide
///
/// It should not be necessary to implement this trait manually. Instead, use
/// the [`sol!`] procedural macro to parse Solidity syntax into types that
/// implement this trait.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use alloy_sol_types::{sol_data::*, SolType};
///
/// type Uint256DynamicArray = Array<Uint<256>>;
/// assert_eq!(Uint256DynamicArray::sol_type_name(), "uint256[]");
///
/// type Erc20FunctionArgs = (Address, Uint<256>);
/// assert_eq!(Erc20FunctionArgs::sol_type_name(), "(address,uint256)");
///
/// type LargeComplexType = (FixedArray<Array<Bool>, 2>, (FixedBytes<13>, String));
/// assert_eq!(LargeComplexType::sol_type_name(), "(bool[][2],(bytes13,string))");
/// ```
///
/// The previous example can be entirely replicated with the [`sol!`] macro:
///
/// ```
/// use alloy_sol_types::{sol, SolType};
///
/// type Uint256DynamicArray = sol!(uint256[]);
/// assert_eq!(Uint256DynamicArray::sol_type_name(), "uint256[]");
///
/// type Erc20FunctionArgs = sol!((address, uint256));
/// assert_eq!(Erc20FunctionArgs::sol_type_name(), "(address,uint256)");
///
/// type LargeComplexType = sol!((bool[][2],(bytes13,string)));
/// assert_eq!(LargeComplexType::sol_type_name(), "(bool[][2],(bytes13,string))");
/// ```
///
/// For more complex usage, it's recommended to use the
/// [`SolValue`](crate::SolValue) trait for primitive types, and the `Sol*`
/// traits for other types created with [`sol!`]:
///
/// ```
/// use alloy_primitives::Address;
/// use alloy_sol_types::{sol, SolCall, SolStruct, SolValue};
///
/// sol! {
/// struct MyStruct {
/// bool a;
/// uint64 b;
/// address c;
/// }
///
/// enum MyEnum {
/// A,
/// B,
/// C,
/// }
///
/// function myFunction(MyStruct my_struct, MyEnum my_enum);
/// }
///
/// // `SolValue`
/// let my_bool = true;
/// let _ = my_bool.abi_encode();
///
/// let my_struct = MyStruct { a: true, b: 1, c: Address::ZERO };
/// let _ = my_struct.abi_encode();
///
/// let my_enum = MyEnum::A;
/// let _ = my_enum.abi_encode();
///
/// // `SolCall`
/// let my_function_call = myFunctionCall { my_struct, my_enum };
/// let _ = my_function_call.abi_encode();
/// ```
///
/// [`sol!`]: crate::sol
pub trait SolType: Sized {
/// The corresponding Rust type.
type RustType: SolTypeValue<Self> + 'static;
/// The corresponding [ABI token type](Token).
///
/// This is the intermediate representation of the type that is used for
/// ABI encoding and decoding.
type Token<'a>: Token<'a>;
/// The name of this type in Solidity.
const SOL_NAME: &'static str;
/// The statically-known ABI-encoded size of the type.
///
/// If this is not known at compile time, this should be `None`, which indicates that the
/// encoded size is dynamic.
const ENCODED_SIZE: Option<usize>;
/// The statically-known Non-standard Packed Mode ABI-encoded size of the type.
///
/// If this is not known at compile time, this should be `None`, which indicates that the
/// encoded size is dynamic.
const PACKED_ENCODED_SIZE: Option<usize>;
/// Whether the ABI-encoded size is dynamic.
///
/// There should be no need to override the default implementation.
const DYNAMIC: bool = Self::ENCODED_SIZE.is_none();
/// Returns the name of this type in Solidity.
#[deprecated(since = "0.6.3", note = "use `SOL_NAME` instead")]
#[inline]
fn sol_type_name() -> Cow<'static, str> {
Self::SOL_NAME.into()
}
/// Calculate the ABI-encoded size of the data, counting both head and tail
/// words. For a single-word type this will always be 32.
#[inline]
fn abi_encoded_size<E: ?Sized + SolTypeValue<Self>>(rust: &E) -> usize {
rust.stv_abi_encoded_size()
}
/// Returns `true` if the given token can be detokenized with this type.
fn valid_token(token: &Self::Token<'_>) -> bool;
/// Returns an error if the given token cannot be detokenized with this
/// type.
#[inline]
fn type_check(token: &Self::Token<'_>) -> Result<()> {
if Self::valid_token(token) {
Ok(())
} else {
Err(crate::Error::type_check_fail_token::<Self>(token))
}
}
/// Detokenize this type's value from the given token.
///
/// See the [`abi::token`] module for more information.
fn detokenize(token: Self::Token<'_>) -> Self::RustType;
/// Tokenizes the given value into this type's token.
///
/// See the [`abi::token`] module for more information.
#[inline]
fn tokenize<E: ?Sized + SolTypeValue<Self>>(rust: &E) -> Self::Token<'_> {
rust.stv_to_tokens()
}
/// Encode this data according to EIP-712 `encodeData` rules, and hash it
/// if necessary.
///
/// Implementer's note: All single-word types are encoded as their word.
/// All multi-word types are encoded as the hash the concatenated data
/// words for each element
///
/// <https://eips.ethereum.org/EIPS/eip-712#definition-of-encodedata>
#[inline]
fn eip712_data_word<E: ?Sized + SolTypeValue<Self>>(rust: &E) -> Word {
rust.stv_eip712_data_word()
}
/// Returns the length of this value when ABI-encoded in Non-standard Packed Mode.
///
/// See [`abi_encode_packed`][SolType::abi_encode_packed] for more details.
#[inline]
fn abi_packed_encoded_size<E: ?Sized + SolTypeValue<Self>>(rust: &E) -> usize {
rust.stv_abi_packed_encoded_size()
}
/// Non-standard Packed Mode ABI encoding.
///
/// See [`abi_encode_packed`][SolType::abi_encode_packed] for more details.
#[inline]
fn abi_encode_packed_to<E: ?Sized + SolTypeValue<Self>>(rust: &E, out: &mut Vec<u8>) {
rust.stv_abi_encode_packed_to(out)
}
/// Non-standard Packed Mode ABI encoding.
///
/// This is different from normal ABI encoding:
/// - types shorter than 32 bytes are concatenated directly, without padding or sign extension;
/// - dynamic types are encoded in-place and without the length;
/// - array elements are padded, but still encoded in-place.
///
/// More information can be found in the [Solidity docs](https://docs.soliditylang.org/en/latest/abi-spec.html#non-standard-packed-mode).
#[inline]
fn abi_encode_packed<E: ?Sized + SolTypeValue<Self>>(rust: &E) -> Vec<u8> {
let mut out = Vec::with_capacity(Self::abi_packed_encoded_size(rust));
Self::abi_encode_packed_to(rust, &mut out);
out
}
/// Tokenizes and ABI-encodes the given value by wrapping it in a
/// single-element sequence.
///
/// See the [`abi`] module for more information.
#[inline]
fn abi_encode<E: ?Sized + SolTypeValue<Self>>(rust: &E) -> Vec<u8> {
abi::encode(&rust.stv_to_tokens())
}
/// Tokenizes and ABI-encodes the given value as function parameters.
///
/// See the [`abi`] module for more information.
#[inline]
fn abi_encode_params<E: ?Sized + SolTypeValue<Self>>(rust: &E) -> Vec<u8>
where
for<'a> Self::Token<'a>: TokenSeq<'a>,
{
abi::encode_params(&rust.stv_to_tokens())
}
/// Tokenizes and ABI-encodes the given value as a sequence.
///
/// See the [`abi`] module for more information.
#[inline]
fn abi_encode_sequence<E: ?Sized + SolTypeValue<Self>>(rust: &E) -> Vec<u8>
where
for<'a> Self::Token<'a>: TokenSeq<'a>,
{
abi::encode_sequence(&rust.stv_to_tokens())
}
/// Decodes this type's value from an ABI blob by interpreting it as a
/// single-element sequence.
///
/// See the [`abi`] module for more information.
#[inline]
fn abi_decode(data: &[u8], validate: bool) -> Result<Self::RustType> {
abi::decode::<Self::Token<'_>>(data, validate)
.and_then(validate_and_detokenize::<Self>(validate))
}
/// Decodes this type's value from an ABI blob by interpreting it as
/// function parameters.
///
/// See the [`abi`] module for more information.
#[inline]
fn abi_decode_params<'de>(data: &'de [u8], validate: bool) -> Result<Self::RustType>
where
Self::Token<'de>: TokenSeq<'de>,
{
abi::decode_params::<Self::Token<'_>>(data, validate)
.and_then(validate_and_detokenize::<Self>(validate))
}
/// Decodes this type's value from an ABI blob by interpreting it as a
/// sequence.
///
/// See the [`abi`] module for more information.
#[inline]
fn abi_decode_sequence<'de>(data: &'de [u8], validate: bool) -> Result<Self::RustType>
where
Self::Token<'de>: TokenSeq<'de>,
{
abi::decode_sequence::<Self::Token<'_>>(data, validate)
.and_then(validate_and_detokenize::<Self>(validate))
}
}
#[inline]
fn validate_and_detokenize<T: SolType>(
validate: bool,
) -> impl FnOnce(T::Token<'_>) -> Result<T::RustType> {
move |token| {
if validate {
T::type_check(&token)?;
}
Ok(T::detokenize(token))
}
}