Skip to content

Commit

Permalink
fix: explicit liquidation checks
Browse files Browse the repository at this point in the history
  • Loading branch information
J committed Nov 7, 2023
1 parent ed86fbe commit 1ace78e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
14 changes: 13 additions & 1 deletion programs/marginfi/src/instructions/marginfi_account/liquidate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::constants::{
INSURANCE_VAULT_SEED, LIQUIDATION_INSURANCE_FEE, LIQUIDATION_LIQUIDATOR_FEE, MAX_PRICE_AGE_SEC,
};
use crate::events::{AccountEventHeader, LendingAccountLiquidateEvent, LiquidationBalances};
use crate::prelude::*;
use crate::state::marginfi_account::{
calc_asset_amount, calc_asset_value, RiskEngine, RiskRequirementType,
};
Expand All @@ -13,6 +12,7 @@ use crate::{
constants::{LIQUIDITY_VAULT_AUTHORITY_SEED, LIQUIDITY_VAULT_SEED},
state::marginfi_account::{BankAccountWrapper, MarginfiAccount},
};
use crate::{check, prelude::*};
use anchor_lang::prelude::*;
use anchor_spl::token::{Token, TokenAccount, Transfer};
use fixed::types::I80F48;
Expand Down Expand Up @@ -71,6 +71,18 @@ pub fn lending_account_liquidate(
ctx: Context<LendingAccountLiquidate>,
asset_amount: u64,
) -> MarginfiResult {
check!(
asset_amount > 0,
MarginfiError::IllegalLiquidation,
"Asset amount must be positive"
);

check!(
ctx.accounts.asset_bank.key() != ctx.accounts.liab_bank.key(),
MarginfiError::IllegalLiquidation,
"Asset and liability bank cannot be the same"
);

let LendingAccountLiquidate {
liquidator_marginfi_account: liquidator_marginfi_account_loader,
liquidatee_marginfi_account: liquidatee_marginfi_account_loader,
Expand Down
16 changes: 16 additions & 0 deletions programs/marginfi/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ macro_rules! check {
return Err(error_code.into());
}
};

($cond:expr, $err:expr, $($arg:tt)*) => {
if !($cond) {
let error_code: $crate::errors::MarginfiError = $err;
#[cfg(not(feature = "test-bpf"))]
anchor_lang::prelude::msg!(
"Error \"{}\" thrown at {}:{}",
error_code,
file!(),
line!()
);
#[cfg(not(feature = "test-bpf"))]
anchor_lang::prelude::msg!($($arg)*);
return Err(error_code.into());
}
};
}

#[macro_export]
Expand Down

0 comments on commit 1ace78e

Please sign in to comment.