Skip to main content

sol_trade_sdk/utils/
mod.rs

1pub mod calc;
2pub mod price;
3use crate::trading;
4use crate::TradingClient;
5use solana_sdk::pubkey::Pubkey;
6use solana_sdk::signature::Keypair;
7use solana_sdk::signer::Signer;
8
9impl TradingClient {
10    #[inline]
11    pub async fn get_sol_balance(&self, payer: &Pubkey) -> Result<u64, anyhow::Error> {
12        trading::common::utils::get_sol_balance(&self.infrastructure.rpc, payer).await
13    }
14
15    #[inline]
16    pub async fn get_payer_sol_balance(&self) -> Result<u64, anyhow::Error> {
17        trading::common::utils::get_sol_balance(&self.infrastructure.rpc, &self.payer.pubkey())
18            .await
19    }
20
21    #[inline]
22    pub async fn get_token_balance(
23        &self,
24        payer: &Pubkey,
25        mint: &Pubkey,
26    ) -> Result<u64, anyhow::Error> {
27        trading::common::utils::get_token_balance(&self.infrastructure.rpc, payer, mint).await
28    }
29
30    #[inline]
31    pub async fn get_payer_token_balance(&self, mint: &Pubkey) -> Result<u64, anyhow::Error> {
32        trading::common::utils::get_token_balance(
33            &self.infrastructure.rpc,
34            &self.payer.pubkey(),
35            mint,
36        )
37        .await
38    }
39
40    /// 使用与交易一致的 ATA 推导(含 seed 优化)查询 payer 某 mint 的余额;卖出前查余额应使用此接口并传入池的 base_token_program,否则若使用 seed ATA 会查错账户。
41    #[inline]
42    pub async fn get_payer_token_balance_with_program(
43        &self,
44        mint: &Pubkey,
45        token_program: &Pubkey,
46    ) -> Result<u64, anyhow::Error> {
47        trading::common::utils::get_token_balance_with_options(
48            &self.infrastructure.rpc,
49            &self.payer.pubkey(),
50            mint,
51            token_program,
52            self.use_seed_optimize,
53        )
54        .await
55    }
56
57    #[inline]
58    pub fn get_payer_pubkey(&self) -> Pubkey {
59        self.payer.pubkey()
60    }
61
62    #[inline]
63    pub fn get_payer(&self) -> &Keypair {
64        self.payer.as_ref()
65    }
66
67    #[inline]
68    pub async fn transfer_sol(
69        &self,
70        payer: &Keypair,
71        receive_wallet: &Pubkey,
72        amount: u64,
73    ) -> Result<(), anyhow::Error> {
74        trading::common::utils::transfer_sol(
75            &self.infrastructure.rpc,
76            payer,
77            receive_wallet,
78            amount,
79        )
80        .await
81    }
82
83    #[inline]
84    pub async fn close_token_account(&self, mint: &Pubkey) -> Result<(), anyhow::Error> {
85        trading::common::utils::close_token_account(
86            &self.infrastructure.rpc,
87            self.payer.as_ref(),
88            mint,
89        )
90        .await
91    }
92}