Skip to content

Commit

Permalink
Update CLI to adapt to new sdk params (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
jarry-xiao authored Mar 17, 2023
1 parent 326023b commit 0cd6c59
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 96 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "phoenix-cli"
version = "0.2.1"
version = "0.2.2"
description = "CLI and associated library for interacting with the Phoenix program from the command line"
edition = "2021"
license = "MIT"
Expand Down Expand Up @@ -34,7 +34,7 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
spl-associated-token-account = { version = "1.1.1", features = [ "no-entrypoint" ] }
phoenix-v1 = { version = "0.2.2", features = ["no-entrypoint"] }
phoenix-sdk = "0.2.1"
phoenix-sdk = "0.3.3"
bytemuck = "1.13.0"
reqwest = "0.11.14"
bincode = "1.3.3"
3 changes: 2 additions & 1 deletion src/lib/helpers/market_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ pub async fn get_book_levels(

pub async fn get_all_approved_seats_for_market(
sdk: &SDKClient,
market: &Pubkey,
) -> anyhow::Result<Vec<(Pubkey, Account)>> {
// Get discriminant for seat account
let seat_account_discriminant = get_discriminant("phoenix::program::accounts::Seat")?;
Expand All @@ -163,7 +164,7 @@ pub async fn get_all_approved_seats_for_market(
0,
[
seat_account_discriminant.to_le_bytes().to_vec(),
sdk.active_market_key.to_bytes().to_vec(),
market.to_bytes().to_vec(),
]
.concat(),
));
Expand Down
108 changes: 66 additions & 42 deletions src/lib/helpers/print_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,25 @@ use phoenix_sdk::sdk_client::*;
use solana_sdk::program_pack::Pack;
use solana_sdk::pubkey::Pubkey;

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

let bids = book.bids.iter().map(|lvl| {
(
sdk.ticks_to_float_price(lvl.price_in_ticks),
lvl.size_in_base_lots as f64 * sdk.base_lots_to_base_units_multiplier(),
)
let bids = book.bids.iter().filter_map(|lvl| {
Some((
sdk.ticks_to_float_price(market, lvl.price_in_ticks).ok()?,
lvl.size_in_base_lots as f64 * sdk.base_lots_to_base_units_multiplier(market).ok()?,
))
});
let price_precision: usize =
get_precision(10_u64.pow(sdk.quote_decimals) / sdk.tick_size_in_quote_atoms_per_base_unit);
let size_precision: usize = get_precision(sdk.num_base_lots_per_base_unit);
let price_precision: usize = get_precision(
10_u64.pow(meta.quote_decimals) / meta.tick_size_in_quote_atoms_per_base_unit,
);
let size_precision: usize = get_precision(meta.num_base_lots_per_base_unit);
let bid_strings = bids
.into_iter()
.map(|(price, size)| {
Expand Down Expand Up @@ -72,6 +74,7 @@ pub fn print_book(sdk: &SDKClient, book: &Ladder) {
);
println!("{}", str);
}
Ok(())
}

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

let meta = sdk.get_market_metadata(market_pubkey);

let base_vault = get_vault_address(market_pubkey, &base_pubkey).0;
let quote_vault = get_vault_address(market_pubkey, &quote_pubkey).0;

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

println!(
"Base Vault balance: {:.3}",
get_decimal_string(base_vault_acct.amount, sdk.base_decimals).parse::<f64>()?
get_decimal_string(base_vault_acct.amount, meta.base_decimals).parse::<f64>()?
);

println!(
"Quote Vault balance: {:.3}",
get_decimal_string(quote_vault_acct.amount, sdk.quote_decimals).parse::<f64>()?
get_decimal_string(quote_vault_acct.amount, meta.quote_decimals).parse::<f64>()?
);

println!("Base Token: {}", base_pubkey);
Expand Down Expand Up @@ -187,68 +192,84 @@ pub async fn print_market_details(
println!(
"Uncollected fees, in quote units: {}",
get_decimal_string(
sdk.quote_lots_to_quote_atoms(market.get_uncollected_fee_amount().as_u64()),
sdk.quote_lots_to_quote_atoms(
market_pubkey,
market.get_uncollected_fee_amount().as_u64()
)?,
market_metadata.quote_decimals
)
);
println!(
"Collected fees, in quote units: {}",
get_decimal_string(
sdk.quote_lots_to_quote_atoms(market.get_collected_fee_amount().as_u64()),
sdk.quote_lots_to_quote_atoms(
market_pubkey,
market.get_collected_fee_amount().as_u64()
)?,
market_metadata.quote_decimals
)
);

Ok(())
}

pub fn print_trader_state(sdk: &SDKClient, pubkey: &Pubkey, state: &TraderState) {
pub fn print_trader_state(
sdk: &SDKClient,
market_pubkey: &Pubkey,
pubkey: &Pubkey,
state: &TraderState,
) -> anyhow::Result<()> {
let meta = sdk.get_market_metadata(market_pubkey);
if state.base_lots_locked == 0
&& state.base_lots_free == 0
&& state.quote_lots_locked == 0
&& state.quote_lots_free == 0
{
return;
return Ok(());
}
println!("--------------------------------");
println!("Trader pubkey: {:?}", pubkey);
println!(
"Base token locked: {}",
get_decimal_string(
sdk.base_lots_to_base_atoms(state.base_lots_locked.into()),
sdk.base_decimals
sdk.base_lots_to_base_atoms(market_pubkey, state.base_lots_locked.into())?,
meta.base_decimals
)
);
println!(
"Base token free: {}",
get_decimal_string(
sdk.base_lots_to_base_atoms(state.base_lots_free.into()),
sdk.base_decimals
sdk.base_lots_to_base_atoms(market_pubkey, state.base_lots_free.into())?,
meta.base_decimals
)
);
println!(
"Quote token locked: {}",
get_decimal_string(
sdk.quote_lots_to_quote_atoms(state.quote_lots_locked.into()),
sdk.quote_decimals
sdk.quote_lots_to_quote_atoms(market_pubkey, state.quote_lots_locked.into())?,
meta.quote_decimals
)
);
println!(
"Quote token free: {}",
get_decimal_string(
sdk.quote_lots_to_quote_atoms(state.quote_lots_free.into()),
sdk.quote_decimals
sdk.quote_lots_to_quote_atoms(market_pubkey, state.quote_lots_free.into())?,
meta.quote_decimals
)
);
Ok(())
}

pub async fn log_market_events(sdk: &mut SDKClient, market_events: Vec<PhoenixEvent>) {
pub async fn log_market_events(
sdk: &mut SDKClient,
market_events: Vec<PhoenixEvent>,
) -> anyhow::Result<()> {
for event in market_events {
let market_pubkey = event.market;
if sdk.active_market_key != market_pubkey {
sdk.add_market(&market_pubkey).await.unwrap();
sdk.change_active_market(&market_pubkey).unwrap();
if !sdk.markets.contains_key(&market_pubkey) {
sdk.add_market(&market_pubkey).await?;
}
let metadata = sdk.get_market_metadata(&market_pubkey);
match event.details {
MarketEventDetails::Fill(fill) => {
let Fill {
Expand All @@ -263,11 +284,11 @@ pub async fn log_market_events(sdk: &mut SDKClient, market_events: Vec<PhoenixEv
let fill_data = vec![
maker.to_string(),
taker.to_string(),
(sdk.ticks_to_float_price(price_in_ticks)).to_string(),
(sdk.ticks_to_float_price(&market_pubkey, price_in_ticks))?.to_string(),
format!("{:?}", side_filled),
get_decimal_string(
sdk.base_lots_to_base_atoms(base_lots_filled),
sdk.base_decimals,
sdk.base_lots_to_base_atoms(&market_pubkey, base_lots_filled)?,
metadata.base_decimals,
),
];
println!("{}", finalize_log(keys, fill_data));
Expand All @@ -285,11 +306,12 @@ pub async fn log_market_events(sdk: &mut SDKClient, market_events: Vec<PhoenixEv
let place_data = vec![
maker.to_string(),
"".to_string(),
(sdk.ticks_to_float_price(price_in_ticks)).to_string(),
sdk.ticks_to_float_price(&market_pubkey, price_in_ticks)?
.to_string(),
format!("{:?}", side),
get_decimal_string(
sdk.base_lots_to_base_atoms(base_lots_placed),
sdk.base_decimals,
sdk.base_lots_to_base_atoms(&market_pubkey, base_lots_placed)?,
metadata.base_decimals,
),
];

Expand All @@ -309,11 +331,12 @@ pub async fn log_market_events(sdk: &mut SDKClient, market_events: Vec<PhoenixEv
let reduce_data = vec![
maker.to_string(),
"".to_string(),
(sdk.ticks_to_float_price(price_in_ticks)).to_string(),
sdk.ticks_to_float_price(&market_pubkey, price_in_ticks)?
.to_string(),
format!("{:?}", side),
get_decimal_string(
sdk.base_lots_to_base_atoms(base_lots_removed),
sdk.base_decimals,
sdk.base_lots_to_base_atoms(&market_pubkey, base_lots_removed)?,
metadata.base_decimals,
),
];
println!("{}", finalize_log(keys, reduce_data));
Expand All @@ -324,14 +347,15 @@ pub async fn log_market_events(sdk: &mut SDKClient, market_events: Vec<PhoenixEv
} = fill_summary;
println!(
"Total quote token fees paid: {}",
sdk.quote_atoms_to_quote_unit_as_float(total_quote_fees)
sdk.quote_atoms_to_quote_unit_as_float(&market_pubkey, total_quote_fees)?
);
}
_ => {
continue;
}
}
}
Ok(())
}
pub fn initialize_log(event: &PhoenixEvent, event_type: String) -> Vec<String> {
let base_schema: Vec<String> = vec![
Expand Down
11 changes: 7 additions & 4 deletions src/lib/processor/process_get_all_markets.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::helpers::{market_helpers::get_all_markets, print_helpers::print_market_summary_data};
use anyhow::anyhow;
use ellipsis_client::EllipsisClient;
use phoenix::program::MarketHeader;
use phoenix_sdk::sdk_client::SDKClient;
use serde::{Deserialize, Serialize};
use solana_sdk::pubkey::Pubkey;
use std::{mem::size_of, str::FromStr};
use std::collections::HashMap;
use crate::helpers::{market_helpers::get_all_markets, print_helpers::print_market_summary_data};
use std::{mem::size_of, str::FromStr};

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

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

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

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

Ok(markets.get(cluster).ok_or(anyhow!("No markets found for cluster"))?.clone())
Ok(markets
.get(cluster)
.ok_or(anyhow!("No markets found for cluster"))?
.clone())
}
2 changes: 1 addition & 1 deletion src/lib/processor/process_get_book_levels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub async fn process_get_book_levels(
if book.bids.is_empty() && book.asks.is_empty() {
println!("Book is empty");
} else {
print_book(sdk, &book);
print_book(sdk, market_pubkey, &book)?;
}
Ok(())
}
2 changes: 1 addition & 1 deletion src/lib/processor/process_get_full_book.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub async fn process_get_full_book(market_pubkey: &Pubkey, sdk: &SDKClient) -> a
if book.bids.is_empty() && book.asks.is_empty() {
println!("Book is empty");
} else {
print_book(sdk, &book);
print_book(sdk, market_pubkey, &book)?;
}
Ok(())
}
2 changes: 1 addition & 1 deletion src/lib/processor/process_get_market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use solana_sdk::pubkey::Pubkey;
use std::mem::size_of;

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

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

0 comments on commit 0cd6c59

Please sign in to comment.