ABI.TypeEncoder (abi v0.1.19)

ABI.TypeEncoder is responsible for encoding types to the format expected by Solidity. We generally take a function selector and an array of data and encode that array according to the specification.

Link to this section Summary

Functions

Encodes the given data based on the function selector.

Simiar to ABI.TypeEncoder.encode/2 except we accept an array of types instead of a function selector. We also do not pre-pend the method id.

Link to this section Functions

Link to this function

encode(data, function_selector)

Encodes the given data based on the function selector.

examples

Examples

iex> [69, true]
...> |> ABI.TypeEncoder.encode(
...>      %ABI.FunctionSelector{
...>        function: "baz",
...>        types: [
...>          %{type: {:uint, 32}},
...>          %{type: :bool}
...>        ],
...>        returns: :bool
...>      }
...>    )
...> |> Base.encode16(case: :lower)
"cdcd77c000000000000000000000000000000000000000000000000000000000000000450000000000000000000000000000000000000000000000000000000000000001"

iex> ["BAT"]
...> |> ABI.TypeEncoder.encode(
...>      %ABI.FunctionSelector{
...>        function: "price",
...>        types: [
...>          %{type: :string}
...>        ],
...>        returns: {:uint, 256}
...>      }
...>    )
...> |> Base.encode16(case: :lower)
"fe2c6198000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000034241540000000000000000000000000000000000000000000000000000000000"


iex> [Base.decode16!("ffffffffffffffffffffffffffffffffffffffff", case: :lower)]
...> |> ABI.TypeEncoder.encode(
...>      %ABI.FunctionSelector{
...>        function: "price",
...>        types: [
...>          %{type: :address}
...>        ]
...>      }
...>    )
...> |> Base.encode16(case: :lower)
"aea91078000000000000000000000000ffffffffffffffffffffffffffffffffffffffff"

iex> [1]
...> |> ABI.TypeEncoder.encode(
...>      %ABI.FunctionSelector{
...>        function: "price",
...>        types: [
...>          %{type: :address}
...>        ]
...>      }
...>    )
...> |> Base.encode16(case: :lower)
"aea910780000000000000000000000000000000000000000000000000000000000000001"

iex> ["hello world"]
...> |> ABI.TypeEncoder.encode(
...>      %ABI.FunctionSelector{
...>        function: nil,
...>        types: [
...>          %{type: :string},
...>        ]
...>      }
...>    )
...> |> Base.encode16(case: :lower)
"000000000000000000000000000000000000000000000000000000000000000b68656c6c6f20776f726c64000000000000000000000000000000000000000000"

iex> [{"awesome", true}]
...> |> ABI.TypeEncoder.encode(
...>      %ABI.FunctionSelector{
...>        function: nil,
...>        types: [
...>          %{type: {:tuple, [:string, :bool]}}
...>        ]
...>      }
...>    )
...> |> Base.encode16(case: :lower)
"000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000007617765736f6d6500000000000000000000000000000000000000000000000000"

iex> [{17, true, <<32, 64>>}]
...> |> ABI.TypeEncoder.encode(
...>      %ABI.FunctionSelector{
...>        function: nil,
...>        types: [
...>          %{type: {:tuple, [{:uint, 32}, :bool, {:bytes, 2}]}}
...>        ]
...>      }
...>    )
...> |> Base.encode16(case: :lower)
"000000000000000000000000000000000000000000000000000000000000001100000000000000000000000000000000000000000000000000000000000000012040000000000000000000000000000000000000000000000000000000000000"

iex> [[17, 1]]
...> |> ABI.TypeEncoder.encode(
...>      %ABI.FunctionSelector{
...>        function: "baz",
...>        types: [
...>          %{type: {:array, {:uint, 32}, 2}}
...>        ]
...>      }
...>    )
...> |> Base.encode16(case: :lower)
"3d0ec53300000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000001"

iex> [[17, 1], true]
...> |> ABI.TypeEncoder.encode(
...>      %ABI.FunctionSelector{
...>        function: nil,
...>        types: [
...>          %{type: {:array, {:uint, 32}, 2}},
...>          %{type: :bool}
...>        ]
...>      }
...>    )
...> |> Base.encode16(case: :lower)
"000000000000000000000000000000000000000000000000000000000000001100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001"

iex> [[17, 1]]
...> |> ABI.TypeEncoder.encode(
...>      %ABI.FunctionSelector{
...>        function: nil,
...>        types: [
...>          %{type: {:array, {:uint, 32}}}
...>        ]
...>      }
...>    )
...> |> Base.encode16(case: :lower)
"000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000001"
Link to this function

encode_bytes(bytes)

Link to this function

encode_raw(data, types)

Simiar to ABI.TypeEncoder.encode/2 except we accept an array of types instead of a function selector. We also do not pre-pend the method id.

examples

Examples

iex> [{"awesome", true}]
...> |> ABI.TypeEncoder.encode_raw([%{type: {:tuple, [:string, :bool]}}])
...> |> Base.encode16(case: :lower)
"000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000007617765736f6d6500000000000000000000000000000000000000000000000000"