Getting liquidity pair price of CAKE-LP (WBNB/BTCB) or SUSHI/ETH on BSC

Hi there,

Is there a quick way of getting the liquidity pair price of cake-LP WBNB/BTCB?

https://bscscan.com/address/0x61eb789d75a95caa3ff50ed7e47b96c132fec082

the contract is = 0x61eb789d75a95caa3ff50ed7e47b96c132fec082

and it has total supply of 18,128.511952 Cake-LP

This token pair has a value of $165,044,465

Hence, for 1 cake-LP price is $165,044,465/18,128.511952 or $9,104.12

Currently, I have to take several steps to get to the cake-LP price. Is there a quicker method?

For example if I have 2.395 token of the liquidity pair cake-LP (0x61eb789d75a95caa3ff50ed7e47b96c132fec082) and 4.2753 token of the liquidity pair cake-LP (SUSHI-ETH LP) (0x16aFc4F2Ad82986bbE2a4525601F8199AB9c832D)

https://bscscan.com/address/0x16aFc4F2Ad82986bbE2a4525601F8199AB9c832D

What is the total liquidity of my portfolio?

1 Like

Hi @tesla888

Steps:

  1. Call getReserves from LP smart contract:
    image
  2. You can see that info there:
    image
  3. capContractTokenA + capContractTokenB = 79.649.854$+79.524.210$ = 159.174.064$
    159.174.064$ - is a cap of LP token
  4. Call totalSupply from LP smart contract: image
  5. Convert totalSupply from wei = 16663 Tokens is a total supply of Cake-LP
  6. 159.174.064$ /16663 tokens = 9552$ - price of one LP token

Hope this will help you :mage:

1 Like

How to get price of tokenA and tokenB? You can use Moralis Web3 API, bscscan, etc.or directly get info from pancakerouter WBNB/BUSD and TOKEN/WBNB

I think that I got it working now but is there a more elegant way to code this?

price0_CumulativeLast = contract.functions.price0CumulativeLast().call()
price1_CumulativeLast = contract.functions.price1CumulativeLast().call()
token_0 = contract.functions.token0().call()
token_1 = contract.functions.token1().call()



token0_Address = web3.toChecksumAddress(token_0)
token1_Address = web3.toChecksumAddress(token_1)
#for Binance Smart Chain
token0_price = 'https://deep-index.moralis.io/api/v2/erc20/' + token0_Address + '/price?chain=' + chain
token1_price = 'https://deep-index.moralis.io/api/v2/erc20/' + token1_Address + '/price?chain=' + chain

headers = {
    'x-api-key': "17Hehy28h5JsgsBVOtUccIDWj012R2mby6uvbsvyFL6PTPUnq9HCPlzrXnV95Uwo"

}



token0_symbol = 'https://deep-index.moralis.io/api/v2/erc721/' + token0_Address + 'metadata?chain=' + chain
token1_symbol = 'https://deep-index.moralis.io/api/v2/erc721/' + token1_Address + 'metadata?chain=' + chain

response_0 = requests.request('GET', token0_price, headers=headers)
resp_0 = response_0.json()
#print(resp)
token0_priceUSD = resp_0['usdPrice']

response_1 = requests.request('GET', token1_price, headers=headers)
resp_1 = response_1.json()
#print(resp)
token1_priceUSD = resp_1['usdPrice']



print('------------------------------------------------------------------')

print('\n\nLiquidity pair\'s first token contract address: ', token_0) #BTC
total_supply_token_0 = web3.fromWei(reserve[0],'ether')
print('Total supply of token 0: ', total_supply_token_0)
print('Token 0 price: ', token0_priceUSD )

print('\n\nLiquidity pair\'s second token contract address:', token_1) # BNB
total_supply_token_1 = web3.fromWei(reserve[1],'ether')
print('Total supply of token 1 ', total_supply_token_1 )
print('Token 1 price: ', token1_priceUSD )


lp_total = web3.fromWei((total_supply), 'ether')
print('\n\nTotal supply of liquidity pair:', lp_total )
lp_price = (token0_priceUSD * float(total_supply_token_0) + token1_priceUSD * float(total_supply_token_1))/float(lp_total)

print('LP pair price is: ', lp_price)


print('------------------------------------------------------------------')

1 Like