From b303c7dcd4cb30d060bac73108af76b84d676bd5 Mon Sep 17 00:00:00 2001 From: Chris Heaney Date: Thu, 2 Jan 2025 14:39:05 -0500 Subject: [PATCH] tweaks reviewing changes --- programs/drift/src/controller/liquidation.rs | 12 +--- .../drift/src/controller/liquidation/tests.rs | 4 +- programs/drift/src/instructions/keeper.rs | 55 +++++++++---------- 3 files changed, 32 insertions(+), 39 deletions(-) diff --git a/programs/drift/src/controller/liquidation.rs b/programs/drift/src/controller/liquidation.rs index 859e0ffd7..d83357c21 100644 --- a/programs/drift/src/controller/liquidation.rs +++ b/programs/drift/src/controller/liquidation.rs @@ -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, @@ -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; @@ -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, @@ -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, @@ -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, diff --git a/programs/drift/src/controller/liquidation/tests.rs b/programs/drift/src/controller/liquidation/tests.rs index 26964a5f9..c6c6c02f1 100644 --- a/programs/drift/src/controller/liquidation/tests.rs +++ b/programs/drift/src/controller/liquidation/tests.rs @@ -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, @@ -8532,7 +8533,6 @@ pub mod liquidate_spot_with_swap { now, slot, &state, - asset_transfer + (asset_transfer / 400) + 1, ); assert_eq!(res, Err(ErrorCode::InvalidLiquidation)); @@ -8540,6 +8540,7 @@ pub mod liquidate_spot_with_swap { let res = liquidate_spot_with_swap_begin( 0, 1, + asset_transfer, &mut user, &user_key, &mut user_stats, @@ -8552,7 +8553,6 @@ pub mod liquidate_spot_with_swap { now, slot, &state, - asset_transfer, ); assert_eq!(res, Ok(())); diff --git a/programs/drift/src/instructions/keeper.rs b/programs/drift/src/instructions/keeper.rs index 3d009559b..29b5f6836 100644 --- a/programs/drift/src/instructions/keeper.rs +++ b/programs/drift/src/instructions/keeper.rs @@ -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, @@ -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 @@ -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, @@ -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; @@ -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;