Skip to content

Commit

Permalink
add checked_to_num in processor.rs where necessary; check quote token…
Browse files Browse the repository at this point in the history
… vault in bankruptcy (#118)

Co-authored-by: dd <[email protected]>
  • Loading branch information
dafyddd and dd authored Jan 18, 2022
1 parent 3583fa1 commit 55bc724
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Mango Program Change Log

## v3.3.1
Deployed: | Slot:
1. Check quote token vault inside resolve_token_bankruptcy
2. Add checked to num for general safety

## v3.3.0
Deployed: Jan 17, 2022 at 00:45:05 UTC | Slot: 116,585,405
1. CancelAllPerpOrdersSide - cancels all order on one side
Expand Down
22 changes: 16 additions & 6 deletions program/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3813,7 +3813,7 @@ impl Processor {
let health_per_lot =
lot_price * (ONE_I80F48 - pmi.init_asset_weight - pmi.liquidation_fee);
let max_transfer = -init_health / health_per_lot;
let max_transfer: i64 = max_transfer.checked_ceil().unwrap().to_num();
let max_transfer: i64 = max_transfer.checked_ceil().unwrap().checked_to_num().unwrap();

let base_transfer =
max_transfer.min(base_transfer_request).min(liqee_perp_account.base_position);
Expand All @@ -3830,7 +3830,7 @@ impl Processor {
let health_per_lot =
lot_price * (ONE_I80F48 - pmi.init_liab_weight + pmi.liquidation_fee);
let max_transfer = -init_health / health_per_lot;
let max_transfer: i64 = max_transfer.checked_floor().unwrap().to_num();
let max_transfer: i64 = max_transfer.checked_floor().unwrap().checked_to_num().unwrap();

let base_transfer =
max_transfer.max(base_transfer_request).max(liqee_perp_account.base_position);
Expand Down Expand Up @@ -3984,7 +3984,8 @@ impl Processor {
.min(-quote_pos) // minimum of what liqor wants and what liqee has
.checked_ceil() // round up and convert to native quote token
.unwrap()
.to_num::<u64>()
.checked_to_num::<u64>()
.unwrap()
.min(insurance_vault.amount); // take min of what ins. fund has

if liab_transfer_u64 != 0 {
Expand Down Expand Up @@ -4158,20 +4159,24 @@ impl Processor {
let insurance_transfer = (liab_transfer * liab_price / liab_fee)
.checked_ceil()
.unwrap()
.to_num::<u64>()
.checked_to_num::<u64>()
.unwrap()
.min(insurance_vault.amount);

if insurance_transfer != 0 {
// First transfer quote currency into Mango quote vault from insurance fund
check!(signer_ai.key == &mango_group.signer_key, MangoErrorCode::InvalidSignerKey)?;
let signers_seeds = gen_signer_seeds(&mango_group.signer_nonce, mango_group_ai.key);
invoke_transfer(
token_prog_ai,
insurance_vault_ai,
quote_vault_ai,
quote_vault_ai, // this vault is checked in conditional branch below
signer_ai,
&[&signers_seeds],
insurance_transfer,
)?;

// Transfer equivalent amount of liabilities adjusted for fees
let liab_transfer = I80F48::from_num(insurance_transfer) * liab_fee / liab_price;

check!(
Expand All @@ -4180,7 +4185,11 @@ impl Processor {
)?;
let mut liab_node_bank = NodeBank::load_mut_checked(liab_node_bank_ai, program_id)?;

// Only load quote banks if they are different from liab banks to prevent double mut borrow
if liab_index == QUOTE_INDEX {
check!(quote_vault_ai.key == &liab_node_bank.vault, MangoErrorCode::InvalidVault)?;

// Increase the quote balance on the liqor equivalent to insurance transfer
checked_change_net(
&mango_cache.root_bank_cache[QUOTE_INDEX],
&mut liab_node_bank,
Expand All @@ -4190,7 +4199,7 @@ impl Processor {
I80F48::from_num(insurance_transfer),
)?;
} else {
// Load the bank for quote token
// Load the bank for quote token which we now know is different from liab banks
let quote_root_bank = RootBank::load_checked(quote_root_bank_ai, program_id)?;
check!(
&mango_group.tokens[QUOTE_INDEX].root_bank == quote_root_bank_ai.key,
Expand All @@ -4202,6 +4211,7 @@ impl Processor {
quote_root_bank.node_banks.contains(quote_node_bank_ai.key),
MangoErrorCode::InvalidNodeBank
)?;
check!(quote_vault_ai.key == &quote_node_bank.vault, MangoErrorCode::InvalidVault)?;

checked_change_net(
&mango_cache.root_bank_cache[QUOTE_INDEX],
Expand Down

0 comments on commit 55bc724

Please sign in to comment.