checked math + rustfmt

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
This commit is contained in:
microwavedcola1 2022-05-17 17:08:00 +02:00
parent 15ef563812
commit 2de149f74c
5 changed files with 14 additions and 18 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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<i64> {
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());
}
}

View File

@ -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)
}