1

How can I get to the price in a liquidity pool? I assumed it would be as simple as getting the balances of both tokens and doing quoteBalance / baseBalance, but that doesn't seem to be the case. Below is a sample code I was using to the get the pool information, but the division of the end values returned in the dictionaries base_token_info and quote_token_info does not match at all what I see on Solscan, what am I missing?

from solders.pubkey import Pubkey
from solana.rpc.api import Client
import json
import base64
from utils import LIQUIDITY_STATE_LAYOUT_V4


def get_token_amounts_example():
    endpoint = 'https://api.mainnet-beta.solana.com'
    solana_client = Client(endpoint)
    pool_address = '58oQChx4yWmvKdwLLZzBi4ChoCc2fqCUWBkwMihLYQo2' # SOL-USDC
    pool = Pubkey.from_string(pool_address)
    info = json.loads(solana_client.get_account_info_json_parsed(pool).to_json())
    data = info['result']['value']['data']
    data_64 = base64.b64decode(data[0])
    token_account_data = LIQUIDITY_STATE_LAYOUT_V4.parse(data_64)

    marketId = Pubkey.from_bytes(token_account_data.marketId)
    base_token_account = Pubkey.from_bytes(token_account_data.baseVault)
    quote_token_account = Pubkey.from_bytes(token_account_data.quoteVault)
    owner = Pubkey.from_bytes(token_account_data.owner)


    print('marketId', marketId)
    print('base_token_account', base_token_account)
    print('quote_token_account', quote_token_account)
    print('owner', owner)

    base_token_info = solana_client.get_token_account_balance(base_token_account).to_json()
    base_token_info = json.loads(base_token_info)
    quote_token_info = solana_client.get_token_account_balance(quote_token_account).to_json()
    quote_token_info = json.loads(quote_token_info)
    owner_token_info = solana_client.get_token_account_balance(owner).to_json()
    owner_token_info = json.loads(owner_token_info)

    print('base_token_info', base_token_info)
    print('quote_token_info', quote_token_info)
    print('owner_token_info', owner_token_info)

get_token_amounts_example()

1
  • I have something very similar to this in Javascript but I am struggling to write working code in Python that is able to get the baseVault and quoteVault property values from the liquidity pool. I can see that you are importing LIQUIDITY_STATE_LAYOUT_V4 like this from utils import LIQUIDITY_STATE_LAYOUT_V4 and using it like this token_account_data = LIQUIDITY_STATE_LAYOUT_V4.parse(data_64) However, I do not understand where the LIQUIDITY_STATE_LAYOUT_V4 is coming from. I couldn't find anything on this anywhere online. Is this a custom class that resembles the corresponding struct from Javascrip
    – Sven
    Commented Apr 16 at 8:43

3 Answers 3

2

This works for me: (I get the sol price from raydium)

basepriceinsol = quotebalance / basebalance

basepriceinusd = basepriceinsol * solprice

ValueUSDbaseBalance= baseBalance * basepriceinusd

ValueUSDquoteBalace = quoteBalace * solprice

LIQUIDITY = ValueUSDbaseBalance + ValueUSDquoteBalace

4
  • don't think you saw what is returned when you fetch the token balances (I tested in Python and Typescript btw). Here is what you get: base_token_info = {'uiAmount': 24340.758636285, 'decimals': 9, 'amount': '24340758636285', 'uiAmountString': '24340.758636285'} quote_token_info = {'uiAmount': 3846894.249247, 'decimals': 6, 'amount': '3846894249247', 'uiAmountString': '3846894.249247'} Now, if you do 3846894 / 24340, you get 158... Solana price is at 96 right now, so nowhere close
    – lsrth
    Commented Feb 7 at 13:39
  • to be fair, I do agree that your logic is correct, it's just that the numbers don't make sense when you apply that logic to the values returned using the RPC API
    – lsrth
    Commented Feb 7 at 13:41
  • This is what I got: NAME: Bat (🦇) --------------------------------------------------- Mint: FSyViTWXCQPP6haLH7TbVKhAy6waRXXtt2uA47xBQo8o Pair: JDwheQTaKX424rEiYwKzGzZqNEepdLRZHTEX4pCarWbR Quote Token: Wrapped SOL (SOL) SOL Price: $ 96.23 USD Token Amount: 1000000000 Sol Amount: 500 SOL Token Price USD: $ 0.000048116293 Liquidez del Pool: $96232.59 USD Open Time: 16:08:00 Catch Time: 16:01:06 and the LIQUIDITY is the same as showing in the DEX Commented Feb 7 at 13:47
  • are quotebalance == quoteBalace?
    – AdityaDees
    Commented May 20 at 16:26
0

The logic above by San Martín Figueroa Pablo is correct, you'll just need to divide the final liquidity number by lamports_per_sol, which is 1000000000

0

im getting like this value so how to do the rest, there is nothing for sol price

marketId 37TthZhcvdYpPmK93duSx9VS7MNdANcSWs2FksqU4uH6

base_token_account DmCGScSJPMMJkTJ5V7TntVBJ4qRYF63tw9JbHfvqXBC9

quote_token_account ANvbHfcBnK6fM6GnvhmBjpT4tu81m2A4vpd4YH9AHaxT

owner GThUX1Atko4tqhN2NaiTazWSeFWMuiUvfFnyJyUghFMJ

base_token_info {'jsonrpc': '2.0', 'result': {'context': {'slot': 299257286, 'apiVersion': '1.18.25'}, 'value': {'uiAmount': 638189810.816194, 'decimals': 6, 'amount': '638189810816194', 'uiAmountString': '638189810.816194'}}, 'id': 0}
quote_token_info {'jsonrpc': '2.0', 'result': {'context': {'slot': 299257290, 'apiVersion': '1.18.25'}, 'value': {'uiAmount': 0.180270432, 'decimals': 9, 'amount': '180270432', 'uiAmountString': '0.180270432'}}, 'id': 0}

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.