abi v0.1.3 ABI.TypeEncoder

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

Link to this section 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> ["hello world"]
...> |> ABI.TypeEncoder.encode(
...>      %ABI.FunctionSelector{
...>        function: nil,
...>        types: [
...>          :string,
...>        ]
...>      }
...>    )
...> |> Base.encode16(case: :lower)
"000000000000000000000000000000000000000000000000000000000000000b00000000000000000000000000000000000000000068656c6c6f20776f726c64"

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

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

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)