W3WS.Listener (w3ws v0.3.1)
W3WS Listener which handles the websocket connection and subscribes to configured ethereum events.
If an ABI is provided and the event has a matching selector in the ABI
the event will be decoded and passed to the handler in the Env
. If no matching
selector is found the non-decoded event will be passed to the handler.
ABIs are subscription specific, so different ABIs can be used to decode different events using separate subscriptions.
See W3WS.Listener.start_link/1
for all configuration options.
Summary
Functions
Returns a specification to start this module under a supervisor.
Returns the last block number received by block_ping
. If block_ping
is not enabled
or the first block_ping
response hasn't been received yet, nil
is returned.
Start the listener
Types
listener_config()
@type listener_config() :: [ uri: String.t(), block_ping: pos_integer() | nil, resubscribe: pos_integer() | nil, subscriptions: [subscription_config()] | nil ]
subscription_config()
Functions
child_spec(init_arg)
Returns a specification to start this module under a supervisor.
See Supervisor
.
get_block(pid)
Returns the last block number received by block_ping
. If block_ping
is not enabled
or the first block_ping
response hasn't been received yet, nil
is returned.
start_link(config)
@spec start_link(listener_config()) :: {:ok, pid()}
Start the listener
Examples
Start a listener with no subscriptions
{:ok, listener} = W3WS.Listener.start_link(uri: "http://localhost:8545")
Start a listener with subscriptions
{:ok, listener} = W3WS.Listener.start_link(
uri: "http://localhost:8545",
block_ping: :timer.seconds(10),
resubscribe: :timer.minutes(5),
subscriptions: [
[
abi_files: ["path/to/abi.json"],
context: %{chain_id: 1},
handler: W3WS.Handler.DefaultHandler,
topics: ["Transfer"],
address: "0x73d578371eb449726d727376393b02bb3b8e6a57"
]
]
)