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
237
use solana_client::{
rpc_config::{RpcAccountInfoConfig, RpcProgramAccountsConfig},
rpc_filter::{Memcmp, RpcFilterType},
};
use solana_commitment_config::CommitmentConfig;
use solana_network_sdk::Solana;
use solana_sdk::{account::Account, pubkey::Pubkey};
use std::sync::Arc;
use crate::types::MeteoraError;
use solana_network_sdk::types::Mode;
pub mod events;
pub mod global;
pub mod pool;
pub mod price;
pub mod token;
pub mod trade;
pub mod types;
/// A client for interacting with the Meteora protocol on Solana
/// Provides methods to fetch account data, program accounts, and SPL token accounts
pub struct MeteoraClient {
pub solana: Arc<Solana>,
pub commitment: CommitmentConfig,
}
impl MeteoraClient {
/// Creates a new MeteoraClient with the default confirmed commitment
///
/// # Params
/// mode - Solana Network Mode
///
/// # Example
/// ```
/// use meteora_client::MeteoraClient;
///
/// let client = MeteoraClient::new(solana_network_sdk::types::Mode::MAIN);
/// ```
pub fn new(mode: Mode) -> Result<Self, MeteoraError> {
Ok(Self {
solana: Arc::new(
Solana::new(mode).map_err(|e| MeteoraError::Error(format!("{:?}", e)))?,
),
commitment: CommitmentConfig::confirmed(),
})
}
/// Creates a new MeteoraClient with a custom commitment level
///
/// # Params
/// mode - Solana Network Mode
/// commitment - The commitment level for queries
///
/// # Example
/// ```
/// use meteora_client::MeteoraClient;
/// use solana_commitment_config::CommitmentConfig;
///
/// let client = MeteoraClient::new(solana_network_sdk::types::Mode::MAIN);
/// ```
pub fn new_with_commitment(
mode: Mode,
commitment: CommitmentConfig,
) -> Result<Self, MeteoraError> {
Ok(Self {
solana: Arc::new(
Solana::new(mode).map_err(|e| MeteoraError::Error(format!("{:?}", e)))?,
),
commitment: CommitmentConfig::confirmed(),
})
}
/// Fetches the raw account data for a given address
///
/// # Params
/// address - The Pubkey of the account to fetch
///
/// # Example
/// ```
/// use solana_sdk::pubkey;
/// use meteora_client::MeteoraClient;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = MeteoraClient::new(solana_network_sdk::types::Mode::MAIN);
/// let account_pubkey = pubkey!("So11111111111111111111111111111111111111112");
/// let account_data = client.get_account_data(&account_pubkey)?;
/// # Ok(())
/// # }
/// ```
pub async fn get_account_data(&self, address: &Pubkey) -> Result<Vec<u8>, MeteoraError> {
match self
.solana
.client
.clone()
.unwrap()
.get_account_with_commitment(address, self.commitment)
.await
{
Ok(account) => {
if let Some(account) = account.value {
Ok(account.data)
} else {
Err(MeteoraError::AccountNotFound(format!(
"Account {} not found",
address
)))
}
}
Err(e) => Err(MeteoraError::RpcError(e.to_string())),
}
}
/// Fetches raw account data for multiple addresses in a single request
///
/// # Params
/// addresses - Slice of Pubkeys to fetch
///
/// # Example
/// ```
/// use solana_sdk::pubkey;
/// use meteora_client::MeteoraClient;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = MeteoraClient::new(solana_network_sdk::types::Mode::MAIN);
/// let addresses = vec![
/// pubkey!("So11111111111111111111111111111111111111112"),
/// pubkey!("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v"),
/// ];
/// let accounts_data = client.get_multiple_accounts_data(&addresses)?;
/// # Ok(())
/// # }
/// ```
pub async fn get_multiple_accounts_data(
&self,
addresses: &[Pubkey],
) -> Result<Vec<Vec<u8>>, MeteoraError> {
match self
.solana
.client
.clone()
.unwrap()
.get_multiple_accounts_with_commitment(addresses, self.commitment)
.await
{
Ok(accounts) => {
let mut results = Vec::new();
for account in accounts.value {
if let Some(account) = account {
results.push(account.data);
} else {
results.push(Vec::new());
}
}
Ok(results)
}
Err(e) => Err(MeteoraError::RpcError(e.to_string())),
}
}
/// Fetches all accounts owned by a program with optional filters
///
/// # Params
/// program_id - The program ID to query
/// filters - Optional filters to apply to the query
///
/// # Example
/// ```
/// use solana_sdk::pubkey;
/// use solana_client::rpc_filter::RpcFilterType;
/// use meteora_client::MeteoraClient;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = MeteoraClient::new(solana_network_sdk::types::Mode::MAIN);
/// let program_id = pubkey!("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA");
/// let filters = vec![RpcFilterType::DataSize(165)];
/// let program_accounts = client.get_program_accounts(&program_id, Some(filters))?;
/// # Ok(())
/// # }
/// ```
pub async fn get_program_accounts(
&self,
program_id: &Pubkey,
filters: Option<Vec<RpcFilterType>>,
) -> Result<Vec<(Pubkey, Account)>, MeteoraError> {
let config = RpcProgramAccountsConfig {
filters: Some(filters.unwrap_or_default()),
account_config: RpcAccountInfoConfig {
commitment: Some(self.commitment),
encoding: None,
data_slice: None,
min_context_slot: None,
},
with_context: None,
sort_results: None,
};
match self
.solana
.client
.clone()
.unwrap()
.get_program_accounts_with_config(program_id, config)
.await
{
Ok(accounts) => Ok(accounts),
Err(e) => Err(MeteoraError::RpcError(e.to_string())),
}
}
/// Fetches all SPL token accounts for a specific mint address
///
/// # Params
/// mint - The mint address of the token
///
/// # Example
/// ```
/// use solana_sdk::pubkey;
/// use meteora_client::MeteoraClient;
///
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = MeteoraClient::new(solana_network_sdk::types::Mode::MAIN);
/// let usdc_mint = pubkey!("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v");
/// let token_accounts = client.get_spl_token_accounts_by_mint(&usdc_mint)?;
/// Ok(())
/// }
/// ```
pub async fn get_spl_token_accounts_by_mint(
&self,
mint: &Pubkey,
) -> Result<Vec<(Pubkey, Account)>, MeteoraError> {
let filters = vec![
RpcFilterType::DataSize(165),
RpcFilterType::Memcmp(Memcmp::new_base58_encoded(0, &mint.to_bytes())),
];
self.get_program_accounts(&spl_token::id(), Some(filters))
.await
}
}