Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { Address } from '../../model/account/Address';
import { MosaicId } from '../../model/mosaic/MosaicId';
import { MosaicRestrictionType } from '../../model/restriction/MosaicRestrictionType';
import { MosaicRestrictionEntryType } from '../../model/restriction/MosaicRestrictionEntryType';
import { SearchCriteria } from './SearchCriteria';

/**
Expand All @@ -32,7 +32,7 @@ export interface RestrictionMosaicSearchCriteria extends SearchCriteria {
/**
* Mosaic restriction entity type. (optional)
*/
entryType?: MosaicRestrictionType;
entryType?: MosaicRestrictionEntryType;

/**
* Mosaic restriction target address. (optional)
Expand Down
38 changes: 15 additions & 23 deletions src/service/MosaicRestrictionTransactionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { NetworkType } from '../model/network/NetworkType';
import { MosaicAddressRestriction } from '../model/restriction/MosaicAddressRestriction';
import { MosaicGlobalRestriction } from '../model/restriction/MosaicGlobalRestriction';
import { MosaicGlobalRestrictionItem } from '../model/restriction/MosaicGlobalRestrictionItem';
import { MosaicRestrictionEntryType } from '../model/restriction/MosaicRestrictionEntryType';
import { MosaicRestrictionType } from '../model/restriction/MosaicRestrictionType';
import { Deadline } from '../model/transaction/Deadline';
import { MosaicAddressRestrictionTransaction } from '../model/transaction/MosaicAddressRestrictionTransaction';
Expand Down Expand Up @@ -125,7 +126,9 @@ export class MosaicRestrictionTransactionService {
this.getGlobalRestrictionEntry(resolvedMosaicId, restrictionKey).pipe(
mergeMap((restrictionEntry: MosaicGlobalRestrictionItem | undefined) => {
if (!restrictionEntry) {
throw new Error('Global restriction is not valid for RestrictionKey: ' + restrictionKey);
throw new Error(
`Global restriction for mosaic: ${mosaicId} is not valid for with RestrictionKey: ${restrictionKey}`,
);
}
return this.getAddressRestrictionEntry(resolvedMosaicId, restrictionKey, resolvedAddress).pipe(
map((optionalValue) => {
Expand Down Expand Up @@ -156,16 +159,14 @@ export class MosaicRestrictionTransactionService {
* @return {Observable<string | undefined>}
*/
private getAddressRestrictionEntry(mosaicId: MosaicId, restrictionKey: UInt64, targetAddress: Address): Observable<UInt64 | undefined> {
return this.restrictionMosaicRepository.search({ mosaicId, targetAddress }).pipe(
return this.restrictionMosaicRepository.search({ mosaicId, targetAddress, entryType: MosaicRestrictionEntryType.ADDRESS }).pipe(
map((mosaicRestriction) => {
return (mosaicRestriction.data[0] as MosaicAddressRestriction).getRestriction(restrictionKey)?.restrictionValue;
}),
catchError((err: Error) => {
const error = JSON.parse(err.message);
if (error && error.statusCode && error.statusCode === 404) {
return of(undefined);
}
throw new Error(err.message);
return mosaicRestriction.data
.find(
(r) =>
r instanceof MosaicAddressRestriction && r.mosaicId.equals(mosaicId) && r.targetAddress.equals(targetAddress),
)!
.getRestriction(restrictionKey)?.restrictionValue;
}),
);
}
Expand All @@ -177,20 +178,11 @@ export class MosaicRestrictionTransactionService {
* @return {Observable<MosaicGlobalRestrictionItem | undefined>}
*/
private getGlobalRestrictionEntry(mosaicId: MosaicId, restrictionKey: UInt64): Observable<MosaicGlobalRestrictionItem | undefined> {
return this.restrictionMosaicRepository.search({ mosaicId }).pipe(
return this.restrictionMosaicRepository.search({ mosaicId, entryType: MosaicRestrictionEntryType.GLOBAL }).pipe(
map((mosaicRestrictionPage: Page<MosaicGlobalRestriction>) => {
const globalRestriction = mosaicRestrictionPage.data.find((r) => r instanceof MosaicGlobalRestriction);
if (globalRestriction !== undefined) {
return globalRestriction.getRestriction(restrictionKey);
}
throw new Error('No global restriction found for mosaic' + mosaicId.toHex());
}),
catchError((err: Error) => {
const error = JSON.parse(err.message);
if (error && error.statusCode && error.statusCode === 404) {
return of(undefined);
}
throw new Error(err.message);
return mosaicRestrictionPage.data
.find((r) => r instanceof MosaicGlobalRestriction && r.mosaicId.equals(mosaicId))!
.getRestriction(restrictionKey);
}),
);
}
Expand Down
10 changes: 7 additions & 3 deletions test/service/MosaicRestrictionTransactionservice.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,14 @@ describe('MosaicRestrictionTransactionService', () => {
const mockRestrictionRepository = mock<RestrictionMosaicRepository>();
const mockNamespaceRepository = mock<NamespaceRepository>();

when(mockRestrictionRepository.search(deepEqual({ mosaicId }))).thenReturn(observableOf(mockGlobalRestriction()));
when(mockRestrictionRepository.search(deepEqual({ mosaicId, targetAddress: account.address }))).thenReturn(
observableOf(mockAddressRestriction()),
when(mockRestrictionRepository.search(deepEqual({ mosaicId, entryType: MosaicRestrictionEntryType.GLOBAL }))).thenReturn(
observableOf(mockGlobalRestriction()),
);
when(
mockRestrictionRepository.search(
deepEqual({ mosaicId, targetAddress: account.address, entryType: MosaicRestrictionEntryType.ADDRESS }),
),
).thenReturn(observableOf(mockAddressRestriction()));
when(mockNamespaceRepository.getLinkedMosaicId(deepEqual(unresolvedMosaicId))).thenReturn(observableOf(mosaicId));
when(mockNamespaceRepository.getLinkedMosaicId(deepEqual(unresolvedAddress))).thenThrow(new Error('invalid namespaceId'));
when(mockNamespaceRepository.getLinkedAddress(deepEqual(unresolvedAddress))).thenReturn(observableOf(account.address));
Expand Down