W3WS.Replayer (w3ws v0.2.1)
W3WS Replayer is a Task
that executes each replay in parallel. Each
replay fetches the logs, decodes if possible, and calls the handler with
each event. Each replay iterates from the from_block
to the to_block
using chunk_size
number of blocks. When the replayer has reached the
to_block
it will stop and shutdown.
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 replay specific, so different ABIs can be used to decode different events using separate replays.
Replayer Options
Options for the replayer.
:abi
- (optional) ABI used to decode events.:abi_files
- (optional) List of paths to ABI files for decoding events.:chunk_size
- (optional) Number of blocks to fetch at a time. See replay options.:chunk_sleep
- (optional) Number of milliseconds to sleep between chunks. Defaults to 10 seconds.:context
- (optional) Context to pass to the handler. Defaults to%{}
.:from_block
- (optional) Starting block to replay from. See replayer options.:handler
- (optional) Handler to call with the event. Defaults to logging the event.:to_block
- (optional) Ending block to replay to. See replayer options.:replays
- List of replays to execute. See replay options.:uri
- Websocket JSON-RPC server URI to connect to.
Replay Options
Common options are inherited from the replayer options if present but can be overridden for a specific replay. This allows you to define common arguments for the entire replayer and then only specify differentiating args for each replay.
W3WS.Replayer.replay(
abi_files: abi_paths,
uri: uri,
from_block: 1_000_000,
chunk_size: 10_000,
handler: MyApp.Handler,
replays: [
[address: "0x0000000000000000000000000000000000000000", topics: ["Transfer"]],
[address: "0x9999999999999999999999999999999999999999", topics: ["Mint"]]
[
address: "0x5555555555555555555555555555555555555555",
from_block: 1_500_000,
handler: MyApp.SwapHandler,
topics: ["Swap"]
]
]
)
:abi
- (optional) ABI used to decode events.:abi_files
- (optional) List of paths to ABI files for decoding events.:address
- (optional) Address to filter events.:chunk_size
- (optional) Number of blocks to fetch at a time. Defaults to5_000
.:chunk_sleep
- (optional) Number of milliseconds to sleep between chunks. Defaults to 10 seconds.:context
- (optional) Context to pass to the handler. Defaults to%{}
.:from_block
- (optional) Starting block to replay from. Defaults to{:before_current, 500_000}
which will resolve to 500k blocks before the current block.:handler
- (optional) Handler to call with the event. Defaults toW3WS.Handler.DefaultHandler
.:to_block
- (optional) Ending block to replay to. Defaults to the current block at the time the replayer starts.:topics
- (optional) List of topics to filter events.
Summary
Functions
Run then replayer linked to the caller. The returned task must be awaited.
Run the replayer linked to the caller, waiting until it completes.
Start a replayer task linked to the given supervisor. The task is not linked
to the caller. Defaults :restart
to :transient
but a different option can
be passed.
Types
chunk_size()
@type chunk_size() :: pos_integer()
from_block()
@type from_block() :: pos_integer() | String.t() | {:before_current, pos_integer()}
replay_args()
@type replay_args() :: [ abi: [map()] | nil, abi_files: [String.t()] | nil, address: String.t() | nil, chunk_size: chunk_size() | nil, chunk_sleep: pos_integer() | nil, context: map() | nil, from_block: from_block() | nil, handler: W3WS.Handler.t() | nil, to_block: to_block() | nil, topics: [String.t()] | nil ]
replayer_args()
@type replayer_args() :: [ abi: [map()] | nil, abi_files: [String.t()] | nil, chunk_size: chunk_size() | nil, chunk_sleep: pos_integer() | nil, context: map() | nil, from_block: from_block() | nil, handler: W3WS.Handler.t() | nil, to_block: to_block() | nil, replays: [replay_args()], uri: String.t() ]
to_block()
@type to_block() :: pos_integer() | String.t()
Functions
async(args)
@spec async(args :: replayer_args()) :: Task.t() | {:error, [term()]}
Run then replayer linked to the caller. The returned task must be awaited.
Examples
replayer = W3WS.Replayer.async(args)
do_other_stuff()
Task.await(replayer)
replay(args, timeout \\ :infinity)
@spec replay(args :: replayer_args(), timeout :: timeout()) :: :ok
Run the replayer linked to the caller, waiting until it completes.
Examples
W3WS.Replayer.replay(args)
start(supervisor, args, options \\ [])
@spec start( supervisor :: Supervisor.supervisor(), args :: replayer_args(), options :: keyword() ) :: DynamicSupervisor.on_start_child()
Start a replayer task linked to the given supervisor. The task is not linked
to the caller. Defaults :restart
to :transient
but a different option can
be passed.
Examples
{:ok, task} = W3WS.Replayer.start(MyApp.TaskSupervisor, args)