W3WS.Replayer (w3ws v0.3.1)

A Task that replays past events.

W3WS.Replayer 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 to 5_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 to W3WS.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

Link to this type

chunk_size()

@type chunk_size() :: pos_integer()
Link to this type

from_block()

@type from_block() :: pos_integer() | String.t() | {:before_current, pos_integer()}
Link to this type

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
]
Link to this type

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()
]
@type to_block() :: pos_integer() | String.t()

Functions

@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)
Link to this function

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)
Link to this function

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)