clockSRC20EventWatcher

Synchronous SRC20 event watcher class

Polling-based SRC20 event watcher that runs in a background thread (synchronous).

Overview

SRC20EventWatcher is a thread-based event watcher that polls for SRC20 Transfer and Approval events, decrypts encrypted amounts using a viewing key, and invokes callbacks with decoded event data.

The watcher runs in a daemon background thread and polls at a configurable interval. It automatically handles block number tracking, log fetching, decryption, and callback invocation.

Typically created via watch_src20_events() or watch_src20_events_with_key() rather than instantiated directly.

Class Definition

class SRC20EventWatcher:
    """Polling-based SRC20 event watcher (sync, runs in a background thread)."""

    def __init__(
        self,
        w3: Web3,
        aes_key: Bytes32,
        *,
        token_address: ChecksumAddress | None = None,
        on_transfer: TransferCallback | None = None,
        on_approval: ApprovalCallback | None = None,
        on_error: ErrorCallback | None = None,
        poll_interval: float = 2.0,
        from_block: int | str = "latest",
    ) -> None

Constructor Parameters

Parameter
Type
Required
Description

w3

Web3

Yes

Web3 instance

aes_key

Yes

32-byte AES-256 viewing key

token_address

ChecksumAddress

No

SRC20 contract to filter. If None, watches all tokens

on_transfer

Callback

No

Callback invoked for Transfer events

on_approval

Callback

No

Callback invoked for Approval events

on_error

Callback

No

Callback for errors

poll_interval

float

No

Seconds between polls (default 2.0)

from_block

int or "latest"

No

Starting block number or "latest" (default)

Methods

start()

Start the background polling thread.

stop()

Signal the polling thread to stop and wait for it to exit.

Properties

is_running

Check if the watcher is currently running.

Returns True if the background thread is alive, False otherwise.

Context Manager Support

The watcher can be used as a context manager:

The watcher starts automatically on __enter__ and stops on __exit__.

Examples

Manual Start/Stop

Context Manager

Check Running Status

Filter by Token Address

Custom Poll Interval

Historical Events

Error Handling

Both Transfer and Approval

How It Works

  1. Thread initialization - Creates a daemon thread named "src20-watcher"

  2. Polling loop - In the background thread:

    • Fetches latest block number

    • Builds eth_getLogs filter with key hash

    • Fetches logs for the block range

    • Decodes and decrypts each log

    • Invokes callbacks with decrypted data

    • Waits for poll_interval seconds

  3. Cleanup - When stopped, signals the thread to exit and waits up to poll_interval * 3 seconds

Callback Signatures

Transfer Callback

See DecryptedTransferLog for field details.

Approval Callback

See DecryptedApprovalLog for field details.

Error Callback

Invoked for:

  • RPC errors (network issues, rate limiting)

  • Decryption failures (wrong key, corrupted data)

  • Any exceptions during log processing

Notes

  • The polling thread is a daemon thread (exits when main thread exits)

  • Thread name is "src20-watcher" for debugging

  • Block tracking is automatic; watcher remembers the last processed block

  • Events are processed in order by block number

  • If on_error is not provided, errors are logged at DEBUG level

  • The watcher computes keccak256(aes_key) once at initialization

Warnings

  • Thread safety - Callbacks run in the background thread; ensure thread-safe operations

  • Daemon thread - Will be forcefully terminated if main thread exits

  • Cleanup required - Always call .stop() or use context manager to clean up

  • Network failures - Errors are silently logged unless on_error is provided

  • Memory usage - Keep poll interval reasonable to avoid excessive log fetching

See Also

Last updated