W3WS.Util (w3ws v0.2.0)

Utility functions

Summary

Functions

Call the handler with the given event

Try to decode the event with the given ABI and apply the handler. The handler is always called, even if the event could not be decoded.

Convert a hex string to a binary

Convert a hex string to an integer

Calculate the keccak256 hash of a binary, optionally encoding as hex

Try to decode an event if an ABI is present

Resolve the ABI from the given options

Convert a binary to hex

Functions

Link to this function

apply_handler(env, handler)

@spec apply_handler(
  env :: W3WS.Env.t(),
  handler :: W3WS.Handler.t()
) :: pid() | {pid(), reference()}

Call the handler with the given event

Examples

iex> apply_handler(%W3WS.Env{}, fn _env -> :ok end) |> is_pid()
true

iex> apply_handler(%W3WS.Env{}, W3WS.Handler.DefaultHandler) |> is_pid()
true

iex> apply_handler(%W3WS.Env{}, {W3WS.Handler.DefaultHandler, :handle, []}) |> is_pid()
true

iex> apply_handler(%W3WS.Env{}, "something")
** (RuntimeError) Invalid handler: "something"
Link to this function

decode_apply(env, abi, handler)

@spec decode_apply(W3WS.Env.t(), W3WS.ABI.t() | nil, W3WS.Handler.t()) ::
  pid() | {pid(), reference()}

Try to decode the event with the given ABI and apply the handler. The handler is always called, even if the event could not be decoded.

@spec from_hex(String.t()) :: binary()

Convert a hex string to a binary

Examples

iex> from_hex("0x010203")
<<1, 2, 3>>

iex> from_hex("0x")
""

iex> from_hex("")
""
Link to this function

integer_from_hex(arg)

@spec integer_from_hex(String.t()) :: integer()

Convert a hex string to an integer

Examples

iex> integer_from_hex("0x7")
7
Link to this function

keccak(binary, opts \\ [])

@spec keccak(binary(), Keyword.t()) :: String.t()

Calculate the keccak256 hash of a binary, optionally encoding as hex

Examples

iex> keccak("foo", hex?: true)
"0x41b1a0649752af1b28b3dc29a1556eee781e4a4c3a1f7f53f90fa834de098c4d"
Link to this function

maybe_decode_event(env, abi)

@spec maybe_decode_event(W3WS.Env.t(), W3WS.ABI.t() | nil) :: W3WS.Env.t()

Try to decode an event if an ABI is present

Link to this function

resolve_abi(opts)

Resolve the ABI from the given options

Examples

iex> resolve_abi(abi: [])
[]

iex> resolve_abi(abi: nil)
nil

iex> resolve_abi(abi: [], abi_files: ["./test/support/files/test_abi.json"])
[
  %ABI.FunctionSelector{
    function: "Transfer", 
    method_id: <<221, 242, 82, 173>>, 
    type: :event, 
    inputs_indexed: [false, false, false], 
    state_mutability: nil, 
    input_names: ["from", "to", "value"], 
    types: [:address, :address, {:uint, 256}], 
    returns: [], 
    return_names: []
  }
]
@spec to_hex(binary() | integer()) :: String.t()

Convert a binary to hex

Examples

iex> to_hex(<<1, 2, 3>>)
"0x010203"

iex> to_hex(1)
"0x1"