Skip to content

Commit 31bf94a

Browse files
authored
Fixed #754 (#757)
1 parent 353c328 commit 31bf94a

File tree

3 files changed

+24
-28
lines changed

3 files changed

+24
-28
lines changed

src/infrastructure/searchCriteria/RestrictionMosaicSearchCriteria.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import { Address } from '../../model/account/Address';
1818
import { MosaicId } from '../../model/mosaic/MosaicId';
19-
import { MosaicRestrictionType } from '../../model/restriction/MosaicRestrictionType';
19+
import { MosaicRestrictionEntryType } from '../../model/restriction/MosaicRestrictionEntryType';
2020
import { SearchCriteria } from './SearchCriteria';
2121

2222
/**
@@ -32,7 +32,7 @@ export interface RestrictionMosaicSearchCriteria extends SearchCriteria {
3232
/**
3333
* Mosaic restriction entity type. (optional)
3434
*/
35-
entryType?: MosaicRestrictionType;
35+
entryType?: MosaicRestrictionEntryType;
3636

3737
/**
3838
* Mosaic restriction target address. (optional)

src/service/MosaicRestrictionTransactionService.ts

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { NetworkType } from '../model/network/NetworkType';
2727
import { MosaicAddressRestriction } from '../model/restriction/MosaicAddressRestriction';
2828
import { MosaicGlobalRestriction } from '../model/restriction/MosaicGlobalRestriction';
2929
import { MosaicGlobalRestrictionItem } from '../model/restriction/MosaicGlobalRestrictionItem';
30+
import { MosaicRestrictionEntryType } from '../model/restriction/MosaicRestrictionEntryType';
3031
import { MosaicRestrictionType } from '../model/restriction/MosaicRestrictionType';
3132
import { Deadline } from '../model/transaction/Deadline';
3233
import { MosaicAddressRestrictionTransaction } from '../model/transaction/MosaicAddressRestrictionTransaction';
@@ -125,7 +126,9 @@ export class MosaicRestrictionTransactionService {
125126
this.getGlobalRestrictionEntry(resolvedMosaicId, restrictionKey).pipe(
126127
mergeMap((restrictionEntry: MosaicGlobalRestrictionItem | undefined) => {
127128
if (!restrictionEntry) {
128-
throw new Error('Global restriction is not valid for RestrictionKey: ' + restrictionKey);
129+
throw new Error(
130+
`Global restriction for mosaic: ${mosaicId} is not valid for with RestrictionKey: ${restrictionKey}`,
131+
);
129132
}
130133
return this.getAddressRestrictionEntry(resolvedMosaicId, restrictionKey, resolvedAddress).pipe(
131134
map((optionalValue) => {
@@ -156,16 +159,14 @@ export class MosaicRestrictionTransactionService {
156159
* @return {Observable<string | undefined>}
157160
*/
158161
private getAddressRestrictionEntry(mosaicId: MosaicId, restrictionKey: UInt64, targetAddress: Address): Observable<UInt64 | undefined> {
159-
return this.restrictionMosaicRepository.search({ mosaicId, targetAddress }).pipe(
162+
return this.restrictionMosaicRepository.search({ mosaicId, targetAddress, entryType: MosaicRestrictionEntryType.ADDRESS }).pipe(
160163
map((mosaicRestriction) => {
161-
return (mosaicRestriction.data[0] as MosaicAddressRestriction).getRestriction(restrictionKey)?.restrictionValue;
162-
}),
163-
catchError((err: Error) => {
164-
const error = JSON.parse(err.message);
165-
if (error && error.statusCode && error.statusCode === 404) {
166-
return of(undefined);
167-
}
168-
throw new Error(err.message);
164+
return mosaicRestriction.data
165+
.find(
166+
(r) =>
167+
r instanceof MosaicAddressRestriction && r.mosaicId.equals(mosaicId) && r.targetAddress.equals(targetAddress),
168+
)!
169+
.getRestriction(restrictionKey)?.restrictionValue;
169170
}),
170171
);
171172
}
@@ -177,20 +178,11 @@ export class MosaicRestrictionTransactionService {
177178
* @return {Observable<MosaicGlobalRestrictionItem | undefined>}
178179
*/
179180
private getGlobalRestrictionEntry(mosaicId: MosaicId, restrictionKey: UInt64): Observable<MosaicGlobalRestrictionItem | undefined> {
180-
return this.restrictionMosaicRepository.search({ mosaicId }).pipe(
181+
return this.restrictionMosaicRepository.search({ mosaicId, entryType: MosaicRestrictionEntryType.GLOBAL }).pipe(
181182
map((mosaicRestrictionPage: Page<MosaicGlobalRestriction>) => {
182-
const globalRestriction = mosaicRestrictionPage.data.find((r) => r instanceof MosaicGlobalRestriction);
183-
if (globalRestriction !== undefined) {
184-
return globalRestriction.getRestriction(restrictionKey);
185-
}
186-
throw new Error('No global restriction found for mosaic' + mosaicId.toHex());
187-
}),
188-
catchError((err: Error) => {
189-
const error = JSON.parse(err.message);
190-
if (error && error.statusCode && error.statusCode === 404) {
191-
return of(undefined);
192-
}
193-
throw new Error(err.message);
183+
return mosaicRestrictionPage.data
184+
.find((r) => r instanceof MosaicGlobalRestriction && r.mosaicId.equals(mosaicId))!
185+
.getRestriction(restrictionKey);
194186
}),
195187
);
196188
}

test/service/MosaicRestrictionTransactionservice.spec.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,14 @@ describe('MosaicRestrictionTransactionService', () => {
8484
const mockRestrictionRepository = mock<RestrictionMosaicRepository>();
8585
const mockNamespaceRepository = mock<NamespaceRepository>();
8686

87-
when(mockRestrictionRepository.search(deepEqual({ mosaicId }))).thenReturn(observableOf(mockGlobalRestriction()));
88-
when(mockRestrictionRepository.search(deepEqual({ mosaicId, targetAddress: account.address }))).thenReturn(
89-
observableOf(mockAddressRestriction()),
87+
when(mockRestrictionRepository.search(deepEqual({ mosaicId, entryType: MosaicRestrictionEntryType.GLOBAL }))).thenReturn(
88+
observableOf(mockGlobalRestriction()),
9089
);
90+
when(
91+
mockRestrictionRepository.search(
92+
deepEqual({ mosaicId, targetAddress: account.address, entryType: MosaicRestrictionEntryType.ADDRESS }),
93+
),
94+
).thenReturn(observableOf(mockAddressRestriction()));
9195
when(mockNamespaceRepository.getLinkedMosaicId(deepEqual(unresolvedMosaicId))).thenReturn(observableOf(mosaicId));
9296
when(mockNamespaceRepository.getLinkedMosaicId(deepEqual(unresolvedAddress))).thenThrow(new Error('invalid namespaceId'));
9397
when(mockNamespaceRepository.getLinkedAddress(deepEqual(unresolvedAddress))).thenReturn(observableOf(account.address));

0 commit comments

Comments
 (0)