Skip to content

horizon-eth/Continuous-Event-Listener-Bun-Viem

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Continuous Event Listener Bun.sh Viem.sh

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.

Features

  • 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

Prerequisites

  • Bun v1.1.30 or higher
  • An Ethereum WebSocket provider URL

Tech Stack

Installation

  1. Clone the repository:
git clone https://github.com/horizon-eth/continuous-event-listener-bun-viem.git
cd continuous-event-listener-bun-viem
  1. Install dependencies:
bun install
  1. Create a .env file based on example.env:
cp example.env .env
  1. 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

Usage

Start the event listener:

bun start

Supported Event Types

ERC20 Token Events

  • Transfer
  • Approval

Uniswap V2 Pool Events

  • Swap
  • Sync
  • Mint
  • Burn

Uniswap V3 Pool Events

  • Swap
  • Flash
  • Mint
  • Burn
  • Collect
  • IncreaseObservationCardinalityNext
  • SetFeeProtocol
  • CollectProtocol

Configuration

The project uses environment variables for configuration. See example.env for all available options:

  • PROVIDER_WSS_URL: WebSocket provider URL for Ethereum network
  • TYPE: Contract type ("TOKEN", "UNISWAPV2", or "UNISWAPV3")
  • ADDRESS: Contract address to monitor
  • EVENT_NAME: Event name to listen for

Performance Optimization

For optimal event listening performance:

  1. Use a reliable WebSocket provider

  2. 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,
      }),
    });
  3. Adjust polling intervals based on your needs:

    watchContractEvent({
      // ... other options
      poll: true,
      pollingInterval: 1000, // Adjust based on requirements
      batch: false, // Process events immediately
    });

Development

The project uses TypeScript with strict type checking. Key files:

  • index.ts: Application entrypoint
  • src/abi/abi.ts: Contract ABIs
  • src/callback/callback.ts: Event processing logic
  • src/config/client.ts: Client configs
  • src/config/config.ts: Application configs
  • src/listener/listener.ts: Event listening logic
  • src/utils/utils.ts: Utility functions

Contributing

  1. Fork the repository
  2. Create your feature branch
  3. Commit your changes
  4. Push to the branch
  5. Create a new Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published