@@ -62,6 +62,7 @@ const _ = require('lodash');
6262export class SDKClient {
6363 static transactionMode = 'TRANSACTION' ;
6464 static queryMode = 'QUERY' ;
65+ static recordMode = 'RECORD' ;
6566 /**
6667 * The client to use for connecting to the main consensus network. The account
6768 * associated with this client will pay for all operations on the main network.
@@ -95,6 +96,13 @@ export class SDKClient {
9596 // populate with consensusnode requests via SDK
9697 constructor ( clientMain : Client , logger : Logger , register : Registry ) {
9798 this . clientMain = clientMain ;
99+
100+ if ( process . env . CONSENSUS_MAX_EXECUTION_TIME ) {
101+ // sets the maximum time in ms for the SDK to wait when submitting
102+ // a transaction/query before throwing a TIMEOUT error
103+ this . clientMain = clientMain . setMaxExecutionTime ( Number ( process . env . CONSENSUS_MAX_EXECUTION_TIME ) ) ;
104+ }
105+
98106 this . logger = logger ;
99107 this . register = register ;
100108 this . operatorAccountId = clientMain . operatorAccountId ? clientMain . operatorAccountId . toString ( ) : 'UNKNOWN' ;
@@ -133,7 +141,7 @@ export class SDKClient {
133141
134142 const duration = parseInt ( process . env . HBAR_RATE_LIMIT_DURATION ! ) ;
135143 const total = parseInt ( process . env . HBAR_RATE_LIMIT_TINYBAR ! ) ;
136- this . hbarLimiter = new HbarLimit ( logger . child ( { name : 'hbar-rate-limit' } ) , Date . now ( ) , total , duration ) ;
144+ this . hbarLimiter = new HbarLimit ( logger . child ( { name : 'hbar-rate-limit' } ) , Date . now ( ) , total , duration , register ) ;
137145 }
138146
139147 async getAccountBalance ( account : string , callerName : string , requestId ?: string ) : Promise < AccountBalance > {
@@ -300,7 +308,7 @@ export class SDKClient {
300308 const requestIdPrefix = formatRequestIdMessage ( requestId ) ;
301309 const currentDateNow = Date . now ( ) ;
302310 try {
303- const shouldLimit = this . hbarLimiter . shouldLimit ( currentDateNow ) ;
311+ const shouldLimit = this . hbarLimiter . shouldLimit ( currentDateNow , SDKClient . queryMode , callerName ) ;
304312 if ( shouldLimit ) {
305313 throw predefined . HBAR_RATE_LIMIT_EXCEEDED ;
306314 }
@@ -348,6 +356,11 @@ export class SDKClient {
348356 if ( e instanceof JsonRpcError ) {
349357 throw predefined . HBAR_RATE_LIMIT_EXCEEDED ;
350358 }
359+
360+ if ( sdkClientError . isGrpcTimeout ( ) ) {
361+ throw predefined . REQUEST_TIMEOUT ;
362+ }
363+
351364 throw sdkClientError ;
352365 }
353366 } ;
@@ -357,7 +370,7 @@ export class SDKClient {
357370 const requestIdPrefix = formatRequestIdMessage ( requestId ) ;
358371 const currentDateNow = Date . now ( ) ;
359372 try {
360- const shouldLimit = this . hbarLimiter . shouldLimit ( currentDateNow ) ;
373+ const shouldLimit = this . hbarLimiter . shouldLimit ( currentDateNow , SDKClient . transactionMode , callerName ) ;
361374 if ( shouldLimit ) {
362375 throw predefined . HBAR_RATE_LIMIT_EXCEEDED ;
363376 }
@@ -411,7 +424,7 @@ export class SDKClient {
411424 if ( ! resp . getRecord ) {
412425 throw new SDKClientError ( { } , `${ requestIdPrefix } Invalid response format, expected record availability: ${ JSON . stringify ( resp ) } ` ) ;
413426 }
414- const shouldLimit = this . hbarLimiter . shouldLimit ( currentDateNow ) ;
427+ const shouldLimit = this . hbarLimiter . shouldLimit ( currentDateNow , SDKClient . recordMode , transactionName ) ;
415428 if ( shouldLimit ) {
416429 throw predefined . HBAR_RATE_LIMIT_EXCEEDED ;
417430 }
@@ -476,7 +489,6 @@ export class SDKClient {
476489 status ,
477490 caller )
478491 . observe ( resolvedCost ) ;
479- this . operatorAccountGauge . labels ( mode , type , this . operatorAccountId ) . dec ( resolvedCost ) ;
480492 } ;
481493
482494 /**
0 commit comments