@@ -64,40 +64,42 @@ export class AccountOpener {
6464 /**
6565 * Tries to open account with underlying only in each CM
6666 */
67- public async openCreditAccounts ( targets : TargetAccount [ ] ) : Promise < void > {
68- const borrower = await this . #prepareBorrower( targets ) ;
67+ public async openCreditAccounts (
68+ targets : TargetAccount [ ] ,
69+ ) : Promise < CreditAccountData [ ] > {
70+ await this . #prepareBorrower( targets ) ;
6971
7072 for ( const c of targets ) {
7173 const cm = this . sdk . marketRegister . findCreditManager ( c . creditManager ) ;
7274 await this . #approve( c . collateral , cm ) ;
7375 }
7476
75- for ( const target of targets ) {
77+ for ( let i = 0 ; i < targets . length ; i ++ ) {
78+ const target = targets [ i ] ;
7679 try {
77- await this . #openAccount( target ) ;
80+ await this . #openAccount( target , i + 1 , targets . length ) ;
7881 } catch ( e ) {
7982 this . #logger?. error ( e ) ;
8083 }
8184 }
82- const accounts = await this . #service. getCreditAccounts ( {
83- owner : borrower . address ,
84- } ) ;
85+ const accounts = await this . getOpenedAccounts ( ) ;
8586 this . #logger?. info ( `opened ${ accounts . length } accounts` ) ;
87+ return accounts ;
8688 }
8789
88- async #openAccount( {
89- creditManager,
90- collateral,
91- leverage = 4 ,
92- slippage = 50 ,
93- } : TargetAccount ) : Promise < CreditAccountData [ ] > {
90+ async #openAccount(
91+ { creditManager, collateral, leverage = 4 , slippage = 50 } : TargetAccount ,
92+ index : number ,
93+ total : number ,
94+ ) : Promise < void > {
9495 const borrower = await this . #getBorrower( ) ;
9596 const cm = this . sdk . marketRegister . findCreditManager ( creditManager ) ;
9697 const symbol = this . sdk . tokensMeta . symbol ( collateral ) ;
9798 const logger = this . #logger?. child ?.( {
9899 creditManager : cm . name ,
99100 collateral : symbol ,
100101 } ) ;
102+ logger ?. debug ( `opening account #${ index } /${ total } ` ) ;
101103 const { minDebt, underlying } = cm . creditFacade ;
102104
103105 const expectedBalances : Asset [ ] = [ ] ;
@@ -123,28 +125,26 @@ export class AccountOpener {
123125 } ) ;
124126 logger ?. debug ( strategy , "found open strategy" ) ;
125127 const debt = minDebt * BigInt ( leverage - 1 ) ;
128+ const collateralLT = BigInt ( cm . collateralTokens [ collateral ] ) ;
129+ const inUnderlying = collateral . toLowerCase ( ) === underlying . toLowerCase ( ) ;
126130 const { tx, calls } = await this . #service. openCA ( {
127131 creditManager : cm . creditManager . address ,
128- averageQuota : [
129- {
130- token : collateral ,
131- balance : this . #calcQuota(
132- strategy . amount ,
133- debt ,
134- BigInt ( cm . collateralTokens [ collateral ] ) ,
135- ) ,
136- } ,
137- ] ,
138- minQuota : [
139- {
140- token : collateral ,
141- balance : this . #calcQuota(
142- strategy . minAmount ,
143- debt ,
144- BigInt ( cm . collateralTokens [ collateral ] ) ,
145- ) ,
146- } ,
147- ] ,
132+ averageQuota : inUnderlying
133+ ? [ ]
134+ : [
135+ {
136+ token : collateral ,
137+ balance : this . #calcQuota( strategy . amount , debt , collateralLT ) ,
138+ } ,
139+ ] ,
140+ minQuota : inUnderlying
141+ ? [ ]
142+ : [
143+ {
144+ token : collateral ,
145+ balance : this . #calcQuota( strategy . minAmount , debt , collateralLT ) ,
146+ } ,
147+ ] ,
148148 collateral : [ { token : underlying , balance : minDebt } ] ,
149149 debt,
150150 calls : strategy . calls ,
@@ -169,10 +169,7 @@ export class AccountOpener {
169169 if ( receipt . status === "reverted" ) {
170170 throw new Error ( `open credit account tx ${ hash } reverted` ) ;
171171 }
172- logger ?. debug (
173- `opened credit account in ${ cm . name } with collateral ${ symbol } ` ,
174- ) ;
175- return this . getOpenedAccounts ( ) ;
172+ logger ?. debug ( `opened credit account ${ index } /${ total } ` ) ;
176173 }
177174
178175 public async getOpenedAccounts ( ) : Promise < CreditAccountData [ ] > {
0 commit comments