Skip to content

Commit

Permalink
Program: try to fix collateral fees when there are OO
Browse files Browse the repository at this point in the history
  • Loading branch information
farnyser committed Jul 12, 2024
1 parent 732fdd7 commit 0320706
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions programs/mango-v4/src/instructions/token_charge_collateral_fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,12 @@ pub fn token_charge_collateral_fees(ctx: Context<TokenChargeCollateralFees>) ->
continue;
}

let (token_position, raw_token_index) = account.token_position_mut(bank.token_index)?;
let token_balance = token_position.native(&bank);
let token_index_in_health_cache = health_cache
.token_infos
.iter()
.position(|x| x.token_index == bank.token_index)
.expect("missing token in health");
let token_balance = token_balances[token_index_in_health_cache].spot_and_perp;
if token_balance <= 0 {
continue;
}
Expand All @@ -124,15 +128,23 @@ pub fn token_charge_collateral_fees(ctx: Context<TokenChargeCollateralFees>) ->
for bank_ai in &ctx.remaining_accounts[0..token_position_count] {
let mut bank = bank_ai.load_mut::<Bank>()?;

let (token_position, raw_token_index) = account.token_position_mut(bank.token_index)?;
let token_balance = token_position.native(&bank);
let token_info = health_cache.token_info(bank.token_index)?;

let token_index_in_health_cache = health_cache
.token_infos
.iter()
.position(|x| x.token_index == bank.token_index)
.expect("missing token in health");

let token_balance = token_balances[token_index_in_health_cache].spot_and_perp;
let health = token_info.health_contribution(HealthType::Maint, token_balance);

if health >= 0 {
continue;
}

let (token_position, raw_token_index) = account.token_position_mut(bank.token_index)?;

let borrow_scaling = (health / total_liab_health).abs();
let fee = borrow_scaling * total_collateral_fees_in_usd / token_info.prices.oracle;

Expand Down

0 comments on commit 0320706

Please sign in to comment.