From b24883d8ff524a0ade55bc98246dd4b771cf5100 Mon Sep 17 00:00:00 2001 From: Hardhat Chad Date: Fri, 13 Jun 2025 16:05:39 -0700 Subject: [PATCH] events --- api/src/event.rs | 34 ++++++++++++++++++++- api/src/state/block.rs | 2 +- api/src/state/market/buy_exact_in.rs | 1 + api/src/state/market/buy_exact_out.rs | 1 + api/src/state/market/sell_exact_in.rs | 1 + api/src/state/market/sell_exact_out.rs | 1 + api/src/state/market/swap.rs | 7 +++-- api/src/state/market/virtual_limit_order.rs | 2 +- program/src/close.rs | 1 + program/src/mine.rs | 2 ++ program/src/open.rs | 12 ++++++++ 11 files changed, 59 insertions(+), 5 deletions(-) diff --git a/api/src/event.rs b/api/src/event.rs index a8e0aa9..dcd5bdb 100644 --- a/api/src/event.rs +++ b/api/src/event.rs @@ -1,6 +1,6 @@ use steel::*; -use crate::state::SwapDirection; +use crate::state::{RewardConfig, SwapDirection}; #[repr(C)] #[derive(Clone, Copy, Debug, Default, PartialEq, Pod, Zeroable)] @@ -34,6 +34,9 @@ pub struct SwapEvent { /// Amount of quote tokens taken in fees. pub quote_fee: u64, + + /// The timestamp of the event. + pub ts: i64, } impl SwapEvent { @@ -56,6 +59,9 @@ pub struct RewardEvent { /// The type of reward. pub rewards_type: u64, + + /// The timestamp of the event. + pub ts: i64, } #[repr(u8)] @@ -66,5 +72,31 @@ pub enum RewardsType { Motherlode = 2, } +#[repr(C)] +#[derive(Clone, Copy, Debug, Default, PartialEq, Pod, Zeroable)] +pub struct OpenEvent { + /// The signer of the open transaction. + pub signer: Pubkey, + + /// The id of the block. + pub id: u64, + + /// The start slot of the block. + pub start_slot: u64, + + /// The base liquidity in the market. + pub liquidity_base: u64, + + /// The quote liquidity in the market. + pub liquidity_quote: u64, + + /// The reward configuration. + pub reward_config: RewardConfig, + + /// The timestamp of the event. + pub ts: i64, +} + event!(SwapEvent); event!(RewardEvent); +event!(OpenEvent); diff --git a/api/src/state/block.rs b/api/src/state/block.rs index 415d439..edc0b32 100644 --- a/api/src/state/block.rs +++ b/api/src/state/block.rs @@ -34,7 +34,7 @@ pub struct Block { /// Configuration specifying how rewards are paid out. #[repr(C)] -#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)] +#[derive(Clone, Copy, Debug, Default, PartialEq, Pod, Zeroable)] pub struct RewardConfig { /// The reward paid to the submitter of the best hash. pub lode_reward: u64, diff --git a/api/src/state/market/buy_exact_in.rs b/api/src/state/market/buy_exact_in.rs index b2a66d2..c95dae1 100644 --- a/api/src/state/market/buy_exact_in.rs +++ b/api/src/state/market/buy_exact_in.rs @@ -64,6 +64,7 @@ impl Market { base_via_curve: base_via_curve as u64, quote_via_curve: quote_via_curve as u64, quote_fee: quote_fee as u64, + ts: 0, }; // Sanity check swap event. diff --git a/api/src/state/market/buy_exact_out.rs b/api/src/state/market/buy_exact_out.rs index 4afed20..7410f25 100644 --- a/api/src/state/market/buy_exact_out.rs +++ b/api/src/state/market/buy_exact_out.rs @@ -69,6 +69,7 @@ impl Market { base_via_curve: base_via_curve as u64, quote_via_curve: quote_via_curve as u64, quote_fee: quote_fee as u64, + ts: 0, }; // Sanity check swap result. diff --git a/api/src/state/market/sell_exact_in.rs b/api/src/state/market/sell_exact_in.rs index 3c212b0..43b3418 100644 --- a/api/src/state/market/sell_exact_in.rs +++ b/api/src/state/market/sell_exact_in.rs @@ -73,6 +73,7 @@ impl Market { base_via_curve: base_via_curve as u64, quote_via_curve: quote_via_curve as u64, quote_fee: quote_fee as u64, + ts: 0, }; // Sanity check swap result. diff --git a/api/src/state/market/sell_exact_out.rs b/api/src/state/market/sell_exact_out.rs index 023765b..fa2e2d5 100644 --- a/api/src/state/market/sell_exact_out.rs +++ b/api/src/state/market/sell_exact_out.rs @@ -71,6 +71,7 @@ impl Market { base_via_curve: base_via_curve as u64, quote_via_curve: quote_via_curve as u64, quote_fee: quote_fee as u64, + ts: 0, }; // Sanity check swap result. diff --git a/api/src/state/market/swap.rs b/api/src/state/market/swap.rs index 0959d9d..0c3e2b9 100644 --- a/api/src/state/market/swap.rs +++ b/api/src/state/market/swap.rs @@ -14,19 +14,22 @@ impl Market { clock: Clock, ) -> Result { // Update snapshot. - self.update_snapshot(clock); + self.update_snapshot(&clock); // Get invariant. let k_pre = self.k(); // Execute swap. - let swap_event = match (direction, precision) { + let mut swap_event = match (direction, precision) { (SwapDirection::Buy, SwapPrecision::ExactIn) => self.buy_exact_in(amount)?, (SwapDirection::Buy, SwapPrecision::ExactOut) => self.buy_exact_out(amount)?, (SwapDirection::Sell, SwapPrecision::ExactIn) => self.sell_exact_in(amount)?, (SwapDirection::Sell, SwapPrecision::ExactOut) => self.sell_exact_out(amount)?, }; + // Update timestamp. + swap_event.ts = clock.unix_timestamp; + // Check invariant. let k_post = self.k(); if k_pre > k_post { diff --git a/api/src/state/market/virtual_limit_order.rs b/api/src/state/market/virtual_limit_order.rs index 74ea3fa..fab5a2d 100644 --- a/api/src/state/market/virtual_limit_order.rs +++ b/api/src/state/market/virtual_limit_order.rs @@ -121,7 +121,7 @@ impl Market { } } - pub(crate) fn update_snapshot(&mut self, clock: Clock) { + pub(crate) fn update_snapshot(&mut self, clock: &Clock) { let slot = clock.slot; let snapshot_slot = (slot / SLOT_WINDOW) * SLOT_WINDOW; if snapshot_slot != self.snapshot.slot { diff --git a/program/src/close.rs b/program/src/close.rs index 9974031..3aa36ae 100644 --- a/program/src/close.rs +++ b/program/src/close.rs @@ -76,6 +76,7 @@ pub fn process_close(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResul authority: block.reward.lode_authority, block_id: block.id, rewards_type: RewardsType::Lode as u64, + ts: clock.unix_timestamp, } .log(); } diff --git a/program/src/mine.rs b/program/src/mine.rs index 559d031..220c2ed 100644 --- a/program/src/mine.rs +++ b/program/src/mine.rs @@ -137,6 +137,7 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult authority: miner.authority, block_id: block.id, rewards_type: RewardsType::Motherlode as u64, + ts: clock.unix_timestamp, } .log(); } @@ -164,6 +165,7 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult authority: miner.authority, block_id: block.id, rewards_type: RewardsType::Nugget as u64, + ts: clock.unix_timestamp, } .log(); } diff --git a/program/src/open.rs b/program/src/open.rs index 33e2b6d..eb75ca2 100644 --- a/program/src/open.rs +++ b/program/src/open.rs @@ -223,6 +223,18 @@ pub fn process_open(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult &[BLOCK, &id.to_le_bytes()], )?; + // Emit event. + OpenEvent { + id, + start_slot, + signer: *signer_info.key, + reward_config: block.reward, + liquidity_base: market.base.liquidity() as u64, + liquidity_quote: market.quote.liquidity() as u64, + ts: clock.unix_timestamp, + } + .log_return(); + Ok(()) }