11import  {  Injectable  }  from  '@angular/core' ; 
22import  {  firstValueFrom  }  from  'rxjs' ; 
3- import  {  EnclaveService  }  from  'src/app/services/enclave/index ' ; 
3+ import  {  EnclaveService  }  from  'src/app/services/enclave' ; 
44import  {  XrplService  }  from  'src/app/services/xrpl.service' ; 
55import  { 
66  checkTxResponseSucceeded , 
@@ -16,7 +16,7 @@ import { parseNumber } from 'src/app/utils/validators';
1616import  {  ifDefined  }  from  'src/helpers/helpers' ; 
1717import  {  TransactionSigned ,  TransactionToSign  }  from  'src/schema/actions' ; 
1818import  *  as  xrpl  from  'xrpl' ; 
19- import  {  IssuedCurrencyAmount  }  from  'xrpl/dist/npm/models/common/index ' ; 
19+ import  {  IssuedCurrencyAmount  }  from  'xrpl/dist/npm/models/common' ; 
2020import  {  Trustline  }  from  'xrpl/dist/npm/models/methods/accountLines' ; 
2121import  {  ConnectorQuery  }  from  './connector' ; 
2222import  {  SessionQuery  }  from  './session.query' ; 
@@ -206,7 +206,8 @@ export class SessionXrplService {
206206   * @see  XrplService.createUnsignedTrustSetTx 
207207   */ 
208208  async  sendTrustSetTx ( 
209-     limitAmount : IssuedCurrencyAmount 
209+     limitAmount : IssuedCurrencyAmount , 
210+     flags ?: number  |  xrpl . TrustSetFlagsInterface 
210211  ) : Promise < xrpl . TxResponse >  { 
211212    const  {  wallet }  =  this . sessionQuery . assumeActiveSession ( ) ; 
212213
@@ -215,7 +216,8 @@ export class SessionXrplService {
215216      async  ( )  => 
216217        await  this . xrplService . createUnsignedTrustSetTx ( 
217218          wallet . xrpl_account . address_base58 , 
218-           limitAmount 
219+           limitAmount , 
220+           flags 
219221        ) , 
220222      {  from : wallet . xrpl_account . address_base58 ,  limitAmount } 
221223    ) ; 
@@ -253,11 +255,11 @@ export class SessionXrplService {
253255      ( await  firstValueFrom ( this . sessionQuery . xrplTrustlines ) )  ??  [ ] ; 
254256
255257    const  txResponses : xrpl . TxResponse [ ]  =  [ ] ; 
256-     for  ( const  trustLine  of  trustLines )  { 
257-       ifDefined ( await  this . checkTrustlineOptIn ( trustLine ) ,  ( txResponse )  => 
258-         txResponses . push ( txResponse ) 
259-       ) ; 
260-     } 
258+     //  for (const trustLine of trustLines) {
259+     //    ifDefined(await this.checkTrustlineOptIn(trustLine), (txResponse) =>
260+     //      txResponses.push(txResponse)
261+     //    );
262+     //  }
261263    return  txResponses ; 
262264  } 
263265
@@ -318,6 +320,115 @@ export class SessionXrplService {
318320    } 
319321  } 
320322
323+   /** 
324+    * Helper: Create a trust line between active session's wallet and targeted account. 
325+    * 
326+    * This creates and sends a `TrustSet` transaction. 
327+    * 
328+    * @return  the `TrustSet` response, or undefined 
329+    */ 
330+   async  createTrustline ( 
331+     currency : string , 
332+     issuer : string , 
333+     value : string , 
334+     rippling : boolean 
335+   ) : Promise < xrpl . TxResponse  |  undefined >  { 
336+     const  limitAmount  =  { 
337+       currency, 
338+       issuer, 
339+       value, 
340+     } ; 
341+ 
342+     if  ( rippling )  { 
343+       const  enableRippling : xrpl . TrustSetFlagsInterface  =  { 
344+         tfClearNoRipple : true , 
345+       } ; 
346+       return  await  withLoggedExchange ( 
347+         'SessionXrplService.createTrustline: sending TrustSet' , 
348+         async  ( )  =>  await  this . sendTrustSetTx ( limitAmount ,  enableRippling ) , 
349+         limitAmount 
350+       ) ; 
351+     } 
352+ 
353+     return  await  withLoggedExchange ( 
354+       'SessionXrplService.createTrustline: sending TrustSet' , 
355+       async  ( )  =>  await  this . sendTrustSetTx ( limitAmount ) , 
356+       limitAmount 
357+     ) ; 
358+   } 
359+ 
360+   /** 
361+    * Default trustline opt-in for each of this account's trust lines. 
362+    * 
363+    * @return  The responses to `TrustSet` transactions sent out (empty if none sent) 
364+    * @see  defaultTrustlineOptIn 
365+    */ 
366+   async  defaultTrustlineOptIns ( ) : Promise < xrpl . TxResponse [ ] >  { 
367+     // TODO(Pi): Check for necessary owner reserves before sending. 
368+     //           See: https://xrpl.org/reserves.html 
369+ 
370+     const  trustLines  = 
371+       ( await  firstValueFrom ( this . sessionQuery . xrplTrustlines ) )  ??  [ ] ; 
372+ 
373+     const  txResponses : xrpl . TxResponse [ ]  =  [ ] ; 
374+     for  ( const  trustLine  of  trustLines )  { 
375+       ifDefined ( await  this . defaultTrustlineOptIn ( trustLine ) ,  ( txResponse )  => 
376+         txResponses . push ( txResponse ) 
377+       ) ; 
378+     } 
379+     return  txResponses ; 
380+   } 
381+ 
382+   /** 
383+    * Helper: Default trustline opt-in for the given trust-line. 
384+    * 
385+    * This sends a `TrustSet` transaction defaulting the limit to zero. 
386+    * 
387+    * @return  the `TrustSet` response, or undefined 
388+    */ 
389+   async  defaultTrustlineOptIn ( 
390+     trustline : Trustline 
391+   ) : Promise < xrpl . TxResponse  |  undefined >  { 
392+     const  limit_peer  =  parseNumber ( trustline . limit_peer ) ; 
393+     if  ( limit_peer  ===  undefined )  { 
394+       throw  panic ( 
395+         'SessionXrplService.defaultTrustlineOptIn: bad limit_peer:' , 
396+         trustline 
397+       ) ; 
398+     } 
399+     if  ( limit_peer  !==  0 )  { 
400+       throw  panic ( 
401+         'SessionXrplService.defaultTrustlineOptIn: limit_peer is not zero:' , 
402+         trustline 
403+       ) ; 
404+     } 
405+ 
406+     const  limit  =  parseNumber ( trustline . limit ) ; 
407+     if  ( limit  ===  undefined )  { 
408+       throw  panic ( 
409+         'SessionXrplService.defaultTrustlineOptIn: bad limit:' , 
410+         trustline 
411+       ) ; 
412+     } 
413+ 
414+     if  ( 0  <  limit )  { 
415+       const  limitAmount  =  { 
416+         currency : trustline . currency , 
417+         issuer : trustline . account , 
418+         value : '0' , 
419+       } ; 
420+       const  defaultFlags : xrpl . TrustSetFlagsInterface  =  { 
421+         tfSetNoRipple : true , 
422+         tfClearFreeze : true , 
423+       } ; 
424+       return  await  withLoggedExchange ( 
425+         'SessionXrplService.defaultTrustlineOptIn: sending TrustSet' , 
426+         async  ( )  =>  await  this . sendTrustSetTx ( limitAmount ,  defaultFlags ) , 
427+         limitAmount 
428+       ) ; 
429+     } 
430+   } 
431+ 
321432  protected  async  prepareUnsignedTransaction ( 
322433    receiverId : string , 
323434    amount : xrpl . Payment [ 'Amount' ] 
0 commit comments