Skip to content

Commit

Permalink
tweaks reviewing changes
Browse files Browse the repository at this point in the history
  • Loading branch information
crispheaney committed Jan 2, 2025
1 parent aabf9c7 commit b303c7d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 39 deletions.
12 changes: 3 additions & 9 deletions programs/drift/src/controller/liquidation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1715,6 +1715,7 @@ pub fn liquidate_spot(
pub fn liquidate_spot_with_swap_begin(
asset_market_index: u16,
liability_market_index: u16,
swap_amount: u64,
user: &mut User,
user_key: &Pubkey,
user_stats: &mut UserStats,
Expand All @@ -1727,7 +1728,6 @@ pub fn liquidate_spot_with_swap_begin(
now: i64,
slot: u64,
state: &State,
swap_amount: u64,
) -> DriftResult {
let liquidation_margin_buffer_ratio = state.liquidation_margin_buffer_ratio;
let initial_pct_to_liquidate = state.initial_pct_to_liquidate as u128;
Expand Down Expand Up @@ -1971,7 +1971,7 @@ pub fn liquidate_spot_with_swap_begin(
liability_weight.safe_add(liquidation_margin_buffer_ratio)?;

// Determine what amount of borrow to transfer to reduce margin shortage to 0
// assume 0 fee and swap is executed at oracle price
// assume 0 liquidator fee and swap is executed at oracle price
let liability_transfer_to_cover_margin_shortage =
calculate_liability_transfer_to_cover_margin_shortage(
margin_shortage,
Expand Down Expand Up @@ -2035,12 +2035,6 @@ pub fn liquidate_spot_with_swap_begin(
return Err(ErrorCode::InvalidLiquidation);
}

validate!(
swap_amount > 0,
ErrorCode::InvalidLiquidation,
"swap_amount is 0"
)?;

validate!(
max_asset_transfer >= swap_amount.cast()?,
ErrorCode::InvalidLiquidation,
Expand Down Expand Up @@ -2102,7 +2096,7 @@ pub fn liquidate_spot_with_swap_end(
user: &mut User,
user_key: &Pubkey,
user_stats: &mut UserStats,
liquidator: &mut User,
_liquidator: &mut User,
liquidator_key: &Pubkey,
_liquidator_stats: &mut UserStats,
perp_market_map: &PerpMarketMap,
Expand Down
4 changes: 2 additions & 2 deletions programs/drift/src/controller/liquidation/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8520,6 +8520,7 @@ pub mod liquidate_spot_with_swap {
let res = liquidate_spot_with_swap_begin(
0,
1,
asset_transfer + (asset_transfer / 400) + 1,
&mut user,
&user_key,
&mut user_stats,
Expand All @@ -8532,14 +8533,14 @@ pub mod liquidate_spot_with_swap {
now,
slot,
&state,
asset_transfer + (asset_transfer / 400) + 1,
);

assert_eq!(res, Err(ErrorCode::InvalidLiquidation));

let res = liquidate_spot_with_swap_begin(
0,
1,
asset_transfer,
&mut user,
&user_key,
&mut user_stats,
Expand All @@ -8552,7 +8553,6 @@ pub mod liquidate_spot_with_swap {
now,
slot,
&state,
asset_transfer,
);

assert_eq!(res, Ok(()));
Expand Down
55 changes: 27 additions & 28 deletions programs/drift/src/instructions/keeper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1250,6 +1250,19 @@ pub fn handle_liquidate_spot_with_swap_begin<'c: 'info, 'info>(
let clock = Clock::get()?;
let now = clock.unix_timestamp;

let user_key = ctx.accounts.user.key();
let liquidator_key = ctx.accounts.liquidator.key();

validate!(
user_key != liquidator_key,
ErrorCode::UserCantLiquidateThemself
)?;

let user = &mut load_mut!(ctx.accounts.user)?;
let user_stats = &mut load_mut!(ctx.accounts.user_stats)?;
let liquidator = &mut load_mut!(ctx.accounts.liquidator)?;
let liquidator_stats = &mut load_mut!(ctx.accounts.liquidator_stats)?;

let remaining_accounts_iter = &mut ctx.remaining_accounts.iter().peekable();
let AccountMaps {
perp_market_map,
Expand All @@ -1266,19 +1279,6 @@ pub fn handle_liquidate_spot_with_swap_begin<'c: 'info, 'info>(
let _token_interface = get_token_interface(remaining_accounts_iter)?;
let mint = get_token_mint(remaining_accounts_iter)?;

let user_key = ctx.accounts.user.key();
let liquidator_key = ctx.accounts.liquidator.key();

validate!(
user_key != liquidator_key,
ErrorCode::UserCantLiquidateThemself
)?;

let user = &mut load_mut!(ctx.accounts.user)?;
let user_stats = &mut load_mut!(ctx.accounts.user_stats)?;
let liquidator = &mut load_mut!(ctx.accounts.liquidator)?;
let liquidator_stats = &mut load_mut!(ctx.accounts.liquidator_stats)?;

let mut asset_spot_market = spot_market_map.get_ref_mut(&asset_market_index)?;
validate!(
asset_spot_market.flash_loan_initial_token_amount == 0
Expand Down Expand Up @@ -1313,10 +1313,22 @@ pub fn handle_liquidate_spot_with_swap_begin<'c: 'info, 'info>(
drop(liability_spot_market);
drop(asset_spot_market);

// todo add validation here;
validate!(
asset_market_index != liability_market_index,
ErrorCode::InvalidSwap,
"asset and liability market the same"
)?;

validate!(
swap_amount != 0,
ErrorCode::InvalidSwap,
"swap_amount cannot be zero"
)?;

liquidate_spot_with_swap_begin(
asset_market_index,
liability_market_index,
swap_amount,
user,
&user_key,
user_stats,
Expand All @@ -1329,24 +1341,11 @@ pub fn handle_liquidate_spot_with_swap_begin<'c: 'info, 'info>(
now,
clock.slot,
state,
swap_amount,
)?;

let mut asset_spot_market = spot_market_map.get_ref_mut(&asset_market_index)?;
let mut liability_spot_market = spot_market_map.get_ref_mut(&liability_market_index)?;

validate!(
asset_market_index != liability_market_index,
ErrorCode::InvalidLiquidateSpotWithSwap,
"asset and liability market the same"
)?;

validate!(
swap_amount != 0,
ErrorCode::InvalidLiquidateSpotWithSwap,
"amount_in cannot be zero"
)?;

let asset_vault = &ctx.accounts.asset_spot_market_vault;
let asset_token_account = &ctx.accounts.asset_token_account;

Expand Down Expand Up @@ -1374,7 +1373,7 @@ pub fn handle_liquidate_spot_with_swap_begin<'c: 'info, 'info>(
validate!(
current_ix.program_id == *ctx.program_id,
ErrorCode::InvalidLiquidateSpotWithSwap,
"LiquidateSpotWithSwap must be a top-level instruction (cant be cpi)"
"LiquidateSpotWithSwapBegin must be a top-level instruction (cant be cpi)"
)?;

let mut index = current_index + 1;
Expand Down

0 comments on commit b303c7d

Please sign in to comment.