1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
use super::*;
use crate::types::OrcaResult;
use base64::{Engine, prelude::BASE64_STANDARD};
use solana_account_decoder::{UiAccountData, UiAccountEncoding};
use solana_client::rpc_request::TokenAccountsFilter;
use solana_sdk::program_pack::Pack;
impl OrcaClient {
/// Get the balance of a specific token for a given owner and mint
///
/// # Params
/// owner - The public key of the token account owner
/// mint - The public key of the token mint
///
/// # Example
/// ```rust
/// use solana_sdk::pubkey;
///
/// # async fn example(client: &OrcaClient) -> OrcaResult<()> {
/// let owner = pubkey!("OwnerPublicKeyHere");
/// let mint = pubkey!("MintPublicKeyHere");
/// let balance = client.get_token_balance(&owner, &mint).await?;
/// println!("Token balance: {}", balance);
/// # Ok(())
/// # }
/// ```
pub async fn get_token_balance(&self, owner: &Pubkey, mint: &Pubkey) -> OrcaResult<u64> {
let token_accounts = self
.solana
.client
.as_ref()
.ok_or(OrcaError::Error("RPC client not available".to_string()))?
.get_token_accounts_by_owner(owner, TokenAccountsFilter::Mint(*mint))
.await
.map_err(|e| OrcaError::Error(format!("Failed to get token accounts: {}", e)))?;
if let Some(account) = token_accounts.first() {
let account_data_bytes = self.decode_account_data(&account.account.data)?;
let account_data: spl_token::state::Account =
spl_token::state::Account::unpack(&account_data_bytes).map_err(|e| {
OrcaError::Error(format!("Failed to unpack token account: {}", e))
})?;
Ok(account_data.amount)
} else {
Ok(0)
}
}
/// Get balances for all tokens owned by a specific account
///
/// # Params
/// owner - The public key of the token account owner
///
/// # Example
/// ```rust
/// use solana_sdk::pubkey;
///
/// # async fn example(client: &OrcaClient) -> OrcaResult<()> {
/// let owner = pubkey!("OwnerPublicKeyHere");
/// let balances = client.get_all_token_balances(&owner).await?;
/// for (mint, balance) in balances {
/// println!("Mint: {}, Balance: {}", mint, balance);
/// }
/// # Ok(())
/// # }
/// ```
pub async fn get_all_token_balances(&self, owner: &Pubkey) -> OrcaResult<Vec<(Pubkey, u64)>> {
let token_accounts = self
.solana
.client
.as_ref()
.ok_or(OrcaError::Error("RPC client not available".to_string()))?
.get_token_accounts_by_owner(owner, TokenAccountsFilter::ProgramId(spl_token::id()))
.await
.map_err(|e| OrcaError::Error(format!("Failed to get token accounts: {}", e)))?;
let mut balances = Vec::new();
for account in token_accounts {
let account_data_bytes = self.decode_account_data(&account.account.data)?;
let account_data: spl_token::state::Account =
spl_token::state::Account::unpack(&account_data_bytes).map_err(|e| {
OrcaError::Error(format!("Failed to unpack token account: {}", e))
})?;
if account_data.amount > 0 {
balances.push((account_data.mint, account_data.amount));
}
}
Ok(balances)
}
/// Ensure a token account exists for the given keypair and mint
/// Creates the account if it doesn't exist
///
/// # Params
/// keypair - The keypair that owns the token account
/// mint - The public key of the token mint
///
/// # Example
/// ```rust
/// use solana_sdk::{pubkey, signer::keypair::Keypair};
///
/// # async fn example(client: &OrcaClient) -> OrcaResult<()> {
/// let keypair = Keypair::new();
/// let mint = pubkey!("MintPublicKeyHere");
/// let token_account = client.ensure_token_account(&keypair, &mint).await?;
/// println!("Token account: {}", token_account);
/// # Ok(())
/// # }
/// ```
pub async fn ensure_token_account(
&self,
keypair: &Keypair,
mint: &Pubkey,
) -> OrcaResult<Pubkey> {
let associated_token_address = self.get_associated_token_address(&keypair.pubkey(), mint);
match self
.solana
.client
.as_ref()
.ok_or(OrcaError::Error("RPC client not available".to_string()))?
.get_account(&associated_token_address)
.await
{
Ok(_) => Ok(associated_token_address),
Err(_) => self.create_associated_token_account(keypair, mint).await,
}
}
/// Create an associated token account for the given keypair and mint
///
/// # Params
/// keypair - The keypair that will own the token account
/// mint - The public key of the token mint
///
/// # Example
/// ```rust
/// use solana_sdk::{pubkey, signer::keypair::Keypair};
///
/// # async fn example(client: &OrcaClient) -> OrcaResult<()> {
/// let keypair = Keypair::new();
/// let mint = pubkey!("MintPublicKeyHere");
/// let token_account = client.create_associated_token_account(&keypair, &mint).await?;
/// println!("Created token account: {}", token_account);
/// # Ok(())
/// # }
/// ```
pub async fn create_associated_token_account(
&self,
keypair: &Keypair,
mint: &Pubkey,
) -> OrcaResult<Pubkey> {
let recent_blockhash = self
.solana
.client
.as_ref()
.ok_or(OrcaError::Error("RPC client not available".to_string()))?
.get_latest_blockhash()
.await
.map_err(|e| OrcaError::Error(format!("Failed to get blockhash: {}", e)))?;
let instruction =
spl_associated_token_account::instruction::create_associated_token_account(
&keypair.pubkey(),
&keypair.pubkey(),
mint,
&spl_token::id(),
);
let message = Message::new(&[instruction], Some(&keypair.pubkey()));
let transaction = Transaction::new(&[keypair], message, recent_blockhash);
self.solana
.client
.as_ref()
.ok_or(OrcaError::Error("RPC client not available".to_string()))?
.send_and_confirm_transaction(&transaction)
.await
.map_err(|e| OrcaError::Error(format!("Failed to create token account: {}", e)))?;
Ok(self.get_associated_token_address(&keypair.pubkey(), mint))
}
/// Get the total supply of a token mint
///
/// # Params
/// mint - The public key of the token mint
///
/// # Example
/// ```rust
/// use solana_sdk::pubkey;
///
/// # async fn example(client: &OrcaClient) -> OrcaResult<()> {
/// let mint = pubkey!("MintPublicKeyHere");
/// let supply = client.get_token_supply(&mint).await?;
/// println!("Token supply: {}", supply);
/// # Ok(())
/// # }
/// ```
pub async fn get_token_supply(&self, mint: &Pubkey) -> OrcaResult<u64> {
let mint_account = self
.solana
.client
.as_ref()
.ok_or(OrcaError::Error("RPC client not available".to_string()))?
.get_account(mint)
.await
.map_err(|e| OrcaError::Error(format!("Failed to get mint account: {}", e)))?;
let mint_data: spl_token::state::Mint = spl_token::state::Mint::unpack(&mint_account.data)
.map_err(|e| OrcaError::Error(format!("Failed to unpack mint data: {}", e)))?;
Ok(mint_data.supply)
}
/// Decode account data from various encoding formats
///
/// # Params
/// ui_account_data - The UI account data to decode
///
pub fn decode_account_data(&self, ui_account_data: &UiAccountData) -> OrcaResult<Vec<u8>> {
match ui_account_data {
UiAccountData::Binary(data, encoding) => match encoding {
UiAccountEncoding::Base64 => BASE64_STANDARD
.decode(data)
.map_err(|e| OrcaError::Error(format!("Base64 decode error: {}", e))),
UiAccountEncoding::Base64Zstd => {
let compressed_data = BASE64_STANDARD
.decode(data)
.map_err(|e| OrcaError::Error(format!("Base64 decode error: {}", e)))?;
zstd::decode_all(&compressed_data[..])
.map_err(|e| OrcaError::Error(format!("Zstd decode error: {}", e)))
}
_ => Err(OrcaError::Error(format!(
"Unsupported encoding: {:?}",
encoding
))),
},
_ => Err(OrcaError::Error(
"Unsupported account data format".to_string(),
)),
}
}
}