Skip to content

Commit 564f98d

Browse files
committed
feat: consolidate rpc clients and simplify code
1 parent 43067ae commit 564f98d

10 files changed

Lines changed: 43 additions & 113 deletions

File tree

examples/pump.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::str::FromStr;
22

33
use anyhow::Result;
44
use raytx::{
5-
get_rpc_client_blocking,
5+
get_rpc_client,
66
pump::{get_bonding_curve_account, get_pda, PUMP_PROGRAM},
77
};
88
use solana_sdk::pubkey::Pubkey;
@@ -18,7 +18,7 @@ async fn main() -> Result<()> {
1818
}
1919

2020
pub async fn get_bonding_curve_by_mint() -> Result<()> {
21-
let client = get_rpc_client_blocking()?;
21+
let client = get_rpc_client()?;
2222
let program_id = Pubkey::from_str(PUMP_PROGRAM)?;
2323
let mint = Pubkey::from_str("8oAK7mKMSnsVgrBgFS6A4uPqL8dh5NHAc7ohsq71pump")?;
2424
let bonding_curve = get_pda(&mint, &program_id)?;

examples/rpc.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use amm_cli::load_amm_keys;
44
use anyhow::{Context, Result};
55
use common::common_utils;
66
use futures_util::{SinkExt, StreamExt};
7-
use raytx::{get_rpc_client_blocking, logger, pump::PUMP_PROGRAM, raydium::get_pool_state_by_mint};
7+
use raytx::{get_rpc_client, logger, pump::PUMP_PROGRAM, raydium::get_pool_state_by_mint};
88
use solana_client::rpc_client::GetConfirmedSignaturesForAddress2Config;
99
use solana_sdk::{commitment_config::CommitmentConfig, pubkey::Pubkey};
1010
use tokio_tungstenite::{connect_async, tungstenite::Message};
@@ -22,7 +22,7 @@ async fn main() -> Result<()> {
2222
}
2323

2424
pub async fn get_amm_info_by_mint() -> Result<()> {
25-
let client = get_rpc_client_blocking()?;
25+
let client = get_rpc_client()?;
2626
let mint = "DrEMQaQqGN2fQwiUgJi6NStLtmni8m3uSkUP678Apump";
2727

2828
let pool_state = get_pool_state_by_mint(client, mint).await?;
@@ -33,7 +33,7 @@ pub async fn get_amm_info_by_mint() -> Result<()> {
3333
}
3434

3535
pub async fn get_amm_info() -> Result<()> {
36-
let client = get_rpc_client_blocking()?;
36+
let client = get_rpc_client()?;
3737
// let amm_pool_id = Pubkey::from_str("3vehHGc8J9doSo6gJoWYG23JG54hc2i7wjdFReX3Rcah")?;
3838
let amm_pool_id = Pubkey::from_str("7Sp76Pv48RaL4he2BfGUhvjqCtvjjfTSnXDXNvk845yL")?;
3939

@@ -94,7 +94,7 @@ pub async fn get_amm_info() -> Result<()> {
9494
}
9595

9696
pub async fn get_signatures() -> Result<()> {
97-
let client = get_rpc_client_blocking()?;
97+
let client = get_rpc_client()?;
9898
let config = GetConfirmedSignaturesForAddress2Config {
9999
before: None,
100100
until: None,

src/api.rs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ use axum::{
88
};
99
use serde::Deserialize;
1010
use serde_json::json;
11-
use solana_client::nonblocking::rpc_client::RpcClient;
11+
use solana_client::rpc_client::RpcClient;
1212
use solana_sdk::{pubkey::Pubkey, signature::Keypair, signer::Signer};
1313
use tracing::{info, warn};
1414

1515
use crate::{
16-
get_rpc_client, get_rpc_client_blocking,
16+
get_rpc_client,
1717
helper::{api_error, api_ok},
1818
pump::{get_pump_info, RaydiumInfo},
1919
raydium::Raydium,
@@ -24,7 +24,6 @@ use crate::{
2424
#[derive(Clone)]
2525
pub struct AppState {
2626
pub client: Arc<RpcClient>,
27-
pub client_blocking: Arc<solana_client::rpc_client::RpcClient>,
2827
pub wallet: Arc<Keypair>,
2928
}
3029

@@ -84,15 +83,8 @@ pub async fn get_pool(
8483
return api_error(&format!("failed to get rpc client: {err}"));
8584
}
8685
};
87-
let client_blocking = match get_rpc_client_blocking() {
88-
Ok(client) => client,
89-
Err(err) => {
90-
return api_error(&format!("failed to get rpc client: {err}"));
91-
}
92-
};
9386
let wallet = state.wallet;
94-
let mut swapx = Raydium::new(client, wallet);
95-
swapx.with_blocking_client(client_blocking);
87+
let swapx = Raydium::new(client, wallet);
9688
match swapx.get_pool(pool_id.as_str()).await {
9789
Ok(data) => api_ok(json!({
9890
"base": data.0,
@@ -115,23 +107,16 @@ pub async fn coins(State(state): State<AppState>, Path(mint): Path<String>) -> i
115107
return api_error(&format!("failed to get rpc client: {err}"));
116108
}
117109
};
118-
let client_blocking = match get_rpc_client_blocking() {
119-
Ok(client) => client,
120-
Err(err) => {
121-
return api_error(&format!("failed to get rpc client: {err}"));
122-
}
123-
};
124110
let wallet = state.wallet;
125111
// query from pump.fun
126-
let mut pump_info = match get_pump_info(client_blocking.clone(), &mint).await {
112+
let mut pump_info = match get_pump_info(client.clone(), &mint).await {
127113
Ok(info) => info,
128114
Err(err) => {
129115
return api_error(&err.to_string());
130116
}
131117
};
132118
if pump_info.complete {
133-
let mut swapx = Raydium::new(client, wallet);
134-
swapx.with_blocking_client(client_blocking);
119+
let swapx = Raydium::new(client, wallet);
135120
match swapx.get_pool_price(None, Some(mint.as_str())).await {
136121
Ok(data) => {
137122
pump_info.raydium_info = Some(RaydiumInfo {

src/lib.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{env, sync::Arc};
33
use anyhow::{anyhow, Result};
44
use rand::seq::SliceRandom;
55
use reqwest::Proxy;
6-
use solana_client::nonblocking::rpc_client::RpcClient;
6+
use solana_client::rpc_client::RpcClient;
77
use solana_sdk::signature::Keypair;
88
use tracing::debug;
99

@@ -55,12 +55,6 @@ pub fn get_rpc_client() -> Result<Arc<RpcClient>> {
5555
return Ok(Arc::new(client));
5656
}
5757

58-
pub fn get_rpc_client_blocking() -> Result<Arc<solana_client::rpc_client::RpcClient>> {
59-
let random_url = get_random_rpc_url()?;
60-
let client = solana_client::rpc_client::RpcClient::new(random_url);
61-
return Ok(Arc::new(client));
62-
}
63-
6458
pub fn get_wallet() -> Result<Arc<Keypair>> {
6559
let wallet = Keypair::from_base58_string(&env::var("PRIVATE_KEY")?);
6660
return Ok(Arc::new(wallet));

src/main.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use axum::{
77
use clap::{ArgGroup, Parser, Subcommand};
88
use raytx::{
99
api::{self, AppState},
10-
get_rpc_client, get_rpc_client_blocking, get_wallet, jito, logger,
10+
get_rpc_client, get_wallet, jito, logger,
1111
raydium::get_pool_info,
1212
swap::{self, SwapDirection, SwapInType},
1313
token,
@@ -81,13 +81,8 @@ async fn main() -> Result<()> {
8181
let cli = Cli::parse();
8282
logger::init();
8383
let client = get_rpc_client()?;
84-
let client_blocking = get_rpc_client_blocking()?;
8584
let wallet = get_wallet()?;
86-
let app_state = AppState {
87-
client,
88-
client_blocking,
89-
wallet,
90-
};
85+
let app_state = AppState { client, wallet };
9186

9287
match &cli.command {
9388
Some(Command::Swap {

src/pool.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use anyhow::{Context, Result};
1+
use anyhow::Result;
22
use common::common_utils;
33
use spl_token_2022::amount_to_ui_amount;
44
use tracing::{debug, warn};
@@ -26,17 +26,12 @@ impl Raydium {
2626
pool_id: Option<&str>,
2727
mint: Option<&str>,
2828
) -> Result<(f64, f64, f64)> {
29-
let client = self
30-
.client_blocking
31-
.clone()
32-
.context("failed to get rpc client")?;
33-
34-
let (amm_pool_id, pool_state) = get_pool_state(client.clone(), pool_id, mint).await?;
29+
let (amm_pool_id, pool_state) = get_pool_state(self.client.clone(), pool_id, mint).await?;
3530

3631
// debug!("pool_state : {:#?}", pool_state);
3732

3833
let load_pubkeys = vec![pool_state.pc_vault, pool_state.coin_vault];
39-
let rsps = common::rpc::get_multiple_accounts(&client, &load_pubkeys).unwrap();
34+
let rsps = common::rpc::get_multiple_accounts(&self.client, &load_pubkeys).unwrap();
4035

4136
let amm_pc_vault_account = rsps[0].clone();
4237
let amm_coin_vault_account = rsps[1].clone();

src/pump.rs

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use std::{str::FromStr, sync::Arc};
22

3-
use anyhow::{anyhow, Context, Result};
3+
use anyhow::{anyhow, Result};
44
use borsh::from_slice;
55
use borsh_derive::{BorshDeserialize, BorshSerialize};
66
use raydium_amm::math::U128;
77
use serde::{Deserialize, Serialize};
8-
use solana_client::nonblocking::rpc_client::RpcClient;
8+
use solana_client::rpc_client::RpcClient;
99
use solana_sdk::{
1010
instruction::{AccountMeta, Instruction},
1111
pubkey::Pubkey,
@@ -40,24 +40,11 @@ pub const PUMP_SELL_METHOD: u64 = 12502976635542562355;
4040
pub struct Pump {
4141
pub client: Arc<RpcClient>,
4242
pub keypair: Arc<Keypair>,
43-
pub client_blocking: Option<Arc<solana_client::rpc_client::RpcClient>>,
4443
}
4544

4645
impl Pump {
4746
pub fn new(client: Arc<RpcClient>, keypair: Arc<Keypair>) -> Self {
48-
Self {
49-
client,
50-
keypair,
51-
client_blocking: None,
52-
}
53-
}
54-
55-
pub fn with_blocking_client(
56-
&mut self,
57-
client: Arc<solana_client::rpc_client::RpcClient>,
58-
) -> &mut Self {
59-
self.client_blocking = Some(client);
60-
self
47+
Self { client, keypair }
6148
}
6249

6350
pub async fn swap(
@@ -83,8 +70,7 @@ impl Pump {
8370
};
8471
let pump_program = Pubkey::from_str(PUMP_PROGRAM)?;
8572
let (bonding_curve, associated_bonding_curve, bonding_curve_account) =
86-
get_bonding_curve_account(self.client_blocking.clone().unwrap(), &mint, &pump_program)
87-
.await?;
73+
get_bonding_curve_account(self.client.clone(), &mint, &pump_program).await?;
8874
let in_ata = get_associated_token_address(&owner, &token_in);
8975
let out_ata = get_associated_token_address(&owner, &token_out);
9076

@@ -169,11 +155,6 @@ impl Pump {
169155
token_in, amount_ui_pretty, token_out
170156
);
171157

172-
let client = self
173-
.client_blocking
174-
.clone()
175-
.context("failed to get rpc client")?;
176-
177158
// Calculate tokens out
178159
let virtual_sol_reserves = U128::from(bonding_curve_account.virtual_sol_reserves);
179160
let virtual_token_reserves = U128::from(bonding_curve_account.virtual_token_reserves);
@@ -267,7 +248,7 @@ impl Pump {
267248
return Err(anyhow!("instructions is empty, no tx required"));
268249
}
269250

270-
tx::new_signed_and_send(&client, &self.keypair, instructions, use_jito).await
251+
tx::new_signed_and_send(&self.client, &self.keypair, instructions, use_jito).await
271252
}
272253
}
273254

src/raydium.rs

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use raydium_amm::state::{AmmInfo, Loadable};
66
use reqwest::Proxy;
77
use serde::Deserialize;
88
use solana_client::{
9-
nonblocking::rpc_client::RpcClient,
9+
rpc_client::RpcClient,
1010
rpc_filter::{Memcmp, RpcFilterType},
1111
};
1212
use solana_sdk::{
@@ -26,7 +26,6 @@ use spl_token_client::token::TokenError;
2626
use std::{str::FromStr, sync::Arc};
2727

2828
use crate::{
29-
get_rpc_client_blocking,
3029
swap::{SwapDirection, SwapInType},
3130
token, tx,
3231
};
@@ -39,7 +38,6 @@ pub const AMM_PROGRAM: &str = "675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8";
3938
pub struct Raydium {
4039
pub client: Arc<RpcClient>,
4140
pub keypair: Arc<Keypair>,
42-
pub client_blocking: Option<Arc<solana_client::rpc_client::RpcClient>>,
4341
pub pool_id: Option<String>,
4442
}
4543

@@ -48,19 +46,10 @@ impl Raydium {
4846
Self {
4947
client,
5048
keypair,
51-
client_blocking: None,
5249
pool_id: None,
5350
}
5451
}
5552

56-
pub fn with_blocking_client(
57-
&mut self,
58-
client: Arc<solana_client::rpc_client::RpcClient>,
59-
) -> &mut Self {
60-
self.client_blocking = Some(client);
61-
self
62-
}
63-
6453
pub fn with_pool_id(&mut self, pool_id: Option<String>) -> &mut Self {
6554
self.pool_id = pool_id;
6655
self
@@ -83,12 +72,8 @@ impl Raydium {
8372
let program_id = spl_token::ID;
8473
let native_mint = spl_token::native_mint::ID;
8574

86-
let (amm_pool_id, pool_state) = get_pool_state(
87-
self.client_blocking.clone().unwrap(),
88-
self.pool_id.as_deref(),
89-
Some(mint_str),
90-
)
91-
.await?;
75+
let (amm_pool_id, pool_state) =
76+
get_pool_state(self.client.clone(), self.pool_id.as_deref(), Some(mint_str)).await?;
9277
// debug!("pool_state: {:#?}", pool_state);
9378

9479
let (token_in, token_out, user_input_token, swap_base_in) = match (
@@ -191,9 +176,9 @@ impl Raydium {
191176

192177
let amm_program = Pubkey::from_str(AMM_PROGRAM)?;
193178
debug!("amm pool id: {amm_pool_id}");
194-
let client = get_rpc_client_blocking()?;
179+
195180
let swap_info_result = amm_cli::calculate_swap_info(
196-
&client,
181+
&self.client,
197182
amm_program,
198183
amm_pool_id,
199184
user_input_token,
@@ -223,8 +208,7 @@ impl Raydium {
223208
// get rent
224209
let rent = self
225210
.client
226-
.get_minimum_balance_for_rent_exemption(Account::LEN)
227-
.await?;
211+
.get_minimum_balance_for_rent_exemption(Account::LEN)?;
228212
// if buy add amount_specified
229213
let total_amount = if token_in == native_mint {
230214
rent + amount_specified
@@ -306,7 +290,7 @@ impl Raydium {
306290
return Err(anyhow!("instructions is empty, no tx required"));
307291
}
308292

309-
tx::new_signed_and_send(&client, &self.keypair, instructions, use_jito).await
293+
tx::new_signed_and_send(&self.client, &self.keypair, instructions, use_jito).await
310294
}
311295
}
312296

0 commit comments

Comments
 (0)