facebook

Contact Us

Monitor your favourite Liquidity Pool

Calender

9th January 2022

Clock

105 min read

Author
Prajwal Arora

Senior Associate Consultant - Development

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

 

Table of Content

    Author 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

    Senior Associate Consultant - Development

    bg bg

    What's Trending in Tech

    bg

    Our Offices

    India

    INDIA

    DG-18-009, Tower B,
    Emaar Digital Greens, Sector 61,
    Gurugram, Haryana
    122011.
    Unit- 117-120, First Floor,
    Welldone Tech Park,
    Sector 48, Sohna road,
    Gurugram, Haryana
    122018.
    USA

    USA

    30N, Gloud St STR E, Sheridan, Wyoming (USA) - 82801
    Singapore

    SINGAPORE

    10 Anson Road, #13-09, International Plaza Singapore 079903.

    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.