Skip to content

Commit

Permalink
checked math in all areas touched by place_perp_order (#109)
Browse files Browse the repository at this point in the history
* checked math in all areas touched by place_perp_order

* update deploy time

* update deploy time

Co-authored-by: dd <[email protected]>
  • Loading branch information
dafyddd and dd authored Jan 11, 2022
1 parent 51172e4 commit 1b1e8dd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Mango Program Change Log

## v3.2.16
Deployed: Jan 11, 2022 at 01:59:05 UTC | Slot: 115,691,635
1. Checked math in all areas touched by place_perp_order

## v3.2.15
Deployed: Jan 10, 2022 at 22:00:54 UTC | Slot: 115,666,186
1. Impose price limits on spot orders
Expand Down
22 changes: 16 additions & 6 deletions program/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1805,19 +1805,29 @@ impl PerpAccount {
pmc: &PerpMarketCache,
price: I80F48,
) -> MangoResult<(I80F48, I80F48)> {
let bids_base_net = self.base_position + self.taker_base + self.bids_quantity;
let asks_base_net = self.base_position + self.taker_base - self.asks_quantity;
let curr_pos = self.base_position + self.taker_base;
let bids_base_net = curr_pos.checked_add(self.bids_quantity).unwrap();
let asks_base_net = curr_pos.checked_sub(self.asks_quantity).unwrap();

if bids_base_net.abs() > asks_base_net.abs() {
let base = I80F48::from_num(bids_base_net * pmi.base_lot_size) * price;
let base = I80F48::from_num(bids_base_net.checked_mul(pmi.base_lot_size).unwrap())
.checked_mul(price)
.unwrap();
let quote = self.get_quote_position(pmc)
+ I80F48::from_num(self.taker_quote * pmi.quote_lot_size)
- I80F48::from_num(self.bids_quantity * pmi.base_lot_size) * price;
- I80F48::from_num(self.bids_quantity.checked_mul(pmi.base_lot_size).unwrap())
.checked_mul(price)
.unwrap();
Ok((base, quote))
} else {
let base = I80F48::from_num(asks_base_net * pmi.base_lot_size) * price;
let base = I80F48::from_num(asks_base_net.checked_mul(pmi.base_lot_size).unwrap())
.checked_mul(price)
.unwrap();
let quote = self.get_quote_position(pmc)
+ I80F48::from_num(self.taker_quote * pmi.quote_lot_size)
+ I80F48::from_num(self.asks_quantity * pmi.base_lot_size) * price;
+ I80F48::from_num(self.asks_quantity.checked_mul(pmi.base_lot_size).unwrap())
.checked_mul(price)
.unwrap();
Ok((base, quote))
}
}
Expand Down

0 comments on commit 1b1e8dd

Please sign in to comment.