ABI.TypeEncoder (abi v0.1.21)

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.

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.

Functions

Link to this function

encode(data, function_selector)

Encodes the given data based on the function selector.

Examples

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

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

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

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

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

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

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

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

iex> [[17, 1]]
...> |> ABI.TypeEncoder.encode(
...>      %ABI.FunctionSelector{
...>        function: nil,
...>        types: [
...>          {: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

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