ABI.FunctionSelector (abi v0.1.19)

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

Link to this section Summary

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.

Link to this section Types

Link to this type

argument_type()

@type argument_type() :: %{
  :type => type(),
  optional(:name) => String.t(),
  optional(:indexed) => boolean()
}
@type t() :: %ABI.FunctionSelector{
  function: String.t(),
  returns: type(),
  types: [argument_type()]
}
@type type() ::
  {:uint, integer()}
  | :bool
  | :bytes
  | :string
  | :address
  | {:array, type()}
  | {:array, type(), non_neg_integer()}
  | {:tuple, [type()]}

Link to this section Functions

Link to this function

decode(signature)

Decodes a function selector to a struct.

examples

Examples

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

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

iex> ABI.FunctionSelector.decode("bark(uint256 name,bool indexed loud)")
%ABI.FunctionSelector{
  function: "bark",
  types: [
    %{type: {:uint, 256}, name: "name"},
    %{type: :bool, name: "loud", indexed: true}
  ]
}

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

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

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

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

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

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

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

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

decode_raw(type_string)

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

examples

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

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

Examples

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