Last active
January 30, 2025 21:46
-
-
Save Taylor123/9b6fa4d0ef90f3790c8648e8f73d639c to your computer and use it in GitHub Desktop.
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
// Fetches accounts from raydium | |
use std::{ | |
fs::File, | |
io::{BufWriter, Read, Write}, | |
str::FromStr, | |
time::{Duration, SystemTime}, | |
}; | |
use serde::Deserialize; | |
use solana_client::{ | |
rpc_client::RpcClient, | |
rpc_config::{RpcAccountInfoConfig, RpcProgramAccountsConfig}, | |
}; | |
use solana_sdk::{commitment_config::CommitmentConfig, pubkey::Pubkey}; | |
const RAYDIUM_API_URL: &str = "https://api-v3.raydium.io"; | |
const POOL_PUBKEY_FILE: &str = "pool_pubkeys.json"; | |
// This is the legacy AMM, which is used by pump.fun. | |
// Source: https://github.com/raydium-io/raydium-amm | |
const RAYDIUM_LEGACY_AMM_PROGRAM_ID: Pubkey = | |
solana_sdk::pubkey!("675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8"); | |
// This is Raydium's new AMM that no longer depends on openbook/serum. | |
const RAYDIUM_AMM_PROGRAM_ID: Pubkey = | |
solana_sdk::pubkey!("CPMMoo8L3F4NbTegBCKVNunggL7H1ZpdTHKxQB5qKP1C"); | |
/// Fetch a page of Raydium AMM pools using `getMultipleAccounts` RPC method | |
pub fn fetch_list_of_pools_from_file(rpc_url: &str, page: usize) -> Duration { | |
let mut file_content = String::new(); | |
let mut file = File::open(POOL_PUBKEY_FILE).unwrap(); | |
file.read_to_string(&mut file_content).unwrap(); | |
let pubkey_strs: Vec<String> = serde_json::from_str(&file_content).unwrap(); | |
let pubkeys: Vec<Pubkey> = pubkey_strs | |
.into_iter() | |
.map(|s| Pubkey::from_str(&s).unwrap()) | |
.collect(); | |
let client = RpcClient::new_with_commitment(rpc_url, CommitmentConfig::confirmed()); | |
let start = SystemTime::now(); | |
// Max 100 accounts per request | |
let max_size = 100; | |
let begin = page * max_size; | |
let end = begin + max_size; | |
let _accounts = client.get_multiple_accounts(&pubkeys[begin..end]).unwrap(); | |
let duration = start.elapsed().unwrap(); | |
println!( | |
"Time to fetch page {:?} - {:?}", | |
page, | |
start.elapsed().unwrap() | |
); | |
duration | |
} | |
#[derive(Deserialize)] | |
struct PoolInfo { | |
pub id: String, | |
} | |
#[derive(Deserialize)] | |
struct RaydiumRespData { | |
pub data: Vec<PoolInfo>, | |
} | |
#[derive(Deserialize)] | |
struct PoolInfoResp { | |
pub data: RaydiumRespData, | |
} | |
/// Use Raydium API to fetch 250k AMM pools and store their pubkeys in a json file. | |
pub fn write_pools_to_file() { | |
// Page is 1-indexed | |
let mut page = 1; | |
let page_size = 1000; | |
let mut pubkeys: Vec<String> = vec![]; | |
while page < 251 { | |
let pools_api_url = format!("{}/pools/info/list?poolType=standard&poolSortField=liquidity&sortType=desc&pageSize={}&page={}", RAYDIUM_API_URL, page_size, page); | |
let pools_resp = reqwest::blocking::get(&pools_api_url) | |
.unwrap() | |
.json::<PoolInfoResp>() | |
.unwrap(); | |
let pool_keys: Vec<String> = pools_resp.data.data.into_iter().map(|p| p.id).collect(); | |
pubkeys.extend(pool_keys); | |
page += 1; | |
} | |
let file = File::create(POOL_PUBKEY_FILE).unwrap(); | |
let mut writer = BufWriter::new(file); | |
serde_json::to_writer(&mut writer, &pubkeys).unwrap(); | |
writer.flush().unwrap(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment