Skip to content

Commit

Permalink
optimize get ata
Browse files Browse the repository at this point in the history
  • Loading branch information
wisarmy committed Dec 27, 2024
1 parent a65420b commit 9faa3ba
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 67 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ SLIPPAGE=10
UNIT_PRICE=1 # micro-lamports, 1 lamport = 1,000,000 micro-lamports (10^6)
UNIT_LIMIT=300000

# jito
# jito (Recommend)
JITO_BLOCK_ENGINE_URL=https://mainnet.block-engine.jito.wtf
JITO_TIP_STREAM_URL=ws://bundles-api-rest.jito.wtf/api/v1/bundles/tip_stream
# only support: 25 50 75 95 99
Expand Down
15 changes: 2 additions & 13 deletions src/pump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,8 @@ impl Pump {
let (bonding_curve, associated_bonding_curve, bonding_curve_account) =
get_bonding_curve_account(self.client_blocking.clone().unwrap(), &mint, &pump_program)
.await?;

let in_ata = token::get_associated_token_address(
self.client.clone(),
self.keypair.clone(),
&token_in,
&owner,
);
let out_ata = token::get_associated_token_address(
self.client.clone(),
self.keypair.clone(),
&token_out,
&owner,
);
let in_ata = get_associated_token_address(&owner, &token_in);
let out_ata = get_associated_token_address(&owner, &token_out);

let mut create_instruction = None;
let mut close_instruction = None;
Expand Down
18 changes: 5 additions & 13 deletions src/raydium.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ use solana_sdk::{
signer::Signer,
system_instruction,
};
use spl_associated_token_account::instruction::create_associated_token_account;
use spl_associated_token_account::{
get_associated_token_address, instruction::create_associated_token_account,
};
use spl_token::{amount_to_ui_amount, ui_amount_to_amount};
use spl_token_client::token::TokenError;
use std::{str::FromStr, sync::Arc};
Expand Down Expand Up @@ -101,18 +103,8 @@ impl Raydium {

debug!("token_in:{token_in}, token_out:{token_out}, user_input_token:{user_input_token}, swap_base_in:{swap_base_in}");

let in_ata = token::get_associated_token_address(
self.client.clone(),
self.keypair.clone(),
&token_in,
&owner,
);
let out_ata = token::get_associated_token_address(
self.client.clone(),
self.keypair.clone(),
&token_out,
&owner,
);
let in_ata = get_associated_token_address(&owner, &token_in);
let out_ata = get_associated_token_address(&owner, &token_out);

let mut create_instruction = None;
let mut close_instruction = None;
Expand Down
42 changes: 2 additions & 40 deletions src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use spl_token_2022::{
state::{Account, Mint},
};
use spl_token_client::{
client::{ProgramClient, ProgramRpcClient, ProgramRpcClientSendTransaction, RpcClientResponse},
token::{Token, TokenError, TokenResult},
client::{ProgramClient, ProgramRpcClient, ProgramRpcClientSendTransaction},
token::{TokenError, TokenResult},
};
use tracing::{trace, warn};

Expand Down Expand Up @@ -158,44 +158,6 @@ pub async fn get_account_info(
// token_client.get_account_info(account).await
// }

pub fn get_associated_token_address(
client: Arc<RpcClient>,
keypair: Arc<Keypair>,
address: &Pubkey,
owner: &Pubkey,
) -> Pubkey {
let token_client = Token::new(
Arc::new(ProgramRpcClient::new(
client.clone(),
ProgramRpcClientSendTransaction,
)),
&spl_token::ID,
address,
None,
Arc::new(Keypair::from_bytes(&keypair.to_bytes()).expect("failed to copy keypair")),
);
token_client.get_associated_token_address(owner)
}

pub async fn create_associated_token_account(
client: Arc<RpcClient>,
keypair: Arc<Keypair>,
address: &Pubkey,
owner: &Pubkey,
) -> Result<RpcClientResponse, TokenError> {
let token_client = Token::new(
Arc::new(ProgramRpcClient::new(
client.clone(),
ProgramRpcClientSendTransaction,
)),
&spl_token::ID,
address,
None,
Arc::new(Keypair::from_bytes(&keypair.to_bytes()).expect("failed to copy keypair")),
);
token_client.create_associated_token_account(owner).await
}

pub async fn get_mint_info(
client: Arc<RpcClient>,
_keypair: Arc<Keypair>,
Expand Down

0 comments on commit 9faa3ba

Please sign in to comment.