@@ -207,69 +207,75 @@ export default class SingularLiquidator
207207 const start = Date . now ( ) ;
208208 let strategyResult : OptimisticStrategyResult | undefined ;
209209
210- const balanceBefore = await this . getExecutorBalance ( acc . underlying ) ;
211-
212- // make liquidatable using first strategy
213- const ml = await this . #strategies[ 0 ] . makeLiquidatable ( acc ) ;
214- result . partialLiquidationCondition = ml . partialLiquidationCondition ;
210+ try {
211+ const balanceBefore = await this . getExecutorBalance ( acc . underlying ) ;
215212
216- for ( const s of this . #strategies) {
217- strategyResult = await this . #liquidateOneOptimisticStrategy(
218- ml . account ,
219- s ,
220- ml . snapshotId ,
221- ) ;
222- if ( strategyResult . success ) {
223- break ;
224- }
225- }
213+ // make liquidatable using first strategy
214+ const ml = await this . #strategies[ 0 ] . makeLiquidatable ( acc ) ;
215+ result . partialLiquidationCondition = ml . partialLiquidationCondition ;
226216
227- if ( strategyResult ) {
228- result . assetOut = strategyResult . preview ?. assetOut ;
229- result . amountOut = strategyResult . preview ?. amountOut ;
230- result . flashLoanAmount = strategyResult . preview ?. flashLoanAmount ;
231- result . calls = strategyResult . preview ?. calls as MultiCall [ ] ;
232- result . pathAmount = strategyResult . preview ?. underlyingBalance ?? 0n ;
233- result . callsHuman = this . creditAccountService . sdk . parseMultiCall ( [
234- ...( strategyResult . preview ?. calls ?? [ ] ) ,
235- ] ) ;
236- const ca = await this . creditAccountService . getCreditAccountData (
237- acc . creditAccount ,
238- ) ;
239- if ( ! ca ) {
240- throw new Error ( `account ${ acc . creditAccount } not found` ) ;
217+ for ( const s of this . #strategies) {
218+ strategyResult = await this . #liquidateOneOptimisticStrategy(
219+ ml . account ,
220+ s ,
221+ ml . snapshotId ,
222+ ) ;
223+ if ( strategyResult . success ) {
224+ break ;
225+ }
241226 }
242- result . balancesAfter = filterDustUSD ( { account : ca , sdk : this . sdk } ) ;
243- result . hfAfter = ca . healthFactor ;
244- result . gasUsed = strategyResult . receipt ?. gasUsed ?? 0n ;
245- result . isError = ! strategyResult . success ;
246-
247- await this . swapper . swap (
248- acc . underlying ,
249- balanceBefore . underlying + result . liquidatorPremium ,
250- ) ;
251- const balanceAfter = await this . getExecutorBalance ( acc . underlying ) ;
252- result . liquidatorPremium =
253- balanceAfter . underlying - balanceBefore . underlying ;
254- result . liquidatorProfit = balanceAfter . eth - balanceBefore . eth ;
255227
256- if ( strategyResult ?. success === false ) {
257- const decoded = await this . errorHandler . explain (
258- strategyResult . error ,
259- acc ,
260- true ,
228+ if ( strategyResult ) {
229+ result . assetOut = strategyResult . preview ?. assetOut ;
230+ result . amountOut = strategyResult . preview ?. amountOut ;
231+ result . flashLoanAmount = strategyResult . preview ?. flashLoanAmount ;
232+ result . calls = strategyResult . preview ?. calls as MultiCall [ ] ;
233+ result . pathAmount = strategyResult . preview ?. underlyingBalance ?? 0n ;
234+ result . callsHuman = this . creditAccountService . sdk . parseMultiCall ( [
235+ ...( strategyResult . preview ?. calls ?? [ ] ) ,
236+ ] ) ;
237+ const ca = await this . creditAccountService . getCreditAccountData (
238+ acc . creditAccount ,
261239 ) ;
262- result . traceFile = decoded . traceFile ;
263- result . error = `cannot liquidate: ${ decoded . longMessage } ` . replaceAll (
264- "\n" ,
265- "\\n" ,
240+ if ( ! ca ) {
241+ throw new Error ( `account ${ acc . creditAccount } not found` ) ;
242+ }
243+ result . balancesAfter = filterDustUSD ( { account : ca , sdk : this . sdk } ) ;
244+ result . hfAfter = ca . healthFactor ;
245+ result . gasUsed = strategyResult . receipt ?. gasUsed ?? 0n ;
246+ result . isError = ! strategyResult . success ;
247+
248+ await this . swapper . swap (
249+ acc . underlying ,
250+ balanceBefore . underlying + result . liquidatorPremium ,
266251 ) ;
267- this . logger . error ( `cannot liquidate: ${ decoded . shortMessage } ` ) ;
252+ const balanceAfter = await this . getExecutorBalance ( acc . underlying ) ;
253+ result . liquidatorPremium =
254+ balanceAfter . underlying - balanceBefore . underlying ;
255+ result . liquidatorProfit = balanceAfter . eth - balanceBefore . eth ;
256+
257+ if ( strategyResult ?. success === false ) {
258+ const decoded = await this . errorHandler . explain (
259+ strategyResult . error ,
260+ acc ,
261+ true ,
262+ ) ;
263+ result . traceFile = decoded . traceFile ;
264+ result . error = `cannot liquidate: ${ decoded . longMessage } ` . replaceAll (
265+ "\n" ,
266+ "\\n" ,
267+ ) ;
268+ this . logger . error ( `cannot liquidate: ${ decoded . shortMessage } ` ) ;
269+ }
270+ } else {
271+ result . isError = true ;
272+ result . error = "no applicable strategy found" ;
273+ this . logger . error ( "no applicable strategy found" ) ;
268274 }
269- } else {
275+ } catch ( e ) {
270276 result . isError = true ;
271- result . error = "no applicable strategy found" ;
272- this . logger . error ( "no applicable strategy found ") ;
277+ result . error = ` ${ e } ` ;
278+ this . logger . error ( e , "cannot liquidate ") ;
273279 }
274280
275281 result . duration = Date . now ( ) - start ;
0 commit comments