ABI.FunctionSelector (abi v0.1.21)

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

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.

Parse a function selector, e.g. from an abi.json file.

Types

@type t() :: %ABI.FunctionSelector{
  function: String.t(),
  names: nil | [String.t() | nil],
  returns: type(),
  types: [type()]
}
@type type() ::
  {:uint, integer()}
  | :bool
  | :bytes
  | :string
  | :address
  | {:array, type()}
  | {:array, type(), non_neg_integer()}
  | {:tuple, [type()]}
  | {:struct, String.t(), [type()], [String.t()]}

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
  ]
}

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

iex> ABI.FunctionSelector.decode("growl(uint,address,string[])")
%ABI.FunctionSelector{
  function: "growl",
  types: [
    {:uint, 256},
    :address,
    {: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: [
    {:array, :address}
  ]
}

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

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

iex> ABI.FunctionSelector.decode("shake((string))")
%ABI.FunctionSelector{
  function: "shake",
  types: [
    {:tuple, [:string]}
  ]
}
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

parse_specification_item(item)

Parse a function selector, e.g. from an abi.json file.

Examples

iex> ABI.FunctionSelector.parse_specification_item(%{"type" => "function", "name" => "fun", "inputs" => [%{"name" => "a", "type" => "uint96", "internalType" => "uint96"}]})
%ABI.FunctionSelector{function: "fun", types: [uint: 96], names: ["a"], returns: nil}

iex> ABI.FunctionSelector.parse_specification_item(%{"type" => "function", "name" => "fun", "inputs" => [%{"name" => "s", "type" => "tuple", "internalType" => "tuple", "components" => [%{"name" => "a", "type" => "uint256", "internalType" => "uint256"},%{"name" => "b", "type" => "address", "internalType" => "address"},%{"name" => "c", "type" => "bytes", "internalType" => "bytes"}]},%{"name" => "d", "type" => "uint256", "internalType" => "uint256"}],"outputs" => [%{"name" => "", "type" => "bytes", "internalType" => "bytes"}],"stateMutability" => "nonpayable"})
%ABI.FunctionSelector{function: "fun", types: [{:tuple, [{:uint, 256}, :address, :bytes]}, {:uint, 256}], names: ["s", "d"], returns: :bytes}

iex> ABI.FunctionSelector.parse_specification_item(%{"type" => "function", "name" => "fun", "inputs" => [%{"name" => "s", "type" => "tuple", "internalType" => "struct Contract.Struct", "components" => [%{"name" => "a", "type" => "uint256", "internalType" => "uint256"},%{"name" => "b", "type" => "address", "internalType" => "address"},%{"name" => "c", "type" => "bytes", "internalType" => "bytes"}]},%{"name" => "d", "type" => "uint256", "internalType" => "uint256"}],"outputs" => [%{"name" => "", "type" => "bytes", "internalType" => "bytes"}],"stateMutability" => "nonpayable"})
%ABI.FunctionSelector{function: "fun", types: [{:struct, "Contract.Struct", [{:uint, 256}, :address, :bytes], ["a", "b", "c"]}, {:uint, 256}], names: ["s", "d"], returns: :bytes}

iex> ABI.FunctionSelector.parse_specification_item(%{"type" => "function", "name" => "fun", "inputs" => [%{"name" => "s", "type" => "tuple", "internalType" => "struct Contract.Struct", "components" => [%{"name" => "a", "type" => "uint256", "internalType" => "uint256"},%{"type" => "address", "internalType" => "address"},%{"name" => "c", "type" => "bytes", "internalType" => "bytes"}]},%{"name" => "d", "type" => "uint256", "internalType" => "uint256"}],"outputs" => [%{"name" => "", "type" => "bytes", "internalType" => "bytes"}],"stateMutability" => "nonpayable"})
%ABI.FunctionSelector{function: "fun", types: [{:struct, "Contract.Struct", [{:uint, 256}, :address, :bytes], ["a", "var1", "c"]}, {:uint, 256}], names: ["s", "d"], returns: :bytes}

iex> ABI.FunctionSelector.parse_specification_item(%{"type" => "function", "name" => "fun", "inputs" => [%{"type" => "tuple", "internalType" => "struct Contract.Struct", "components" => [%{"name" => "a", "type" => "uint256", "internalType" => "uint256"},%{"type" => "address", "internalType" => "address"},%{"name" => "c", "type" => "bytes", "internalType" => "bytes"}]},%{"name" => "d", "type" => "uint256", "internalType" => "uint256"}],"outputs" => [%{"name" => "", "type" => "bytes", "internalType" => "bytes"}],"stateMutability" => "nonpayable"})
%ABI.FunctionSelector{function: "fun", types: [{:struct, "Contract.Struct", [{:uint, 256}, :address, :bytes], ["a", "var1", "c"]}, {:uint, 256}], names: [nil, "d"], returns: :bytes}