4

Currently I am downloading the whole json file from here https://api.raydium.io/v2/sdk/liquidity/mainnet.json and save it into json file, then get the pool info. Code given below for further understanding:

def fetch_pool_keys(mint: str):
    amm_info = {}
    all_pools = {}
    try:
        # Using this so it will be faster else no option, we go the slower way.
        with open('all_pools.json', 'r') as file:
            all_pools = json.load(file)
        amm_info = extract_pool_info(all_pools, mint)
    except:
        resp = requests.get('https://api.raydium.io/v2/sdk/liquidity/mainnet.json', stream=True)
        pools = resp.json()
        official = pools['official']
        unofficial = pools['unOfficial'] 
        all_pools = official + unofficial

        # Store all_pools in a JSON file
        with open('all_pools.json', 'w') as file:
            json.dump(all_pools, file)
        try:
            amm_info = extract_pool_info(all_pools, mint)
        except:
            return "failed"

As you can see, I first check it in the downloaded file but if the key which is base mint and quote mint (SOL/USDC/USDT) is not there, then it downloads the file again.

My main concern: At the time of sending request, the code takes longer to download mainnet.json file and as we all know raydium updates the pools every minute or so. Due to which, I am asking for suggestions to speed up the download process or implement an alternative method to downloading mainnet.json

5
  • Have you had any progress on this topic? Commented Dec 28, 2023 at 15:44
  • Did not work on it... working on something else atm...
    – kokiez
    Commented Dec 28, 2023 at 15:45
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Commented Dec 28, 2023 at 18:19
  • please did you find a solution? something like get pools by token ?
    – iori
    Commented Jan 14 at 22:03
  • @iori the method in my question, you can use quoteMint and baseMint to find the pool info after pool has been downloaded. :). Hope this answers your question!
    – kokiez
    Commented Jan 16 at 8:54

5 Answers 5

5

For a faster way to grab those pool keys on Raydium, especially if you're targeting Raydium pairs, the trick is to hit up the programAccounts directly. Then, just decode what you get with the layouts Raydium SDK hands out. I usually check both OpenBook and Raydium Market accounts for this.

import {
  LIQUIDITY_STATE_LAYOUT_V4,
  MARKET_STATE_LAYOUT_V3,
} from "@raydium-io/raydium-sdk";

// Define a function to fetch and decode OpenBook accounts
async function fetchOpenBookAccounts(connection, baseMint, quoteMint, commitment) {
  const accounts = await connection.getProgramAccounts(
    networkData.openbookProgramId,
    {
      commitment,
      filters: [
        { dataSize: MARKET_STATE_LAYOUT_V3.span },
        {
          memcmp: {
            offset: MARKET_STATE_LAYOUT_V3.offsetOf("baseMint"),
            bytes: baseMint.toBase58(),
          },
        },
        {
          memcmp: {
            offset: MARKET_STATE_LAYOUT_V3.offsetOf("quoteMint"),
            bytes: quoteMint.toBase58(),
          },
        },
      ],
    }
  );

  return accounts.map(({ account }) => MARKET_STATE_LAYOUT_V3.decode(account.data));
}

// Define a function to fetch and decode Market accounts
async function fetchMarketAccounts(connection, base, quote, commitment) {
  const accounts = await connection.getProgramAccounts(
    networkData.marketProgramId,
    {
      commitment,
      filters: [
        { dataSize: LIQUIDITY_STATE_LAYOUT_V4.span },
        {
          memcmp: {
            offset: LIQUIDITY_STATE_LAYOUT_V4.offsetOf("baseMint"),
            bytes: base.toBase58(),
          },
        },
        {
          memcmp: {
            offset: LIQUIDITY_STATE_LAYOUT_V4.offsetOf("quoteMint"),
            bytes: quote.toBase58(),
          },
        },
      ],
    }
  );

  return accounts.map(({ pubkey, account }) => ({
    id: pubkey.toString(),
    ...LIQUIDITY_STATE_LAYOUT_V4.decode(account.data),
  }));
}

Just plug in your connection details, the mint addresses you're interested in, and you're good to go. This should make your work with Raydium pairs faster and more efficient.

5
  • Thanks. Indeed, It is the solution to the question.
    – kokiez
    Commented Feb 7 at 4:50
  • I'm trying to do the same thing but I don't understand what is networkData.marketProgramId,?
    – Ailef
    Commented Feb 13 at 19:21
  • If I now understand correctly, this is the marketProgramId for each pool that's in the mainnet.json file. How to retrieve it though without using the mainnet.json file? Is there a way to make an API call with the pool address or something?
    – Ailef
    Commented Feb 14 at 7:46
  • 1
    @kelvin-adithya Thank you for your answer. I'm implementing the same thing in JS, and your suggestion is guiding me. However, I would need some clarification: can you please better explain where to find the openbookProgramId (probably this: srmqPvymJeFKQ4zGQed1GFppgkRHL9kaELCbyksJtPX) and marketProgramId? Additionally: the object returned by fetchOpenBookAccounts seems different from the one required for a Raydium transaction. Can you add some detail on how to use the retrieved data? Thank you.
    – davioooh
    Commented Feb 15 at 8:48
  • it doesn't work for me Commented Apr 25 at 12:16
3

You could cache the pool keys in a nextjs app in the backend or in a database and add an api that only sends the keys you need. And then refresh the keys from the raydium json every hours or so asynchronously.

0

I just customised the Raydium-sdk to get the poolAddress from dexscreener and pass it to get the poolkeys to execute the swap.

You can try this: https://github.com/TrasherTravis/raydium-sdk-swap-no-mainnet.json-required.git

0

I was facing the same problem. Adding the repo, how I solve the problem.

0

You can now use @raydium-io/raydium-sdk-v2 and easily access it like so

const data = await raydium.api.fetchPoolByMints({ mint1: 'So11111111111111111111111111111111111111112', mint2: '4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R' // optional })

https://www.npmjs.com/package/@raydium-io/raydium-sdk-v2

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.