W3WS.ABI (w3ws v0.2.1)

Ethereum ABI Functions

Summary

Functions

Decodes an event from a raw event

Encodes a list of topics into their keccak hex representation

Loads an ABI from a json decoded ABI spec

Loads ABIs from an Enumerable of abi file paths

Returns a list of ABI fields for the given selector

Types

@type t() :: [
  %ABI.FunctionSelector{
    function: term(),
    input_names: term(),
    inputs_indexed: term(),
    method_id: term(),
    return_names: term(),
    returns: term(),
    state_mutability: term(),
    type: term(),
    types: term()
  }
]

Functions

Link to this function

decode_event(data, abi, topics)

@spec decode_event(
  binary(),
  [
    %ABI.FunctionSelector{
      function: term(),
      input_names: term(),
      inputs_indexed: term(),
      method_id: term(),
      return_names: term(),
      returns: term(),
      state_mutability: term(),
      type: term(),
      types: term()
    }
  ],
  [binary()]
) ::
  {:ok,
   %ABI.FunctionSelector{
     function: term(),
     input_names: term(),
     inputs_indexed: term(),
     method_id: term(),
     return_names: term(),
     returns: term(),
     state_mutability: term(),
     type: term(),
     types: term()
   }, map()}
  | {:error, any()}

Decodes an event from a raw event

Examples

iex> decode_event(
...>   "0x0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000959922be3caee4b8cd9a407cc3ac1c251c2007b10000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000034152420000000000000000000000000000000000000000000000000000000000",
...>   [
...>      %ABI.FunctionSelector{
...>        function: "TokenSupportChange",
...>        method_id: <<1, 72, 203, 165>>,
...>        type: :event,
...>        inputs_indexed: [false, false, false, false],
...>        state_mutability: nil,
...>        input_names: ["supported", "token", "symbol", "decimals"],
...>        types: [:bool, :address, :string, {:uint, 8}],
...>        returns: [],
...>        return_names: []
...>      }
...>   ],
...>   ["0x0148cba56e5d3a8d32fbcea206eae9e449ec0f0def4f642994b3edcd38561deb"]
...> )
{:ok, 
  %ABI.FunctionSelector{
    function: "TokenSupportChange",
    method_id: <<1, 72, 203, 165>>,
    type: :event,
    inputs_indexed: [false, false, false, false],
    state_mutability: nil,
    input_names: ["supported", "token", "symbol", "decimals"],
    types: [:bool, :address, :string, {:uint, 8}],
    returns: [],
    return_names: []
  }, 
  %{
    "decimals" => 18, 
    "supported" => true, 
    "symbol" => "ARB", 
    "token" => "0x959922be3caee4b8cd9a407cc3ac1c251c2007b1"
  }
}
Link to this function

encode_topics(topics, abi)

Encodes a list of topics into their keccak hex representation

Examples

iex> encode_topics([
...>   "0x0000000000000000000000000000000000000000000000000000000000000001", 
...>   "0x0000000000000000000000000000000000000000000000000000000000000002",
...>   "SomeEvent",
...>   ["SomeEvent(uint8,uint8)"],
...>   "MissingAbiEvent(uint8)",
...>  nil
...> ], [
...>   %ABI.FunctionSelector{
...>     function: "SomeEvent",
...>     method_id: <<1, 72, 203, 165>>,
...>     type: :event,
...>     inputs_indexed: [false, false],
...>     state_mutability: nil,
...>     input_names: ["a", "b"],
...>     types: [{:uint, 8}, {:uint, 8}],
...>     returns: [],
...>     return_names: []
...>   } 
...> ])
[
  "0x0000000000000000000000000000000000000000000000000000000000000001", 
  "0x0000000000000000000000000000000000000000000000000000000000000002", 
  "0xf4907308003e0ac1411f27720554a08b629260c5bcd94e153d38a3ad5d4ce8ad", 
  ["0xf4907308003e0ac1411f27720554a08b629260c5bcd94e153d38a3ad5d4ce8ad"], 
  "0x961ac6e850917325cc201160e6c6a650f0be9ec0fcae82c74d760ab6a9c0e7b0", 
  nil
]

Loads an ABI from a json decoded ABI spec

Examples

iex> from_abi([
...>   %{
...>     "name" => "Transfer",
...>     "type" => "event",
...>     "inputs" => [
...>       %{
...>         "name" => "from",
...>         "type" => "address",
...>         "indexed" => false,
...>         "internalType" => "address"
...>       },
...>       %{
...>         "name" => "to",
...>         "type" => "address",
...>         "indexed" => false,
...>         "internalType" => "address"
...>       },
...>       %{
...>         "name" => "value",
...>         "type" => "uint256",
...>         "indexed" => false,
...>         "internalType" => "uint256"
...>       }
...>     ]
...>   }
...> ])
[
  %ABI.FunctionSelector{
    type: :event,
    function: "Transfer",
    method_id: <<221, 242, 82, 173>>,
    input_names: ["from", "to", "value"],
    inputs_indexed: [false, false, false],
    types: [:address, :address, {:uint, 256}]
  }
]
Link to this function

from_files(paths)

Loads ABIs from an Enumerable of abi file paths

Examples

iex> from_files(["./test/support/files/test_abi.json"])
[
  %ABI.FunctionSelector{
    type: :event,
    function: "Transfer",
    method_id: <<221, 242, 82, 173>>,
    input_names: ["from", "to", "value"],
    inputs_indexed: [false, false, false],
    types: [:address, :address, {:uint, 256}]
  }
]
Link to this function

selector_fields(selector)

@spec selector_fields(
  selector :: %ABI.FunctionSelector{
    function: term(),
    input_names: term(),
    inputs_indexed: term(),
    method_id: term(),
    return_names: term(),
    returns: term(),
    state_mutability: term(),
    type: term(),
    types: term()
  }
) :: [map()]

Returns a list of ABI fields for the given selector

Examples

iex> selector_fields(%ABI.FunctionSelector{
...>   function: "SomeEvent", 
...>   method_id: <<1, 72, 203, 165>>,
...>   type: :event,
...>   inputs_indexed: [false, false],
...>   input_names: ["a", "b"],
...>   types: [{:uint, 8}, {:uint, 8}],
...>   returns: [],
...>   return_names: []
...> })
[
  %{name: "a", indexed: false, type: {:uint, 8}},
  %{name: "b", indexed: false, type: {:uint, 8}}
]