1616
1717import { Observable , of } from 'rxjs' ;
1818import { catchError , map , switchMap } from 'rxjs/operators' ;
19- import { RestrictionMosaicHttp } from '../infrastructure/RestrictionMosaicHttp' ;
2019import { Address } from '../model/account/Address' ;
2120import { NetworkType } from '../model/blockchain/NetworkType' ;
2221import { MosaicId } from '../model/mosaic/MosaicId' ;
23- import { MosaicAddressRestriction } from '../model/restriction/MosaicAddressRestriction' ;
2422import { MosaicGlobalRestriction } from '../model/restriction/MosaicGlobalRestriction' ;
2523import { MosaicGlobalRestrictionItem } from '../model/restriction/MosaicGlobalRestrictionItem' ;
2624import { MosaicRestrictionType } from '../model/restriction/MosaicRestrictionType' ;
@@ -29,6 +27,8 @@ import { MosaicAddressRestrictionTransaction } from '../model/transaction/Mosaic
2927import { MosaicGlobalRestrictionTransaction } from '../model/transaction/MosaicGlobalRestrictionTransaction' ;
3028import { Transaction } from '../model/transaction/Transaction' ;
3129import { UInt64 } from '../model/UInt64' ;
30+ import { RestrictionMosaicRepository } from "../infrastructure/RestrictionMosaicRespository" ;
31+ import { NamespaceId } from "../model/namespace/NamespaceId" ;
3232
3333/**
3434 * MosaicRestrictionTransactionService service
@@ -37,11 +37,12 @@ export class MosaicRestrictionTransactionService {
3737
3838 private readonly defaultMosaicAddressRestrictionVaule = UInt64 . fromHex ( 'FFFFFFFFFFFFFFFF' ) ;
3939 private readonly defaultMosaicGlobalRestrictionVaule = UInt64 . fromUint ( 0 ) ;
40+
4041 /**
4142 * Constructor
42- * @param restrictionHttp
43+ * @param restrictionMosaicRepository
4344 */
44- constructor ( private readonly restrictionHttp : RestrictionMosaicHttp ) {
45+ constructor ( private readonly restrictionMosaicRepository : RestrictionMosaicRepository ) {
4546 }
4647
4748 /**
@@ -61,13 +62,13 @@ export class MosaicRestrictionTransactionService {
6162 restrictionKey : UInt64 ,
6263 restrictionValue : string ,
6364 restrictionType : MosaicRestrictionType ,
64- referenceMosaicId : MosaicId = new MosaicId ( UInt64 . fromUint ( 0 ) . toDTO ( ) ) ,
65+ referenceMosaicId : MosaicId | NamespaceId = new MosaicId ( UInt64 . fromUint ( 0 ) . toDTO ( ) ) ,
6566 maxFee : UInt64 = new UInt64 ( [ 0 , 0 ] ) ) : Observable < Transaction > {
6667 this . validateInput ( restrictionValue ) ;
6768 return this . getGlobalRestrictionEntry ( mosaicId , restrictionKey ) . pipe (
6869 map ( ( restrictionEntry : MosaicGlobalRestrictionItem | undefined ) => {
6970 const currentValue = restrictionEntry ? UInt64 . fromNumericString ( restrictionEntry . restrictionValue ) :
70- this . defaultMosaicGlobalRestrictionVaule ;
71+ this . defaultMosaicGlobalRestrictionVaule ;
7172 const currentType = restrictionEntry ? restrictionEntry . restrictionType : MosaicRestrictionType . NONE ;
7273
7374 return MosaicGlobalRestrictionTransaction . create (
@@ -85,7 +86,7 @@ export class MosaicRestrictionTransactionService {
8586 } ) ,
8687 catchError ( ( err ) => {
8788 throw Error ( err ) ;
88- } ) ) ;
89+ } ) ) ;
8990 }
9091
9192 /**
@@ -111,11 +112,9 @@ export class MosaicRestrictionTransactionService {
111112 if ( ! restrictionEntry ) {
112113 throw Error ( 'Global restriction is not valid for RetrictionKey: ' + restrictionKey ) ;
113114 }
114- return this . restrictionHttp . getMosaicAddressRestriction ( mosaicId , targetAddress ) . pipe (
115- map ( ( addressRestriction : MosaicAddressRestriction ) => {
116- const addressEntry = addressRestriction . restrictions . get ( restrictionKey . toHex ( ) ) ;
117- const currentValue = addressEntry ? UInt64 . fromNumericString ( addressEntry ) :
118- this . defaultMosaicAddressRestrictionVaule ;
115+ return this . getAddressRestrictionEntry ( mosaicId , restrictionKey , targetAddress ) . pipe (
116+ map ( ( optionalValue ) => {
117+ const currentValue = optionalValue ? UInt64 . fromNumericString ( optionalValue ) : this . defaultMosaicAddressRestrictionVaule ;
119118 return MosaicAddressRestrictionTransaction . create (
120119 deadline ,
121120 mosaicId ,
@@ -126,35 +125,40 @@ export class MosaicRestrictionTransactionService {
126125 currentValue ,
127126 maxFee ,
128127 ) ;
129- } ) ,
130- catchError ( ( err : Error ) => {
131- const error = JSON . parse ( err . message ) ;
132- if ( error && error . statusCode && error . statusCode === 404 ) {
133- return of ( MosaicAddressRestrictionTransaction . create (
134- deadline ,
135- mosaicId ,
136- restrictionKey ,
137- targetAddress ,
138- UInt64 . fromNumericString ( restrictionValue ) ,
139- networkType ,
140- this . defaultMosaicAddressRestrictionVaule ,
141- maxFee ,
142- ) ) ;
143- }
144- throw Error ( err . message ) ;
145- } ) ) ;
128+ } ) ) ;
146129 } ) ,
147130 ) ;
148131 }
149132
133+ /**
134+ * Get address global restriction previous value and type
135+ * @param mosaicId - Mosaic identifier
136+ * @param restrictionKey - Mosaic global restriction key
137+ * @param targetAddress - The target address
138+ * @return {Observable<string | undefined> }
139+ */
140+ private getAddressRestrictionEntry ( mosaicId : MosaicId , restrictionKey : UInt64 , targetAddress : Address ) : Observable < string | undefined > {
141+ return this . restrictionMosaicRepository . getMosaicAddressRestriction ( mosaicId , targetAddress ) . pipe (
142+ map ( ( mosaicRestriction ) => {
143+ return mosaicRestriction . restrictions . get ( restrictionKey . toHex ( ) ) ;
144+ } ) ,
145+ catchError ( ( err : Error ) => {
146+ const error = JSON . parse ( err . message ) ;
147+ if ( error && error . statusCode && error . statusCode === 404 ) {
148+ return of ( undefined ) ;
149+ }
150+ throw Error ( err . message ) ;
151+ } ) ) ;
152+ }
153+
150154 /**
151155 * Get mosaic global restriction prvious value and type
152156 * @param mosaicId - Mosaic identifier
153157 * @param restrictionKey - Mosaic global restriction key
154158 * @return {Observable<MosaicGlobalRestrictionItem | undefined> }
155159 */
156160 private getGlobalRestrictionEntry ( mosaicId : MosaicId , restrictionKey : UInt64 ) : Observable < MosaicGlobalRestrictionItem | undefined > {
157- return this . restrictionHttp . getMosaicGlobalRestriction ( mosaicId ) . pipe (
161+ return this . restrictionMosaicRepository . getMosaicGlobalRestriction ( mosaicId ) . pipe (
158162 map ( ( mosaicRestriction : MosaicGlobalRestriction ) => {
159163 return mosaicRestriction . restrictions . get ( restrictionKey . toHex ( ) ) ;
160164 } ) ,
@@ -164,7 +168,7 @@ export class MosaicRestrictionTransactionService {
164168 return of ( undefined ) ;
165169 }
166170 throw Error ( err . message ) ;
167- } ) ) ;
171+ } ) ) ;
168172 }
169173
170174 /**
0 commit comments