pub trait SolTypeDecode: Sized + Sealed {
type AlloyType: AlloySolType;
// Required method
fn detokenize(
token: <Self::AlloyType as AlloySolType>::Token<'_>,
) -> Result<Self, Error>;
// Provided method
fn decode(data: &[u8]) -> Result<Self, Error> { ... }
}
Expand description
A Rust/ink! equivalent of a Solidity ABI type that implements logic for Solidity ABI decoding.
§Rust/ink! to Solidity ABI type mapping
Rust/ink! type | Solidity ABI type | Notes |
---|---|---|
bool | bool | |
iN for N ∈ {8,16,32,64,128} | intN | e.g i8 ↔ int8 |
uN for N ∈ {8,16,32,64,128} | uintN | e.g u8 ↔ uint8 |
U256 | uint256 | |
String | string | |
Box<str> | string | |
Address / H160 | address | Address is a type alias for the H160 type used for addresses in pallet-revive |
[T; N] for const N: usize | T[N] | e.g. [i8; 64] ↔ int8[64] |
Vec<T> | T[] | e.g. Vec<i8> ↔ int8[] |
Box<[T]> | T[] | e.g. Box<[i8]> ↔ int8[] |
FixedBytes<N> for 1 <= N <= 32 | bytesN | e.g. FixedBytes<32> ↔ bytes32 , FixedBytes<N> is just a newtype wrapper for [u8; N] that also implements From<u8> |
DynBytes | bytes | DynBytes is just a newtype wrapper for Vec<u8> that also implements From<Box<[u8]>> |
(T1, T2, T3, ... T12) | (U1, U2, U3, ... U12) | where T1 ↔ U1 , … T12 ↔ U12 e.g. (bool, u8, Address) ↔ (bool, uint8, address) |
Option<T> | (bool, T) | e.g. Option<u8> ↔ (bool, uint8) |
Ref: https://docs.soliditylang.org/en/latest/abi-spec.html#types
§Option<T>
representation
Rust’s Option<T>
type doesn’t have a semantically equivalent Solidity ABI type,
because Solidity enums are field-less.
So Option<T>
is mapped to a tuple representation instead (i.e. (bool, T)
),
because this representation allows preservation of semantic information in Solidity,
by using the bool
as a “flag” indicating the variant
(i.e. false
for None
and true
for Some
) such that:
Option::None
is mapped to(false, <default_value>)
where<default_value>
is the zero bytes only representation ofT
(e.g.0u8
foru8
orVec::new()
forVec<T>
)Option::Some(value)
is mapped to(true, value)
The resulting type in Solidity can be represented as a struct with a field for the “flag” and another for the data.
Note that enum
in Solidity is encoded as uint8
in Solidity ABI
encoding, while the encoding for bool
is equivalent to the encoding
of uint8
, with true
equivalent to 1
and false
equivalent to 0
.
Therefore, the bool
“flag” can be safely interpreted as a bool
or enum
(or even
uint8
) in Solidity code.
§Note
This trait is sealed and cannot be implemented for types outside ink_primitives
.
Required Associated Types§
Required Methods§
Sourcefn detokenize(
token: <Self::AlloyType as AlloySolType>::Token<'_>,
) -> Result<Self, Error>
fn detokenize( token: <Self::AlloyType as AlloySolType>::Token<'_>, ) -> Result<Self, Error>
Detokenizes this type’s value from the given token.
Provided Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.