Telegram bot price

How to build a telegram bot which retrieve price in real time from a DEX on python

Requirements

Python libraries

You will need to install web3 and telegram-bot python libraries

pip install web3 python-telegram-bot --upgrade

Creating bot on telegram

You will first need to contact BotFather on telegram : https://t.me/botfatherarrow-up-right and follow the instructions Don't forget to not your token for later !

Coding the bot

Basic configuration

Now let's start coding our bot : we need to import web and some telegram-bot modules and some basic configuration.

Some variables we need

We will need a few variables like the factories addresses, and some ABIs.

Initializing DEXs and pools

Next we need to retrieve pools informations from DEXs in order to do so we will call the factory contract of each DEX and list all pairs available.

We will first define a Dex, Token and Pair class.

We also define a function to update the liquidity on a pair which will be useful later.

Once we have done that we can populate a list of dex with all its pairs.

Nice we now have our list of DEXs but there are no pairs associated with them so we need to write a function to retrieve them.

That's good but there is still a problem we create a new token for each pair even if we alredy know it. To avoid this we will create a dict containing all tokens we already know about.

And we update our code.

Calculate price

Now we need to know from which pair we want to calculate the price from, we will try all dexes and keep one with the highest avax (or usdt) liquidity. For each token we will calculate 2 prices : one in AVAX on in USDT.

Once we've done that we need to calculate the price.

Telegram function

Let's code the telegram function now. If we find a avax or usdt pair for this token we send a message with the current highest liquid price. (we transform avax in wavax since pairs are in wavax)

Final code

What's next ?

There is still some problems : if someone create a new pair on a DEX we can't check its price without rebooting the bot, we could fix this easily by adding a command doing this / since we only update liquidity on request and we do after we found which pair as highest liquidity this pair can't change, here as well we could do a command to update this or a timer to do this automatically, we could improve aswell the output format for super high or super low price coins.

Last updated