Skip to content

Commit 0cd6c59

Browse files
authored
Update CLI to adapt to new sdk params (#28)
1 parent 326023b commit 0cd6c59

13 files changed

+128
-96
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "phoenix-cli"
3-
version = "0.2.1"
3+
version = "0.2.2"
44
description = "CLI and associated library for interacting with the Phoenix program from the command line"
55
edition = "2021"
66
license = "MIT"
@@ -34,7 +34,7 @@ serde = { version = "1.0", features = ["derive"] }
3434
serde_json = "1.0"
3535
spl-associated-token-account = { version = "1.1.1", features = [ "no-entrypoint" ] }
3636
phoenix-v1 = { version = "0.2.2", features = ["no-entrypoint"] }
37-
phoenix-sdk = "0.2.1"
37+
phoenix-sdk = "0.3.3"
3838
bytemuck = "1.13.0"
3939
reqwest = "0.11.14"
4040
bincode = "1.3.3"

src/lib/helpers/market_helpers.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ pub async fn get_book_levels(
154154

155155
pub async fn get_all_approved_seats_for_market(
156156
sdk: &SDKClient,
157+
market: &Pubkey,
157158
) -> anyhow::Result<Vec<(Pubkey, Account)>> {
158159
// Get discriminant for seat account
159160
let seat_account_discriminant = get_discriminant("phoenix::program::accounts::Seat")?;
@@ -163,7 +164,7 @@ pub async fn get_all_approved_seats_for_market(
163164
0,
164165
[
165166
seat_account_discriminant.to_le_bytes().to_vec(),
166-
sdk.active_market_key.to_bytes().to_vec(),
167+
market.to_bytes().to_vec(),
167168
]
168169
.concat(),
169170
));

src/lib/helpers/print_helpers.rs

Lines changed: 66 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,25 @@ use phoenix_sdk::sdk_client::*;
1111
use solana_sdk::program_pack::Pack;
1212
use solana_sdk::pubkey::Pubkey;
1313

14-
pub fn print_book(sdk: &SDKClient, book: &Ladder) {
15-
let asks = book.asks.iter().map(|lvl| {
16-
(
17-
sdk.ticks_to_float_price(lvl.price_in_ticks),
18-
lvl.size_in_base_lots as f64 * sdk.base_lots_to_base_units_multiplier(),
19-
)
14+
pub fn print_book(sdk: &SDKClient, market: &Pubkey, book: &Ladder) -> anyhow::Result<()> {
15+
let meta = sdk.get_market_metadata(market);
16+
let asks = book.asks.iter().filter_map(|lvl| {
17+
Some((
18+
sdk.ticks_to_float_price(market, lvl.price_in_ticks).ok()?,
19+
lvl.size_in_base_lots as f64 * sdk.base_lots_to_base_units_multiplier(market).ok()?,
20+
))
2021
});
2122

22-
let bids = book.bids.iter().map(|lvl| {
23-
(
24-
sdk.ticks_to_float_price(lvl.price_in_ticks),
25-
lvl.size_in_base_lots as f64 * sdk.base_lots_to_base_units_multiplier(),
26-
)
23+
let bids = book.bids.iter().filter_map(|lvl| {
24+
Some((
25+
sdk.ticks_to_float_price(market, lvl.price_in_ticks).ok()?,
26+
lvl.size_in_base_lots as f64 * sdk.base_lots_to_base_units_multiplier(market).ok()?,
27+
))
2728
});
28-
let price_precision: usize =
29-
get_precision(10_u64.pow(sdk.quote_decimals) / sdk.tick_size_in_quote_atoms_per_base_unit);
30-
let size_precision: usize = get_precision(sdk.num_base_lots_per_base_unit);
29+
let price_precision: usize = get_precision(
30+
10_u64.pow(meta.quote_decimals) / meta.tick_size_in_quote_atoms_per_base_unit,
31+
);
32+
let size_precision: usize = get_precision(meta.num_base_lots_per_base_unit);
3133
let bid_strings = bids
3234
.into_iter()
3335
.map(|(price, size)| {
@@ -72,6 +74,7 @@ pub fn print_book(sdk: &SDKClient, book: &Ladder) {
7274
);
7375
println!("{}", str);
7476
}
77+
Ok(())
7578
}
7679

7780
pub fn get_precision(mut target: u64) -> usize {
@@ -117,6 +120,8 @@ pub async fn print_market_details(
117120
let base_pubkey = market_metadata.base_mint;
118121
let quote_pubkey = market_metadata.quote_mint;
119122

123+
let meta = sdk.get_market_metadata(market_pubkey);
124+
120125
let base_vault = get_vault_address(market_pubkey, &base_pubkey).0;
121126
let quote_vault = get_vault_address(market_pubkey, &quote_pubkey).0;
122127

@@ -143,12 +148,12 @@ pub async fn print_market_details(
143148

144149
println!(
145150
"Base Vault balance: {:.3}",
146-
get_decimal_string(base_vault_acct.amount, sdk.base_decimals).parse::<f64>()?
151+
get_decimal_string(base_vault_acct.amount, meta.base_decimals).parse::<f64>()?
147152
);
148153

149154
println!(
150155
"Quote Vault balance: {:.3}",
151-
get_decimal_string(quote_vault_acct.amount, sdk.quote_decimals).parse::<f64>()?
156+
get_decimal_string(quote_vault_acct.amount, meta.quote_decimals).parse::<f64>()?
152157
);
153158

154159
println!("Base Token: {}", base_pubkey);
@@ -187,68 +192,84 @@ pub async fn print_market_details(
187192
println!(
188193
"Uncollected fees, in quote units: {}",
189194
get_decimal_string(
190-
sdk.quote_lots_to_quote_atoms(market.get_uncollected_fee_amount().as_u64()),
195+
sdk.quote_lots_to_quote_atoms(
196+
market_pubkey,
197+
market.get_uncollected_fee_amount().as_u64()
198+
)?,
191199
market_metadata.quote_decimals
192200
)
193201
);
194202
println!(
195203
"Collected fees, in quote units: {}",
196204
get_decimal_string(
197-
sdk.quote_lots_to_quote_atoms(market.get_collected_fee_amount().as_u64()),
205+
sdk.quote_lots_to_quote_atoms(
206+
market_pubkey,
207+
market.get_collected_fee_amount().as_u64()
208+
)?,
198209
market_metadata.quote_decimals
199210
)
200211
);
201212

202213
Ok(())
203214
}
204215

205-
pub fn print_trader_state(sdk: &SDKClient, pubkey: &Pubkey, state: &TraderState) {
216+
pub fn print_trader_state(
217+
sdk: &SDKClient,
218+
market_pubkey: &Pubkey,
219+
pubkey: &Pubkey,
220+
state: &TraderState,
221+
) -> anyhow::Result<()> {
222+
let meta = sdk.get_market_metadata(market_pubkey);
206223
if state.base_lots_locked == 0
207224
&& state.base_lots_free == 0
208225
&& state.quote_lots_locked == 0
209226
&& state.quote_lots_free == 0
210227
{
211-
return;
228+
return Ok(());
212229
}
213230
println!("--------------------------------");
214231
println!("Trader pubkey: {:?}", pubkey);
215232
println!(
216233
"Base token locked: {}",
217234
get_decimal_string(
218-
sdk.base_lots_to_base_atoms(state.base_lots_locked.into()),
219-
sdk.base_decimals
235+
sdk.base_lots_to_base_atoms(market_pubkey, state.base_lots_locked.into())?,
236+
meta.base_decimals
220237
)
221238
);
222239
println!(
223240
"Base token free: {}",
224241
get_decimal_string(
225-
sdk.base_lots_to_base_atoms(state.base_lots_free.into()),
226-
sdk.base_decimals
242+
sdk.base_lots_to_base_atoms(market_pubkey, state.base_lots_free.into())?,
243+
meta.base_decimals
227244
)
228245
);
229246
println!(
230247
"Quote token locked: {}",
231248
get_decimal_string(
232-
sdk.quote_lots_to_quote_atoms(state.quote_lots_locked.into()),
233-
sdk.quote_decimals
249+
sdk.quote_lots_to_quote_atoms(market_pubkey, state.quote_lots_locked.into())?,
250+
meta.quote_decimals
234251
)
235252
);
236253
println!(
237254
"Quote token free: {}",
238255
get_decimal_string(
239-
sdk.quote_lots_to_quote_atoms(state.quote_lots_free.into()),
240-
sdk.quote_decimals
256+
sdk.quote_lots_to_quote_atoms(market_pubkey, state.quote_lots_free.into())?,
257+
meta.quote_decimals
241258
)
242259
);
260+
Ok(())
243261
}
244262

245-
pub async fn log_market_events(sdk: &mut SDKClient, market_events: Vec<PhoenixEvent>) {
263+
pub async fn log_market_events(
264+
sdk: &mut SDKClient,
265+
market_events: Vec<PhoenixEvent>,
266+
) -> anyhow::Result<()> {
246267
for event in market_events {
247268
let market_pubkey = event.market;
248-
if sdk.active_market_key != market_pubkey {
249-
sdk.add_market(&market_pubkey).await.unwrap();
250-
sdk.change_active_market(&market_pubkey).unwrap();
269+
if !sdk.markets.contains_key(&market_pubkey) {
270+
sdk.add_market(&market_pubkey).await?;
251271
}
272+
let metadata = sdk.get_market_metadata(&market_pubkey);
252273
match event.details {
253274
MarketEventDetails::Fill(fill) => {
254275
let Fill {
@@ -263,11 +284,11 @@ pub async fn log_market_events(sdk: &mut SDKClient, market_events: Vec<PhoenixEv
263284
let fill_data = vec![
264285
maker.to_string(),
265286
taker.to_string(),
266-
(sdk.ticks_to_float_price(price_in_ticks)).to_string(),
287+
(sdk.ticks_to_float_price(&market_pubkey, price_in_ticks))?.to_string(),
267288
format!("{:?}", side_filled),
268289
get_decimal_string(
269-
sdk.base_lots_to_base_atoms(base_lots_filled),
270-
sdk.base_decimals,
290+
sdk.base_lots_to_base_atoms(&market_pubkey, base_lots_filled)?,
291+
metadata.base_decimals,
271292
),
272293
];
273294
println!("{}", finalize_log(keys, fill_data));
@@ -285,11 +306,12 @@ pub async fn log_market_events(sdk: &mut SDKClient, market_events: Vec<PhoenixEv
285306
let place_data = vec![
286307
maker.to_string(),
287308
"".to_string(),
288-
(sdk.ticks_to_float_price(price_in_ticks)).to_string(),
309+
sdk.ticks_to_float_price(&market_pubkey, price_in_ticks)?
310+
.to_string(),
289311
format!("{:?}", side),
290312
get_decimal_string(
291-
sdk.base_lots_to_base_atoms(base_lots_placed),
292-
sdk.base_decimals,
313+
sdk.base_lots_to_base_atoms(&market_pubkey, base_lots_placed)?,
314+
metadata.base_decimals,
293315
),
294316
];
295317

@@ -309,11 +331,12 @@ pub async fn log_market_events(sdk: &mut SDKClient, market_events: Vec<PhoenixEv
309331
let reduce_data = vec![
310332
maker.to_string(),
311333
"".to_string(),
312-
(sdk.ticks_to_float_price(price_in_ticks)).to_string(),
334+
sdk.ticks_to_float_price(&market_pubkey, price_in_ticks)?
335+
.to_string(),
313336
format!("{:?}", side),
314337
get_decimal_string(
315-
sdk.base_lots_to_base_atoms(base_lots_removed),
316-
sdk.base_decimals,
338+
sdk.base_lots_to_base_atoms(&market_pubkey, base_lots_removed)?,
339+
metadata.base_decimals,
317340
),
318341
];
319342
println!("{}", finalize_log(keys, reduce_data));
@@ -324,14 +347,15 @@ pub async fn log_market_events(sdk: &mut SDKClient, market_events: Vec<PhoenixEv
324347
} = fill_summary;
325348
println!(
326349
"Total quote token fees paid: {}",
327-
sdk.quote_atoms_to_quote_unit_as_float(total_quote_fees)
350+
sdk.quote_atoms_to_quote_unit_as_float(&market_pubkey, total_quote_fees)?
328351
);
329352
}
330353
_ => {
331354
continue;
332355
}
333356
}
334357
}
358+
Ok(())
335359
}
336360
pub fn initialize_log(event: &PhoenixEvent, event_type: String) -> Vec<String> {
337361
let base_schema: Vec<String> = vec![

src/lib/processor/process_get_all_markets.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
use crate::helpers::{market_helpers::get_all_markets, print_helpers::print_market_summary_data};
12
use anyhow::anyhow;
23
use ellipsis_client::EllipsisClient;
34
use phoenix::program::MarketHeader;
45
use phoenix_sdk::sdk_client::SDKClient;
56
use serde::{Deserialize, Serialize};
67
use solana_sdk::pubkey::Pubkey;
7-
use std::{mem::size_of, str::FromStr};
88
use std::collections::HashMap;
9-
use crate::helpers::{market_helpers::get_all_markets, print_helpers::print_market_summary_data};
9+
use std::{mem::size_of, str::FromStr};
1010

1111
pub async fn process_get_all_markets(client: &EllipsisClient) -> anyhow::Result<()> {
1212
let accounts = get_all_markets(client).await?;
@@ -36,7 +36,7 @@ pub async fn process_get_all_markets_no_gpa(
3636

3737
for market in markets {
3838
let market_pubkey = Pubkey::from_str(&market)?;
39-
let sdk = SDKClient::new(&market_pubkey, &client.payer, network_url).await;
39+
let sdk = SDKClient::new(&client.payer, network_url).await?;
4040

4141
let market_account_data = sdk.client.get_account_data(&market_pubkey).await?;
4242
let (header_bytes, _market_bytes) = market_account_data.split_at(size_of::<MarketHeader>());
@@ -72,5 +72,8 @@ async fn get_market_config(client: &EllipsisClient) -> anyhow::Result<JsonMarket
7272

7373
let markets: HashMap<String, JsonMarketConfig> = serde_json::from_str(&body)?;
7474

75-
Ok(markets.get(cluster).ok_or(anyhow!("No markets found for cluster"))?.clone())
75+
Ok(markets
76+
.get(cluster)
77+
.ok_or(anyhow!("No markets found for cluster"))?
78+
.clone())
7679
}

src/lib/processor/process_get_book_levels.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub async fn process_get_book_levels(
1212
if book.bids.is_empty() && book.asks.is_empty() {
1313
println!("Book is empty");
1414
} else {
15-
print_book(sdk, &book);
15+
print_book(sdk, market_pubkey, &book)?;
1616
}
1717
Ok(())
1818
}

src/lib/processor/process_get_full_book.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub async fn process_get_full_book(market_pubkey: &Pubkey, sdk: &SDKClient) -> a
88
if book.bids.is_empty() && book.asks.is_empty() {
99
println!("Book is empty");
1010
} else {
11-
print_book(sdk, &book);
11+
print_book(sdk, market_pubkey, &book)?;
1212
}
1313
Ok(())
1414
}

src/lib/processor/process_get_market.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use solana_sdk::pubkey::Pubkey;
55
use std::mem::size_of;
66

77
pub async fn process_get_market(market_pubkey: &Pubkey, sdk: &SDKClient) -> anyhow::Result<()> {
8-
let market_metadata = sdk.get_active_market_metadata();
8+
let market_metadata = sdk.get_market_metadata(market_pubkey);
99

1010
let market_account_data = sdk.client.get_account_data(market_pubkey).await?;
1111
let (header_bytes, market_bytes) = market_account_data.split_at(size_of::<MarketHeader>());

0 commit comments

Comments
 (0)