Get Token Supply Percentage In Pool

Get how much token supply percentage is present in a pool.

We sometime want to know how much of the total token supply is present in a particular pool. For this we need to do two steps

  • Get the amount of tokens currently present in pool.

  • Get the total token supply and find the percentage

Here's a code snippet for you try out directly in replit.

import { Connection, PublicKey } from "@solana/web3.js";
import { OpenOrders } from "@project-serum/serum";
import { gql, GraphQLClient } from "graphql-request";

const SHYFT_API_KEY = 'YOUR-KEY';

const graphQLEndpoint = `https://programs.shyft.to/v0/graphql/?api_key=${SHYFT_API_KEY}`;
const rpcEndpoint = `https://rpc.shyft.to/?api_key=${SHYFT_API_KEY}`;

const graphQLClient = new GraphQLClient(graphQLEndpoint, {
  method: `POST`,
  jsonSerializer: {
    parse: JSON.parse,
    stringify: JSON.stringify,
  },
});

async function queryLpByAddress(address:string) {
  // We only fetch fields necessary for us
  const query = gql`
    query MyQuery($where: Raydium_LiquidityPoolv4_bool_exp) {
  Raydium_LiquidityPoolv4(
    where: {pubkey: {_eq: ${JSON.stringify(address)}}}
  ) {
    baseDecimal
    baseMint
    baseNeedTakePnl
    baseVault
    marketId
    marketProgramId
    openOrders
    quoteDecimal
    quoteMint
    quoteNeedTakePnl
    quoteVault
  }
}`;

  return await graphQLClient.request(query);
}

//We have to check how much tokens are present in openbook market as well
export async function parsePoolInfo(poolInfo) {
  const OPENBOOK_PROGRAM_ID = new PublicKey(
    "srmqPvymJeFKQ4zGQed1GFppgkRHL9kaELCbyksJtPX"
  );

  //to load openOorders from openbook
  const connection = new Connection(rpcEndpoint, "confirmed");

  const openOrders = await OpenOrders.load(
    connection,
    new PublicKey(poolInfo.openOrders),
    OPENBOOK_PROGRAM_ID
  );

  const baseDecimal = 10 ** poolInfo.baseDecimal; // e.g. 10 ^ 6
  const quoteDecimal = 10 ** poolInfo.quoteDecimal;

  const baseTokenAmount = await connection.getTokenAccountBalance(
    new PublicKey(poolInfo.baseVault)
  );
  const quoteTokenAmount = await connection.getTokenAccountBalance(
    new PublicKey(poolInfo.quoteVault)
  );

  const basePnl = poolInfo.baseNeedTakePnl / baseDecimal;
  const quotePnl = poolInfo.quoteNeedTakePnl / quoteDecimal;

  const openOrdersBaseTokenTotal =
    openOrders.baseTokenTotal / baseDecimal;
  const openOrdersQuoteTokenTotal =
    openOrders.quoteTokenTotal / quoteDecimal;

  const base =
    (baseTokenAmount.value?.uiAmount || 0) + openOrdersBaseTokenTotal - basePnl;
  //You can do the same for quote tokens also. This doesnt work for SOL.
  const quote =
    (quoteTokenAmount.value?.uiAmount || 0) +
    openOrdersQuoteTokenTotal -
    quotePnl;

  //We get the current token supply through RPC and find the percentage
  const baseSupply = await (await connection.getTokenSupply(new PublicKey(poolInfo.baseMint)))
  console.log(`Total Base tokens: ${baseSupply.value.uiAmount}`)
  console.log(`Base tokens in Pool: ${base}`)
  console.log(`Pecentage of total base tokens in Pool: ${(base/baseSupply?.value?.uiAmount) * 100} %`)
}


//This is bonk-usdc pool addres
const poolInfo = await queryLpByAddress('HVNwzt7Pxfu76KHCMQPTLuTCLTm6WnQ1esLv4eizseSv');
await parsePoolInfo(poolInfo.Raydium_LiquidityPoolv4[0])

Last updated