Trait SolTypeDecode

Source
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! typeSolidity ABI typeNotes
boolbool
iN for N ∈ {8,16,32,64,128}intNe.g i8int8
uN for N ∈ {8,16,32,64,128}uintNe.g u8uint8
U256uint256
Stringstring
Box<str>string
Address / H160addressAddress is a type alias for the H160 type used for addresses in pallet-revive
[T; N] for const N: usizeT[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 <= 32bytesNe.g. FixedBytes<32>bytes32, FixedBytes<N> is just a newtype wrapper for [u8; N] that also implements From<u8>
DynBytesbytesDynBytes is just a newtype wrapper for Vec<u8> that also implements From<Box<[u8]>>
(T1, T2, T3, ... T12)(U1, U2, U3, ... U12)where T1U1, … T12U12 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 of T (e.g. 0u8 for u8 or Vec::new() for Vec<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§

Source

type AlloyType: AlloySolType

Equivalent Solidity ABI type from [alloy_sol_types].

Required Methods§

Source

fn detokenize( token: <Self::AlloyType as AlloySolType>::Token<'_>, ) -> Result<Self, Error>

Detokenizes this type’s value from the given token.

Provided Methods§

Source

fn decode(data: &[u8]) -> Result<Self, Error>

Solidity ABI decode into this type.

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.

Implementations on Foreign Types§

Source§

impl SolTypeDecode for bool

Source§

type AlloyType = Bool

Source§

fn detokenize( token: <Self::AlloyType as AlloySolType>::Token<'_>, ) -> Result<Self, Error>

Source§

impl SolTypeDecode for i8

Source§

type AlloyType = Int<8>

Source§

fn detokenize( token: <Self::AlloyType as AlloySolType>::Token<'_>, ) -> Result<Self, Error>

Source§

impl SolTypeDecode for i16

Source§

type AlloyType = Int<16>

Source§

fn detokenize( token: <Self::AlloyType as AlloySolType>::Token<'_>, ) -> Result<Self, Error>

Source§

impl SolTypeDecode for i32

Source§

type AlloyType = Int<32>

Source§

fn detokenize( token: <Self::AlloyType as AlloySolType>::Token<'_>, ) -> Result<Self, Error>

Source§

impl SolTypeDecode for i64

Source§

type AlloyType = Int<64>

Source§

fn detokenize( token: <Self::AlloyType as AlloySolType>::Token<'_>, ) -> Result<Self, Error>

Source§

impl SolTypeDecode for i128

Source§

type AlloyType = Int<128>

Source§

fn detokenize( token: <Self::AlloyType as AlloySolType>::Token<'_>, ) -> Result<Self, Error>

Source§

impl SolTypeDecode for u8

Source§

type AlloyType = Uint<8>

Source§

fn detokenize( token: <Self::AlloyType as AlloySolType>::Token<'_>, ) -> Result<Self, Error>

Source§

impl SolTypeDecode for u16

Source§

type AlloyType = Uint<16>

Source§

fn detokenize( token: <Self::AlloyType as AlloySolType>::Token<'_>, ) -> Result<Self, Error>

Source§

impl SolTypeDecode for u32

Source§

type AlloyType = Uint<32>

Source§

fn detokenize( token: <Self::AlloyType as AlloySolType>::Token<'_>, ) -> Result<Self, Error>

Source§

impl SolTypeDecode for u64

Source§

type AlloyType = Uint<64>

Source§

fn detokenize( token: <Self::AlloyType as AlloySolType>::Token<'_>, ) -> Result<Self, Error>

Source§

impl SolTypeDecode for u128

Source§

type AlloyType = Uint<128>

Source§

fn detokenize( token: <Self::AlloyType as AlloySolType>::Token<'_>, ) -> Result<Self, Error>

Source§

impl SolTypeDecode for ()

Source§

type AlloyType = ()

Source§

fn detokenize( token: <Self::AlloyType as AlloySolType>::Token<'_>, ) -> Result<Self, Error>

Source§

impl SolTypeDecode for Box<str>

Source§

type AlloyType = String

Source§

fn detokenize( token: <Self::AlloyType as AlloySolType>::Token<'_>, ) -> Result<Self, Error>

Source§

impl SolTypeDecode for String

Source§

type AlloyType = String

Source§

fn detokenize( token: <Self::AlloyType as AlloySolType>::Token<'_>, ) -> Result<Self, Error>

Source§

impl<T> SolTypeDecode for Cow<'_, [T]>

Source§

type AlloyType = Array<<T as SolTypeDecode>::AlloyType>

Source§

fn detokenize( token: <Self::AlloyType as AlloySolType>::Token<'_>, ) -> Result<Self, Error>

Source§

impl<T: SolTypeDecode> SolTypeDecode for Option<T>

Source§

type AlloyType = (Bool, <T as SolTypeDecode>::AlloyType)

Source§

fn detokenize( token: <Self::AlloyType as AlloySolType>::Token<'_>, ) -> Result<Self, Error>

Source§

impl<T: SolTypeDecode> SolTypeDecode for Box<[T]>

Source§

type AlloyType = Array<<T as SolTypeDecode>::AlloyType>

Source§

fn detokenize( token: <Self::AlloyType as AlloySolType>::Token<'_>, ) -> Result<Self, Error>

Source§

impl<T: SolTypeDecode> SolTypeDecode for Vec<T>

Source§

type AlloyType = Array<<T as SolTypeDecode>::AlloyType>

Source§

fn detokenize( token: <Self::AlloyType as AlloySolType>::Token<'_>, ) -> Result<Self, Error>

Source§

impl<T: SolTypeDecode, const N: usize> SolTypeDecode for [T; N]

Source§

type AlloyType = FixedArray<<T as SolTypeDecode>::AlloyType, N>

Source§

fn detokenize( token: <Self::AlloyType as AlloySolType>::Token<'_>, ) -> Result<Self, Error>

Source§

impl<TupleElement0: SolTypeDecode> SolTypeDecode for (TupleElement0,)

Source§

type AlloyType = (<TupleElement0 as SolTypeDecode>::AlloyType,)

Source§

fn detokenize( token: <Self::AlloyType as AlloySolType>::Token<'_>, ) -> Result<Self, Error>

Source§

impl<TupleElement0: SolTypeDecode, TupleElement1: SolTypeDecode> SolTypeDecode for (TupleElement0, TupleElement1)

Source§

type AlloyType = (<TupleElement0 as SolTypeDecode>::AlloyType, <TupleElement1 as SolTypeDecode>::AlloyType)

Source§

fn detokenize( token: <Self::AlloyType as AlloySolType>::Token<'_>, ) -> Result<Self, Error>

Source§

impl<TupleElement0: SolTypeDecode, TupleElement1: SolTypeDecode, TupleElement2: SolTypeDecode> SolTypeDecode for (TupleElement0, TupleElement1, TupleElement2)

Source§

type AlloyType = (<TupleElement0 as SolTypeDecode>::AlloyType, <TupleElement1 as SolTypeDecode>::AlloyType, <TupleElement2 as SolTypeDecode>::AlloyType)

Source§

fn detokenize( token: <Self::AlloyType as AlloySolType>::Token<'_>, ) -> Result<Self, Error>

Source§

impl<TupleElement0: SolTypeDecode, TupleElement1: SolTypeDecode, TupleElement2: SolTypeDecode, TupleElement3: SolTypeDecode> SolTypeDecode for (TupleElement0, TupleElement1, TupleElement2, TupleElement3)

Source§

type AlloyType = (<TupleElement0 as SolTypeDecode>::AlloyType, <TupleElement1 as SolTypeDecode>::AlloyType, <TupleElement2 as SolTypeDecode>::AlloyType, <TupleElement3 as SolTypeDecode>::AlloyType)

Source§

fn detokenize( token: <Self::AlloyType as AlloySolType>::Token<'_>, ) -> Result<Self, Error>

Source§

impl<TupleElement0: SolTypeDecode, TupleElement1: SolTypeDecode, TupleElement2: SolTypeDecode, TupleElement3: SolTypeDecode, TupleElement4: SolTypeDecode> SolTypeDecode for (TupleElement0, TupleElement1, TupleElement2, TupleElement3, TupleElement4)

Source§

type AlloyType = (<TupleElement0 as SolTypeDecode>::AlloyType, <TupleElement1 as SolTypeDecode>::AlloyType, <TupleElement2 as SolTypeDecode>::AlloyType, <TupleElement3 as SolTypeDecode>::AlloyType, <TupleElement4 as SolTypeDecode>::AlloyType)

Source§

fn detokenize( token: <Self::AlloyType as AlloySolType>::Token<'_>, ) -> Result<Self, Error>

Source§

impl<TupleElement0: SolTypeDecode, TupleElement1: SolTypeDecode, TupleElement2: SolTypeDecode, TupleElement3: SolTypeDecode, TupleElement4: SolTypeDecode, TupleElement5: SolTypeDecode> SolTypeDecode for (TupleElement0, TupleElement1, TupleElement2, TupleElement3, TupleElement4, TupleElement5)

Source§

type AlloyType = (<TupleElement0 as SolTypeDecode>::AlloyType, <TupleElement1 as SolTypeDecode>::AlloyType, <TupleElement2 as SolTypeDecode>::AlloyType, <TupleElement3 as SolTypeDecode>::AlloyType, <TupleElement4 as SolTypeDecode>::AlloyType, <TupleElement5 as SolTypeDecode>::AlloyType)

Source§

fn detokenize( token: <Self::AlloyType as AlloySolType>::Token<'_>, ) -> Result<Self, Error>

Source§

impl<TupleElement0: SolTypeDecode, TupleElement1: SolTypeDecode, TupleElement2: SolTypeDecode, TupleElement3: SolTypeDecode, TupleElement4: SolTypeDecode, TupleElement5: SolTypeDecode, TupleElement6: SolTypeDecode> SolTypeDecode for (TupleElement0, TupleElement1, TupleElement2, TupleElement3, TupleElement4, TupleElement5, TupleElement6)

Source§

type AlloyType = (<TupleElement0 as SolTypeDecode>::AlloyType, <TupleElement1 as SolTypeDecode>::AlloyType, <TupleElement2 as SolTypeDecode>::AlloyType, <TupleElement3 as SolTypeDecode>::AlloyType, <TupleElement4 as SolTypeDecode>::AlloyType, <TupleElement5 as SolTypeDecode>::AlloyType, <TupleElement6 as SolTypeDecode>::AlloyType)

Source§

fn detokenize( token: <Self::AlloyType as AlloySolType>::Token<'_>, ) -> Result<Self, Error>

Source§

impl<TupleElement0: SolTypeDecode, TupleElement1: SolTypeDecode, TupleElement2: SolTypeDecode, TupleElement3: SolTypeDecode, TupleElement4: SolTypeDecode, TupleElement5: SolTypeDecode, TupleElement6: SolTypeDecode, TupleElement7: SolTypeDecode> SolTypeDecode for (TupleElement0, TupleElement1, TupleElement2, TupleElement3, TupleElement4, TupleElement5, TupleElement6, TupleElement7)

Source§

type AlloyType = (<TupleElement0 as SolTypeDecode>::AlloyType, <TupleElement1 as SolTypeDecode>::AlloyType, <TupleElement2 as SolTypeDecode>::AlloyType, <TupleElement3 as SolTypeDecode>::AlloyType, <TupleElement4 as SolTypeDecode>::AlloyType, <TupleElement5 as SolTypeDecode>::AlloyType, <TupleElement6 as SolTypeDecode>::AlloyType, <TupleElement7 as SolTypeDecode>::AlloyType)

Source§

fn detokenize( token: <Self::AlloyType as AlloySolType>::Token<'_>, ) -> Result<Self, Error>

Source§

impl<TupleElement0: SolTypeDecode, TupleElement1: SolTypeDecode, TupleElement2: SolTypeDecode, TupleElement3: SolTypeDecode, TupleElement4: SolTypeDecode, TupleElement5: SolTypeDecode, TupleElement6: SolTypeDecode, TupleElement7: SolTypeDecode, TupleElement8: SolTypeDecode> SolTypeDecode for (TupleElement0, TupleElement1, TupleElement2, TupleElement3, TupleElement4, TupleElement5, TupleElement6, TupleElement7, TupleElement8)

Source§

type AlloyType = (<TupleElement0 as SolTypeDecode>::AlloyType, <TupleElement1 as SolTypeDecode>::AlloyType, <TupleElement2 as SolTypeDecode>::AlloyType, <TupleElement3 as SolTypeDecode>::AlloyType, <TupleElement4 as SolTypeDecode>::AlloyType, <TupleElement5 as SolTypeDecode>::AlloyType, <TupleElement6 as SolTypeDecode>::AlloyType, <TupleElement7 as SolTypeDecode>::AlloyType, <TupleElement8 as SolTypeDecode>::AlloyType)

Source§

fn detokenize( token: <Self::AlloyType as AlloySolType>::Token<'_>, ) -> Result<Self, Error>

Source§

impl<TupleElement0: SolTypeDecode, TupleElement1: SolTypeDecode, TupleElement2: SolTypeDecode, TupleElement3: SolTypeDecode, TupleElement4: SolTypeDecode, TupleElement5: SolTypeDecode, TupleElement6: SolTypeDecode, TupleElement7: SolTypeDecode, TupleElement8: SolTypeDecode, TupleElement9: SolTypeDecode> SolTypeDecode for (TupleElement0, TupleElement1, TupleElement2, TupleElement3, TupleElement4, TupleElement5, TupleElement6, TupleElement7, TupleElement8, TupleElement9)

Source§

type AlloyType = (<TupleElement0 as SolTypeDecode>::AlloyType, <TupleElement1 as SolTypeDecode>::AlloyType, <TupleElement2 as SolTypeDecode>::AlloyType, <TupleElement3 as SolTypeDecode>::AlloyType, <TupleElement4 as SolTypeDecode>::AlloyType, <TupleElement5 as SolTypeDecode>::AlloyType, <TupleElement6 as SolTypeDecode>::AlloyType, <TupleElement7 as SolTypeDecode>::AlloyType, <TupleElement8 as SolTypeDecode>::AlloyType, <TupleElement9 as SolTypeDecode>::AlloyType)

Source§

fn detokenize( token: <Self::AlloyType as AlloySolType>::Token<'_>, ) -> Result<Self, Error>

Source§

impl<TupleElement0: SolTypeDecode, TupleElement1: SolTypeDecode, TupleElement2: SolTypeDecode, TupleElement3: SolTypeDecode, TupleElement4: SolTypeDecode, TupleElement5: SolTypeDecode, TupleElement6: SolTypeDecode, TupleElement7: SolTypeDecode, TupleElement8: SolTypeDecode, TupleElement9: SolTypeDecode, TupleElement10: SolTypeDecode> SolTypeDecode for (TupleElement0, TupleElement1, TupleElement2, TupleElement3, TupleElement4, TupleElement5, TupleElement6, TupleElement7, TupleElement8, TupleElement9, TupleElement10)

Source§

type AlloyType = (<TupleElement0 as SolTypeDecode>::AlloyType, <TupleElement1 as SolTypeDecode>::AlloyType, <TupleElement2 as SolTypeDecode>::AlloyType, <TupleElement3 as SolTypeDecode>::AlloyType, <TupleElement4 as SolTypeDecode>::AlloyType, <TupleElement5 as SolTypeDecode>::AlloyType, <TupleElement6 as SolTypeDecode>::AlloyType, <TupleElement7 as SolTypeDecode>::AlloyType, <TupleElement8 as SolTypeDecode>::AlloyType, <TupleElement9 as SolTypeDecode>::AlloyType, <TupleElement10 as SolTypeDecode>::AlloyType)

Source§

fn detokenize( token: <Self::AlloyType as AlloySolType>::Token<'_>, ) -> Result<Self, Error>

Source§

impl<TupleElement0: SolTypeDecode, TupleElement1: SolTypeDecode, TupleElement2: SolTypeDecode, TupleElement3: SolTypeDecode, TupleElement4: SolTypeDecode, TupleElement5: SolTypeDecode, TupleElement6: SolTypeDecode, TupleElement7: SolTypeDecode, TupleElement8: SolTypeDecode, TupleElement9: SolTypeDecode, TupleElement10: SolTypeDecode, TupleElement11: SolTypeDecode> SolTypeDecode for (TupleElement0, TupleElement1, TupleElement2, TupleElement3, TupleElement4, TupleElement5, TupleElement6, TupleElement7, TupleElement8, TupleElement9, TupleElement10, TupleElement11)

Source§

type AlloyType = (<TupleElement0 as SolTypeDecode>::AlloyType, <TupleElement1 as SolTypeDecode>::AlloyType, <TupleElement2 as SolTypeDecode>::AlloyType, <TupleElement3 as SolTypeDecode>::AlloyType, <TupleElement4 as SolTypeDecode>::AlloyType, <TupleElement5 as SolTypeDecode>::AlloyType, <TupleElement6 as SolTypeDecode>::AlloyType, <TupleElement7 as SolTypeDecode>::AlloyType, <TupleElement8 as SolTypeDecode>::AlloyType, <TupleElement9 as SolTypeDecode>::AlloyType, <TupleElement10 as SolTypeDecode>::AlloyType, <TupleElement11 as SolTypeDecode>::AlloyType)

Source§

fn detokenize( token: <Self::AlloyType as AlloySolType>::Token<'_>, ) -> Result<Self, Error>

Implementors§

Source§

impl SolTypeDecode for U256

Source§

type AlloyType = Uint<256>

Source§

impl SolTypeDecode for DynBytes

Source§

type AlloyType = Bytes

Source§

impl SolTypeDecode for Address

Source§

type AlloyType = Address

Source§

impl<const N: usize> SolTypeDecode for FixedBytes<N>
where ByteCount<N>: SupportedFixedBytes,

Source§

type AlloyType = FixedBytes<N>