Skip to content

Commit 5899cce

Browse files
committed
Added #195 fixed compatibility issue on new rxjs version
1 parent 99fdb13 commit 5899cce

20 files changed

+580
-536
lines changed

src/infrastructure/AccountHttp.ts

Lines changed: 130 additions & 90 deletions
Large diffs are not rendered by default.

src/infrastructure/BlockHttp.ts

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

17-
import {from as observableFrom, Observable} from 'rxjs';
18-
import {map, mergeMap} from 'rxjs/operators';
17+
import { ClientResponse } from 'http';
18+
import {from as observableFrom, Observable, throwError} from 'rxjs';
19+
import {catchError, map, mergeMap} from 'rxjs/operators';
1920
import {PublicAccount} from '../model/account/PublicAccount';
2021
import {BlockInfo} from '../model/blockchain/BlockInfo';
2122
import { MerklePathItem } from '../model/blockchain/MerklePathItem';
@@ -79,29 +80,33 @@ export class BlockHttp extends Http implements BlockRepository {
7980
* @returns Observable<BlockInfo>
8081
*/
8182
public getBlockByHeight(height: number): Observable<BlockInfo> {
82-
return observableFrom(this.blockRoutesApi.getBlockByHeight(height)).pipe(map((blockDTO: BlockInfoDTO) => {
83-
const networkType = parseInt((blockDTO.block.version as number).toString(16).substr(0, 2), 16);
84-
return new BlockInfo(
85-
blockDTO.meta.hash,
86-
blockDTO.meta.generationHash,
87-
new UInt64(blockDTO.meta.totalFee),
88-
blockDTO.meta.numTransactions,
89-
blockDTO.block.signature,
90-
PublicAccount.createFromPublicKey(blockDTO.block.signer, networkType),
91-
networkType,
92-
parseInt((blockDTO.block.version as number).toString(16).substr(2, 2), 16), // Tx version
93-
blockDTO.block.type,
94-
new UInt64(blockDTO.block.height),
95-
new UInt64(blockDTO.block.timestamp),
96-
new UInt64(blockDTO.block.difficulty),
97-
blockDTO.block.feeMultiplier,
98-
blockDTO.block.previousBlockHash,
99-
blockDTO.block.blockTransactionsHash,
100-
blockDTO.block.blockReceiptsHash,
101-
blockDTO.block.stateHash,
102-
extractBeneficiary(blockDTO, networkType),
103-
);
104-
}));
83+
return observableFrom(this.blockRoutesApi.getBlockByHeight(height)).pipe(
84+
map((response: { response: ClientResponse; body: BlockInfoDTO; } ) => {
85+
const blockDTO = response.body;
86+
const networkType = parseInt((blockDTO.block.version as number).toString(16).substr(0, 2), 16);
87+
return new BlockInfo(
88+
blockDTO.meta.hash,
89+
blockDTO.meta.generationHash,
90+
new UInt64(blockDTO.meta.totalFee),
91+
blockDTO.meta.numTransactions,
92+
blockDTO.block.signature,
93+
PublicAccount.createFromPublicKey(blockDTO.block.signer, networkType),
94+
networkType,
95+
parseInt((blockDTO.block.version as number).toString(16).substr(2, 2), 16), // Tx version
96+
blockDTO.block.type,
97+
new UInt64(blockDTO.block.height),
98+
new UInt64(blockDTO.block.timestamp),
99+
new UInt64(blockDTO.block.difficulty),
100+
blockDTO.block.feeMultiplier,
101+
blockDTO.block.previousBlockHash,
102+
blockDTO.block.blockTransactionsHash,
103+
blockDTO.block.blockReceiptsHash,
104+
blockDTO.block.stateHash,
105+
extractBeneficiary(blockDTO, networkType),
106+
);
107+
}),
108+
catchError((error) => throwError(this.errorHandling(error))),
109+
);
105110
}
106111

107112
/**
@@ -117,11 +122,14 @@ export class BlockHttp extends Http implements BlockRepository {
117122
this.queryParams(queryParams).pageSize,
118123
this.queryParams(queryParams).id,
119124
this.queryParams(queryParams).order))
120-
.pipe(map((transactionsDTO: TransactionInfoDTO[]) => {
125+
.pipe(map((response: { response: ClientResponse; body: TransactionInfoDTO[]; }) => {
126+
const transactionsDTO = response.body;
121127
return transactionsDTO.map((transactionDTO) => {
122128
return CreateTransactionFromDTO(transactionDTO);
123129
});
124-
}));
130+
}),
131+
catchError((error) => throwError(this.errorHandling(error))),
132+
);
125133
}
126134

127135
/**
@@ -132,31 +140,35 @@ export class BlockHttp extends Http implements BlockRepository {
132140
*/
133141
public getBlocksByHeightWithLimit(height: number, limit: LimitType = LimitType.N_25): Observable<BlockInfo[]> {
134142
return observableFrom(
135-
this.blockRoutesApi.getBlocksByHeightWithLimit(height, limit)).pipe(map((blocksDTO: BlockInfoDTO[]) => {
136-
return blocksDTO.map((blockDTO) => {
137-
const networkType = parseInt((blockDTO.block.version as number).toString(16).substr(0, 2), 16);
138-
return new BlockInfo(
139-
blockDTO.meta.hash,
140-
blockDTO.meta.generationHash,
141-
new UInt64(blockDTO.meta.totalFee),
142-
blockDTO.meta.numTransactions,
143-
blockDTO.block.signature,
144-
PublicAccount.createFromPublicKey(blockDTO.block.signer, networkType),
145-
networkType,
146-
parseInt((blockDTO.block.version as number).toString(16).substr(2, 2), 16), // Tx version
147-
blockDTO.block.type,
148-
new UInt64(blockDTO.block.height),
149-
new UInt64(blockDTO.block.timestamp),
150-
new UInt64(blockDTO.block.difficulty),
151-
blockDTO.block.feeMultiplier,
152-
blockDTO.block.previousBlockHash,
153-
blockDTO.block.blockTransactionsHash,
154-
blockDTO.block.blockReceiptsHash,
155-
blockDTO.block.stateHash,
156-
extractBeneficiary(blockDTO, networkType),
157-
);
158-
});
159-
}));
143+
this.blockRoutesApi.getBlocksByHeightWithLimit(height, limit)).pipe(
144+
map((response: { response: ClientResponse; body: BlockInfoDTO[]; }) => {
145+
const blocksDTO = response.body;
146+
return blocksDTO.map((blockDTO) => {
147+
const networkType = parseInt((blockDTO.block.version as number).toString(16).substr(0, 2), 16);
148+
return new BlockInfo(
149+
blockDTO.meta.hash,
150+
blockDTO.meta.generationHash,
151+
new UInt64(blockDTO.meta.totalFee),
152+
blockDTO.meta.numTransactions,
153+
blockDTO.block.signature,
154+
PublicAccount.createFromPublicKey(blockDTO.block.signer, networkType),
155+
networkType,
156+
parseInt((blockDTO.block.version as number).toString(16).substr(2, 2), 16), // Tx version
157+
blockDTO.block.type,
158+
new UInt64(blockDTO.block.height),
159+
new UInt64(blockDTO.block.timestamp),
160+
new UInt64(blockDTO.block.difficulty),
161+
blockDTO.block.feeMultiplier,
162+
blockDTO.block.previousBlockHash,
163+
blockDTO.block.blockTransactionsHash,
164+
blockDTO.block.blockReceiptsHash,
165+
blockDTO.block.stateHash,
166+
extractBeneficiary(blockDTO, networkType),
167+
);
168+
});
169+
}),
170+
catchError((error) => throwError(this.errorHandling(error))),
171+
);
160172
}
161173

