@@ -306,24 +306,43 @@ export default class BatchLiquidator
306306 }
307307
308308 #sliceBatches( accounts : CreditAccountData [ ] ) : CreditAccountData [ ] [ ] {
309- // sort by healthFactor bin ASC, debt DESC
310- const sortedAccounts = accounts . sort ( ( a , b ) => {
311- if ( a . healthFactor !== b . healthFactor ) {
312- return healthFactorBin ( a ) - healthFactorBin ( b ) ;
313- }
314- if ( b . totalDebtUSD > a . totalDebtUSD ) {
315- return 1 ;
316- } else if ( b . totalDebtUSD === a . totalDebtUSD ) {
317- return 0 ;
318- } else {
319- return - 1 ;
309+ // Group accounts by oracle - so that price updates contain only tokens known to the oracle
310+ const accountsByOracle = new Map < Address , CreditAccountData [ ] > ( ) ;
311+ for ( const ca of accounts ) {
312+ const market = this . sdk . marketRegister . findByCreditManager (
313+ ca . creditManager ,
314+ ) ;
315+ const oracle = market . priceOracle . address ;
316+ if ( accountsByOracle . has ( oracle ) ) {
317+ accountsByOracle . set ( oracle , [ ] ) ;
320318 }
321- } ) ;
319+ accountsByOracle . get ( oracle ) ! . push ( ca ) ;
320+ }
322321
322+ // Sort accounts within each oracle by healthFactor bin ASC, debt DESC
323+ for ( const oracleAccounts of accountsByOracle . values ( ) ) {
324+ oracleAccounts . sort ( ( a , b ) => {
325+ if ( a . healthFactor !== b . healthFactor ) {
326+ return healthFactorBin ( a ) - healthFactorBin ( b ) ;
327+ }
328+ if ( b . totalDebtUSD > a . totalDebtUSD ) {
329+ return 1 ;
330+ } else if ( b . totalDebtUSD === a . totalDebtUSD ) {
331+ return 0 ;
332+ } else {
333+ return - 1 ;
334+ }
335+ } ) ;
336+ }
337+
338+ // Create batches from each oracle's accounts
323339 const batches : CreditAccountData [ ] [ ] = [ ] ;
324- for ( let i = 0 ; i < sortedAccounts . length ; i += this . config . batchSize ) {
325- batches . push ( sortedAccounts . slice ( i , i + this . config . batchSize ) ) ;
340+ for ( const oracleAccounts of accountsByOracle . values ( ) ) {
341+ for ( let i = 0 ; i < oracleAccounts . length ; i += this . config . batchSize ) {
342+ batches . push ( oracleAccounts . slice ( i , i + this . config . batchSize ) ) ;
343+ }
326344 }
345+
327346 return batches ;
328347 }
329348
0 commit comments