Uniswap V2 is a decentralized exchange protocol that allows users to trade tokens without the need for a trusted intermediary. By listening to pool events in Uniswap V2, developers can gain insights into key actions such as swaps, liquidity provision, and token burns/mints, helping them create applications with real-time data updates.
This repository provides examples of how to listen for various events in a Uniswap V2 pool using ethers v5.7.2 and ethers v6.0.0.
It works across most EVM-compatible chains, but if you experience any issues, feel free to reach out for assistance.
-
Rename
.env.example
to.env
and provide the following environment variables:WSS_PROVIDER_URL
: WebSocket URL for your provider (e.g., Infura, Alchemy).POOL_ADDRESS
: The address of the Uniswap V2 pool.EVENT_NAME
: The event you want to listen to (see below for event names).
-
Install project dependencies by running the following command:
npm install
Uniswap V2 pool events allow you to listen to key actions within a liquidity pool. These events can provide valuable insights for tracking liquidity, swaps, and reserves.
Event Name | Returns |
---|---|
Swap | (sender, amount0In, amount1In, amount0Out, amount1Out, to) |
Sync | (reserve0, reserve1) |
Mint | (sender, amount0, amount1) |
Burn | (sender, amount0, amount1, to) |
- Swap: Triggered whenever a swap happens, indicating which tokens were swapped and in what quantity.
- Sync: Triggered when the reserves of the pool are updated.
- Mint: Fired when liquidity is added to the pool.
- Burn: Fired when liquidity is removed from the pool.
In ethers v5, the following methods are used to listen for events emitted by the pool:
pool.on(eventName, listener)
: Listen for an event continuously, reacting to every occurrence of the event.pool.once(eventName, listener)
: Listen for an event once, then stop listening after the first occurrence.pool.off(eventName, listener)
: Remove a specific listener for an event, stopping it from receiving future events.pool.removeListener(eventName, listener)
: Alias foroff
, removing a specific listener.pool.removeAllListeners(eventName)
: Remove all listeners for a specific event.
In ethers v6, the API is slightly adjusted, but the functionality remains similar:
pool.addListener(eventName, listener)
: Listen for an event continuously (alias foron
).pool.on(eventName, listener)
: Same asaddListener
, listens continuously.pool.once(eventName, listener)
: Listen for an event once, then stop.pool.off(eventName, listener)
: Remove a specific listener from an event.pool.removeListener(eventName, listener)
: Alias foroff
, removes a specific listener.pool.removeAllListeners(eventName)
: Remove all listeners for a specific event.
The following methods allow you to query listener information:
Method | Description |
---|---|
pool.listeners(eventName) |
Returns an array of listeners registered for the event. |
pool.listenerCount(eventName) |
Returns the number of listeners registered for the event. |
To query past events in the pool's history, use the following methods.
pool.queryFilter(eventName, fromBlock, toBlock)
:- Fetches historical event data for a specific event.
- Parameters:
eventName
: The event you want to query (optional).fromBlock
(default:0
): The starting block number for the query.toBlock
(default:"latest"
): The ending block number for the query.
pool.queryFilter(eventName, fromBlock, toBlock)
: Same as v5, used to fetch historical events.pool.getEvent(eventName)
:- Returns the event object for a given name.
- Use Case: Helpful when event names conflict with JavaScript reserved words or for programmatically accessing contract events.