-
-
Save luan1412167/3fa3f720dcb15b4c140d00c92fd6c894 to your computer and use it in GitHub Desktop.
Raydium getPools.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
LIQUIDITY_STATE_LAYOUT_V4, | |
MARKET_STATE_LAYOUT_V3, | |
DEVNET_PROGRAM_ID, | |
MAINNET_PROGRAM_ID, | |
} from "@raydium-io/raydium-sdk"; | |
import { | |
Connection, | |
PublicKey, | |
GetProgramAccountsConfig, | |
} from "@solana/web3.js"; | |
// Define a function to fetch and decode OpenBook accounts | |
async function fetchOpenBookAccounts( | |
connection: Connection, | |
baseMint: PublicKey, | |
quoteMint: PublicKey, | |
commitment: any | |
) { | |
const accounts = await connection.getProgramAccounts( | |
// networkData.openbookProgramId, | |
// MAINNET_PROGRAM_ID.OPENBOOK_MARKET, | |
DEVNET_PROGRAM_ID.OPENBOOK_MARKET, | |
{ | |
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: Connection, | |
baseMint: PublicKey, | |
quoteMint: PublicKey, | |
commitment: any | |
) { | |
const accounts = await connection.getProgramAccounts( | |
// networkData.marketProgramId, | |
// MAINNET_PROGRAM_ID.AmmV4, | |
DEVNET_PROGRAM_ID.AmmV4, | |
{ | |
commitment, | |
filters: [ | |
{ dataSize: LIQUIDITY_STATE_LAYOUT_V4.span }, | |
{ | |
memcmp: { | |
offset: LIQUIDITY_STATE_LAYOUT_V4.offsetOf("baseMint"), | |
bytes: baseMint.toBase58(), | |
}, | |
}, | |
{ | |
memcmp: { | |
offset: LIQUIDITY_STATE_LAYOUT_V4.offsetOf("quoteMint"), | |
bytes: quoteMint.toBase58(), | |
}, | |
}, | |
], | |
} | |
); | |
return accounts.map(({ pubkey, account }) => ({ | |
id: pubkey.toString(), | |
...LIQUIDITY_STATE_LAYOUT_V4.decode(account.data), | |
})); | |
} | |
async function main() { | |
const SOLANA_CONNECTION = new Connection( | |
// you rpc url here, | |
"confirmed" | |
); | |
// Slerf | |
// const baseMint = new PublicKey( | |
// "7BgBvyjrZX1YKz4oh9mjb8ZScatkkwb8DzFx7LoiVkM3" | |
// ); | |
// DEVNET - BASE/ATX | |
const baseMint = new PublicKey( | |
"3zL6LK3z5oj2iDK8HQ7XDGqYY7XrPHeVFKC6gaEUSuqn" | |
); | |
const quoteMint = new PublicKey( | |
"AF5pQJq6F5u2gq5durDS6ZgKU3DCoCYKZNzvHHyXPGBp" | |
); | |
// let openBookAccounts = await fetchOpenBookAccounts( | |
// SOLANA_CONNECTION, | |
// baseMint, | |
// quoteMint, | |
// "confirmed" | |
// ); | |
// console.log(openBookAccounts); | |
// console.log(`Number of OpenBook accounts: ${openBookAccounts.length}`); | |
let marketAccounts = await fetchMarketAccounts( | |
SOLANA_CONNECTION, | |
baseMint, | |
quoteMint, | |
"confirmed" | |
); | |
console.log(marketAccounts); | |
console.log(`poolOpenTime: ${marketAccounts[0].poolOpenTime}`); | |
console.log(`Number of market accounts: ${marketAccounts.length}`); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment