keywatch_src20_events_with_key

Watch SRC20 events with an explicit viewing key

Watch SRC20 Transfer and Approval events using an explicit viewing key, without requiring wallet authentication.

Overview

watch_src20_events_with_key() and async_watch_src20_events_with_key() create event watchers that poll for SRC20 events using a provided viewing key. Unlike watch_src20_events, these functions do not require encryption state or private key — only a plain Web3 instance and the 32-byte viewing key.

This is useful when:

  • You have a viewing key but don't have the wallet's private key

  • You want to watch someone else's events (if they shared their viewing key)

  • You want to avoid the RPC call to fetch the key from Directory

Signature

def watch_src20_events_with_key(
    w3: Web3,
    *,
    viewing_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",
) -> SRC20EventWatcher

async def async_watch_src20_events_with_key(
    w3: AsyncWeb3,
    *,
    viewing_key: Bytes32,
    token_address: ChecksumAddress | None = None,
    on_transfer: AsyncTransferCallback | TransferCallback | None = None,
    on_approval: AsyncApprovalCallback | ApprovalCallback | None = None,
    on_error: AsyncErrorCallback | ErrorCallback | None = None,
    poll_interval: float = 2.0,
    from_block: int | str = "latest",
) -> AsyncSRC20EventWatcher

Parameters

Parameter
Type
Required
Description

w3

Web3 or AsyncWeb3

Yes

Standard Web3 instance (no Seismic namespace required)

viewing_key

Yes

32-byte AES-256 viewing key

token_address

ChecksumAddress

No

SRC20 contract to filter. If None, watches all SRC20 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 (decryption failures, RPC errors)

poll_interval

float

No

Seconds between polls (default 2.0)

from_block

int or "latest"

No

Starting block number or "latest" (default)

Returns

Type
Description

Sync watcher (already started)

Async watcher (already started)

Examples

Basic Usage (Sync)

Watch Specific Token

Read-Only Monitoring

With Context Manager

Error Handling

Historical Events

Async Usage

Async Context Manager

Monitor Multiple Keys

How It Works

  1. Compute key hash - Calculates keccak256(viewing_key) for event filtering

  2. Create watcher - Instantiates SRC20EventWatcher or AsyncSRC20EventWatcher with the viewing key

  3. Start polling - Begins background polling (thread for sync, task for async)

  4. Process events - For each poll:

    • Fetches logs via eth_getLogs with key hash filter

    • Decodes event topics and data

    • Decrypts encrypted amounts using AES-256-GCM

    • Invokes callbacks with decrypted data

Callback Signatures

Transfer Callback

See DecryptedTransferLog for field details.

Approval Callback

See DecryptedApprovalLog for field details.

Error Callback

Notes

  • Does not require EncryptionState or wallet private key

  • Works with standard Web3 or AsyncWeb3 instances

  • No RPC calls are made to fetch the viewing key (unlike watch_src20_events)

  • The watcher starts automatically upon creation

  • For sync version, polls in a background daemon thread

  • For async version, runs as an asyncio.Task

  • Events are only visible if they match your key hash

  • Call .stop() or use context manager to clean up

Warnings

  • Key security - Keep viewing keys secure; they allow decrypting all events

  • Shared keys - Be cautious when sharing viewing keys; recipients can see all events

  • Network connectivity - Polls will fail silently if RPC connection drops (check on_error)

  • Thread safety - Sync version spawns a daemon thread; ensure proper cleanup

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

See Also

Last updated