Filtering Add Liquidity Events on PancakeSwap DEX on Binance Smart Chain using Web3
Decentralized exchanges (DEXs) have become a crucial part of the cryptocurrency ecosystem, providing users with the ability to trade assets without relying on traditional centralized exchanges. PancakeSwap, built on the Binance Smart Chain (BSC), is one such DEX that has gained popularity for its fast transactions and low fees. In this article, we will explore how to filter and monitor Add Liquidity events on PancakeSwap using the Web3 library in Python.
Setting Up the Environment
Before we dive into the code, ensure that you have the necessary dependencies installed. You can use the following commands to install the required libraries:
pip install web3
Now, let’s take a look at the Python script that filters Add Liquidity events on PancakeSwap:
Topic Filters:
In Ethereum and Binance Smart Chain (BSC), events are identified by their event signature, which is hashed and represented as a topic. The topic_filter
in the script is an array of two such topics which represents the mint function
and the hex version of the pancake-swap V2 router
topic_filter = [
"0x4c209b5fc8ad50758f13e2e1088ba56a560dff690a1c6fef26394f4c03821c4f",
"0x00000000000000000000000010ed43c718714eb63d5aa57b78b54704e256024e"
]
So what exactly does the mint function do?
The mint
function in the PancakeSwap smart contract is responsible for creating liquidity by minting LP (Liquidity Provider) tokens. Here's a brief overview of its key functionalities:
function mint(address to) external lock returns (uint liquidity) {
(uint112 _reserve0, uint112 _reserve1,) = getReserves(); // gas savings
uint balance0 = IERC20(token0).balanceOf(address(this));
uint balance1 = IERC20(token1).balanceOf(address(this));
uint amount0 = balance0.sub(_reserve0);
uint amount1 = balance1.sub(_reserve1);
bool feeOn = _mintFee(_reserve0, _reserve1);
uint _totalSupply = totalSupply; //
// Calculate liquidity based on token balances and reserves
if (_totalSupply == 0) {
// Handle initial liquidity minting
} else {
// Calculate liquidity based on existing total supply
}
// Ensure a positive amount of liquidity is minted
require(liquidity > 0, 'Pancake: INSUFFICIENT_LIQUIDITY_MINTED');
// Mint LP tokens and update balances
_mint(to, liquidity);
_update(balance0, balance1, _reserve0, _reserve1);
// Update last cumulative token values if fees are involved
if (feeOn) kLast = uint(reserve0).mul(reserve1);
// Emit Mint event
emit Mint(msg.sender, amount0, amount1);
}
In the process of refining our Python script for optimal functionality, we specifically tailor it to filter events related to the mint
function. However, it’s important to note that the mint
function can also be utilized for transfers involving liquidity pools, particularly during token purchases and sales.
To ensure our script accurately captures only the AddLiquidity
function and avoids false positives from other uses of the mint
function, we implement a data input filter. This filter scrutinizes the input data of our transactions, allowing the script to focus solely on those transactions that initiate the ‘AddLiquidity’ function. By checking if the data input starts with the function signature of AddLiquidity,
we enhance the precision of our event monitoring.
While not explicitly included in the current code, an additional check can be considered for further refinement. This involves filtering the creation time of the token. Given that liquidity can be added to a token at any point in its lifecycle, incorporating a check for newly created pairs helps narrow our focus. This additional check prevents the script from capturing liquidity additions in tokens that have been in existence for some time, aligning our monitoring efforts with the specific goal of identifying newly created pairs.

So let’s break this image down.
Firs this represent the addLiquidity Event which can be found in here.
Address: Represent the Pair address of the token
Data: Represent the amount of Tokens added vs WBNB in Hexadecimal format.
How This Provides an Edge in Trading
- Early Detection of Liquidity Additions:
- By monitoring the
mint
function events, the script provides early detection of new liquidity additions to PancakeSwap, an early entry into a project can have gargantuan financial returns.
2. Automation of Trading Strategies:
- Traders can automate strategies based on Add Liquidity events triggered by the
mint
function, optimizing their trading decisions.
3. Market Sentiment Analysis:
- Analyzing the frequency and size of liquidity additions helps gauge market sentiment and anticipate potential price movements.
4. Customization for Specific Pairs:
- The script can be tailored to monitor specific pairs of interest by adjusting the
topic_filter
to target events related to those pairs.
I find this method optimal instead of scanning for transactions and having to decode their data input to find information about the creation pairs.

Please note Trading newly created pairs is very dangerous if you do not know how to analyse in-depth a token and I advise against if you are a beginner and to learn more about how to analyse tokens look forward to my upcoming articles.
I have created a GitHub repository Dex Essential Trading Scripts which have scripts that can help you craft your degen tool box and gain an edge on the DeFi market. Here is the full script related to this article