ABI.TypeDecoder (abi v0.1.21)

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.

Summary

Functions

Decodes the given data based on the function selector.

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

Functions

Link to this function

decode(encoded_data, function_selector)

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> "000000000000000000000000000000000000000000000000000000000000000b68656c6c6f20776f726c64000000000000000000000000000000000000000000"
...> |> Base.decode16!(case: :lower)
...> |> 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> "00000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000001"
...> |> Base.decode16!(case: :lower)
...> |> ABI.TypeDecoder.decode(
...>      %ABI.FunctionSelector{
...>        function: nil,
...>        types: [
...>          {:struct, "Cat", [{:uint, 32}, :bool], ["age", "cool"]}
...>        ]
...>      }
...>    )
[{17, true}]

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

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

iex> "0000000000000000000000000000000000000000000000000000000000000011000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000011020000000000000000000000000000000000000000000000000000000000000"
...> |> Base.decode16!(case: :lower)
...> |> ABI.TypeDecoder.decode(
...>      %ABI.FunctionSelector{
...>        function: nil,
...>        types: [
...>          {:array, {:uint, 32}, 2},
...>          :bool,
...>          {:bytes, 2}
...>        ]
...>      }
...>    )
[[17, 1], true, <<16, 32>>]

iex> "000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000007617765736f6d6500000000000000000000000000000000000000000000000000"
...> |> Base.decode16!(case: :lower)
...> |> ABI.TypeDecoder.decode(
...>      %ABI.FunctionSelector{
...>        function: nil,
...>        types: [
...>          {:tuple, [:string, :bool]}
...>        ]
...>      }
...>    )
[{"awesome", true}]

iex> "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000"
...> |> Base.decode16!(case: :lower)
...> |> ABI.TypeDecoder.decode(
...>      %ABI.FunctionSelector{
...>        function: nil,
...>        types: [
...>          {:tuple, [{:array, :address}]}
...>        ]
...>      }
...>    )
[{[]}]

iex> "00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000c556e617574686f72697a656400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000204a2bf2ff0a4eaf1890c8d8679eaa446fb852c4000000000000000000000000861d9af488d5fa485bb08ab6912fff4f7450849a"
...> |> Base.decode16!(case: :lower)
...> |> ABI.TypeDecoder.decode(
...>      %ABI.FunctionSelector{
...>        function: nil,
...>        types: [{:tuple,[
...>          :string,
...>          {:array, {:uint, 256}}
...>        ]}]
...>      }
...>    )
[{
  "Unauthorized",
  [
    184341788326688649239867304918349890235378717380,
    765664983403968947098136133435535343021479462042,
  ]
}]

iex> "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000034241540000000000000000000000000000000000000000000000000000000000"
...> |> Base.decode16!(case: :lower)
...> |> ABI.TypeDecoder.decode(
...>      %ABI.FunctionSelector{
...>        function: "price",
...>        types: [
...>          :string
...>        ],
...>        returns: {:uint, 256}
...>      }
...>    )
["BAT"]
Link to this function

decode_bytes(data, size_in_bytes, padding_direction)

@spec decode_bytes(binary(), integer(), atom()) :: {binary(), binary()}
Link to this function

decode_raw(encoded_data, types)

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

Examples

iex> "000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000007617765736f6d6500000000000000000000000000000000000000000000000000"
...> |> Base.decode16!(case: :lower)
...> |> ABI.TypeDecoder.decode_raw([{:tuple, [:string, :bool]}])
[{"awesome", true}]