162174
/**
@@ -171,14 +183,18 @@ export class BlockHttp extends Http implements BlockRepository {
171183
*/
172184
public getMerkleReceipts(height: number, hash: string): Observable<MerkleProofInfo> {
173185
return observableFrom(
174-
this.blockRoutesApi.getMerkleReceipts(height, hash)).pipe(map((merkleProofReceipt: MerkleProofInfoDTO) => {
175-
return new MerkleProofInfo(
176-
new MerkleProofInfoPayload(
177-
merkleProofReceipt.payload.merklePath!.map(
178-
(payload) => new MerklePathItem(payload.position, payload.hash))),
179-
merkleProofReceipt.type,
180-
);
181-
}));
186+
this.blockRoutesApi.getMerkleReceipts(height, hash)).pipe(
187+
map((response: { response: ClientResponse; body: MerkleProofInfoDTO; } ) => {
188+
const merkleProofReceipt = response.body;
189+
return new MerkleProofInfo(
190+
new MerkleProofInfoPayload(
191+
merkleProofReceipt.payload.merklePath!.map(
192+
(payload) => new MerklePathItem(payload.position, payload.hash))),
193+
merkleProofReceipt.type,
194+
);
195+
}),
196+
catchError((error) => throwError(this.errorHandling(error))),
197+
);
182198
}
183199

