@@ -19,7 +19,7 @@ use templar_common::{
1919 market:: { error:: RetrievalError , DepositMsg , LiquidateMsg , MarketConfiguration } ,
2020 oracle:: pyth:: { OracleResponse , PriceIdentifier } ,
2121} ;
22- use tracing:: { error, info, instrument} ;
22+ use tracing:: { error, info, instrument, warn } ;
2323
2424use crate :: {
2525 near:: { get_access_key_data, send_tx, serialize_and_encode, view, RpcError } ,
@@ -217,10 +217,34 @@ impl<S: Swap> Liquidator<S> {
217217
218218 let collateral_asset = configuration. collateral_asset . contract_id ( ) ;
219219
220- let ( swap_amount , liquidation_amount) = self
220+ let liquidation_amount = self
221221 . liquidation_amount ( & position, & oracle_response, configuration)
222222 . await ?;
223223
224+ let swap_output_amount = if self . asset . as_ref ( ) == & borrow_asset {
225+ let asset_balance = self . get_asset_balance ( self . asset . as_ref ( ) . clone ( ) ) . await ?;
226+ if asset_balance >= liquidation_amount {
227+ 0 . into ( )
228+ } else {
229+ ( liquidation_amount. 0 - asset_balance. 0 ) . into ( )
230+ }
231+ } else {
232+ liquidation_amount
233+ } ;
234+
235+ let swap_amount = self
236+ . swap
237+ . quote ( & self . asset , & borrow_asset, swap_output_amount)
238+ . await
239+ . map_err ( LiquidatorError :: QuoteError ) ?;
240+
241+ let available = self . get_asset_balance ( self . asset . as_ref ( ) . clone ( ) ) . await ?;
242+
243+ if available < swap_amount {
244+ warn ! ( "Insufficient asset balance for liquidation: {available:?} < {swap_amount:?}" ) ;
245+ return Ok ( ( ) ) ;
246+ }
247+
224248 // Implement this function based on your liquidation strategy
225249 if !self
226250 . should_liquidate ( swap_amount, liquidation_amount)
@@ -230,14 +254,7 @@ impl<S: Swap> Liquidator<S> {
230254 return Ok ( ( ) ) ;
231255 }
232256
233- let should_swap = if self . asset . as_ref ( ) == & borrow_asset {
234- let asset_balance = self . get_asset_balance ( self . asset . as_ref ( ) . clone ( ) ) . await ?;
235- asset_balance >= liquidation_amount
236- } else {
237- true
238- } ;
239-
240- if should_swap {
257+ if swap_amount > 0 . into ( ) {
241258 match self
242259 . swap
243260 . swap ( & self . asset , & borrow_asset, swap_amount)
@@ -298,8 +315,7 @@ impl<S: Swap> Liquidator<S> {
298315 position : & BorrowPosition ,
299316 oracle_response : & OracleResponse ,
300317 configuration : MarketConfiguration ,
301- ) -> LiquidatorResult < ( U128 , U128 ) > {
302- let borrow_asset = & configuration. borrow_asset ;
318+ ) -> LiquidatorResult < U128 > {
303319 let price_pair = configuration
304320 . price_oracle_configuration
305321 . create_price_pair ( oracle_response) ?;
@@ -310,21 +326,7 @@ impl<S: Swap> Liquidator<S> {
310326 "Failed to calculate minimum acceptable liquidation amount" . to_owned ( ) ,
311327 )
312328 } ) ?;
313- // Here we would check a quote for the swap to ensure desired profit margin is met
314- let quote_to_liquidate = self
315- . swap
316- . quote (
317- & self . asset ,
318- & borrow_asset. clone ( ) . into_nep141 ( ) . ok_or_else ( || {
319- LiquidatorError :: StandardSupportError (
320- "Only NEP-141 borrow assets supported" . to_owned ( ) ,
321- )
322- } ) ?,
323- min_liquidation_amount. into ( ) ,
324- )
325- . await
326- . map_err ( LiquidatorError :: QuoteError ) ?;
327- Ok ( ( quote_to_liquidate, min_liquidation_amount. into ( ) ) )
329+ Ok ( min_liquidation_amount. into ( ) )
328330 }
329331
330332 #[ instrument( skip( self ) , level = "debug" ) ]
0 commit comments