Skip to content

Commit

Permalink
DeepBook indexer - modify events (#19550)
Browse files Browse the repository at this point in the history
## Description 

Modified the OrderFilled event and added a new OrderExpired event.

## Test plan 

Integration test.

---

## Release notes

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [ ] Protocol: 
- [ ] Nodes (Validators and Full nodes): 
- [ ] Indexer: 
- [ ] JSON-RPC: 
- [ ] GraphQL: 
- [ ] CLI: 
- [ ] Rust SDK:
- [ ] REST API:
  • Loading branch information
0xaslan authored Sep 26, 2024
1 parent 8bd13d0 commit 2362d04
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 16 deletions.
16 changes: 16 additions & 0 deletions crates/sui-deepbook-indexer/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ pub struct MoveOrderFilledEvent {
pub price: u64,
pub taker_is_bid: bool,
pub taker_fee: u64,
pub taker_fee_is_deep: bool,
pub maker_fee: u64,
pub maker_fee_is_deep: bool,
pub base_quantity: u64,
pub quote_quantity: u64,
pub maker_balance_manager_id: ObjectID,
Expand All @@ -36,6 +38,20 @@ pub struct MoveOrderCanceledEvent {
pub timestamp: u64,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct MoveOrderExpiredEvent {
pub balance_manager_id: ObjectID,
pub pool_id: ObjectID,
pub order_id: u128,
pub client_order_id: u64,
pub trader: SuiAddress,
pub price: u64,
pub is_bid: bool,
pub original_quantity: u64,
pub base_asset_quantity_canceled: u64,
pub timestamp: u64,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct MoveOrderModifiedEvent {
pub balance_manager_id: ObjectID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ CREATE TABLE IF NOT EXISTS order_fills
taker_client_order_id BIGINT NOT NULL,
price BIGINT NOT NULL,
taker_fee BIGINT NOT NULL,
taker_fee_is_deep BOOLEAN NOT NULL,
maker_fee BIGINT NOT NULL,
maker_fee_is_deep BOOLEAN NOT NULL,
taker_is_bid BOOLEAN NOT NULL,
base_quantity BIGINT NOT NULL,
quote_quantity BIGINT NOT NULL,
Expand Down Expand Up @@ -178,4 +180,21 @@ CREATE TABLE IF NOT EXISTS sui_error_transactions
failure_status TEXT NOT NULL,
package TEXT NOT NULL,
cmd_idx BIGINT
);
);

CREATE TABLE IF NOT EXISTS pools
(
pool_id TEXT PRIMARY KEY,
pool_name TEXT NOT NULL,
base_asset_id TEXT NOT NULL,
base_asset_decimals SMALLINT NOT NULL,
base_asset_symbol TEXT NOT NULL,
base_asset_name TEXT NOT NULL,
quote_asset_id TEXT NOT NULL,
quote_asset_decimals SMALLINT NOT NULL,
quote_asset_symbol TEXT NOT NULL,
quote_asset_name TEXT NOT NULL,
min_size INTEGER NOT NULL,
lot_size INTEGER NOT NULL,
tick_size INTEGER NOT NULL
);
2 changes: 2 additions & 0 deletions crates/sui-deepbook-indexer/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ pub struct OrderFill {
pub taker_client_order_id: i64,
pub price: i64,
pub taker_fee: i64,
pub taker_fee_is_deep: bool,
pub maker_fee: i64,
pub maker_fee_is_deep: bool,
pub taker_is_bid: bool,
pub base_quantity: i64,
pub quote_quantity: i64,
Expand Down
21 changes: 21 additions & 0 deletions crates/sui-deepbook-indexer/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ diesel::table! {
taker_client_order_id -> Int8,
price -> Int8,
taker_fee -> Int8,
taker_fee_is_deep -> Bool,
maker_fee -> Int8,
maker_fee_is_deep -> Bool,
taker_is_bid -> Bool,
base_quantity -> Int8,
quote_quantity -> Int8,
Expand Down Expand Up @@ -93,6 +95,24 @@ diesel::table! {
}
}

diesel::table! {
pools (pool_id) {
pool_id -> Text,
pool_name -> Text,
base_asset_id -> Text,
base_asset_decimals -> Int2,
base_asset_symbol -> Text,
base_asset_name -> Text,
quote_asset_id -> Text,
quote_asset_decimals -> Int2,
quote_asset_symbol -> Text,
quote_asset_name -> Text,
min_size -> Int4,
lot_size -> Int4,
tick_size -> Int4,
}
}

diesel::table! {
progress_store (task_name) {
task_name -> Text,
Expand Down Expand Up @@ -200,6 +220,7 @@ diesel::allow_tables_to_appear_in_same_query!(
order_fills,
order_updates,
pool_prices,
pools,
progress_store,
proposals,
rebates,
Expand Down
48 changes: 33 additions & 15 deletions crates/sui-deepbook-indexer/src/sui_deepbook_indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ use sui_types::execution_status::ExecutionStatus;
use sui_types::full_checkpoint_content::CheckpointTransaction;

use crate::events::{
MoveBalanceEvent, MoveFlashLoanBorrowedEvent, MoveOrderCanceledEvent, MoveOrderFilledEvent,
MoveOrderModifiedEvent, MoveOrderPlacedEvent, MovePriceAddedEvent, MoveProposalEvent,
MoveRebateEvent, MoveStakeEvent, MoveTradeParamsUpdateEvent, MoveVoteEvent,
MoveBalanceEvent, MoveFlashLoanBorrowedEvent, MoveOrderCanceledEvent, MoveOrderExpiredEvent,
MoveOrderFilledEvent, MoveOrderModifiedEvent, MoveOrderPlacedEvent, MovePriceAddedEvent,
MoveProposalEvent, MoveRebateEvent, MoveStakeEvent, MoveTradeParamsUpdateEvent, MoveVoteEvent,
};
use crate::metrics::DeepBookIndexerMetrics;
use crate::postgres_manager::PgPool;
Expand Down Expand Up @@ -376,7 +376,6 @@ fn process_sui_event(
match ev.type_.name.as_str() {
"OrderPlaced" => {
info!("Observed Deepbook Order Placed {:?}", ev);
// metrics.total_sui_token_deposited.inc();
let move_event: MoveOrderPlacedEvent = bcs::from_bytes(&ev.contents)?;
let txn_kind = tx.transaction.transaction_data().clone().into_kind();
let first_command = txn_kind.iter_commands().next();
Expand Down Expand Up @@ -405,7 +404,6 @@ fn process_sui_event(
}
"OrderModified" => {
info!("Observed Deepbook Order Modified {:?}", ev);
// metrics.total_sui_token_deposited.inc();
let move_event: MoveOrderModifiedEvent = bcs::from_bytes(&ev.contents)?;
let txn_kind = tx.transaction.transaction_data().clone().into_kind();
let first_command = txn_kind.iter_commands().next();
Expand Down Expand Up @@ -434,7 +432,6 @@ fn process_sui_event(
}
"OrderCanceled" => {
info!("Observed Deepbook Order Canceled {:?}", ev);
// metrics.total_sui_token_deposited.inc();
let move_event: MoveOrderCanceledEvent = bcs::from_bytes(&ev.contents)?;
let txn_kind = tx.transaction.transaction_data().clone().into_kind();
let first_command = txn_kind.iter_commands().next();
Expand All @@ -461,9 +458,36 @@ fn process_sui_event(
balance_manager_id: move_event.balance_manager_id.to_string(),
}))
}
"OrderExpired" => {
info!("Observed Deepbook Order Expired {:?}", ev);
let move_event: MoveOrderExpiredEvent = bcs::from_bytes(&ev.contents)?;
let txn_kind = tx.transaction.transaction_data().clone().into_kind();
let first_command = txn_kind.iter_commands().next();
let package = if let Some(Command::MoveCall(move_call)) = first_command {
move_call.package.to_string()
} else {
"".to_string()
};
Some(ProcessedTxnData::OrderUpdate(OrderUpdate {
digest: tx.transaction.digest().to_string(),
sender: tx.transaction.sender_address().to_string(),
checkpoint,
package,
status: OrderUpdateStatus::Expired,
pool_id: move_event.pool_id.to_string(),
order_id: move_event.order_id,
client_order_id: move_event.client_order_id,
price: move_event.price,
is_bid: move_event.is_bid,
onchain_timestamp: move_event.timestamp,
original_quantity: move_event.original_quantity,
quantity: move_event.base_asset_quantity_canceled,
trader: move_event.trader.to_string(),
balance_manager_id: move_event.balance_manager_id.to_string(),
}))
}
"OrderFilled" => {
info!("Observed Deepbook Order Filled {:?}", ev);
// metrics.total_sui_token_deposited.inc();
let move_event: MoveOrderFilledEvent = bcs::from_bytes(&ev.contents)?;
let txn_kind = tx.transaction.transaction_data().clone().into_kind();
let first_command = txn_kind.iter_commands().next();
Expand All @@ -485,7 +509,9 @@ fn process_sui_event(
price: move_event.price,
taker_is_bid: move_event.taker_is_bid,
taker_fee: move_event.taker_fee,
taker_fee_is_deep: move_event.taker_fee_is_deep,
maker_fee: move_event.maker_fee,
maker_fee_is_deep: move_event.maker_fee_is_deep,
base_quantity: move_event.base_quantity,
quote_quantity: move_event.quote_quantity,
maker_balance_manager_id: move_event.maker_balance_manager_id.to_string(),
Expand All @@ -495,7 +521,6 @@ fn process_sui_event(
}
"FlashLoanBorrowed" => {
info!("Observed Deepbook Flash Loan Borrowed {:?}", ev);
// metrics.total_sui_token_deposited.inc();
let move_event: MoveFlashLoanBorrowedEvent = bcs::from_bytes(&ev.contents)?;
let txn_kind = tx.transaction.transaction_data().clone().into_kind();
let first_command = txn_kind.iter_commands().next();
Expand All @@ -517,7 +542,6 @@ fn process_sui_event(
}
"PriceAdded" => {
info!("Observed Deepbook Price Addition {:?}", ev);
// metrics.total_sui_token_deposited.inc();
let move_event: MovePriceAddedEvent = bcs::from_bytes(&ev.contents)?;
let txn_kind = tx.transaction.transaction_data().clone().into_kind();
let first_command = txn_kind.iter_commands().next();
Expand All @@ -538,7 +562,6 @@ fn process_sui_event(
}
"BalanceEvent" => {
info!("Observed Deepbook Balance Event {:?}", ev);
// metrics.total_sui_token_deposited.inc();
let move_event: MoveBalanceEvent = bcs::from_bytes(&ev.contents)?;
let txn_kind = tx.transaction.transaction_data().clone().into_kind();
let first_command = txn_kind.iter_commands().next();
Expand All @@ -560,7 +583,6 @@ fn process_sui_event(
}
"ProposalEvent" => {
info!("Observed Deepbook Proposal Event {:?}", ev);
// metrics.total_sui_token_deposited.inc();
let move_event: MoveProposalEvent = bcs::from_bytes(&ev.contents)?;
let txn_kind = tx.transaction.transaction_data().clone().into_kind();
let first_command = txn_kind.iter_commands().next();
Expand All @@ -583,7 +605,6 @@ fn process_sui_event(
}
"RebateEvent" => {
info!("Observed Deepbook Rebate Event {:?}", ev);
// metrics.total_sui_token_deposited.inc();
let move_event: MoveRebateEvent = bcs::from_bytes(&ev.contents)?;
let txn_kind = tx.transaction.transaction_data().clone().into_kind();
let first_command = txn_kind.iter_commands().next();
Expand All @@ -605,7 +626,6 @@ fn process_sui_event(
}
"StakeEvent" => {
info!("Observed Deepbook Stake Event {:?}", ev);
// metrics.total_sui_token_deposited.inc();
let move_event: MoveStakeEvent = bcs::from_bytes(&ev.contents)?;
let txn_kind = tx.transaction.transaction_data().clone().into_kind();
let first_command = txn_kind.iter_commands().next();
Expand All @@ -628,7 +648,6 @@ fn process_sui_event(
}
"TradeParamsUpdateEvent" => {
info!("Observed Deepbook Trade Params Update Event {:?}", ev);
// metrics.total_sui_token_deposited.inc();
let move_event: MoveTradeParamsUpdateEvent = bcs::from_bytes(&ev.contents)?;
let txn_kind = tx.transaction.transaction_data().clone().into_kind();
let first_command = txn_kind.iter_commands().next();
Expand Down Expand Up @@ -662,7 +681,6 @@ fn process_sui_event(
}
"VoteEvent" => {
info!("Observed Deepbook Vote Event {:?}", ev);
// metrics.total_sui_token_deposited.inc();
let move_event: MoveVoteEvent = bcs::from_bytes(&ev.contents)?;
let txn_kind = tx.transaction.transaction_data().clone().into_kind();
let first_command = txn_kind.iter_commands().next();
Expand Down
6 changes: 6 additions & 0 deletions crates/sui-deepbook-indexer/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub(crate) enum OrderUpdateStatus {
Placed,
Modified,
Canceled,
Expired,
}

impl Display for OrderUpdateStatus {
Expand All @@ -46,6 +47,7 @@ impl Display for OrderUpdateStatus {
OrderUpdateStatus::Placed => "Placed",
OrderUpdateStatus::Modified => "Modified",
OrderUpdateStatus::Canceled => "Canceled",
OrderUpdateStatus::Expired => "Expired",
};
write!(f, "{str}")
}
Expand Down Expand Up @@ -106,7 +108,9 @@ pub struct OrderFill {
pub(crate) price: u64,
pub(crate) taker_is_bid: bool,
pub(crate) taker_fee: u64,
pub(crate) taker_fee_is_deep: bool,
pub(crate) maker_fee: u64,
pub(crate) maker_fee_is_deep: bool,
pub(crate) base_quantity: u64,
pub(crate) quote_quantity: u64,
pub(crate) maker_balance_manager_id: String,
Expand All @@ -128,7 +132,9 @@ impl OrderFill {
taker_client_order_id: self.taker_client_order_id as i64,
price: self.price as i64,
taker_fee: self.taker_fee as i64,
taker_fee_is_deep: self.taker_fee_is_deep,
maker_fee: self.maker_fee as i64,
maker_fee_is_deep: self.maker_fee_is_deep,
taker_is_bid: self.taker_is_bid,
base_quantity: self.base_quantity as i64,
quote_quantity: self.quote_quantity as i64,
Expand Down

0 comments on commit 2362d04

Please sign in to comment.