@@ -49,7 +49,10 @@ export abstract class AbstractPartialLiquidatorContract
4949 #registeredCMs = new AddressMap < boolean > ( ) ;
5050 #address?: Address ;
5151 #router: Address ;
52- #creditManagers: CreditSuite [ ] = [ ] ;
52+ /**
53+ * Credit managers for which async write operations (register, obtaining degen, etc...) are pending
54+ */
55+ #pendingCreditManagers: CreditSuite [ ] = [ ] ;
5356 #optimalDeleverageHF = new AddressMap < bigint > ( ) ;
5457 /**
5558 * Mapping of internal creditManagers to creditAccounts
@@ -73,8 +76,16 @@ export abstract class AbstractPartialLiquidatorContract
7376 this . logger = DI . create ( DI . Logger , this . name . replaceAll ( " " , "" ) ) ;
7477 }
7578
79+ public addCreditManager ( cm : CreditSuite ) : void {
80+ this . logger . debug (
81+ `adding credit manager ${ cm . creditManager . name } (${ cm . creditManager . address } )` ,
82+ ) ;
83+ this . #pendingCreditManagers. push ( cm ) ;
84+ }
85+
7686 /**
7787 * Registers credit manager addresses in liquidator contract if necessary
88+ * Can be called multiple times, each time processes pending credit managers
7889 */
7990 public async configure ( ) : Promise < void > {
8091 this . #creditAccounts = await this . #getLiquidatorAccounts( ) ;
@@ -85,7 +96,7 @@ export abstract class AbstractPartialLiquidatorContract
8596 this . logger . warn ( `failed to obtain degen NFTs: ${ e } ` ) ;
8697 }
8798
88- for ( const cm of this . #creditManagers ) {
99+ for ( const cm of this . #pendingCreditManagers ) {
89100 const { address, name } = cm . creditManager ;
90101 const ca = this . #creditAccounts. get ( address ) ;
91102 if ( ca === ADDRESS_0X0 ) {
@@ -101,9 +112,14 @@ export abstract class AbstractPartialLiquidatorContract
101112 if ( this . config . liquidationMode === "deleverage" ) {
102113 await this . #setOptimalDeleverageHF( ) ;
103114 }
115+
116+ this . logger . debug (
117+ `configured ${ this . #pendingCreditManagers. length } credit managers` ,
118+ ) ;
119+ this . #pendingCreditManagers = [ ] ;
104120 }
105121
106- protected async updateRouterAddress ( router : Address ) : Promise < void > {
122+ protected async configureRouterAddress ( router : Address ) : Promise < void > {
107123 const receipt = await this . client . simulateAndWrite ( {
108124 abi : parseAbi ( [ "function setRouter(address newRouter)" ] ) ,
109125 address : this . address ,
@@ -122,21 +138,14 @@ export abstract class AbstractPartialLiquidatorContract
122138
123139 public abstract deploy ( ) : Promise < void > ;
124140
125- public addCreditManager ( cm : CreditSuite ) : void {
126- this . logger . debug (
127- `adding credit manager ${ cm . creditManager . name } (${ cm . creditManager . address } )` ,
128- ) ;
129- this . #creditManagers. push ( cm ) ;
130- }
131-
132141 /**
133142 * Returns mapping [Credit Manager Address] => [Address of Partialidator's CA in this CM]
134143 * @returns
135144 */
136145 async #getLiquidatorAccounts( ) : Promise < AddressMap < Address > > {
137146 const results = await this . client . pub . multicall ( {
138147 allowFailure : false ,
139- contracts : this . #creditManagers . map ( cm => ( {
148+ contracts : this . #pendingCreditManagers . map ( cm => ( {
140149 abi : parseAbi ( [
141150 "function cmToCA(address creditManager) view returns (address creditAccount)" ,
142151 ] ) ,
@@ -147,7 +156,7 @@ export abstract class AbstractPartialLiquidatorContract
147156 } ) ;
148157 this . logger . debug ( `loaded ${ results . length } liquidator credit accounts` ) ;
149158 return new AddressMap (
150- this . #creditManagers . map ( ( cm , i ) => [
159+ this . #pendingCreditManagers . map ( ( cm , i ) => [
151160 cm . creditManager . address ,
152161 results [ i ] ,
153162 ] ) ,
@@ -161,7 +170,7 @@ export abstract class AbstractPartialLiquidatorContract
161170 async #claimDegenNFTs( ) : Promise < void > {
162171 const account = this . address ;
163172 let nfts = 0 ;
164- for ( const cm of this . #creditManagers ) {
173+ for ( const cm of this . #pendingCreditManagers ) {
165174 const { address, name } = cm . creditManager ;
166175 const { degenNFT } = cm . creditFacade ;
167176 const account = this . #creditAccounts. get ( address ) ;
0 commit comments