Date: Time:
Welcome, Log in by clicking  Here!

GetTokenInfo.py

How to get Dex data using web3

In order to get token information from a decentralized exchange (DEX) using Web3, you need to use a node because a node is your gateway to the blockchain.

A blockchain node is like a computer that keeps a full copy of the blockchain and can interact with it. Nodes listen to and broadcast transactions, update the blockchain and allow others to read from it.

Web3 is a JavaScript library that helps you interact with the blockchain. Web3 itself doesn’t directly  connect to the blockchain. This is where a node (like Infura, Alchemy, or your own Ethereum node) comes in, allowing Web3 to communicate with the blockchain network.

Alchemy.com

ABI (Application Binary Interface)

An ABI is a description of a smart contract’s interface on a blockchain, like Ethereum. It defines how you can interact with the contract- A list of functions, what kind of inputs and outputs those functions expect, and what events the contract emits.

List of functions

ABI import from abi.py

Two contract ABIS need to be copied and pasted

One for the lp contract

And one for the ERC contract

Next, we use the lp address and ABI to define the functions for our token price:

Printing the reserves function returns 3 values. the first two are used to calculate the price While the third is a “blocktimestamplast” value that I’m not sure what it is used for (which is why I left “IDK” in the description of the printed reserves).

The full code can be found on my GitHub Repository

The reason I have separate files for each token is because some decimal points are different and I have yet to worry about that.

Share on Facebook, X Or send me a message!

Posted on September 24, 2024 in coding,crypto by henryoviedo93

Comments on 'GetTokenInfo.py' (0)

Comments are closed.