2

I have a collection of wallet addresses and I need to find the amount of a token that each of those addresses hold in some liquidity pools. Lets suppose the liquidity poll is for token A and B. So I need to find the amount of token A that each addresses has in the liquidity pool. How can I get that?

To be clear, I am not talking about the lp_token, rather I am talking about one of the tokens that the addresses have staked.

TylerH
  • 20,799
  • 66
  • 75
  • 101
samman adhikari
  • 571
  • 1
  • 7
  • 17

2 Answers2

4

The amount of underlying tokens a liquidity provider owns in a Uniswap v2 pool is proportional to the provider's share of the LP tokens.

For example, let's say the pool has 1000 USDC in its reserves, and the totalSupply of the LP token is equal to 100. Then someone owning 1 LP token would have the rights to 10.0 USDC in the pool.

Example Python code, for the USDC/WETH pool:

from web3 import Web3

web3 = Web3(Web3.HTTPProvider(PROVIDER_URL))
pair = '0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc'
contract = web3.eth.contract(address=pair, abi=v2_pool_abi)

reserves = contract.functions.getReserves().call()
reserve_usdc = reserves[0]
total_supply = contract.functions.totalSupply().call()

lp_address = '0x76E2E2D4d655b83545D4c50D9521F5bc63bC5329' 
lp_balance = contract.functions.balanceOf(lp_address).call()
lp_usdc = reserve_usdc * lp_balance / total_supply   
usdc_decimals = 6
lp_usdc_adjusted = lp_usdc / 10 ** usdc_decimals
print(f"liquidity provider {lp_address} has {lp_usdc_adjusted} USDC in USDC/WETH pool")
kfx
  • 8,136
  • 3
  • 28
  • 52
  • thanks a lot.. a quick followup question, is it the same for other swaps as well (i.e sushi, ape, pancake) etc. if i am not wrong they are just a fork of uni, so it should be the case right? – samman adhikari Sep 23 '22 at 14:08
  • yes - uniswap v2 code should work with most of these – kfx Sep 23 '22 at 14:12
1
    const Web3 = require('web3');
    const web3 = new Web3(`https://mainnet.infura.io/v3/${process.env.INFURA}`); // Web3 instance connected to Ethereum mainnet
    const abi = require("/ABI/UNISWAPV2/USDC/WETH-USDC.json")
    function getReserves(factoryAddress) {
    try {
        const factory = new web3.eth.Contract(abi, factoryAddress);
        factory.methods.getReserves().call()
        .then(result => {
                console.log("WETH", result._reserve1/(10**18));
                console.log("USDC",result._reserve0/(10**6));
            })
            .catch(error => {
                console.error(error);
            });
    } catch (error) {
        console.error(error);
    }
    }
getReserves("0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc")


avariant
  • 2,234
  • 5
  • 25
  • 33
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 25 '23 at 21:00