Block Gas Tracker is a Bun-based TypeScript server that streams real-time block and gas metrics for multiple EVM-compatible blockchains. Clients connect via WebSocket and can subscribe to one or more chains to receive block data and metrics. The project uses Winston for debug-level logging with log rotation.
- Real-time block and gas metrics for multiple EVM chains
- WebSocket API with multi-chain subscriptions per client
- Debug-level logging with Winston and log rotation (3 days)
- Install dependencies:
bun install
- Copy
.env.exampleto.env:cp .env.example .env
- Configure your environment by updating the
.envfile with your WebSocket RPC URLs for each chain (see Environment Variables section below). - Start the server:
bun run server.ts
Set the following environment variables for RPC URLs (example names, see config/chains.ts for all):
WSS_MAINNET_URLWSS_BSC_URLWSS_ARBITRUM_URLWSS_BASE_URLWSS_AVALANCHE_URLWSS_SONIC_URLWSS_OPTIMISM_URLWSS_MANTLE_URLWSS_POLYGON_URLWSS_LINEA_URLSERVER_PORT(optional, defaults to 8080)
This project uses Winston for logging. Logs are written to logs/application-%DATE%.log and rotated daily, retaining logs for 3 days. Console output is also enabled at debug level.
- Endpoint:
ws://localhost:8080 - How it works:
Connect via WebSocket and send subscription/unsubscription messages for supported chains. The server streams block data for each subscribed chain.
Client → Server:
- Subscribe:
{ "action": "subscribe", "chainId": "1" } - Unsubscribe:
{ "action": "unsubscribe", "chainId": "1" }
Server → Client:
- On each new block:
{ "blockNumber": "18456789", "baseFeePerGas": "1234567890", "gasUsed": "21000", "gasLimit": "30000000", "gasPrice": "1234567890" }
const ws = new WebSocket("ws://localhost:8080");
ws.onopen = () => {
ws.send(JSON.stringify({ action: "subscribe", chainId: "1" }));
ws.send(JSON.stringify({ action: "subscribe", chainId: "137" })); // Polygon
};
ws.onmessage = (msg) => console.log(JSON.parse(msg.data));
// To unsubscribe:
ws.send(JSON.stringify({ action: "unsubscribe", chainId: "1" }));See config/chains.ts for supported chain IDs and RPC URLs. Example:
| Chain Name | Chain ID | Env Variable |
|---|---|---|
| Ethereum | 1 | WSS_MAINNET_URL |
| BSC | 56 | WSS_BSC_URL |
| Arbitrum | 42161 | WSS_ARBITRUM_URL |
| Polygon | 137 | WSS_POLYGON_URL |
| ... | ... | ... |
- Bun (runtime)
- TypeScript
- viem (EVM chain client)
- Winston & winston-daily-rotate-file (logging)
MIT