Skip to content

Commit

Permalink
Lou/logs (#250)
Browse files Browse the repository at this point in the history
* [wip] maker indexing

* trigger on all market orders

* log taker signature

* test: update tests

* use emit_stack instead of emit to save heap space

* remove PlaceOrderLog

---------

Co-authored-by: Maximilian Schneider <[email protected]>
  • Loading branch information
Lou-Kamades and mschneider authored Apr 16, 2024
1 parent 6ba26ed commit 5dd7400
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ pub fn cancel_all_and_place_orders<'c: 'info, 'info>(
} = book.new_order(
order,
&mut market,
&ctx.accounts.market.key(),
&mut event_heap,
oracle_price_lots,
Some(&mut open_orders_account),
Expand Down
4 changes: 2 additions & 2 deletions programs/openbook-v2/src/instructions/deposit.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::accounts_ix::Deposit;
use crate::error::*;
use crate::logs::DepositLog;
use crate::logs::{emit_stack, DepositLog};
use crate::token_utils::*;
use anchor_lang::prelude::*;

Expand Down Expand Up @@ -33,7 +33,7 @@ pub fn deposit(ctx: Context<Deposit>, base_amount: u64, quote_amount: u64) -> Re
market.quote_deposit_total += quote_amount;

if base_amount > 0 || quote_amount > 0 {
emit!(DepositLog {
emit_stack(DepositLog {
open_orders_account: ctx.accounts.open_orders_account.key(),
signer: ctx.accounts.owner.key(),
base_amount,
Expand Down
1 change: 1 addition & 0 deletions programs/openbook-v2/src/instructions/place_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub fn place_order<'c: 'info, 'info>(
} = book.new_order(
&order,
&mut market,
&ctx.accounts.market.key(),
&mut event_heap,
oracle_price_lots,
Some(&mut open_orders_account),
Expand Down
1 change: 1 addition & 0 deletions programs/openbook-v2/src/instructions/place_take_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub fn place_take_order<'c: 'info, 'info>(
} = book.new_order(
&order,
&mut market,
&ctx.accounts.market.key(),
&mut event_heap,
oracle_price_lots,
None,
Expand Down
6 changes: 3 additions & 3 deletions programs/openbook-v2/src/instructions/set_delegate.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anchor_lang::prelude::*;

use crate::accounts_ix::*;
use crate::logs::SetDelegateLog;
use crate::logs::{emit_stack, SetDelegateLog};
use crate::pubkey_option::NonZeroPubkeyOption;

pub fn set_delegate(ctx: Context<SetDelegate>) -> Result<()> {
Expand All @@ -16,9 +16,9 @@ pub fn set_delegate(ctx: Context<SetDelegate>) -> Result<()> {

account.delegate = delegate_account;

emit!(SetDelegateLog {
emit_stack(SetDelegateLog {
open_orders_account: ctx.accounts.open_orders_account.key(),
delegate: delegate_account.into()
delegate: delegate_account.into(),
});

Ok(())
Expand Down
5 changes: 3 additions & 2 deletions programs/openbook-v2/src/instructions/settle_funds.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anchor_lang::prelude::*;

use crate::accounts_ix::*;
use crate::logs::emit_stack;
use crate::logs::SettleFundsLog;
use crate::state::*;
use crate::token_utils::*;
Expand Down Expand Up @@ -73,12 +74,12 @@ pub fn settle_funds<'info>(ctx: Context<'_, '_, '_, 'info, SettleFunds<'info>>)
seeds,
)?;

emit!(SettleFundsLog {
emit_stack(SettleFundsLog {
open_orders_account: ctx.accounts.open_orders_account.key(),
base_native: pa.base_free_native,
quote_native: pa.quote_free_native,
referrer_rebate,
referrer: ctx.accounts.referrer_account.as_ref().map(|acc| acc.key())
referrer: ctx.accounts.referrer_account.as_ref().map(|acc| acc.key()),
});

pa.base_free_native = 0;
Expand Down
4 changes: 2 additions & 2 deletions programs/openbook-v2/src/instructions/sweep_fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::state::market_seeds;
use anchor_lang::prelude::*;

use crate::accounts_ix::*;
use crate::logs::SweepFeesLog;
use crate::logs::{emit_stack, SweepFeesLog};
use crate::token_utils::*;

pub fn sweep_fees(ctx: Context<SweepFees>) -> Result<()> {
Expand All @@ -24,7 +24,7 @@ pub fn sweep_fees(ctx: Context<SweepFees>) -> Result<()> {
seeds,
)?;

emit!(SweepFeesLog {
emit_stack(SweepFeesLog {
market: ctx.accounts.market.key(),
amount,
receiver: ctx.accounts.token_receiver_account.key(),
Expand Down
22 changes: 22 additions & 0 deletions programs/openbook-v2/src/logs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
use anchor_lang::prelude::*;
use borsh::BorshSerialize;

#[inline(never)] // ensure fresh stack frame
pub fn emit_stack<T: anchor_lang::Event>(e: T) {
use std::io::{Cursor, Write};

// stack buffer, stack frames are 4kb
let mut buffer = [0u8; 3000];

let mut cursor = Cursor::new(&mut buffer[..]);
cursor.write_all(&T::DISCRIMINATOR).unwrap();
e.serialize(&mut cursor)
.expect("event must fit into stack buffer");

let pos = cursor.position() as usize;
anchor_lang::solana_program::log::sol_log_data(&[&buffer[..pos]]);
}

#[event]
pub struct DepositLog {
pub open_orders_account: Pubkey,
Expand Down Expand Up @@ -33,6 +49,12 @@ pub struct FillLog {
pub quantity: i64, // number of base lots
}

#[event]
pub struct TakerSignatureLog {
pub market: Pubkey,
pub seq_num: u64,
}

#[event]
pub struct MarketMetaDataLog {
pub market: Pubkey,
Expand Down
14 changes: 7 additions & 7 deletions programs/openbook-v2/src/state/open_orders_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use derivative::Derivative;
use static_assertions::const_assert_eq;
use std::mem::size_of;

use crate::logs::FillLog;
use crate::logs::{emit_stack, FillLog};
use crate::pubkey_option::NonZeroPubkeyOption;
use crate::{error::*, logs::OpenOrdersPositionLog};

Expand Down Expand Up @@ -205,13 +205,13 @@ impl OpenOrdersAccount {
0
};

emit!(FillLog {
emit_stack(FillLog {
market: self.market,
taker_side: fill.taker_side,
maker_slot: fill.maker_slot,
maker_out: fill.maker_out(),
timestamp: fill.timestamp,
seq_num: fill.seq_num,
seq_num: fill.market_seq_num,
maker: fill.maker,
maker_client_order_id: fill.maker_client_order_id,
maker_fee: maker_fees,
Expand All @@ -224,7 +224,7 @@ impl OpenOrdersAccount {
});

let pa = &self.position;
emit!(OpenOrdersPositionLog {
emit_stack(OpenOrdersPositionLog {
owner: self.owner,
open_orders_account_num: self.account_num,
market: self.market,
Expand All @@ -236,7 +236,7 @@ impl OpenOrdersAccount {
locked_maker_fees: pa.locked_maker_fees,
referrer_rebates_available: pa.referrer_rebates_available,
maker_volume: pa.maker_volume,
taker_volume: pa.taker_volume
taker_volume: pa.taker_volume,
})
}

Expand All @@ -260,7 +260,7 @@ impl OpenOrdersAccount {
pa.referrer_rebates_available += referrer_amount;
market.referrer_rebates_accrued += referrer_amount;

emit!(OpenOrdersPositionLog {
emit_stack(OpenOrdersPositionLog {
owner: self.owner,
open_orders_account_num: self.account_num,
market: self.market,
Expand All @@ -272,7 +272,7 @@ impl OpenOrdersAccount {
locked_maker_fees: pa.locked_maker_fees,
referrer_rebates_available: pa.referrer_rebates_available,
maker_volume: pa.maker_volume,
taker_volume: pa.taker_volume
taker_volume: pa.taker_volume,
})
}

Expand Down
12 changes: 9 additions & 3 deletions programs/openbook-v2/src/state/orderbook/book.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::logs::TotalOrderFillEvent;
use crate::logs::*;
use crate::state::MAX_OPEN_ORDERS;
use crate::{
error::*,
Expand Down Expand Up @@ -62,6 +62,7 @@ impl<'a> Orderbook<'a> {
&mut self,
order: &Order,
open_book_market: &mut Market,
market_pk: &Pubkey,
event_heap: &mut EventHeap,
oracle_price_lots: Option<i64>,
mut open_orders_account: Option<&mut OpenOrdersAccount>,
Expand Down Expand Up @@ -229,7 +230,7 @@ impl<'a> Orderbook<'a> {
maker_out,
best_opposing.node.owner_slot,
now_ts,
event_heap.header.seq_num,
market.seq_num,
best_opposing.node.owner,
best_opposing.node.client_order_id,
best_opposing.node.timestamp,
Expand All @@ -240,6 +241,11 @@ impl<'a> Orderbook<'a> {
match_base_lots,
);

emit_stack(TakerSignatureLog {
market: *market_pk,
seq_num: market.seq_num,
});

process_fill_event(
fill,
market,
Expand Down Expand Up @@ -298,7 +304,7 @@ impl<'a> Orderbook<'a> {
),
};

emit!(TotalOrderFillEvent {
emit_stack(TotalOrderFillEvent {
side: side.into(),
taker: *owner,
total_quantity_paid,
Expand Down
6 changes: 3 additions & 3 deletions programs/openbook-v2/src/state/orderbook/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ pub struct FillEvent {
pub maker_slot: u8,
pub padding: [u8; 4],
pub timestamp: u64,
pub seq_num: u64,
pub market_seq_num: u64,

pub maker: Pubkey,

Expand All @@ -269,7 +269,7 @@ impl FillEvent {
maker_out: bool,
maker_slot: u8,
timestamp: u64,
seq_num: u64,
market_seq_num: u64,
maker: Pubkey,
maker_client_order_id: u64,
maker_timestamp: u64,
Expand All @@ -285,7 +285,7 @@ impl FillEvent {
maker_out: maker_out.into(),
maker_slot,
timestamp,
seq_num,
market_seq_num,
maker,
maker_client_order_id,
maker_timestamp,
Expand Down
7 changes: 7 additions & 0 deletions programs/openbook-v2/src/state/orderbook/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ mod tests {
let (mut openbook_market, oracle_price_lots, mut event_heap, book_accs) =
test_setup(5000.0);
let mut book = book_accs.orderbook();
let market_pk = Pubkey::new_unique();

let mut new_order =
|book: &mut Orderbook, event_heap: &mut EventHeap, side, price_lots, now_ts| -> u128 {
Expand All @@ -121,6 +122,7 @@ mod tests {
self_trade_behavior: SelfTradeBehavior::DecrementTake,
},
&mut openbook_market,
&market_pk,
event_heap,
oracle_price_lots,
Some(&mut account),
Expand Down Expand Up @@ -229,6 +231,7 @@ mod tests {
fn book_new_order() {
let (mut market, oracle_price_lots, mut event_heap, book_accs) = test_setup(1000.0);
let mut book = book_accs.orderbook();
let market_pk = Pubkey::new_unique();

// Add lots and fees to make sure to exercise unit conversion
market.base_lot_size = 10;
Expand Down Expand Up @@ -262,6 +265,7 @@ mod tests {
self_trade_behavior: SelfTradeBehavior::DecrementTake,
},
&mut market,
&market_pk,
&mut event_heap,
oracle_price_lots,
Some(&mut maker),
Expand Down Expand Up @@ -308,6 +312,7 @@ mod tests {
self_trade_behavior: SelfTradeBehavior::DecrementTake,
},
&mut market,
&market_pk,
&mut event_heap,
oracle_price_lots,
Some(&mut taker),
Expand Down Expand Up @@ -371,6 +376,7 @@ mod tests {
let (mut market, oracle_price_lots, mut event_heap, book_accs) = test_setup(5000.0);
let quote_lot_size = market.quote_lot_size;
let mut book = book_accs.orderbook();
let market_pk = Pubkey::new_unique();

let mut new_order = |book: &mut Orderbook,
event_heap: &mut EventHeap,
Expand All @@ -395,6 +401,7 @@ mod tests {
self_trade_behavior: SelfTradeBehavior::DecrementTake,
},
&mut market,
&market_pk,
event_heap,
oracle_price_lots,
Some(&mut account),
Expand Down

0 comments on commit 5dd7400

Please sign in to comment.