Skip to content

Commit

Permalink
Merge pull request #198 from blockworks-foundation/deposit_fix
Browse files Browse the repository at this point in the history
Deposit fix
  • Loading branch information
dafyddd authored Jul 8, 2022
2 parents d7d7946 + ddfd41e commit 107c50c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 20 deletions.
22 changes: 18 additions & 4 deletions program/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -809,22 +809,36 @@ impl Processor {

check!(!mango_account.is_bankrupt, MangoErrorCode::Bankrupt)?;

let mango_cache = MangoCache::load_checked(mango_cache_ai, program_id, &mango_group)?;
let mut mango_cache =
MangoCache::load_mut_checked(mango_cache_ai, program_id, &mango_group)?;

let token_index = mango_group
.find_root_bank_index(root_bank_ai.key)
.ok_or(throw_err!(MangoErrorCode::InvalidRootBank))?;

// Find the node_bank pubkey in root_bank, if not found error
let root_bank = RootBank::load_checked(root_bank_ai, program_id)?;
let mut root_bank = RootBank::load_mut_checked(root_bank_ai, program_id)?;
check!(root_bank.node_banks.contains(node_bank_ai.key), MangoErrorCode::InvalidNodeBank)?;
let mut node_bank = NodeBank::load_mut_checked(node_bank_ai, program_id)?;
check_eq!(&node_bank.vault, vault_ai.key, MangoErrorCode::InvalidVault)?;

// Check validity of root bank cache
// Update root bank and cache to prevent interest leaching
let now_ts = Clock::get()?.unix_timestamp as u64;
root_bank.update_index_without_banks(now_ts, node_bank.deposits, node_bank.borrows)?;
mango_cache.root_bank_cache[token_index] = RootBankCache {
deposit_index: root_bank.deposit_index,
borrow_index: root_bank.borrow_index,
last_update: now_ts,
};

mango_emit_heap!(UpdateRootBankLog {
mango_group: *mango_group_ai.key,
token_index: token_index as u64,
deposit_index: mango_cache.root_bank_cache[token_index].deposit_index.to_bits(),
borrow_index: mango_cache.root_bank_cache[token_index].borrow_index.to_bits()
});

let root_bank_cache = &mango_cache.root_bank_cache[token_index];
root_bank_cache.check_valid(&mango_group, now_ts)?;

let mode = mango_group.tokens[token_index].spot_market_mode;

Expand Down
41 changes: 25 additions & 16 deletions program/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,24 +461,16 @@ impl RootBank {
self.node_banks.iter().position(|pk| pk == node_bank_pk)
}

pub fn update_index(
/// There can only be one node_bank as of v3.5.
/// So this function can be used directly by just passing in the indexed deposits and borrows of that node bank
pub fn update_index_without_banks(
&mut self,
node_bank_ais: &[AccountInfo],
program_id: &Pubkey,
now_ts: u64,
) -> MangoResult<()> {
let mut native_deposits = ZERO_I80F48;
let mut native_borrows = ZERO_I80F48;

for node_bank_ai in node_bank_ais.iter() {
let node_bank = NodeBank::load_checked(node_bank_ai, program_id)?;
native_deposits = native_deposits
.checked_add(node_bank.deposits.checked_mul(self.deposit_index).unwrap())
.unwrap();
native_borrows = native_borrows
.checked_add(node_bank.borrows.checked_mul(self.borrow_index).unwrap())
.unwrap();
}
deposits: I80F48,
borrows: I80F48,
) -> MangoResult {
let native_deposits = deposits.checked_mul(self.deposit_index).unwrap();
let native_borrows = borrows.checked_mul(self.borrow_index).unwrap();

// TODO - is this a good assumption?
let utilization = native_borrows.checked_div(native_deposits).unwrap_or(ZERO_I80F48);
Expand Down Expand Up @@ -513,6 +505,23 @@ impl RootBank {

Ok(())
}
pub fn update_index(
&mut self,
node_bank_ais: &[AccountInfo],
program_id: &Pubkey,
now_ts: u64,
) -> MangoResult<()> {
let mut deposits = ZERO_I80F48;
let mut borrows = ZERO_I80F48;

for node_bank_ai in node_bank_ais.iter() {
let node_bank = NodeBank::load_checked(node_bank_ai, program_id)?;
deposits = deposits.checked_add(node_bank.deposits).unwrap();
borrows = borrows.checked_add(node_bank.borrows).unwrap();
}

self.update_index_without_banks(now_ts, deposits, borrows)
}

/// Socialize the loss on lenders and return (native_loss, percentage_loss)
pub fn socialize_loss(
Expand Down

0 comments on commit 107c50c

Please sign in to comment.