Search code examples
pythonweb3py

Web3.py check liquidity pool balance


I'm trying to compute the balance of a lp address given the address of the token. So I have this function:

web3 = Web3(Web3.HTTPProvider("https://bsc-dataseed.binance.org/"))

def CheckLiquidity(TokenAddress, web3):
   LPAddress = GetLiquidityAddress(TokenAddress) #returns the web3.toChecksumAddress()
   balance = web3.eth.get_balance(LPAddress)
   bnbBalance = web3.fromWei(balance, 'ether)

Only problem that this returns me 0... for every contract I tried. I also manually checked on bsc scan the wbnb balance in the pool and is not 0. Can someone help me please?


Solution

  • There is an example in Web3 Ethereum Defi package.

        pair = get_deployed_contract(web3, "UniswapV2Pair.json", pair_address)
        token_a, token_b, timestamp = pair.functions.getReserves().call()
    
        # Check we got the liquidity
        assert token_a == 10 * 10**18
        assert token_b == 17_000 * 10**18
    

    See full example.