From 2de149f74cad05e586fe670a12b6b9e0c5ad9181 Mon Sep 17 00:00:00 2001 From: microwavedcola1 Date: Tue, 17 May 2022 17:08:00 +0200 Subject: [PATCH] checked math + rustfmt Signed-off-by: microwavedcola1 --- keeper/src/crank.rs | 2 +- keeper/src/update_funding.rs | 9 ++------- programs/mango-v4/src/instructions/mod.rs | 4 ++-- programs/mango-v4/src/state/orderbook/book.rs | 14 +++++++------- programs/mango-v4/src/state/perp_market.rs | 3 ++- 5 files changed, 14 insertions(+), 18 deletions(-) diff --git a/keeper/src/crank.rs b/keeper/src/crank.rs index 263f6ca08..a968cb690 100644 --- a/keeper/src/crank.rs +++ b/keeper/src/crank.rs @@ -1,6 +1,6 @@ use std::sync::Arc; -use crate::{consume_events, update_index, MangoClient, update_funding}; +use crate::{consume_events, update_funding, update_index, MangoClient}; use anyhow::ensure; diff --git a/keeper/src/update_funding.rs b/keeper/src/update_funding.rs index af5b58b89..3c2f83a3c 100644 --- a/keeper/src/update_funding.rs +++ b/keeper/src/update_funding.rs @@ -1,13 +1,8 @@ use std::{sync::Arc, time::Duration}; +use mango_v4::state::PerpMarket; - -use mango_v4::state::{PerpMarket}; - -use solana_sdk::{ - instruction::{Instruction}, - pubkey::Pubkey, -}; +use solana_sdk::{instruction::Instruction, pubkey::Pubkey}; use tokio::time; use crate::MangoClient; diff --git a/programs/mango-v4/src/instructions/mod.rs b/programs/mango-v4/src/instructions/mod.rs index 877ff7009..aa21e474e 100644 --- a/programs/mango-v4/src/instructions/mod.rs +++ b/programs/mango-v4/src/instructions/mod.rs @@ -13,6 +13,7 @@ pub use perp_cancel_order_by_client_order_id::*; pub use perp_consume_events::*; pub use perp_create_market::*; pub use perp_place_order::*; +pub use perp_update_funding::*; pub use register_token::*; pub use serum3_cancel_order::*; pub use serum3_create_open_orders::*; @@ -21,7 +22,6 @@ pub use serum3_place_order::*; pub use serum3_register_market::*; pub use serum3_settle_funds::*; pub use set_stub_oracle::*; -pub use perp_update_funding::*; pub use update_index::*; pub use withdraw::*; @@ -40,6 +40,7 @@ mod perp_cancel_order_by_client_order_id; mod perp_consume_events; mod perp_create_market; mod perp_place_order; +mod perp_update_funding; mod register_token; mod serum3_cancel_order; mod serum3_create_open_orders; @@ -48,6 +49,5 @@ mod serum3_place_order; mod serum3_register_market; mod serum3_settle_funds; mod set_stub_oracle; -mod perp_update_funding; mod update_index; mod withdraw; diff --git a/programs/mango-v4/src/state/orderbook/book.rs b/programs/mango-v4/src/state/orderbook/book.rs index 55dcc2c0c..daa3ce634 100644 --- a/programs/mango-v4/src/state/orderbook/book.rs +++ b/programs/mango-v4/src/state/orderbook/book.rs @@ -86,27 +86,27 @@ impl<'a> Book<'a> { /// Get the quantity of valid bids above and including the price pub fn get_bids_size_above(&self, price: i64, max_depth: i64, now_ts: u64) -> i64 { - let mut s = 0; + let mut sum: i64 = 0; for (_, bid) in self.bids.iter_valid(now_ts) { - if price > bid.price() || s >= max_depth { + if price > bid.price() || sum >= max_depth { break; } - s += bid.quantity; + sum = sum.checked_add(bid.quantity).unwrap(); } - s.min(max_depth) + sum.min(max_depth) } /// Walk up the book `quantity` units and return the price at that level. If `quantity` units /// not on book, return None pub fn get_impact_price(&self, side: Side, quantity: i64, now_ts: u64) -> Option { - let mut s = 0; + let mut sum: i64 = 0; let book_side = match side { Side::Bid => self.bids.iter_valid(now_ts), Side::Ask => self.asks.iter_valid(now_ts), }; for (_, order) in book_side { - s += order.quantity; - if s >= quantity { + sum = sum.checked_add(order.quantity).unwrap(); + if sum >= quantity { return Some(order.price()); } } diff --git a/programs/mango-v4/src/state/perp_market.rs b/programs/mango-v4/src/state/perp_market.rs index 286880cc7..2cf102242 100644 --- a/programs/mango-v4/src/state/perp_market.rs +++ b/programs/mango-v4/src/state/perp_market.rs @@ -115,7 +115,8 @@ impl PerpMarket { let diff_price = match (bid, ask) { (Some(bid), Some(ask)) => { // calculate mid-market rate - let book_price = self.lot_to_native_price((bid + ask) / 2); + let mid_price = (bid.checked_add(ask)) / 2; + let book_price = self.lot_to_native_price(mid_price); let diff = cm!(book_price / index_price - I80F48::ONE); diff.clamp(self.min_funding, self.max_funding) }