How can I know the current liquidity pool value in BNB, My idea to do this is get the total amount of tokens available in the pancakeswap reserve and then equate that amount to BNB. Any help to understand is very much welcomed

getTokenName =web3.eth.contract(address=tokenAddress,abi= tokenABI)

tokenName = getTokenName.functions.name().call()
tokenSymbol = getTokenName.functions.symbol().call()
decimals = getTokenName.functions.decimals().call()
DECIMAL = 10* decimals
totalSupply= getTokenName.functions.totalSupply().call() //DECIMAL

## Error caused by line below
liquidity_amt = getTokenName.functions.getReserves().call()```

# Alternative I use to no avail
 pair = web3.eth.contract(address=abi.pancake_factory, abi=abi.pancake_factory_abi).functions.getPair(tokenAddress, WBNB).call()


1 Answer 1


You probably get the error because "getReserves" is one of the Pair functions, not the Token Function.

I will assume you trying to do this at BSC. But this will work in other chains with small changes.

For this, you will need this ABI, which contains getReserves, token0 and token1 functions. (You can find this Abi on every pancake lp contract at Bscscan.)

Abi: {"constant":"true","inputs":[],"name":"getReserves","outputs":[{"internalType":"uint112","name":"_reserve0","type":"uint112"},{"internalType":"uint112","name":"_reserve1","type":"uint112"},{"internalType":"uint32","name":"_blockTimestampLast","type":"uint32"}],"payable":"false","stateMutability":"view","type":"function"},{"constant":"true","inputs":[],"name":"token0","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":"false","stateMutability":"view","type":"function"},{"constant":"true","inputs":[],"name":"token1","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":"false","stateMutability":"view","type":"function"}

First, go to Bscscan and get Pancake LP Contract Address of the token you want (remember, for every pair: BNB/BUSD/USDT etc. will be different contract address)

In this example, I will use the RatBoy token with the BNB pair.

  • Token Address: 0xAf98e6c2D3b520F4e9b3D7ea22ad7AeFFFDbc2fd

  • Pair Address (RatBoy/WBNB): 0x87eAb8C104251540A04Fd7660d7AA4C0673bF2Ec

  • Abi: Same as provided above.


from web3 import Web3

class getReservesExample():
    def __init__(self):

        #=== Setup getReserves ABI ===#
        self.abi = {"constant":"true","inputs":[],"name":"getReserves","outputs":[{"internalType":"uint112","name":"_reserve0","type":"uint112"},{"internalType":"uint112","name":"_reserve1","type":"uint112"},{"internalType":"uint32","name":"_blockTimestampLast","type":"uint32"}],"payable":"false","stateMutability":"view","type":"function"},{"constant":"true","inputs":[],"name":"token0","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":"false","stateMutability":"view","type":"function"},{"constant":"true","inputs":[],"name":"token1","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":"false","stateMutability":"view","type":"function"}
        #=== Setup Web3 ===#
        self.bsc = 'https://bsc-dataseed.binance.org/' #rpc 
        self.web3 = Web3(Web3.HTTPProvider(self.bsc)) #web3 connect

        #=== Setup RatBoy Contract Address ===#
        self.token_bnb = self.web3.toChecksumAddress("0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c") #BNB Token Address
        self.token_ratboy = self.web3.toChecksumAddress("0xAf98e6c2D3b520F4e9b3D7ea22ad7AeFFFDbc2fd") #RatBoy Token Address
        self.pair = self.web3.toChecksumAddress("0x87eAb8C104251540A04Fd7660d7AA4C0673bF2Ec") #RatBoy/BNB Pair Contract Address
        #=== Create Pair Contract Instance ===#
        self.pair_contract = self.web3.eth.contract(address=self.pair, abi=self.abi)

        #=== Start ===#

    def run(self):
        # -> Initially, we dont know if reserve0 belongs to the ratboy token or bnb.
        # -> because of this we have to find token0 of pair.
        token0 = self.pair_contract.functions.token0().call() #token0 of pair

        # -> Calling getReserves.
        reserves = self.pair_contract.functions.getReserves().call() #Output: list[reserve0, reserve1, timestamp]

        # -> As said before, we have to know if reserve0 belongs to the ratboy token or bnb.
        if token0 == self.token_bnb:
            pool_token = reserves[1]
            pool_wbnb = reserves[0]
            pool_token = reserves[0]
            pool_wbnb = reserves[1]

        # -> Extra: finding "Human Readable" BNB value in the pool.
        value_pool_wbnb = pool_wbnb/(10**18) #18 is bnb token decimals

        # -> Finally, print the result.
        print(f"RatBoy BNB Reserves: {pool_wbnb} ({value_pool_wbnb:.4f} WBNB)")
        print(f"RatBoy Token Reserves: {pool_token} \n")


It's simple code but works.


getReservesExample Output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.