Skip to main content
Contracts emit events to record what happened, such as a token transfer. This recipe watches Transfer events from USDC on Sei Mainnet as they happen. Then it fetches past Transfer events. USDC has many transfers, so new events usually arrive within a minute.

Pick the right endpoint

Use a different public endpoint for each task:
Watch live events over WebSocket. Watchers that poll the public HTTP endpoints for new events can miss events or report them twice. Fetch past events over HTTP, because the public WebSocket endpoints do not serve eth_getLogs.

Install

The TypeScript examples use top-level await. Save each one as a .mts file, such as script.mts. Run it with npx tsx script.mts on Node.js 18 or later. In a new npm project, tsx compiles a plain .ts file as CommonJS, where top-level await does not work.

Watch new events

Each script prints a line for every USDC transfer. The script runs until you stop it with Ctrl+C.
You are done when you see:
The scripts print the full sender and recipient addresses. The example output above shortens them. Sei has instant finality, so each event is final when it arrives. You do not need to wait for confirmations or handle reorganizations.
The web3.py example uses the subscription manager, which needs web3.py 7.7 or later.

Fetch past events

eth_getLogs returns the events in a block range. On the public endpoints, one request can cover at most 2,000 blocks. A larger range fails with block range too large. This example fetches the transfers from the latest 1,000 blocks.
You are done when you see:
To cover a longer range, split it into windows of 2,000 blocks or fewer. Request the windows one after another. For a full history of a contract, use an indexer instead of eth_getLogs.