@@ -169,6 +169,9 @@ export class Scanner {
169169 ! this . config . updateReservePrices ,
170170 } ;
171171 accounts = await this . #getAllCreditAccounts( queue , blockNumber ) ;
172+ if ( accounts . length === 0 ) {
173+ accounts = await this . #getExpiredCreditAccounts( blockNumber ) ;
174+ }
172175 }
173176 if ( this . config . restakingWorkaround ) {
174177 const before = accounts . length ;
@@ -261,6 +264,64 @@ export class Scanner {
261264 return accounts ;
262265 }
263266
267+ async #getExpiredCreditAccounts(
268+ blockNumber ?: bigint ,
269+ ) : Promise < CreditAccountData [ ] > {
270+ const timestamp = this . caService . sdk . timestamp ;
271+ const expiredCMs = new AddressSet ( ) ;
272+
273+ for ( const m of this . caService . sdk . marketRegister . markets ) {
274+ // nothing borrowed === no accounts
275+ if ( m . pool . pool . totalBorrowed === 0n ) {
276+ continue ;
277+ }
278+ for ( const cm of m . creditManagers ) {
279+ const borrowed =
280+ m . pool . pool . creditManagerDebtParams . get ( cm . creditManager . address )
281+ ?. borrowed ?? 0n ;
282+ const expired =
283+ cm . creditFacade . expirable &&
284+ cm . creditFacade . expirationDate < timestamp ;
285+
286+ if ( expired && borrowed > 0n ) {
287+ expiredCMs . add ( cm . creditManager . address ) ;
288+ }
289+ }
290+ }
291+
292+ if ( expiredCMs . size === 0 ) {
293+ return [ ] ;
294+ }
295+ this . log . info ( `found ${ expiredCMs . size } expired credit managers` ) ;
296+
297+ let result : CreditAccountData [ ] = [ ] ;
298+ if ( this . config . optimistic ) {
299+ result = await this . caService . getCreditAccounts (
300+ {
301+ ignoreReservePrices : true ,
302+ minHealthFactor : 0n ,
303+ maxHealthFactor : MAX_UINT256 ,
304+ } ,
305+ blockNumber ,
306+ ) ;
307+ result = result . filter ( ca => expiredCMs . has ( ca . creditManager ) ) ;
308+ } else {
309+ // we can take first expired credit manager, and continue with next one on next block
310+ result = await this . caService . getCreditAccounts (
311+ {
312+ creditManager : expiredCMs . asArray ( ) [ 0 ] ,
313+ ignoreReservePrices : true ,
314+ minHealthFactor : 0n ,
315+ maxHealthFactor : MAX_UINT256 ,
316+ } ,
317+ blockNumber ,
318+ ) ;
319+ }
320+
321+ this . log . debug ( `found ${ result . length } expired credit accounts` ) ;
322+ return result ;
323+ }
324+
264325 async #setupRestakingWorkaround( ) : Promise < void > {
265326 this . #restakingCMAddr = RESTAKING_CMS [ this . config . network ] ;
266327
0 commit comments