184200
/**
@@ -193,13 +209,18 @@ export class BlockHttp extends Http implements BlockRepository {
193209
*/
194210
public getMerkleTransaction(height: number, hash: string): Observable<MerkleProofInfo> {
195211
return observableFrom(
196-
this.blockRoutesApi.getMerkleReceipts(height, hash)).pipe(map((merkleProofTransaction: MerkleProofInfoDTO) => {
197-
return new MerkleProofInfo(
198-
new MerkleProofInfoPayload(
199-
merkleProofTransaction.payload.merklePath!.map((payload) => new MerklePathItem(payload.position, payload.hash))),
200-
merkleProofTransaction.type,
201-
);
202-
}));
212+
this.blockRoutesApi.getMerkleReceipts(height, hash)).pipe(
213+
map((response: { response: ClientResponse; body: MerkleProofInfoDTO; } ) => {
214+
const merkleProofTransaction = response.body;
215+
return new MerkleProofInfo(
216+
new MerkleProofInfoPayload(
217+
merkleProofTransaction.payload.merklePath!.map((payload) =>
218+
new MerklePathItem(payload.position, payload.hash))),
219+
merkleProofTransaction.type,
220+
);
221+
}),
222+
catchError((error) => throwError(this.errorHandling(error))),
223+
);
203224
}
204225

205226
/**
@@ -212,9 +233,11 @@ export class BlockHttp extends Http implements BlockRepository {
212233
return this.getNetworkTypeObservable().pipe(
213234
mergeMap((networkType) => observableFrom(
214235
this.blockRoutesApi.getBlockReceipts(height)).pipe(
215-
map((receiptDTO: StatementsDTO) => {
236+
map((response: { response: ClientResponse; body: StatementsDTO; }) => {
237+
const receiptDTO = response.body;
216238
return CreateStatementFromDTO(receiptDTO, networkType);
217239
}),
240+
catchError((error) => throwError(this.errorHandling(error))),
218241
),
219242
),
220243
);

src/infrastructure/ChainHttp.ts

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

17-
import {from as observableFrom, Observable} from 'rxjs';
18-
import {map} from 'rxjs/operators';
17+
import { ClientResponse } from 'http';
18+
import {from as observableFrom, Observable, throwError} from 'rxjs';
19+
import {catchError, map} from 'rxjs/operators';
1920
import {BlockchainScore} from '../model/blockchain/BlockchainScore';
2021
import {UInt64} from '../model/UInt64';
2122
import { BlockchainScoreDTO,
@@ -50,21 +51,29 @@ export class ChainHttp extends Http implements ChainRepository {
5051
* @returns Observable<UInt64>
5152
*/
5253
public getBlockchainHeight(): Observable<UInt64> {
53-
return observableFrom(this.chainRoutesApi.getBlockchainHeight()).pipe(map((heightDTO: HeightInfoDTO) => {
54-
return new UInt64(heightDTO.height);
55-
}));
54+
return observableFrom(this.chainRoutesApi.getBlockchainHeight()).pipe(
55+
map((response: { response: ClientResponse; body: HeightInfoDTO; } ) => {
56+
const heightDTO = response.body;
57+
return new UInt64(heightDTO.height);
58+
}),
59+
catchError((error) => throwError(this.errorHandling(error))),
60+
);
5661
}
5762

