ABI.FunctionSelector (ex_abi v0.5.7)

Module to help parse the ABI function signatures, e.g. my_function(uint64, string[]).

Link to this section Summary

Types

t()

Struct to represent a function and its input and output types.

Functions

Decodes a function selector to a struct.

Decodes the given type-string as a simple array of types.

Decodes the given type-string as a single type.

Encodes a function call signature.

Encodes the given single type as a type-string.

Link to this section Types

Specs

t() :: %ABI.FunctionSelector{
  function: String.t() | nil,
  input_names: [String.t()],
  inputs_indexed: [boolean()],
  method_id: String.t() | nil,
  returns: [type()],
  type: :event | :function | :constructor | :error,
  types: [type()]
}

Struct to represent a function and its input and output types.

  • :function - Name of the function
  • :types - Function's input types
  • :returns - Function's return types
  • :method_id - First four bits of the hashed function signature
  • :input_names - Names of the input values (argument names)
  • :type - The type of the selector. Events are part of the ABI, but are not considered functions
  • :inputs_index - A list of true/false values denoting if each input is indexed. Only populated for events.

Specs

type() ::
  {:uint, integer()}
  | :bool
  | :string
  | :address
  | :function
  | {:array, type()}
  | {:array, type(), non_neg_integer()}
  | {:tuple, [type()]}
  | :bytes
  | {:bytes, non_neg_integer()}
  | {:ufixed, non_neg_integer(), non_neg_integer()}
  | {:fixed, non_neg_integer(), non_neg_integer()}
  | {:int, integer()}

Link to this section Functions

Link to this function

decode(signature)

Decodes a function selector to a struct.

Examples

iex> ABI.FunctionSelector.decode("bark(uint256,bool)")
%ABI.FunctionSelector{
  function: "bark",
  types: [
    {:uint, 256},
    :bool
  ],
  returns: []
}

iex> ABI.FunctionSelector.decode("growl(uint,address,string[])")
%ABI.FunctionSelector{
  function: "growl",
  types: [
    {:uint, 256},
    :address,
    {:array, :string}
  ],
  returns: []
}

iex> ABI.FunctionSelector.decode("rollover()")
%ABI.FunctionSelector{
  function: "rollover",
  types: [],
  returns: []
}

iex> ABI.FunctionSelector.decode("do_playDead3()")
%ABI.FunctionSelector{
  function: "do_playDead3",
  types: [],
  returns: []
}

iex> ABI.FunctionSelector.decode("pet(address[])")
%ABI.FunctionSelector{
  function: "pet",
  types: [
    {:array, :address}
  ],
  returns: []
}

iex> ABI.FunctionSelector.decode("paw(string[2])")
%ABI.FunctionSelector{
  function: "paw",
  types: [
    {:array, :string, 2}
  ],
  returns: []
}

iex> ABI.FunctionSelector.decode("scram(uint256[])")
%ABI.FunctionSelector{
  function: "scram",
  types: [
    {:array, {:uint, 256}}
  ],
  returns: []
}

iex> ABI.FunctionSelector.decode("shake((string))")
%ABI.FunctionSelector{
  function: "shake",
  types: [
    {:tuple, [:string]}
  ],
  returns: []
}
Link to this function

decode_raw(type_string)

Decodes the given type-string as a simple array of types.

Examples

iex> ABI.FunctionSelector.decode_raw("string,uint256")
[:string, {:uint, 256}]

iex> ABI.FunctionSelector.decode_raw("")
[]
Link to this function

decode_type(single_type)

Decodes the given type-string as a single type.

Examples

iex> ABI.FunctionSelector.decode_type("uint256")
{:uint, 256}

iex> ABI.FunctionSelector.decode_type("(bool,address)")
{:tuple, [:bool, :address]}

iex> ABI.FunctionSelector.decode_type("address[][3]")
{:array, {:array, :address}, 3}
Link to this function

encode(function_selector)

Encodes a function call signature.

Examples

iex> ABI.FunctionSelector.encode(%ABI.FunctionSelector{
...>   function: "bark",
...>   types: [
...>     {:uint, 256},
...>     :bool,
...>     {:array, :string},
...>     {:array, :string, 3},
...>     {:tuple, [{:uint, 256}, :bool]}
...>   ]
...> })
"bark(uint256,bool,string[],string[3],(uint256,bool))"
Link to this function

encode_type(single_type)

Encodes the given single type as a type-string.

Examples

iex> ABI.FunctionSelector.encode_type({:uint, 256})
"uint256"

iex> ABI.FunctionSelector.encode_type({:tuple, [:bool, :address]})
"(bool,address)"

iex> ABI.FunctionSelector.encode_type({:array, {:array, :address}, 3})
"address[][3]"
Link to this function

simple_types?(list, item)

Specs

simple_types?([map()], map()) :: boolean()