Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions bots/crates/market-maker/examples/initialization_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ async fn main() -> anyhow::Result<()> {
.await?;

let seat = e2e
.find_seat(&maker_address)?
.fetch_seat(&maker_address)
.await?
.expect("Should have a seat")
.index;

Expand All @@ -80,7 +81,8 @@ async fn main() -> anyhow::Result<()> {
maker_keypair: maker.insecure_clone().to_base58_string(),
market: e2e.market.market,
maker_seat: e2e
.view_market()?
.view_market()
.await?
.seats
.iter()
.find(|s| s.user == maker_address)
Expand Down
1 change: 1 addition & 0 deletions bots/crates/market-maker/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,5 @@ pub async fn initialize_context_from_cli(
target_base,
initial_price_feed_response,
)
.await
}
7 changes: 2 additions & 5 deletions bots/crates/market-maker/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ use solana_client::{
},
};
use strum_macros::Display;
use tokio::{
sync::watch,
time::sleep,
};
use tokio::sync::watch;
use tokio_stream::StreamExt;
use transaction_parser::views::try_market_view_all_from_owner_and_data;

Expand Down Expand Up @@ -241,6 +238,6 @@ async fn throttled_order_update(

// Sleep for the throttle window in milliseconds before doing work again.
// This effectively means the loop only does the cancel/post work once every window of time.
sleep(Duration::from_millis(throttle_window_ms)).await;
tokio::time::sleep(Duration::from_millis(throttle_window_ms)).await;
}
}
6 changes: 3 additions & 3 deletions bots/crates/market-maker/src/maker_context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub struct MakerContext {

impl MakerContext {
/// Creates a new maker context from a token pair.
pub fn init(
pub async fn init(
rpc: &CustomRpcClient,
maker: Keypair,
base_mint: Address,
Expand All @@ -77,8 +77,8 @@ impl MakerContext {
initial_price_feed_response: OandaCandlestickResponse,
) -> anyhow::Result<Self> {
let market_ctx =
MarketContext::new_from_token_pair(rpc, base_mint, quote_mint, None, None)?;
let market = market_ctx.view_market(rpc)?;
MarketContext::new_from_token_pair(rpc, base_mint, quote_mint, None, None).await?;
let market = market_ctx.view_market(rpc).await?;
let latest_state = MakerState::new_from_market(maker.pubkey(), market)?;
let mid_price = get_normalized_mid_price(initial_price_feed_response, &pair, &market_ctx)?;
let maker_address = maker.pubkey();
Expand Down
7 changes: 4 additions & 3 deletions client/examples/close_seat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,20 @@ async fn main() -> anyhow::Result<()> {
.send_single_signer(&e2e.rpc, &trader)
.await?;

let market = e2e.view_market()?;
let market = e2e.view_market().await?;
print_kv!("Seats before", market.header.num_seats, LogColor::Info);

let user_seat = e2e
.find_seat(&trader.pubkey())?
.fetch_seat(&trader.pubkey())
.await?
.expect("User should have been registered on deposit");

e2e.market
.close_seat(trader.pubkey(), user_seat.index)
.send_single_signer(&e2e.rpc, &trader)
.await?;

let market = e2e.view_market()?;
let market = e2e.view_market().await?;
print_kv!("Seats after", market.header.num_seats, LogColor::Info);

Ok(())
Expand Down
5 changes: 3 additions & 2 deletions client/examples/deposit_and_withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ async fn main() -> anyhow::Result<()> {
.send_single_signer(&e2e.rpc, &trader)
.await?;

println!("{:#?}", e2e.view_market());
println!("{:#?}", e2e.view_market().await?);

let user_seat = e2e
.find_seat(&trader.pubkey())?
.fetch_seat(&trader.pubkey())
.await?
.expect("User should have been registered on deposit");

let res = e2e
Expand Down
11 changes: 5 additions & 6 deletions client/examples/many_instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,11 @@ async fn main() -> anyhow::Result<()> {
)
.await?;

let seats: Vec<SectorIndex> = traders
let market_seats = e2e.view_market().await?.seats;
let trader_seats: Vec<SectorIndex> = traders
.iter()
.map(|trader| {
e2e.find_seat(&trader.address())
.ok()
.flatten()
e2e.find_seat(&market_seats, &trader.address())
.expect("Trader should have a seat")
.index
})
Expand All @@ -76,7 +75,7 @@ async fn main() -> anyhow::Result<()> {

let (deposits, withdraws): (Vec<Instruction>, Vec<Instruction>) = traders
.iter()
.zip(seats)
.zip(trader_seats)
.map(|(trader, seat)| {
let trader_addr = trader.address();
let (deposit, withdraw) = base_amounts.get(&trader_addr).unwrap();
Expand Down Expand Up @@ -108,7 +107,7 @@ async fn main() -> anyhow::Result<()> {
.sorted_by_key(|v| v.0)
.collect_vec();

let market = e2e.view_market()?;
let market = e2e.view_market().await?;

// Check that seats are ordered by address (ascending) and compare the final state of each
// user's seat to the expected state.
Expand Down
28 changes: 14 additions & 14 deletions client/examples/market_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ impl BalanceDelta {
}

impl Balances {
pub fn get(e2e: &E2e, user: &Address) -> anyhow::Result<Self> {
pub async fn fetch(e2e: &E2e, user: &Address) -> anyhow::Result<Self> {
Ok(Self {
base: e2e.get_base_balance(user)?,
quote: e2e.get_quote_balance(user)?,
base: e2e.get_base_balance(user).await?,
quote: e2e.get_quote_balance(user).await?,
})
}

Expand Down Expand Up @@ -109,15 +109,15 @@ struct MarketSnapshot {
}

impl MarketSnapshot {
pub fn new(e2e: &E2e, order_ctx: &OrderContext) -> anyhow::Result<Self> {
pub async fn new(e2e: &E2e, order_ctx: &OrderContext<'_>) -> anyhow::Result<Self> {
let order_info = order_ctx.order_info()?;
let OrderContext {
maker,
maker_side,
taker,
..
} = order_ctx;
let market = e2e.view_market()?;
let market = e2e.view_market().await?;

let maker_seat = market
.seats
Expand All @@ -136,8 +136,8 @@ impl MarketSnapshot {
})
.cloned();

let taker_balances = Balances::get(e2e, &taker.pubkey())?;
let market_balances = Balances::get(e2e, &e2e.market.market)?;
let taker_balances = Balances::fetch(e2e, &taker.pubkey()).await?;
let market_balances = Balances::fetch(e2e, &e2e.market.market).await?;

Ok(Self {
maker_order,
Expand Down Expand Up @@ -214,7 +214,7 @@ async fn post_maker_order(
.await?;

// The maker should have the first and only seat in the market.
let market = e2e.view_market()?;
let market = e2e.view_market().await?;
let maker_seat = market.seats.first().expect("Should have one market seat");
assert_eq!(maker_seat.user, maker.pubkey());

Expand Down Expand Up @@ -258,8 +258,8 @@ async fn initialize_traders_and_market(ctx: &OrderContext<'_>) -> anyhow::Result
)
.await?;

let maker_init = Balances::get(&e2e, &ctx.maker.pubkey())?;
let taker_init = Balances::get(&e2e, &ctx.taker.pubkey())?;
let maker_init = Balances::fetch(&e2e, &ctx.maker.pubkey()).await?;
let taker_init = Balances::fetch(&e2e, &ctx.taker.pubkey()).await?;

maker_init.check(maker_base, maker_quote)?;
taker_init.check(taker_base, taker_quote)?;
Expand Down Expand Up @@ -361,7 +361,7 @@ async fn main() -> anyhow::Result<()> {
let e2e = initialize_traders_and_market(&ctx).await?;

// Local helpers.
let create_snapshot = || MarketSnapshot::new(&e2e, &ctx);
let create_snapshot = async || MarketSnapshot::new(&e2e, &ctx).await;
let taker_base_size = ctx.taker_size_base()?;
let taker_quote_size = ctx.taker_size_quote()?;

Expand All @@ -376,17 +376,17 @@ async fn main() -> anyhow::Result<()> {

// -----------------------------------------------------------------------------------------
// Send the first taker order.
let before_1 = create_snapshot()?;
let before_1 = create_snapshot().await?;
let fill_1 = run_partial_fill(&e2e, &ctx, Denomination::Base).await?;
let after_1 = create_snapshot()?;
let after_1 = create_snapshot().await?;

assert_fill_deltas(side, &before_1, &after_1, taker_base_size, taker_quote_size);

// -----------------------------------------------------------------------------------------
// Send the second taker order.
let before_2 = after_1;
let fill_2 = run_partial_fill(&e2e, &ctx, Denomination::Quote).await?;
let after_2 = create_snapshot()?;
let after_2 = create_snapshot().await?;

assert_fill_deltas(side, &before_2, &after_2, taker_base_size, taker_quote_size);

Expand Down
19 changes: 13 additions & 6 deletions client/examples/post_and_cancel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ async fn main() -> anyhow::Result<()> {
.send_single_signer(&e2e.rpc, trader)
.await?;

println!("Market after user deposit\n{:#?}", e2e.view_market()?);
println!("Market after user deposit\n{:#?}", e2e.view_market().await?);

let user_seat = e2e
.find_seat(&trader.pubkey())?
.fetch_seat(&trader.pubkey())
.await?
.expect("User should have been registered on deposit");

let order_info_args = OrderInfoArgs::new(
Expand Down Expand Up @@ -75,9 +76,12 @@ async fn main() -> anyhow::Result<()> {
post_ask_res.parsed_transaction.signature
);

println!("Market after posting user ask:\n{:#?}", e2e.view_market()?);
println!(
"Market after posting user ask:\n{:#?}",
e2e.view_market().await?
);

let user_seat = e2e.find_seat(&trader.pubkey())?.unwrap();
let user_seat = e2e.fetch_seat(&trader.pubkey()).await?.unwrap();
println!("User seat after posting ask: {user_seat:#?}");

let cancel_ask_res = e2e
Expand All @@ -98,10 +102,13 @@ async fn main() -> anyhow::Result<()> {
cancel_ask_res.parsed_transaction.signature
);

let user_seat = e2e.find_seat(&trader.pubkey())?.unwrap();
let user_seat = e2e.fetch_seat(&trader.pubkey()).await?.unwrap();
println!("User seat after canceling ask: {user_seat:#?}");

println!("Market after canceling user ask:\n{:#?}", e2e.view_market());
println!(
"Market after canceling user ask:\n{:#?}",
e2e.view_market().await?
);

Ok(())
}
16 changes: 10 additions & 6 deletions client/examples/post_asks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ async fn main() -> anyhow::Result<()> {
.send_single_signer(&e2e.rpc, trader)
.await?;

println!("Market after user deposit\n{:#?}", e2e.view_market()?);
println!("Market after user deposit\n{:#?}", e2e.view_market().await?);

let user_seat = e2e
.find_seat(&trader.pubkey())?
.fetch_seat(&trader.pubkey())
.await?
.expect("User should have been registered on deposit");

let order_info_args = OrderInfoArgs::new(
Expand All @@ -70,9 +71,12 @@ async fn main() -> anyhow::Result<()> {
post_ask_res.parsed_transaction.signature
);

println!("Market after posting user ask:\n{:#?}", e2e.view_market()?);
println!(
"Market after posting user ask:\n{:#?}",
e2e.view_market().await?
);

let user_seat = e2e.find_seat(&trader.pubkey())?.unwrap();
let user_seat = e2e.fetch_seat(&trader.pubkey()).await?.unwrap();
println!("User seat after posting ask: {user_seat:#?}");

// Post an ask. The user provides base as collateral and receives quote when filled.
Expand Down Expand Up @@ -104,10 +108,10 @@ async fn main() -> anyhow::Result<()> {

println!(
"Market after posting many user asks:\n{:#?}",
e2e.view_market()?
e2e.view_market().await?
);

let user_seat = e2e.find_seat(&trader.pubkey())?.unwrap();
let user_seat = e2e.fetch_seat(&trader.pubkey()).await?.unwrap();
println!("User seat after posting many asks: {user_seat:#?}");

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion client/examples/two_seats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async fn main() -> anyhow::Result<()> {
.send_single_signer(&e2e.rpc, payer_1)
.await?;

let market = e2e.view_market()?;
let market = e2e.view_market().await?;

// Sanity check.
assert!(payer_1.pubkey() != payer_2.pubkey());
Expand Down
20 changes: 12 additions & 8 deletions client/src/context/market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ impl Denomination {
impl MarketContext {
/// Creates a new [`MarketContext`] from two mint token addresses. Verifies that the tokens
/// exist on-chain but does not verify the market does.
pub fn new_from_token_pair(
pub async fn new_from_token_pair(
rpc: &CustomRpcClient,
base_mint: Address,
quote_mint: Address,
base_mint_authority: Option<Keypair>,
quote_mint_authority: Option<Keypair>,
) -> anyhow::Result<Self> {
let base = TokenContext::new_from_existing(rpc, base_mint, base_mint_authority)?;
let quote = TokenContext::new_from_existing(rpc, quote_mint, quote_mint_authority)?;
let base = TokenContext::new_from_existing(rpc, base_mint, base_mint_authority).await?;
let quote = TokenContext::new_from_existing(rpc, quote_mint, quote_mint_authority).await?;

let (market_address, _bump) = find_market_address(&base.mint_address, &quote.mint_address);
let base_market_ata = base.get_ata_for(&market_address);
Expand Down Expand Up @@ -141,18 +141,22 @@ impl MarketContext {
.expect("Should be a single signer instruction")
}

pub fn view_market(&self, rpc: &CustomRpcClient) -> anyhow::Result<MarketViewAll> {
let market_account = rpc.client.get_account(&self.market)?;
pub async fn view_market(&self, rpc: &CustomRpcClient) -> anyhow::Result<MarketViewAll> {
let market_account = rpc.client.get_account(&self.market).await?;
try_market_view_all_from_owner_and_data(market_account.owner, &market_account.data)
}

pub fn find_seat(
pub async fn fetch_seat(
&self,
rpc: &CustomRpcClient,
user: &Address,
) -> anyhow::Result<Option<MarketSeatView>> {
let market_seats = self.view_market(rpc)?.seats;
Ok(market_seats.into_iter().find(|seat| &seat.user == user))
let market = self.view_market(rpc).await?;
Ok(self.find_seat(&market.seats, user))
}

pub fn find_seat(&self, seats: &[MarketSeatView], user: &Address) -> Option<MarketSeatView> {
seats.iter().find(|seat| &seat.user == user).cloned()
}

pub fn close_seat(&self, user: Address, sector_index_hint: u32) -> SingleSignerInstruction {
Expand Down
Loading