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()

2 Answers 2

1

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

3
  • 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
    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
    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 Feb 7 at 13:47
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

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.