@@ -7,21 +7,24 @@ import type {
77import {
88 AddressSet ,
99 hexEq ,
10+ isVersionRange ,
1011 MAX_UINT256 ,
1112 PERCENTAGE_FACTOR ,
13+ VERSION_RANGE_310 ,
1214 WAD ,
1315} from "@gearbox-protocol/sdk" ;
1416import { iBotListV310Abi } from "@gearbox-protocol/sdk/abi/310/generated" ;
1517import { iCreditManagerV300Abi } from "@gearbox-protocol/sdk/abi/v300" ;
16- import { iPartialLiquidationBotV310Abi } from "@gearbox-protocol/sdk/plugins/bots" ;
1718import type { Address , Block } from "viem" ;
1819import { getContract } from "viem" ;
1920import type { Config } from "../config/index.js" ;
2021import { DI } from "../di.js" ;
2122import { type ILogger , Logger } from "../log/index.js" ;
23+ import { DELEVERAGE_PERMISSIONS } from "../utils/permissions.js" ;
2224import type Client from "./Client.js" ;
2325import type { ILiquidatorService } from "./liquidate/index.js" ;
2426import { type INotifier , ZeroHFAccountsMessage } from "./notifier/index.js" ;
27+ import { PartialLiquidationBotV310Contract } from "./PartialLiquidationBotV310Contract.js" ;
2528
2629const RESTAKING_CMS : Partial < Record < NetworkType , Address > > = {
2730 Mainnet :
@@ -80,20 +83,18 @@ export class Scanner {
8083 this . #maxHealthFactor = MAX_UINT256 ;
8184 }
8285 if ( this . config . liquidationMode === "deleverage" ) {
86+ const [ botMinHealthFactor ] = await PartialLiquidationBotV310Contract . get (
87+ this . caService . sdk ,
88+ this . config . partialLiquidationBot ,
89+ ) . loadHealthFactors ( ) ;
8390 if ( this . config . optimistic ) {
8491 this . #minHealthFactor = 0n ;
8592 this . #maxHealthFactor = MAX_UINT256 ;
8693 } else {
8794 this . #minHealthFactor = WAD ;
88- const botMinHealthFactor = await this . client . pub . readContract ( {
89- address : this . config . partialLiquidationBot ,
90- abi : iPartialLiquidationBotV310Abi ,
91- functionName : "minHealthFactor" ,
92- } ) ;
93- this . #maxHealthFactor =
94- ( BigInt ( botMinHealthFactor ) * WAD ) / PERCENTAGE_FACTOR ;
95+ this . #maxHealthFactor = ( botMinHealthFactor * WAD ) / PERCENTAGE_FACTOR ;
9596 this . log . info (
96- `deleverage bot max health factor is ${ botMinHealthFactor / 100 } % (${ this . #maxHealthFactor} )` ,
97+ `deleverage bot max health factor is ${ botMinHealthFactor / 100n } % (${ this . #maxHealthFactor} )` ,
9798 ) ;
9899 }
99100 }
@@ -411,27 +412,33 @@ export class Scanner {
411412 }
412413
413414 async #filterDeleverageAccounts(
414- accounts : CreditAccountData [ ] ,
415+ accounts_ : CreditAccountData [ ] ,
415416 partialLiquidationBot : Address ,
416417 blockNumber ?: bigint ,
417418 ) : Promise < CreditAccountData [ ] > {
418- const botList = this . caService . sdk . botListContract ?. address ;
419- if ( ! botList ) {
420- this . log . warn (
421- "bot list contract not found, skipping deleverage accounts filtering" ,
422- ) ;
423- return accounts ;
419+ if ( this . config . optimistic ) {
420+ return accounts_ ;
424421 }
422+
423+ const accounts = accounts_ . filter ( ca => {
424+ const cm = this . caService . sdk . marketRegister . findCreditManager (
425+ ca . creditManager ,
426+ ) ;
427+ return isVersionRange ( cm . creditFacade . version , VERSION_RANGE_310 ) ;
428+ } ) ;
429+
425430 const res = await this . client . pub . multicall ( {
426- contracts : accounts . map (
427- ca =>
428- ( {
429- address : botList ,
430- abi : iBotListV310Abi ,
431- functionName : "getBotStatus" ,
432- args : [ partialLiquidationBot , ca . creditAccount ] ,
433- } ) as const ,
434- ) ,
431+ contracts : accounts . map ( ca => {
432+ const cm = this . caService . sdk . marketRegister . findCreditManager (
433+ ca . creditManager ,
434+ ) ;
435+ return {
436+ address : cm . creditFacade . botList ,
437+ abi : iBotListV310Abi ,
438+ functionName : "getBotStatus" ,
439+ args : [ partialLiquidationBot , ca . creditAccount ] ,
440+ } as const ;
441+ } ) ,
435442 allowFailure : true ,
436443 blockNumber,
437444 } ) ;
@@ -442,15 +449,19 @@ export class Scanner {
442449 const r = res [ i ] ;
443450 if ( r . status === "success" ) {
444451 const [ permissions , forbidden ] = r . result ;
445- if ( ! ! permissions && ! forbidden ) {
452+ if (
453+ ! ! permissions &&
454+ ! forbidden &&
455+ ( permissions & DELEVERAGE_PERMISSIONS ) === DELEVERAGE_PERMISSIONS
456+ ) {
446457 result . push ( ca ) ;
447458 }
448459 } else if ( r . status === "failure" ) {
449460 errored ++ ;
450461 }
451462 }
452463 this . log . debug (
453- { errored, before : accounts . length , after : result . length , botList } ,
464+ { errored, before : accounts_ . length , after : result . length } ,
454465 "filtered accounts for deleverage" ,
455466 ) ;
456467 return result ;
@@ -474,6 +485,14 @@ export class Scanner {
474485 return this . #liquidatableAccounts;
475486 }
476487
488+ public get minHealthFactor ( ) : bigint {
489+ return this . #minHealthFactor;
490+ }
491+
492+ public get maxHealthFactor ( ) : bigint {
493+ return this . #maxHealthFactor;
494+ }
495+
477496 public async stop ( ) : Promise < void > {
478497 this . #unwatch?.( ) ;
479498 this . log . info ( "stopped" ) ;
0 commit comments