Skip to content

Commit ac884d0

Browse files
author
Grégory Saive
authored
Merge branch 'master' into g370-deadline-hotfix
2 parents 5a58673 + 679de2f commit ac884d0

File tree

4 files changed

+145
-174
lines changed

4 files changed

+145
-174
lines changed

src/infrastructure/AccountHttp.ts

Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -61,32 +61,10 @@ export class AccountHttp extends Http implements AccountRepository {
6161
*/
6262
public getAccountInfo(address: Address): Observable<AccountInfo> {
6363
return observableFrom(this.accountRoutesApi.getAccountInfo(address.plain())).pipe(
64-
map(({body}) => new AccountInfo(
65-
Address.createFromEncoded(body.account.address),
66-
UInt64.fromNumericString(body.account.addressHeight),
67-
body.account.publicKey,
68-
UInt64.fromNumericString(body.account.publicKeyHeight),
69-
body.account.accountType.valueOf(),
70-
body.account.linkedAccountKey,
71-
body.account.activityBuckets.map((bucket) => {
72-
return new ActivityBucket(
73-
bucket.startHeight,
74-
bucket.totalFeesPaid,
75-
bucket.beneficiaryCount,
76-
bucket.rawScore,
77-
);
78-
}),
79-
body.account.mosaics.map((mosaicDTO) => new Mosaic(
80-
new MosaicId(mosaicDTO.id),
81-
UInt64.fromNumericString(mosaicDTO.amount),
82-
)),
83-
UInt64.fromNumericString(body.account.importance),
84-
UInt64.fromNumericString(body.account.importanceHeight),
85-
)),
64+
map(({body}) => this.toAccountInfo(body)),
8665
catchError((error) => throwError(this.errorHandling(error))),
8766
);
8867
}
89-
9068
/**
9169
* Gets AccountsInfo for different accounts.
9270
* @param addresses List of Address
@@ -98,35 +76,45 @@ export class AccountHttp extends Http implements AccountRepository {
9876
};
9977
return observableFrom(
10078
this.accountRoutesApi.getAccountsInfo(accountIdsBody)).pipe(
101-
map(({body}) => body.map((accountInfoDTO: AccountInfoDTO) => {
102-
return new AccountInfo(
103-
Address.createFromEncoded(accountInfoDTO.account.address),
104-
UInt64.fromNumericString(accountInfoDTO.account.addressHeight),
105-
accountInfoDTO.account.publicKey,
106-
UInt64.fromNumericString(accountInfoDTO.account.publicKeyHeight),
107-
accountInfoDTO.account.accountType.valueOf(),
108-
accountInfoDTO.account.linkedAccountKey,
109-
accountInfoDTO.account.activityBuckets.map((bucket) => {
110-
return new ActivityBucket(
111-
bucket.startHeight,
112-
bucket.totalFeesPaid,
113-
bucket.beneficiaryCount,
114-
bucket.rawScore,
115-
);
116-
}),
117-
accountInfoDTO.account.mosaics.map((mosaicDTO) => new Mosaic(
118-
new MosaicId(mosaicDTO.id),
119-
UInt64.fromNumericString(mosaicDTO.amount),
120-
)),
121-
UInt64.fromNumericString(accountInfoDTO.account.importance),
122-
UInt64.fromNumericString(accountInfoDTO.account.importanceHeight),
123-
);
124-
125-
})),
79+
map(({body}) => body.map(this.toAccountInfo)),
12680
catchError((error) => throwError(this.errorHandling(error))),
12781
);
12882
}
12983

84+
85+
/**
86+
* This method maps a AccountInfoDTO from rest to the SDK's AccountInfo model object.
87+
*
88+
* @internal
89+
* @param {AccountInfoDTO} dto AccountInfoDTO the dto object from rest.
90+
* @returns AccountInfo model
91+
*/
92+
private toAccountInfo(dto: AccountInfoDTO):AccountInfo {
93+
return new AccountInfo(
94+
Address.createFromEncoded(dto.account.address),
95+
UInt64.fromNumericString(dto.account.addressHeight),
96+
dto.account.publicKey,
97+
UInt64.fromNumericString(dto.account.publicKeyHeight),
98+
dto.account.accountType.valueOf(),
99+
dto.account.linkedAccountKey,
100+
dto.account.activityBuckets.map((bucket) => {
101+
return new ActivityBucket(
102+
bucket.startHeight,
103+
bucket.totalFeesPaid,
104+
bucket.beneficiaryCount,
105+
bucket.rawScore,
106+
);
107+
}),
108+
dto.account.mosaics.map((mosaicDTO) => new Mosaic(
109+
new MosaicId(mosaicDTO.id),
110+
UInt64.fromNumericString(mosaicDTO.amount),
111+
)),
112+
UInt64.fromNumericString(dto.account.importance),
113+
UInt64.fromNumericString(dto.account.importanceHeight),
114+
);
115+
}
116+
117+
130118
/**
131119
* Gets an array of confirmed transactions for which an account is signer or receiver.
132120
* @param address - * Address can be created rawAddress or publicKey

src/infrastructure/BlockHttp.ts

Lines changed: 35 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { MerkleProofInfo } from '../model/blockchain/MerkleProofInfo';
2323
import { NetworkType } from '../model/blockchain/NetworkType';
2424
import {Transaction} from '../model/transaction/Transaction';
2525
import {UInt64} from '../model/UInt64';
26-
import { BlockRoutesApi } from './api';
26+
import { BlockInfoDTO, BlockRoutesApi } from './api';
2727
import {BlockRepository} from './BlockRepository';
2828
import {Http} from './Http';
2929
import {QueryParams} from './QueryParams';
@@ -72,30 +72,7 @@ export class BlockHttp extends Http implements BlockRepository {
7272
*/
7373
public getBlockByHeight(height: string): Observable<BlockInfo> {
7474
return observableFrom(this.blockRoutesApi.getBlockByHeight(height)).pipe(
75-
map(({body}) => {
76-
const blockDTO = body;
77-
const networkType = parseInt((blockDTO.block.version as number).toString(16).substr(0, 2), 16);
78-
return new BlockInfo(
79-
blockDTO.meta.hash,
80-
blockDTO.meta.generationHash,
81-
UInt64.fromNumericString(blockDTO.meta.totalFee),
82-
blockDTO.meta.numTransactions,
83-
blockDTO.block.signature,
84-
PublicAccount.createFromPublicKey(blockDTO.block.signerPublicKey, networkType),
85-
networkType,
86-
parseInt((blockDTO.block.version as number).toString(16).substr(2, 2), 16), // Tx version
87-
blockDTO.block.type,
88-
UInt64.fromNumericString(blockDTO.block.height),
89-
UInt64.fromNumericString(blockDTO.block.timestamp),
90-
UInt64.fromNumericString(blockDTO.block.difficulty),
91-
blockDTO.block.feeMultiplier,
92-
blockDTO.block.previousBlockHash,
93-
blockDTO.block.transactionsHash,
94-
blockDTO.block.receiptsHash,
95-
blockDTO.block.stateHash,
96-
extractBeneficiary(blockDTO, networkType),
97-
);
98-
}),
75+
map(({body}) => this.toBlockInfo(body)),
9976
catchError((error) => throwError(this.errorHandling(error))),
10077
);
10178
}
@@ -129,33 +106,43 @@ export class BlockHttp extends Http implements BlockRepository {
129106
public getBlocksByHeightWithLimit(height: string, limit: LimitType = LimitType.N_25): Observable<BlockInfo[]> {
130107
return observableFrom(
131108
this.blockRoutesApi.getBlocksByHeightWithLimit(height, limit)).pipe(
132-
map(({body}) => body.map((blockDTO) => {
133-
const networkType = parseInt((blockDTO.block.version as number).toString(16).substr(0, 2), 16);
134-
return new BlockInfo(
135-
blockDTO.meta.hash,
136-
blockDTO.meta.generationHash,
137-
UInt64.fromNumericString(blockDTO.meta.totalFee),
138-
blockDTO.meta.numTransactions,
139-
blockDTO.block.signature,
140-
PublicAccount.createFromPublicKey(blockDTO.block.signerPublicKey, networkType),
141-
networkType,
142-
parseInt((blockDTO.block.version as number).toString(16).substr(2, 2), 16), // Tx version
143-
blockDTO.block.type,
144-
UInt64.fromNumericString(blockDTO.block.height),
145-
UInt64.fromNumericString(blockDTO.block.timestamp),
146-
UInt64.fromNumericString(blockDTO.block.difficulty),
147-
blockDTO.block.feeMultiplier,
148-
blockDTO.block.previousBlockHash,
149-
blockDTO.block.transactionsHash,
150-
blockDTO.block.receiptsHash,
151-
blockDTO.block.stateHash,
152-
extractBeneficiary(blockDTO, networkType),
153-
);
154-
})),
109+
map(({body}) => body.map((blockDTO) => this.toBlockInfo(blockDTO))),
155110
catchError((error) => throwError(this.errorHandling(error))),
156111
);
157112
}
158113

114+
115+
/**
116+
* This method maps a BlockInfoDTO from rest to the SDK's BlockInfo model object.
117+
*
118+
* @internal
119+
* @param {BlockInfoDTO} dto the dto object from rest.
120+
* @returns {BlockInfo} a BlockInfo model
121+
*/
122+
private toBlockInfo(dto: BlockInfoDTO): BlockInfo {
123+
const networkType = dto.block.network.valueOf();
124+
return new BlockInfo(
125+
dto.meta.hash,
126+
dto.meta.generationHash,
127+
UInt64.fromNumericString(dto.meta.totalFee),
128+
dto.meta.numTransactions,
129+
dto.block.signature,
130+
PublicAccount.createFromPublicKey(dto.block.signerPublicKey, networkType),
131+
networkType,
132+
dto.block.version,
133+
dto.block.type,
134+
UInt64.fromNumericString(dto.block.height),
135+
UInt64.fromNumericString(dto.block.timestamp),
136+
UInt64.fromNumericString(dto.block.difficulty),
137+
dto.block.feeMultiplier,
138+
dto.block.previousBlockHash,
139+
dto.block.transactionsHash,
140+
dto.block.receiptsHash,
141+
dto.block.stateHash,
142+
extractBeneficiary(dto, networkType),
143+
);
144+
}
145+
159146
/**
160147
* Get the merkle path for a given a transaction and block
161148
* Returns the merkle path for a [transaction](https://nemtech.github.io/concepts/transaction.html)

src/infrastructure/RestrictionMosaicHttp.ts

Lines changed: 51 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import { MosaicGlobalRestrictionItem } from '../model/restriction/MosaicGlobalRe
2525
import { RestrictionMosaicRoutesApi } from './api/restrictionMosaicRoutesApi';
2626
import {Http} from './Http';
2727
import { RestrictionMosaicRepository } from './RestrictionMosaicRespository';
28+
import { MosaicAddressRestrictionDTO } from "./model/mosaicAddressRestrictionDTO";
29+
import { MosaicGlobalRestrictionDTO } from "./model/mosaicGlobalRestrictionDTO";
2830

2931
/**
3032
* RestrictionMosaic http repository.
@@ -58,20 +60,7 @@ export class RestrictionMosaicHttp extends Http implements RestrictionMosaicRepo
5860
getMosaicAddressRestriction(mosaicId: MosaicId, address: Address): Observable<MosaicAddressRestriction> {
5961
return observableFrom(
6062
this.restrictionMosaicRoutesApi.getMosaicAddressRestriction(mosaicId.toHex(), address.plain())).pipe(
61-
map(({body}) => {
62-
const payload = body.mosaicRestrictionEntry;
63-
const restirctionItems = new Map<string, string>();
64-
payload.restrictions.forEach((restriction) => {
65-
restirctionItems.set(restriction.key, restriction.value);
66-
});
67-
return new MosaicAddressRestriction(
68-
payload.compositeHash,
69-
payload.entryType.valueOf(),
70-
new MosaicId(payload.mosaicId),
71-
Address.createFromEncoded(payload.targetAddress),
72-
restirctionItems,
73-
);
74-
}),
63+
map(({body}) => this.toMosaicAddressRestriction(body)),
7564
catchError((error) => throwError(this.errorHandling(error))),
7665
);
7766
}
@@ -89,23 +78,32 @@ export class RestrictionMosaicHttp extends Http implements RestrictionMosaicRepo
8978
};
9079
return observableFrom(
9180
this.restrictionMosaicRoutesApi.getMosaicAddressRestrictions(mosaicId.toHex(), accountIds)).pipe(
92-
map(({body}) => body.map((payload) => {
93-
const restirctionItems = new Map<string, string>();
94-
payload.mosaicRestrictionEntry.restrictions.forEach((restriction) => {
95-
restirctionItems.set(restriction.key, restriction.value);
96-
});
97-
return new MosaicAddressRestriction(
98-
payload.mosaicRestrictionEntry.compositeHash,
99-
payload.mosaicRestrictionEntry.entryType.valueOf(),
100-
new MosaicId(payload.mosaicRestrictionEntry.mosaicId),
101-
Address.createFromEncoded(payload.mosaicRestrictionEntry.targetAddress),
102-
restirctionItems,
103-
);
104-
})),
81+
map(({body}) => body.map(this.toMosaicAddressRestriction)),
10582
catchError((error) => throwError(this.errorHandling(error))),
10683
);
10784
}
10885

86+
/**
87+
* This method maps a MosaicAddressRestrictionDTO from rest to the SDK's MosaicAddressRestriction model object.
88+
*
89+
* @internal
90+
* @param {MosaicAddressRestrictionDTO} dto the MosaicAddressRestrictionDTO object from rest.
91+
* @returns {MosaicAddressRestriction} a MosaicAddressRestriction model
92+
*/
93+
private toMosaicAddressRestriction(dto: MosaicAddressRestrictionDTO): MosaicAddressRestriction {
94+
const restrictionItems = new Map<string, string>();
95+
dto.mosaicRestrictionEntry.restrictions.forEach((restriction) => {
96+
restrictionItems.set(restriction.key, restriction.value);
97+
});
98+
return new MosaicAddressRestriction(
99+
dto.mosaicRestrictionEntry.compositeHash,
100+
dto.mosaicRestrictionEntry.entryType.valueOf(),
101+
new MosaicId(dto.mosaicRestrictionEntry.mosaicId),
102+
Address.createFromEncoded(dto.mosaicRestrictionEntry.targetAddress),
103+
restrictionItems,
104+
);
105+
}
106+
109107
/**
110108
* Get mosaic global restriction.
111109
* @summary Get mosaic global restrictions for a given mosaic identifier.
@@ -115,23 +113,7 @@ export class RestrictionMosaicHttp extends Http implements RestrictionMosaicRepo
115113
getMosaicGlobalRestriction(mosaicId: MosaicId): Observable<MosaicGlobalRestriction> {
116114
return observableFrom(
117115
this.restrictionMosaicRoutesApi.getMosaicGlobalRestriction(mosaicId.toHex())).pipe(
118-
map(({body}) => {
119-
const payload = body.mosaicRestrictionEntry;
120-
const restirctionItems = new Map<string, MosaicGlobalRestrictionItem>();
121-
payload.restrictions.forEach((restriction) =>
122-
restirctionItems.set(restriction.key,
123-
new MosaicGlobalRestrictionItem(
124-
new MosaicId(restriction.restriction.referenceMosaicId),
125-
restriction.restriction.restrictionValue,
126-
restriction.restriction.restrictionType.valueOf(),
127-
)));
128-
return new MosaicGlobalRestriction(
129-
payload.compositeHash,
130-
payload.entryType.valueOf(),
131-
new MosaicId(payload.mosaicId),
132-
restirctionItems,
133-
);
134-
}),
116+
map(({body}) => this.toMosaicGlobalRestriction(body)),
135117
catchError((error) => throwError(this.errorHandling(error))),
136118
);
137119
}
@@ -148,23 +130,32 @@ export class RestrictionMosaicHttp extends Http implements RestrictionMosaicRepo
148130
};
149131
return observableFrom(
150132
this.restrictionMosaicRoutesApi.getMosaicGlobalRestrictions(mosaicIdsBody)).pipe(
151-
map(({body}) => body.map((payload) => {
152-
const restirctionItems = new Map<string, MosaicGlobalRestrictionItem>();
153-
payload.mosaicRestrictionEntry.restrictions.forEach((restriction) =>
154-
restirctionItems.set(restriction.key,
155-
new MosaicGlobalRestrictionItem(
156-
new MosaicId(restriction.restriction.referenceMosaicId),
157-
restriction.restriction.restrictionValue,
158-
restriction.restriction.restrictionType.valueOf(),
159-
)));
160-
return new MosaicGlobalRestriction(
161-
payload.mosaicRestrictionEntry.compositeHash,
162-
payload.mosaicRestrictionEntry.entryType.valueOf(),
163-
new MosaicId(payload.mosaicRestrictionEntry.mosaicId),
164-
restirctionItems,
165-
);
166-
})),
133+
map(({body}) => body.map(this.toMosaicGlobalRestriction)),
167134
catchError((error) => throwError(this.errorHandling(error))),
168135
);
169136
}
137+
138+
/**
139+
* This method maps a MosaicGlobalRestrictionDTO from rest to the SDK's MosaicGlobalRestriction model object.
140+
*
141+
* @internal
142+
* @param {MosaicGlobalRestrictionDTO} dto the MosaicGlobalRestrictionDTO object from rest.
143+
* @returns {MosaicGlobalRestriction} a MosaicGlobalRestriction model
144+
*/
145+
private toMosaicGlobalRestriction(dto: MosaicGlobalRestrictionDTO): MosaicGlobalRestriction {
146+
const restirctionItems = new Map<string, MosaicGlobalRestrictionItem>();
147+
dto.mosaicRestrictionEntry.restrictions.forEach((restriction) =>
148+
restirctionItems.set(restriction.key,
149+
new MosaicGlobalRestrictionItem(
150+
new MosaicId(restriction.restriction.referenceMosaicId),
151+
restriction.restriction.restrictionValue,
152+
restriction.restriction.restrictionType.valueOf(),
153+
)));
154+
return new MosaicGlobalRestriction(
155+
dto.mosaicRestrictionEntry.compositeHash,
156+
dto.mosaicRestrictionEntry.entryType.valueOf(),
157+
new MosaicId(dto.mosaicRestrictionEntry.mosaicId),
158+
restirctionItems,
159+
);
160+
}
170161
}

0 commit comments

Comments
 (0)