A TypeScript project for real-time event listening using viem.sh and Bun runtime. This project provides high-performance event listening capabilities for ERC20 tokens and Uniswap V2/V3 pools.
- Real-time event monitoring using WebSocket connections
- Support for multiple contract types:
- ERC20 Tokens (Transfer, Approval events)
- Uniswap V2 Pools (Swap, Sync, Mint, Burn events)
- Uniswap V3 Pools (Swap, Flash, Mint, Burn, Collect events)
- Type-safe contract interactions using viem.sh
- High-performance execution with Bun runtime
- Configurable through environment variables
- Bun v1.1.30 or higher
- An Ethereum WebSocket provider URL
- Runtime: Bun - A fast all-in-one JavaScript runtime
- Ethereum Client: viem v2.23.6 - Type-safe Ethereum interactions
- Smart Contract Standards:
- @openzeppelin/contracts v5.2.0
- @uniswap/v2-core v1.0.1
- @uniswap/v3-core v1.0.1
- Clone the repository:
git clone https://github.com/horizon-eth/continuous-event-listener-bun-viem.git
cd continuous-event-listener-bun-viem
- Install dependencies:
bun install
- Create a
.env
file based onexample.env
:
cp example.env .env
- Configure your environment variables in
.env
:
PROVIDER_WSS_URL="your_websocket_provider_url"
TYPE="TOKEN" # or "UNISWAPV2" or "UNISWAPV3"
ADDRESS="your_contract_address"
EVENT_NAME="Transfer" # or other supported events
Start the event listener:
bun start
- Transfer
- Approval
- Swap
- Sync
- Mint
- Burn
- Swap
- Flash
- Mint
- Burn
- Collect
- IncreaseObservationCardinalityNext
- SetFeeProtocol
- CollectProtocol
The project uses environment variables for configuration. See example.env
for all available options:
PROVIDER_WSS_URL
: WebSocket provider URL for Ethereum networkTYPE
: Contract type ("TOKEN", "UNISWAPV2", or "UNISWAPV3")ADDRESS
: Contract address to monitorEVENT_NAME
: Event name to listen for
For optimal event listening performance:
-
Use a reliable WebSocket provider
-
Configure WebSocket connection parameters:
const client_wss = createPublicClient({ chain: mainnet, transport: webSocket(PROVIDER_WSS_URL, { keepAlive: true, reconnect: true, retryCount: 10, retryDelay: 100, timeout: 10000, }), });
-
Adjust polling intervals based on your needs:
watchContractEvent({ // ... other options poll: true, pollingInterval: 1000, // Adjust based on requirements batch: false, // Process events immediately });
The project uses TypeScript with strict type checking. Key files:
index.ts
: Application entrypointsrc/abi/abi.ts
: Contract ABIssrc/callback/callback.ts
: Event processing logicsrc/config/client.ts
: Client configssrc/config/config.ts
: Application configssrc/listener/listener.ts
: Event listening logicsrc/utils/utils.ts
: Utility functions
- Fork the repository
- Create your feature branch
- Commit your changes
- Push to the branch
- Create a new Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.