5863
/**
5964
* Gets current blockchain score
6065
* @returns Observable<BlockchainScore>
6166
*/
6267
public getBlockchainScore(): Observable<BlockchainScore> {
63-
return observableFrom(this.chainRoutesApi.getBlockchainScore()).pipe(map((blockchainScoreDTO: BlockchainScoreDTO) => {
64-
return new BlockchainScore(
65-
new UInt64(blockchainScoreDTO.scoreLow),
66-
new UInt64(blockchainScoreDTO.scoreHigh),
67-
);
68-
}));
68+
return observableFrom(this.chainRoutesApi.getBlockchainScore()).pipe(
69+
map((response: { response: ClientResponse; body: BlockchainScoreDTO; } ) => {
70+
const blockchainScoreDTO = response.body;
71+
return new BlockchainScore(
72+
new UInt64(blockchainScoreDTO.scoreLow),
73+
new UInt64(blockchainScoreDTO.scoreHigh),
74+
);
75+
}),
76+
catchError((error) => throwError(this.errorHandling(error))),
77+
);
6978
}
7079
}

src/infrastructure/DiagnosticHttp.ts

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

17-
import {from as observableFrom, Observable} from 'rxjs';
18-
import {map} from 'rxjs/operators';
17+
import { ClientResponse } from 'http';
18+
import {from as observableFrom, Observable, throwError} from 'rxjs';
19+
import {catchError, map} from 'rxjs/operators';
1920
import {BlockchainStorageInfo} from '../model/blockchain/BlockchainStorageInfo';
2021
import { ServerInfo } from '../model/diagnostic/ServerInfo';
2122
import { DiagnosticRoutesApi, ServerDTO, StorageInfoDTO } from './api';
@@ -49,13 +50,17 @@ export class DiagnosticHttp extends Http implements DiagnosticRepository {
4950
*/
5051
public getDiagnosticStorage(): Observable<BlockchainStorageInfo> {
5152
return observableFrom(
52-
this.diagnosticRoutesApi.getDiagnosticStorage()).pipe(map((blockchainStorageInfoDTO: StorageInfoDTO) => {
53-
return new BlockchainStorageInfo(
54-
blockchainStorageInfoDTO.numBlocks,
55-
blockchainStorageInfoDTO.numTransactions,
56-
blockchainStorageInfoDTO.numAccounts,
57-
);
58-
}));
53+
this.diagnosticRoutesApi.getDiagnosticStorage()).pipe(
54+
map((response: { response: ClientResponse; body: StorageInfoDTO; } ) => {
55+
const blockchainStorageInfoDTO = response.body;
56+
return new BlockchainStorageInfo(
57+
blockchainStorageInfoDTO.numBlocks,
58+
blockchainStorageInfoDTO.numTransactions,
59+
blockchainStorageInfoDTO.numAccounts,
60+
);
61+
}),
62+
catchError((error) => throwError(this.errorHandling(error))),
63+
);
5964
}
6065

6166
/**
@@ -64,9 +69,13 @@ export class DiagnosticHttp extends Http implements DiagnosticRepository {
6469
*/
6570
public getServerInfo(): Observable<ServerInfo> {
6671
return observableFrom(
67-
this.diagnosticRoutesApi.getServerInfo()).pipe(map((serverDTO: ServerDTO) => {
68-
return new ServerInfo(serverDTO.serverInfo.restVersion,
69-
serverDTO.serverInfo.sdkVersion);
70-
}));
72+
this.diagnosticRoutesApi.getServerInfo()).pipe(
73+
map((response: { response: ClientResponse; body: ServerDTO; } ) => {
74+
const serverDTO = response.body;
75+
return new ServerInfo(serverDTO.serverInfo.restVersion,
76+
serverDTO.serverInfo.sdkVersion);
77+
}),
78+
catchError((error) => throwError(this.errorHandling(error))),
79+
);
7180
}
7281
}

src/infrastructure/Http.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,12 @@ export abstract class Http {
5757
order: queryParams ? queryParams.order : undefined,
5858
};
5959
}
60+
61+
errorHandling(error: any): Error {
62+
const formattedError = {
63+
statusCode: error.response.statusCode,
64+
errorDetails: error.response.body,
65+
};
66+
return new Error(JSON.stringify(formattedError));
67+
}
6068
}

0 commit comments

Comments
 (0)