閱讀時間 6 分鐘
概覽
With the recent uptick in DeFi activity and Memecoins on the Solana blockchain, there has been a growing interest in finding ways to track new liquidity pools created on the Raydium DEX. In this guide, we will learn how to listen to the Solana blockchain for new liquidity pools created on the Raydium DEX using Solana WebSockets.
Raydium protocol may change over time. Here is a link to their GitHub repositories for their AMM and CLMM.
To ensure you are accessing the latest information on new liquidity pools created on Raydium DEX, try using the Metis Marketplace Add-on, which has a /new-pools REST API Endpoint that returns the 100 most recent token and liquidity pool launches on Raydium.
Prefer a video walkthrough? Follow along with Sahil and how to use websockets to track new Raydium Liquidity Pools.
先決條件
- Experience with Solana Websockets
- Knowledge of DeFi Basics
- Solana基礎知識
- 已安裝Node.js(版本 18.16 或更高)
- Typescript and ts-node - installation instructions are indicated in the guide
What is Raydium?
Raydium is a decentralized exchange (DEX) and automated market maker (AMM) that is built on the Solana blockchain. It is designed to provide fast and low-cost trading for users. Raydium is a key player in the Solana DeFi ecosystem and has been a popular choice for users to trade and provide liquidity for various tokens.
What are Liquidity Pools?
Liquidity pools are a vital component of decentralized exchanges DeFi. They are used to facilitate trading and provide liquidity for various tokens. Liquidity pools are made up of pairs of tokens and are used to facilitate trades on DEXs. Users can often provide liquidity to these pools and earn fees in return.
設定您的專案
建立新專案
Create a new project directory in your terminal with the following:
mkdir raydium-lp-tracker
cd raydium-lp-tracker
為您的應用程式建立一個名為app.ts 的檔案:
echo > app.ts
請使用「yes」參數初始化您的專案,以便為新套件採用預設值:
毛線 初始化 --是
#或
npm 初始化 --yes
Install the Solana web3.js library:
紗線 新增 @solana/web3.js@1
#或
npm 安裝 @solana/web3.js@1
Get a Quicknode Endpoint
使用您的 Quicknode 端點連線至 Solana 叢集
若要在 Solana 上進行開發,您需要一個 API 端點來連線至該網路。您可以使用公開節點,或自行部署並管理基礎架構;不過,若您希望將回應時間縮短 8 倍,不妨將繁重的工作交給我們處理。
了解為何在 索拉納 選擇 Quicknode,立即開始免費試用 這裡. 我們將使用一個 索拉納 主網 終點。
複製該 HTTP 服務提供者連結:
Note: we will also use the WSS Endpoint as well, so keep the window open or save the URL for later.
Import Required Libraries
開啟 app.ts and import the required libraries:
import { Connection, PublicKey } from '@solana/web3.js';
Define Constants
Define the constants for the Raydium DEX and your Quicknode Endpoint:
const RAYDIUM_PUBLIC_KEY = "675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8";
const HTTP_URL = "YOUR_QUICKNODE_HTTP_ENDPOINT";
const WSS_URL = "YOUR_QUICKNODE_WSS_ENDPOINT";
const RAYDIUM = new PublicKey(RAYDIUM_PUBLIC_KEY);
const INSTRUCTION_NAME = "initialize2";
const connection = new Connection(HTTP_URL, {
wsEndpoint: WSS_URL
});
Here, we just define the Raydium LP Program public key and establish our connection to the Solana mainnet. We are also defining the name of the instruction that we will be looking for in the logs. We are looking for the "initialize2" instruction, which is the Raydium LP program instruction responsible for creating new liquidity pools.
Create a WebSocket Connection
Create a function to connect to the Solana blockchain using WebSockets:
async function startConnection(connection: Connection, programAddress: PublicKey, searchInstruction: string): Promise<void> {
console.log("Monitoring logs for program:", programAddress.toString());
connection.onLogs(
programAddress,
({ logs, err, signature }) => {
if (err) return;
if (logs && logs.some(log => log.includes(searchInstruction))) {
console.log("Signature for 'initialize2':", `https://explorer.solana.com/tx/${signature}`);
fetchRaydiumMints(signature, connection);
}
},
"finalized"
);
}
This function establishes a simple WebSocket connection to the Solana blockchain and listens for logs from the Raydium DEX program using the onLogs method.
When a log that contains the "searchInstruction" is found, we will fetch the token mints associated with the transaction (we will define this next).
Fetch LP Mints
We now need a way to fetch our Token Mints from the transaction signature. We will need to fetch the signature details and then parse the results to find the relevant information. In your program, create a new function, fetchRaydiumMints:
async function fetchRaydiumMints(txId: string, connection: Connection) {
try {
const tx = await connection.getParsedTransaction(
txId,
{
maxSupportedTransactionVersion: 0,
commitment: 'confirmed'
});
//@ts-ignore
const accounts = (tx?.transaction.message.instructions).find(ix => ix.programId.toBase58() === RAYDIUM_PUBLIC_KEY).accounts as PublicKey[];
if (!accounts) {
console.log("No accounts found in the transaction.");
return;
}
const tokenAIndex = 8;
const tokenBIndex = 9;
const tokenAAccount = accounts[tokenAIndex];
const tokenBAccount = accounts[tokenBIndex];
const displayData = [
{ "Token": "A", "Account Public Key": tokenAAccount.toBase58() },
{ "Token": "B", "Account Public Key": tokenBAccount.toBase58() }
];
console.log("New LP Found");
console.table(displayData);
} catch {
console.log("Error fetching transaction:", txId);
return;
}
}
There's a lot here, but it is not too complicated.
- First, we are fetching the transaction details using the
getParsedTransactionmethod. - Next, we look for the Raydium Program ID in the array of instructions and extract the accounts associated with the transaction.
- If we find the accounts, we extract the token mints from their expected positions in the array (8 and 9).
- Finally, we log the token mints to the console.
Start the Connection
Now that we have our functions defined, we can start the connection and listen for new liquidity pools:
startConnection(connection, RAYDIUM, INSTRUCTION_NAME).catch(console.error);
Run the Program
To run the program, use the following command:
ts-node app.ts
And now, sit back and watch as new liquidity pools are created on the Raydium DEX! You should see a notice in your terminal each time a new LP is created, matching our search criteria:

總結
Great Job! You now have an LP tracker that can listen for new liquidity pools created on the Raydium DEX. You can use this as a starting point to build more complex applications that track and monitor DeFi activity on the Solana blockchain or to implement custom logic in response to new liquidity pools being created.
If you have any questions or need help, feel free to reach out to us on our Discord or Twitter.
我們 ❤️ 您的回饋!
如果您有任何意見回饋或對新主題的建議,請告訴我們。我們非常樂意聽取您的意見。
