Watch Transfers
Subscribe to real-time token transfer events on EVM or Solana.
Installs viem, @solana/web3.js
"use client";
import { useWatchContractEvent } from "wagmi";
import { useState } from "react";
import { formatUnits } from "viem";
const erc20TransferEventAbi = [
{
name: "Transfer",
type: "event",
inputs: [
{ name: "from", type: "address", indexed: true },
{ name: "to", type: "address", indexed: true },
{ name: "value", type: "uint256", indexed: false },
],
},
] as const;
type TransferLog = { from: string; to: string; value: string; txHash: string };
export function WatchTransfers() {
const [tokenAddress, setTokenAddress] = useState("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"); // USDC
const [transfers, setTransfers] = useState<TransferLog[]>([]);
const [decimals] = useState(6);
useWatchContractEvent({
address: tokenAddress as `0x${string}`,
abi: erc20TransferEventAbi,
eventName: "Transfer",
onLogs(logs) {
const newTransfers = logs.map((log) => ({
from: log.args.from as string,
to: log.args.to as string,
value: formatUnits(log.args.value as bigint, decimals),
txHash: log.transactionHash,
}));
setTransfers((prev) => [...newTransfers, ...prev].slice(0, 50));
},
});
return (
<div>
<h2>Watch Token Transfers</h2>
<input value={tokenAddress} onChange={(e) => setTokenAddress(e.target.value)} placeholder="Token contract address" />
<p>{transfers.length} transfers captured</p>
<div style={{ maxHeight: "400px", overflow: "auto" }}>
{transfers.map((t) => (
<div key={t.txHash} style={{ borderBottom: "1px solid #eee", padding: "0.5rem 0", fontSize: "0.85rem" }}>
<p>{t.from.slice(0, 10)}... → {t.to.slice(0, 10)}...</p>
<p><strong>{t.value}</strong> tokens</p>
</div>
))}
</div>
</div>
);
}Watch Transfers — Learn
Real-time blockchain data
Blockchains are append-only: new blocks are added continuously. To build real-time UIs, you need to listen for new data as it's produced.
EVM: Event subscriptions
EVM smart contracts can emit "events" — structured log entries that are stored in transaction receipts. The ERC-20 standard defines:
event Transfer(address indexed from, address indexed to, uint256 value);
The indexed keyword means you can filter by from or to in your subscription:
// Watch only transfers TO your address
useWatchContractEvent({
address: usdcAddress,
abi: erc20Abi,
eventName: "Transfer",
args: { to: myAddress }, // Filter by indexed param
onLogs(logs) { ... },
});
Under the hood, wagmi opens a WebSocket connection to the RPC node and uses eth_subscribe to receive new events in real-time.
Solana: Log subscriptions
Solana programs don't have typed events like EVM. Instead, they emit log messages via msg!() or emit!() (Anchor). You subscribe to logs that mention a specific account:
connection.onLogs(mintAddress, (logInfo) => {
// logInfo.logs = array of raw log strings
// logInfo.signature = transaction signature
});
For structured events, Anchor programs use emit!() which encodes event data in a standardized format. Libraries like @coral-xyz/anchor can parse these.
Polling vs WebSocket
WebSocket (preferred): Real-time push notifications. Lower latency, less RPC usage. But connections can drop and need reconnection logic.
Polling: Periodically call getLogs or getSignaturesForAddress. More reliable but higher latency and more RPC calls.
For production apps, use WebSocket for the real-time feed with polling as a fallback to catch any events missed during disconnections.
Performance considerations
Popular tokens (USDC, USDT, WETH) generate enormous volumes of events:
- USDC on Ethereum: ~1,000+ transfers per block (~12 seconds)
- USDC on Solana: thousands of transactions per second
For high-volume tokens:
- Filter server-side when possible (indexed params on EVM)
- Buffer and batch UI updates (don't re-render per event)
- Consider using an indexer (The Graph, Helius) instead of raw RPC subscriptions