Monitor your favourite Liquidity Pool

Posted By : Prajwal Arora | 23-Dec-2021

To monitor a liquidity pool on pancake swap you just need a few things to get started before writing the program itself.
1. Pair address (token 1 / token 2)
2. Token 1 address and abi
3. Token 2 address and abi

In the code given below I am monitoring the Cake/BNB pool from pancake swap. I am just monitoring a single token, the amount of cake incoming or outgoing,
you can also monitor BNB similarly along with cake.


Variables used below in code 'upperLimit' & 'lowerLimit' are just a threshold at which the program will stop running, it basically means when 1000 tokens are added to the pair
or removed from the pair, stop running the program.

CODE -

const Web3 = require("web3");

const token1ABI = require('./token1abi.json');

//const token2ABI = require('./token2abi.json');

const token1Address = "0x0E09FaBB73Bd3Ade0a17ECC321fD13a19e81cE82";

//const token2Address = "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c";

const pairAddress = "0x0eD7e52944161450477ee417DE9Cd3a859b14fD0";

 

const web3 = new Web3('https://bsc-dataseed.binance.org/');

 

var block = 0;

var balance = 0;

const upperLimit = 1000;

const lowerLimit = -1000;

 

const contract1 = new web3.eth.Contract(token1ABI, token1Address);

//const contract2 = new web3.eth.Contract(token2ABI, token2Address);

 

const events1 = setInterval(() => {

   contract1.getPastEvents('Transfer', { fromBlock: 'latest' })

   .then((events) => {

 

       //console.log(events);

 

       if(events.length != 0) {

 

           if(block == events[0].blockNumber) {

               console.log("waiting for new block...");

               return;

           }

 

           console.log(events[0].blockNumber);

           block = events[0].blockNumber;

 

           for(i = 0; i < events.length; i++) {

 

               if(events[i].returnValues[1] == pairAddress) {

                   var amount = (events[i].returnValues[2])/10**18;

                   balance += amount;

               }

      

               if(events[i].returnValues[0] == pairAddress) {

                   var amount = (events[i].returnValues[2])/10**18;

                   balance -= amount;

               }

           }

           console.log("Live count = " + balance);

           if(balance >= upperLimit || balance <= lowerLimit) {

               clearInterval(events1);

           }

       }

   })

 

},1000)

 

The place in the above code where I am using clearInterval(), that's where you be creative and do wonders with this small piece of code. 
you could create a function to run a transaction for yourself like withdraw or deposit Cake or BNB in or from the pool. Since every 
new block is formed after 3 seconds there is a very good chance for you to make some quick cash. Possibilities are endless you could also 
create some sort of an alarm to notify you whenever the limits are exceeded... Have fun with this!

OUTPUT OF THE ABOVE CODE -

13732354
Live count = 8
waiting for a new block...
waiting for a new block...
waiting for new block...
13732355
Live count = 7.863673201229996
waiting for new block...
13732356
Live count = 9.625894849052218
waiting for new block...
waiting for new block...
waiting for new block...
13732357
Live count = 9.625894849052218
waiting for new block...
waiting for a new block...
13732358
Live count = 9.625894849052218
waiting for a new block...
waiting for a new block...
13732359
Live count = 5.6679325103143

 

About Author

loading
Prajwal Arora

Prajwal is a blockchain developer having years of experience in software engineering. He is an expert in python, shell scripts, MERN stack, and more. He also has experience in building infrastructure as code templates with IaC tools like terraform and AWS

Leave a Comment

Name is required

Comment is required

Recaptcha is required.

No Comments Yet.

    Request For Proposal

    By using this site, you allow our use of cookies. For more information on the cookies we use and how to delete or block them, please read our cookie notice.

    We would love to hear from you!

    Oodles | Blockchain Development Company

    Please enter a valid Phone Number

    Please remove URL from text

    Recaptcha is required.