Skip to content

Commit 2f70b52

Browse files
committed
Fixed Github 367 - TS-111
1 parent d57c5a5 commit 2f70b52

File tree

1 file changed

+35
-48
lines changed

1 file changed

+35
-48
lines changed

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} blockDTO the dto object from rest.
120+
* @returns {BlockInfo} a BlockInfo model
121+
*/
122+
private toBlockInfo(blockDTO: BlockInfoDTO): BlockInfo {
123+
const networkType = blockDTO.block.network.valueOf();
124+
return new BlockInfo(
125+
blockDTO.meta.hash,
126+
blockDTO.meta.generationHash,
127+
UInt64.fromNumericString(blockDTO.meta.totalFee),
128+
blockDTO.meta.numTransactions,
129+
blockDTO.block.signature,
130+
PublicAccount.createFromPublicKey(blockDTO.block.signerPublicKey, networkType),
131+
networkType,
132+
blockDTO.block.version,
133+
blockDTO.block.type,
134+
UInt64.fromNumericString(blockDTO.block.height),
135+
UInt64.fromNumericString(blockDTO.block.timestamp),
136+
UInt64.fromNumericString(blockDTO.block.difficulty),
137+
blockDTO.block.feeMultiplier,
138+
blockDTO.block.previousBlockHash,
139+
blockDTO.block.transactionsHash,
140+
blockDTO.block.receiptsHash,
141+
blockDTO.block.stateHash,
142+
extractBeneficiary(blockDTO, 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)

0 commit comments

Comments
 (0)