Skip to content

Commit 15a40fe

Browse files
authored
Merge branch 'master' into task/g376_operation_uint64
2 parents 3089652 + 477e7e2 commit 15a40fe

File tree

4 files changed

+58
-54
lines changed

4 files changed

+58
-54
lines changed

package-lock.json

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"mocha": "^4.0.1",
5151
"nyc": "^14.1.1",
5252
"secure-random": "^1.1.1",
53-
"ts-mockito": "^2.2.7",
53+
"ts-mockito": "^2.4.0",
5454
"ts-node": "^5.0.1",
5555
"tslint": "^5.8.0",
5656
"typescript": "^2.5.3",

src/service/MosaicRestrictionTransactionService.ts

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@
1616

1717
import { Observable, of } from 'rxjs';
1818
import { catchError, map, switchMap } from 'rxjs/operators';
19-
import { RestrictionMosaicHttp } from '../infrastructure/RestrictionMosaicHttp';
2019
import { Address } from '../model/account/Address';
2120
import { NetworkType } from '../model/blockchain/NetworkType';
2221
import { MosaicId } from '../model/mosaic/MosaicId';
23-
import { MosaicAddressRestriction } from '../model/restriction/MosaicAddressRestriction';
2422
import { MosaicGlobalRestriction } from '../model/restriction/MosaicGlobalRestriction';
2523
import { MosaicGlobalRestrictionItem } from '../model/restriction/MosaicGlobalRestrictionItem';
2624
import { MosaicRestrictionType } from '../model/restriction/MosaicRestrictionType';
@@ -29,6 +27,8 @@ import { MosaicAddressRestrictionTransaction } from '../model/transaction/Mosaic
2927
import { MosaicGlobalRestrictionTransaction } from '../model/transaction/MosaicGlobalRestrictionTransaction';
3028
import { Transaction } from '../model/transaction/Transaction';
3129
import { 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
/**

test/service/MosaicRestrictionTransactionservice.spec.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {expect} from 'chai';
18-
import {of as observableOf} from 'rxjs';
19-
import {deepEqual, instance, mock, when} from 'ts-mockito';
17+
import { expect } from 'chai';
18+
import { of as observableOf } from 'rxjs';
19+
import { deepEqual, instance, mock, when } from 'ts-mockito';
2020
import { KeyGenerator } from '../../src/core/format/KeyGenerator';
21-
import { RestrictionMosaicHttp } from '../../src/infrastructure/RestrictionMosaicHttp';
21+
import { RestrictionMosaicRepository } from '../../src/infrastructure/RestrictionMosaicRespository';
2222
import { Account } from '../../src/model/account/Account';
23-
import {NetworkType} from '../../src/model/blockchain/NetworkType';
23+
import { NetworkType } from '../../src/model/blockchain/NetworkType';
2424
import { MosaicId } from '../../src/model/mosaic/MosaicId';
2525
import { MosaicAddressRestriction } from '../../src/model/restriction/MosaicAddressRestriction';
2626
import { MosaicGlobalRestriction } from '../../src/model/restriction/MosaicGlobalRestriction';
@@ -31,7 +31,7 @@ import { Deadline } from '../../src/model/transaction/Deadline';
3131
import { MosaicAddressRestrictionTransaction } from '../../src/model/transaction/MosaicAddressRestrictionTransaction';
3232
import { MosaicGlobalRestrictionTransaction } from '../../src/model/transaction/MosaicGlobalRestrictionTransaction';
3333
import { TransactionType } from '../../src/model/transaction/TransactionType';
34-
import {UInt64} from '../../src/model/UInt64';
34+
import { UInt64 } from '../../src/model/UInt64';
3535
import { MosaicRestrictionTransactionService } from '../../src/service/MosaicRestrictionTransactionService';
3636
import { TestingAccount } from '../conf/conf.spec';
3737

@@ -52,18 +52,18 @@ describe('MosaicRestrictionTransactionService', () => {
5252
mosaicId = new MosaicId('85BBEA6CC462B244');
5353
mosaicIdWrongKey = new MosaicId('85BBEA6CC462B288');
5454
referenceMosaicId = new MosaicId('1AB129B545561E6A');
55-
const mockRestrictionHttp = mock(RestrictionMosaicHttp);
55+
const mockRestrictionRepository = mock<RestrictionMosaicRepository>();
5656

57-
when(mockRestrictionHttp
57+
when(mockRestrictionRepository
5858
.getMosaicGlobalRestriction(deepEqual(mosaicId)))
5959
.thenReturn(observableOf(mockGlobalRestriction()));
60-
when(mockRestrictionHttp
60+
when(mockRestrictionRepository
6161
.getMosaicGlobalRestriction(deepEqual(mosaicIdWrongKey)))
6262
.thenThrow(new Error());
63-
when(mockRestrictionHttp
63+
when(mockRestrictionRepository
6464
.getMosaicAddressRestriction(deepEqual(mosaicId), deepEqual(account.address)))
6565
.thenReturn(observableOf(mockAddressRestriction()));
66-
const restrictionHttp = instance(mockRestrictionHttp);
66+
const restrictionHttp = instance(mockRestrictionRepository);
6767
mosaicRestrictionTransactionService = new MosaicRestrictionTransactionService(restrictionHttp);
6868
});
6969

0 commit comments

Comments
 (0)