Fetch Liquidity Pools for Token

Fetch all liquidity pools for a token from multiple DEXs.

A single token can have liquidity pools in multiple DEXs. We have already seen how you can fetch liquidity pools for a token from Raydium and Orca separately. Lets see how we can query both Raydium and Orca in a single call and get all pools for a token.

You can copy paste this code snippet in replit to see it in action.

/*
This is an example snippet - you should consider tailoring it
to your service.
*/

async function fetchPools(token) {

//We are fetching all fields of both DEXs, you can pick whatever is eseential.
const liquidityQuery = `
  query MyQuery {
    Raydium_LiquidityPoolv4(
      where: {baseMint: {_eq: ${JSON.stringify(token)}}}
    ) {
      amountWaveRatio
      baseDecimal
      baseLotSize
      baseMint
      baseNeedTakePnl
      baseTotalPnl
      baseVault
      depth
      lpMint
      lpReserve
      lpVault
      marketId
      marketProgramId
      maxOrder
      maxPriceMultiplier
      minPriceMultiplier
      minSeparateDenominator
      minSeparateNumerator
      minSize
      nonce
      openOrders
      orderbookToInitTime
      owner
      pnlDenominator
      pnlNumerator
      poolOpenTime
      punishCoinAmount
      punishPcAmount
      quoteDecimal
      quoteLotSize
      quoteMint
      quoteNeedTakePnl
      quoteTotalPnl
      quoteVault
      resetFlag
      state
      status
      swapBase2QuoteFee
      swapBaseInAmount
      swapBaseOutAmount
      swapFeeDenominator
      swapFeeNumerator
      swapQuote2BaseFee
      swapQuoteInAmount
      swapQuoteOutAmount
      systemDecimalValue
      targetOrders
      tradeFeeDenominator
      tradeFeeNumerator
      volMaxCutRatio
      withdrawQueue
      pubkey
    }
    ORCA_WHIRLPOOLS_whirlpool(
      where: {tokenMintB: {_eq: ${JSON.stringify(token)}}}
    ) {
      _lamports
      feeGrowthGlobalA
      feeGrowthGlobalB
      feeRate
      liquidity
      protocolFeeOwedA
      protocolFeeOwedB
      protocolFeeRate
      rewardLastUpdatedTimestamp
      sqrtPrice
      tickCurrentIndex
      tickSpacing
      tokenMintA
      tokenMintB
      tokenVaultA
      tokenVaultB
      whirlpoolsConfig
      pubkey
    }
  }
`;
  const result = await fetch(
    "https://programs.shyft.to/v0/graphql/?api_key=YOUR-KEY",
    {
      method: "POST",
      body: JSON.stringify({
        query: liquidityQuery,
      })
    }
  );

  return await result.json();
}

async function startQuery() {
  const { errors, data } = await fetchPools('WENWENvqqNya429ubCdR81ZmD69brwQaaBYY6p3LCpk'); //WEN token

  if (errors) {
    // handle those errors like a pro
    console.error(errors);
  }

  // do something great with this precious data
  console.log('Raydium pools: ', data?.Raydium_LiquidityPoolv4.length);
  console.log('Orca pools: ', data?.ORCA_WHIRLPOOLS_whirlpool.length);
}

startQuery();

Once we have parsed pool info, we can get more details from it, like

Fetch Token Pairs (WEN-USDC)

We can tweak the above query to fetch particular token pairs like WEN-USDC pair.

/*
This is an example snippet - you should consider tailoring it
to your service.
*/

async function fetchPools(tokenOne, tokenTwo) {

//We are fetching all fields of both DEXs, you can pick whatever is eseential.
const liquidityQuery = `
  query MyQuery {
    Raydium_LiquidityPoolv4(
      where: {baseMint: {_eq: ${JSON.stringify(tokenOne)}}, quoteMint: {_eq: ${JSON.stringify(tokenTwo)}}}
    ) {
      _updatedAt
      amountWaveRatio
      baseDecimal
      baseLotSize
      baseMint
      baseNeedTakePnl
      baseTotalPnl
      baseVault
      depth
      lpMint
      lpReserve
      lpVault
      marketId
      marketProgramId
      maxOrder
      maxPriceMultiplier
      minPriceMultiplier
      minSeparateDenominator
      minSeparateNumerator
      minSize
      nonce
      openOrders
      orderbookToInitTime
      owner
      pnlDenominator
      pnlNumerator
      poolOpenTime
      punishCoinAmount
      punishPcAmount
      quoteDecimal
      quoteLotSize
      quoteMint
      quoteNeedTakePnl
      quoteTotalPnl
      quoteVault
      resetFlag
      state
      status
      swapBase2QuoteFee
      swapBaseInAmount
      swapBaseOutAmount
      swapFeeDenominator
      swapFeeNumerator
      swapQuote2BaseFee
      swapQuoteInAmount
      swapQuoteOutAmount
      systemDecimalValue
      targetOrders
      tradeFeeDenominator
      tradeFeeNumerator
      volMaxCutRatio
      withdrawQueue
      pubkey
    }
    ORCA_WHIRLPOOLS_whirlpool(
      where: {tokenMintB: {_eq: ${JSON.stringify(tokenTwo)}}, tokenMintA: {_eq: ${JSON.stringify(tokenOne)}}}
    ) {
      _lamports
      feeGrowthGlobalA
      feeGrowthGlobalB
      feeRate
      liquidity
      protocolFeeOwedA
      protocolFeeOwedB
      protocolFeeRate
      rewardLastUpdatedTimestamp
      sqrtPrice
      tickCurrentIndex
      tickSpacing
      tokenMintA
      tokenMintB
      tokenVaultA
      tokenVaultB
      whirlpoolsConfig
      pubkey
    }
  }
`;
  const result = await fetch(
    "https://programs.shyft.to/v0/graphql/?api_key=PjnsMufcmuJVt3E9",
    {
      method: "POST",
      body: JSON.stringify({
        query: liquidityQuery,
      })
    }
  );

  return await result.json();
}

async function startQuery() {
  const { errors, data } = await fetchPools('WENWENvqqNya429ubCdR81ZmD69brwQaaBYY6p3LCpk', 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v'); //WEN-USDC pair

  if (errors) {
    // handle those errors like a pro
    console.error(errors);
  }

  // do something great with this precious data
  console.log('Raydium pools: ', data?.Raydium_LiquidityPoolv4.length);
  console.log('Orca pools: ', data?.ORCA_WHIRLPOOLS_whirlpool.length);
}

startQuery();

Last updated