@@ -23,6 +23,7 @@ import { MirrorNodeClientError } from './../errors/MirrorNodeClientError';
2323import { Logger } from "pino" ;
2424import constants from './../constants' ;
2525import { Histogram , Registry } from 'prom-client' ;
26+ import { formatRequestIdMessage } from '../../formatters' ;
2627
2728export interface ILimitOrderParams {
2829 limit ?: number ;
@@ -140,110 +141,123 @@ export class MirrorNodeClient {
140141 this . logger . info ( `Mirror Node client successfully configured to ${ this . baseUrl } ` ) ;
141142 }
142143
143- async request ( path : string , pathLabel : string , allowedErrorStatuses ?: number [ ] ) : Promise < any > {
144+ async request ( path : string , pathLabel : string , allowedErrorStatuses ?: number [ ] , requestId ?: string ) : Promise < any > {
144145 const start = Date . now ( ) ;
146+ const requestIdPrefix = formatRequestIdMessage ( requestId ) ;
145147 let ms ;
146148 try {
147149 const response = await this . client . get ( path ) ;
148150 ms = Date . now ( ) - start ;
149- this . logger . debug ( `[GET] ${ path } ${ response . status } ${ ms } ms` ) ;
151+ this . logger . debug ( `${ requestIdPrefix } [GET] ${ path } ${ response . status } ${ ms } ms` ) ;
150152 this . mirrorResponseHistogram . labels ( pathLabel , response . status ) . observe ( ms ) ;
151153 return response . data ;
152154 } catch ( error : any ) {
153155 ms = Date . now ( ) - start ;
154156 const effectiveStatusCode = error . response !== undefined ? error . response . status : MirrorNodeClient . unknownServerErrorHttpStatusCode ;
155157 this . mirrorResponseHistogram . labels ( pathLabel , effectiveStatusCode ) . observe ( ms ) ;
156- this . handleError ( error , path , effectiveStatusCode , allowedErrorStatuses ) ;
158+ this . handleError ( error , path , effectiveStatusCode , allowedErrorStatuses , requestId ) ;
157159 }
158160 return null ;
159161 }
160162
161- handleError ( error : any , path : string , effectiveStatusCode : number , allowedErrorStatuses ?: number [ ] ) {
163+ handleError ( error : any , path : string , effectiveStatusCode : number , allowedErrorStatuses ?: number [ ] , requestId ?: string ) {
164+ const requestIdPrefix = formatRequestIdMessage ( requestId ) ;
162165 if ( allowedErrorStatuses && allowedErrorStatuses . length ) {
163166 if ( error . response && allowedErrorStatuses . indexOf ( effectiveStatusCode ) !== - 1 ) {
164- this . logger . debug ( `[GET] ${ path } ${ effectiveStatusCode } status` ) ;
167+ this . logger . debug ( `${ requestIdPrefix } [GET] ${ path } ${ effectiveStatusCode } status` ) ;
165168 return null ;
166169 }
167170 }
168171
169- this . logger . error ( new Error ( error . message ) , `[GET] ${ path } ${ effectiveStatusCode } status` ) ;
172+ this . logger . error ( new Error ( error . message ) , `${ requestIdPrefix } [GET] ${ path } ${ effectiveStatusCode } status` ) ;
170173 throw new MirrorNodeClientError ( error . message , effectiveStatusCode ) ;
171174 }
172175
173- public async getAccountLatestTransactionByAddress ( idOrAliasOrEvmAddress : string ) : Promise < object > {
176+ public async getAccountLatestTransactionByAddress ( idOrAliasOrEvmAddress : string , requestId ?: string ) : Promise < object > {
174177 return this . request ( `${ MirrorNodeClient . GET_ACCOUNTS_ENDPOINT } ${ idOrAliasOrEvmAddress } ?order=desc&limit=1` ,
175178 MirrorNodeClient . GET_ACCOUNTS_ENDPOINT ,
176- [ 400 ] ) ;
179+ [ 400 ] ,
180+ requestId ) ;
177181 }
178182
179- public async getAccount ( idOrAliasOrEvmAddress : string ) : Promise < object > {
183+ public async getAccount ( idOrAliasOrEvmAddress : string , requestId ?: string ) : Promise < object > {
180184 return this . request ( `${ MirrorNodeClient . GET_ACCOUNTS_ENDPOINT } ${ idOrAliasOrEvmAddress } ` ,
181185 MirrorNodeClient . GET_ACCOUNTS_ENDPOINT ,
182- [ 400 , 404 ] ) ;
186+ [ 400 , 404 ] ,
187+ requestId ) ;
183188 }
184189
185- public async getBlock ( hashOrBlockNumber : string | number ) {
190+ public async getBlock ( hashOrBlockNumber : string | number , requestId ?: string ) {
186191 return this . request ( `${ MirrorNodeClient . GET_BLOCK_ENDPOINT } ${ hashOrBlockNumber } ` ,
187192 MirrorNodeClient . GET_BLOCK_ENDPOINT ,
188- [ 400 ] ) ;
193+ [ 400 ] ,
194+ requestId ) ;
189195 }
190196
191- public async getBlocks ( blockNumber ?: number | string [ ] , timestamp ?: string , limitOrderParams ?: ILimitOrderParams ) {
197+ public async getBlocks ( blockNumber ?: number | string [ ] , timestamp ?: string , limitOrderParams ?: ILimitOrderParams , requestId ?: string ) {
192198 const queryParamObject = { } ;
193199 this . setQueryParam ( queryParamObject , 'block.number' , blockNumber ) ;
194200 this . setQueryParam ( queryParamObject , 'timestamp' , timestamp ) ;
195201 this . setLimitOrderParams ( queryParamObject , limitOrderParams ) ;
196202 const queryParams = this . getQueryParams ( queryParamObject ) ;
197203 return this . request ( `${ MirrorNodeClient . GET_BLOCKS_ENDPOINT } ${ queryParams } ` ,
198204 MirrorNodeClient . GET_BLOCKS_ENDPOINT ,
199- [ 400 , 404 ] ) ;
205+ [ 400 , 404 ] ,
206+ requestId ) ;
200207 }
201208
202- public async getContract ( contractIdOrAddress : string ) {
209+ public async getContract ( contractIdOrAddress : string , requestId ?: string ) {
203210 return this . request ( `${ MirrorNodeClient . GET_CONTRACT_ENDPOINT } ${ contractIdOrAddress } ` ,
204211 MirrorNodeClient . GET_CONTRACT_ENDPOINT ,
205- [ 400 , 404 ] ) ;
212+ [ 400 , 404 ] ,
213+ requestId ) ;
206214 }
207215
208- public async getContractResult ( transactionIdOrHash : string ) {
216+ public async getContractResult ( transactionIdOrHash : string , requestId ?: string ) {
209217 return this . request ( `${ MirrorNodeClient . GET_CONTRACT_RESULT_ENDPOINT } ${ transactionIdOrHash } ` ,
210218 MirrorNodeClient . GET_CONTRACT_RESULT_ENDPOINT ,
211- [ 400 , 404 ] ) ;
219+ [ 400 , 404 ] ,
220+ requestId ) ;
212221 }
213222
214- public async getContractResults ( contractResultsParams ?: IContractResultsParams , limitOrderParams ?: ILimitOrderParams ) {
223+ public async getContractResults ( contractResultsParams ?: IContractResultsParams , limitOrderParams ?: ILimitOrderParams , requestId ?: string ) {
215224 const queryParamObject = { } ;
216225 this . setContractResultsParams ( queryParamObject , contractResultsParams ) ;
217226 this . setLimitOrderParams ( queryParamObject , limitOrderParams ) ;
218227 const queryParams = this . getQueryParams ( queryParamObject ) ;
219228 return this . request ( `${ MirrorNodeClient . GET_CONTRACT_RESULTS_ENDPOINT } ${ queryParams } ` ,
220229 MirrorNodeClient . GET_CONTRACT_RESULTS_ENDPOINT ,
221- [ 400 ] ) ;
230+ [ 400 ] ,
231+ requestId ) ;
222232 }
223233
224- public async getContractResultsDetails ( contractId : string , timestamp : string ) {
234+ public async getContractResultsDetails ( contractId : string , timestamp : string , requestId ?: string ) {
225235 return this . request ( `${ this . getContractResultsDetailsByContractIdAndTimestamp ( contractId , timestamp ) } ` ,
226236 MirrorNodeClient . GET_CONTRACT_RESULTS_DETAILS_BY_CONTRACT_ID_ENDPOINT ,
227- [ 400 ] ) ;
237+ [ 400 ] ,
238+ requestId ) ;
228239 }
229240
230241 public async getContractResultsByAddress (
231242 contractIdOrAddress : string ,
232243 contractResultsParams ?: IContractResultsParams ,
233- limitOrderParams ?: ILimitOrderParams ) {
244+ limitOrderParams ?: ILimitOrderParams ,
245+ requestId ?: string ) {
234246 const queryParamObject = { } ;
235247 this . setContractResultsParams ( queryParamObject , contractResultsParams ) ;
236248 this . setLimitOrderParams ( queryParamObject , limitOrderParams ) ;
237249 const queryParams = this . getQueryParams ( queryParamObject ) ;
238250 return this . request ( `${ MirrorNodeClient . getContractResultsByAddressPath ( contractIdOrAddress ) } ${ queryParams } ` ,
239251 MirrorNodeClient . GET_CONTRACT_RESULTS_BY_ADDRESS_ENDPOINT ,
240- [ 400 ] ) ;
252+ [ 400 ] ,
253+ requestId ) ;
241254 }
242255
243- public async getContractResultsByAddressAndTimestamp ( contractIdOrAddress : string , timestamp : string ) {
256+ public async getContractResultsByAddressAndTimestamp ( contractIdOrAddress : string , timestamp : string , requestId ?: string ) {
244257 return this . request ( `${ MirrorNodeClient . getContractResultsByAddressPath ( contractIdOrAddress ) } /${ timestamp } ` ,
245258 MirrorNodeClient . GET_CONTRACT_RESULTS_BY_ADDRESS_ENDPOINT ,
246- [ 206 , 400 , 404 ] ) ;
259+ [ 206 , 400 , 404 ] ,
260+ requestId ) ;
247261 }
248262
249263 private prepareLogsParams (
@@ -265,17 +279,20 @@ export class MirrorNodeClient {
265279
266280 public async getContractResultsLogs (
267281 contractLogsResultsParams ?: IContractLogsResultsParams ,
268- limitOrderParams ?: ILimitOrderParams ) {
282+ limitOrderParams ?: ILimitOrderParams ,
283+ requestId ?: string ) {
269284 const queryParams = this . prepareLogsParams ( contractLogsResultsParams , limitOrderParams ) ;
270285 return this . request ( `${ MirrorNodeClient . GET_CONTRACT_RESULT_LOGS_ENDPOINT } ${ queryParams } ` ,
271286 MirrorNodeClient . GET_CONTRACT_RESULT_LOGS_ENDPOINT ,
272- [ 400 , 404 ] ) ;
287+ [ 400 , 404 ] ,
288+ requestId ) ;
273289 }
274290
275291 public async getContractResultsLogsByAddress (
276292 address : string ,
277293 contractLogsResultsParams ?: IContractLogsResultsParams ,
278- limitOrderParams ?: ILimitOrderParams
294+ limitOrderParams ?: ILimitOrderParams ,
295+ requestId ?: string
279296 ) {
280297 const queryParams = this . prepareLogsParams ( contractLogsResultsParams , limitOrderParams ) ;
281298 const apiEndpoint = MirrorNodeClient . GET_CONTRACT_RESULT_LOGS_BY_ADDRESS_ENDPOINT . replace (
@@ -284,34 +301,37 @@ export class MirrorNodeClient {
284301 ) ;
285302 return this . request ( `${ apiEndpoint } ${ queryParams } ` ,
286303 MirrorNodeClient . GET_CONTRACT_RESULT_LOGS_BY_ADDRESS_ENDPOINT ,
287- [ 400 , 404 ] ) ;
304+ [ 400 , 404 ] ,
305+ requestId ) ;
288306 }
289307
290- public async getLatestBlock ( ) {
291- return this . getBlocks ( undefined , undefined , this . getLimitOrderQueryParam ( 1 , MirrorNodeClient . ORDER . DESC ) ) ;
308+ public async getLatestBlock ( requestId ?: string ) {
309+ return this . getBlocks ( undefined , undefined , this . getLimitOrderQueryParam ( 1 , MirrorNodeClient . ORDER . DESC ) , requestId ) ;
292310 }
293311
294312 public getLimitOrderQueryParam ( limit : number , order : string ) : ILimitOrderParams {
295313 return { limit : limit , order : order } ;
296314 }
297315
298- public async getNetworkExchangeRate ( timestamp ?: string ) {
316+ public async getNetworkExchangeRate ( timestamp ?: string , requestId ?: string ) {
299317 const queryParamObject = { } ;
300318 this . setQueryParam ( queryParamObject , 'timestamp' , timestamp ) ;
301319 const queryParams = this . getQueryParams ( queryParamObject ) ;
302320 return this . request ( `${ MirrorNodeClient . GET_NETWORK_EXCHANGERATE_ENDPOINT } ${ queryParams } ` ,
303321 MirrorNodeClient . GET_NETWORK_EXCHANGERATE_ENDPOINT ,
304- [ 400 , 404 ] ) ;
322+ [ 400 , 404 ] ,
323+ requestId ) ;
305324 }
306325
307- public async getNetworkFees ( timestamp ?: string , order ?: string ) {
326+ public async getNetworkFees ( timestamp ?: string , order ?: string , requestId ?: string ) {
308327 const queryParamObject = { } ;
309328 this . setQueryParam ( queryParamObject , 'timestamp' , timestamp ) ;
310329 this . setQueryParam ( queryParamObject , 'order' , order ) ;
311330 const queryParams = this . getQueryParams ( queryParamObject ) ;
312331 return this . request ( `${ MirrorNodeClient . GET_NETWORK_FEES_ENDPOINT } ${ queryParams } ` ,
313332 MirrorNodeClient . GET_NETWORK_FEES_ENDPOINT ,
314- [ 400 , 404 ] ) ;
333+ [ 400 , 404 ] ,
334+ requestId ) ;
315335 }
316336
317337 private static getContractResultsByAddressPath ( address : string ) {
@@ -362,15 +382,15 @@ export class MirrorNodeClient {
362382 }
363383 }
364384
365- public async resolveEntityType ( entityIdentifier : string ) {
366- const contractResult = await this . getContract ( entityIdentifier ) ;
385+ public async resolveEntityType ( entityIdentifier : string , requestId ?: string ) {
386+ const contractResult = await this . getContract ( entityIdentifier , requestId ) ;
367387 if ( contractResult ) {
368388 return {
369389 type : constants . TYPE_CONTRACT ,
370390 entity : contractResult
371391 } ;
372392 }
373- const accountResult = await this . getAccount ( entityIdentifier ) ;
393+ const accountResult = await this . getAccount ( entityIdentifier , requestId ) ;
374394 if ( accountResult ) {
375395 return {
376396 type : constants . TYPE_ACCOUNT ,
0 commit comments