ABI.TypeDecoder (ex_abi v0.5.7)

ABI.TypeDecoder is responsible for decoding types to the format expected by Solidity. We generally take a function selector and binary data and decode that into the original arguments according to the specification.

Link to this section Summary

Link to this section Functions

Link to this function

decode(encoded_data, selector_or_types, data_type \\ :input)

Decodes the given data based on the function selector.

Note, we don't currently try to guess the function name?

Examples

iex> "00000000000000000000000000000000000000000000000000000000000000450000000000000000000000000000000000000000000000000000000000000001"
...> |> Base.decode16!(case: :lower)
...> |> ABI.TypeDecoder.decode(
...>      %ABI.FunctionSelector{
...>        function: "baz",
...>        types: [
...>          {:uint, 32},
...>          :bool
...>        ],
...>        returns: [:bool]
...>      }
...>    )
[69, true]

iex> "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd6"
...> |> Base.decode16!(case: :lower)
...> |> ABI.TypeDecoder.decode(
...>      %ABI.FunctionSelector{
...>        function: "baz",
...>        types: [
...>          {:int, 8}
...>        ],
...>        returns: [:int]
...>      }
...>    )
[-42]


iex> ABI.TypeEncoder.encode(["hello world"],[:string])
...> |> ABI.TypeDecoder.decode(
...>      %ABI.FunctionSelector{
...>        function: nil,
...>        types: [
...>          :string
...>        ]
...>      }
...>    )
["hello world"]

iex> "00000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000001"
...> |> Base.decode16!(case: :lower)
...> |> ABI.TypeDecoder.decode(
...>      %ABI.FunctionSelector{
...>        function: nil,
...>        types: [
...>          {:tuple, [{:uint, 32}, :bool]}
...>        ]
...>      }
...>    )
[{17, true}]

iex> ABI.TypeEncoder.encode([[17,1]],[{:array,{:uint,32}}])
...> |> ABI.TypeDecoder.decode(
...>      %ABI.FunctionSelector{
...>        function: nil,
...>        types: [
...>          {:array, {:uint, 32}}
...>        ]
...>      }
...>    )
[[17, 1]]

iex> ABI.TypeEncoder.encode([[17, 1], true, <<16, 32>>], [{:array, {:uint, 32}},:bool,{:bytes, 2}])
...> |> ABI.TypeDecoder.decode(
...>      %ABI.FunctionSelector{
...>        function: nil,
...>        types: [
...>          {:array, {:uint, 32}},
...>          :bool,
...>          {:bytes, 2}
...>        ]
...>      }
...>    )
[[17, 1], true, <<16, 32>>]

iex> ABI.TypeEncoder.encode([{"awesome", true}], [{:tuple, [:string, :bool]}])
...> |> ABI.TypeDecoder.decode(
...>      %ABI.FunctionSelector{
...>        function: nil,
...>        types: [
...>          {:tuple, [:string, :bool]}
...>        ]
...>      }
...>    )
[{"awesome", true}]

iex> ABI.TypeEncoder.encode([{[]}],[{:tuple, [{:array, :address}]}])
...> |> ABI.TypeDecoder.decode(
...>      %ABI.FunctionSelector{
...>        function: nil,
...>        types: [
...>          {:tuple, [{:array, :address}]}
...>        ]
...>      }
...>    )
[{[]}]

iex> ABI.TypeEncoder.encode( [{
...>  "Unauthorized",
...>  [
...>    184341788326688649239867304918349890235378717380,
...>    765664983403968947098136133435535343021479462042,
...>  ]
...> }], [{:tuple,[:string, {:array, {:uint, 256}}]}])
...> |> ABI.TypeDecoder.decode(
...>      %ABI.FunctionSelector{
...>        function: nil,
...>        types: [{:tuple,[
...>          :string,
...>          {:array, {:uint, 256}}
...>        ]}]
...>      }
...>    )
[{
  "Unauthorized",
  [
    184341788326688649239867304918349890235378717380,
    765664983403968947098136133435535343021479462042,
  ]
}]
Link to this function

decode_bytes(data, size_in_bytes, atom)

Specs

decode_bytes(binary(), non_neg_integer(), :left) :: {binary(), binary()}
Link to this function

decode_bytes(data, size_in_bytes, atom, rest)

Specs

decode_bytes(binary(), non_neg_integer(), :right, binary()) ::
  {binary(), binary()}
Link to this function

decode_bytes(_, size_in_bytes, atom, full_data, _)

Specs

decode_bytes(binary(), non_neg_integer(), :right, binary(), binary()) ::
  {binary(), binary()}
Link to this function

decode_raw(binary_data, types)

Similar to ABI.TypeDecoder.decode/2 except accepts a list of types instead of a function selector.

Examples

iex> ABI.TypeEncoder.encode([{"awesome", true}], [{:tuple, [:string, :bool]}])
...> |> ABI.TypeDecoder.decode_raw([{:tuple, [:string, :bool]}])
[{"awesome", true}]
Link to this function

do_decode_raw(binary_data, types)