mirror of
https://github.com/d0zingcat/ore.git
synced 2026-05-14 07:26:51 +00:00
events
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -14,19 +14,22 @@ impl Market {
|
||||
clock: Clock,
|
||||
) -> Result<SwapEvent, OreError> {
|
||||
// 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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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(())
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user