2
2
3
3
import { ConfigService } from '@hashgraph/json-rpc-config-service/dist/services' ;
4
4
import {
5
- AccountId ,
6
5
Client ,
7
- ContractCallQuery ,
8
- ContractFunctionResult ,
9
- ContractId ,
10
6
EthereumTransaction ,
11
7
EthereumTransactionData ,
12
8
ExchangeRate ,
@@ -129,7 +125,7 @@ export class SDKClient {
129
125
* @returns {Promise<{ txResponse: TransactionResponse; fileId: FileId | null }> }
130
126
* @throws {SDKClientError } Throws an error if no file ID is created or if the preemptive fee check fails.
131
127
*/
132
- async submitEthereumTransaction (
128
+ public async submitEthereumTransaction (
133
129
transactionBuffer : Uint8Array ,
134
130
callerName : string ,
135
131
requestDetails : RequestDetails ,
@@ -187,137 +183,6 @@ export class SDKClient {
187
183
} ;
188
184
}
189
185
190
- /**
191
- * Submits a contract call query to a smart contract on the Hedera network.
192
- * @param {string } to - The address of the contract to call, in either Solidity or EVM format.
193
- * @param {string } data - The encoded function parameters for the contract call, in hexadecimal format.
194
- * @param {number } gas - The amount of gas to use for the contract call.
195
- * @param {string } from - The address of the sender in EVM format.
196
- * @param {string } callerName - The name of the caller for logging purposes.
197
- * @param {RequestDetails } requestDetails - The request details for logging and tracking.
198
- * @returns {Promise<ContractFunctionResult> } The result of the contract function call.
199
- * @throws {SDKClientError } Throws an SDK client error if the contract call query fails.
200
- */
201
- async submitContractCallQuery (
202
- to : string ,
203
- data : string ,
204
- gas : number ,
205
- from : string ,
206
- callerName : string ,
207
- requestDetails : RequestDetails ,
208
- ) : Promise < ContractFunctionResult > {
209
- const contract = SDKClient . prune0x ( to ) ;
210
- const contractId = contract . startsWith ( '00000000000' )
211
- ? ContractId . fromSolidityAddress ( contract )
212
- : ContractId . fromEvmAddress ( 0 , 0 , contract ) ;
213
-
214
- const contractCallQuery = new ContractCallQuery ( ) . setContractId ( contractId ) . setGas ( gas ) ;
215
-
216
- // data is optional and can be omitted in which case fallback function will be employed
217
- if ( data ) {
218
- contractCallQuery . setFunctionParameters ( Buffer . from ( SDKClient . prune0x ( data ) , 'hex' ) ) ;
219
- }
220
-
221
- if ( from ) {
222
- contractCallQuery . setSenderAccountId ( AccountId . fromEvmAddress ( 0 , 0 , from ) ) ;
223
- }
224
-
225
- if ( this . clientMain . operatorAccountId !== null ) {
226
- contractCallQuery . setPaymentTransactionId ( TransactionId . generate ( this . clientMain . operatorAccountId ) ) ;
227
- }
228
-
229
- return this . executeQuery ( contractCallQuery , this . clientMain , callerName , to , requestDetails , from ) ;
230
- }
231
-
232
- /**
233
- * Submits a contract call query with retries in case of timeout errors.
234
- * @param {string } to - The address of the contract to call.
235
- * @param {string } data - The data to send with the contract call.
236
- * @param {number } gas - The amount of gas to use for the contract call.
237
- * @param {string } from - The address from which the contract call is made.
238
- * @param {string } callerName - The name of the caller for logging purposes.
239
- * @param {RequestDetails } requestDetails - The request details for logging and tracking.
240
- * @returns {Promise<ContractFunctionResult> } The result of the contract function call.
241
- * @throws {JsonRpcError } Throws an error if the error is a JSON-RPC error.
242
- * @throws {SDKClientError } Throws an SDK client error if the error is not a timeout error or if the retries are exhausted.
243
- */
244
- async submitContractCallQueryWithRetry (
245
- to : string ,
246
- data : string ,
247
- gas : number ,
248
- from : string ,
249
- callerName : string ,
250
- requestDetails : RequestDetails ,
251
- ) : Promise < ContractFunctionResult > {
252
- let retries = 0 ;
253
- let resp ;
254
- while ( ConfigService . get ( 'CONTRACT_QUERY_TIMEOUT_RETRIES' ) > retries ) {
255
- try {
256
- resp = await this . submitContractCallQuery ( to , data , gas , from , callerName , requestDetails ) ;
257
- return resp ;
258
- } catch ( e : any ) {
259
- const sdkClientError = new SDKClientError ( e , e . message ) ;
260
- if ( sdkClientError . isTimeoutExceeded ( ) ) {
261
- const delay = retries * 1000 ;
262
- if ( this . logger . isLevelEnabled ( 'trace' ) ) {
263
- this . logger . trace (
264
- `${ requestDetails . formattedRequestId } Contract call query failed with status ${ sdkClientError . message } . Retrying again after ${ delay } ms ...` ,
265
- ) ;
266
- }
267
- retries ++ ;
268
- await new Promise ( ( r ) => setTimeout ( r , delay ) ) ;
269
- continue ;
270
- }
271
- if ( e instanceof JsonRpcError ) {
272
- throw e ;
273
- }
274
- throw sdkClientError ;
275
- }
276
- }
277
- return resp ;
278
- }
279
-
280
- /**
281
- * Increases the query cost and retries the query execution if the initial attempt fails due to insufficient transaction fees.
282
- * @param {Query<any> } query - The query to be executed.
283
- * @param {Hbar } baseCost - The base cost of the query.
284
- * @param {Client } client - The client to use for executing the query.
285
- * @param {number } maxRetries - The maximum number of retries allowed.
286
- * @param {number } currentRetry - The current retry attempt number.
287
- * @param {RequestDetails } requestDetails - The request details for logging and tracking.
288
- * @returns {Promise<{resp: any, cost: Hbar}> } The response of the query execution and the cost used.
289
- * @throws Will throw an error if the maximum number of retries is exceeded or if the error is not due to insufficient transaction fees.
290
- */
291
- async increaseCostAndRetryExecution (
292
- query : Query < any > ,
293
- baseCost : Hbar ,
294
- client : Client ,
295
- maxRetries : number ,
296
- currentRetry : number ,
297
- requestDetails : RequestDetails ,
298
- ) : Promise < { resp : any ; cost : Hbar } > {
299
- const baseMultiplier = constants . QUERY_COST_INCREMENTATION_STEP ;
300
- const multiplier = Math . pow ( baseMultiplier , currentRetry ) ;
301
-
302
- const cost = Hbar . fromTinybars ( baseCost . _valueInTinybar . multipliedBy ( multiplier ) . toFixed ( 0 ) ) ;
303
-
304
- try {
305
- const resp = await query . setQueryPayment ( cost ) . execute ( client ) ;
306
- return { resp, cost } ;
307
- } catch ( e : any ) {
308
- const sdkClientError = new SDKClientError ( e , e . message ) ;
309
- if ( maxRetries > currentRetry && sdkClientError . isInsufficientTxFee ( ) ) {
310
- const newRetry = currentRetry + 1 ;
311
- this . logger . info (
312
- `${ requestDetails . formattedRequestId } Retrying query execution with increased cost, retry number: ${ newRetry } ` ,
313
- ) ;
314
- return await this . increaseCostAndRetryExecution ( query , baseCost , client , maxRetries , newRetry , requestDetails ) ;
315
- }
316
-
317
- throw e ;
318
- }
319
- }
320
-
321
186
/**
322
187
* Executes a Hedera query and handles potential errors.
323
188
* @param {Query<T> } query - The Hedera query to execute.
@@ -330,11 +195,10 @@ export class SDKClient {
330
195
* @throws {Error } Throws an error if the query fails or if rate limits are exceeded.
331
196
* @template T - The type of the query response.
332
197
*/
333
- async executeQuery < T > (
198
+ private async executeQuery < T > (
334
199
query : Query < T > ,
335
200
client : Client ,
336
201
callerName : string ,
337
- interactingEntity : string ,
338
202
requestDetails : RequestDetails ,
339
203
originalCallerAddress ?: string ,
340
204
) : Promise < T > {
@@ -347,20 +211,11 @@ export class SDKClient {
347
211
this . logger . info ( `${ requestIdPrefix } Execute ${ queryConstructorName } query.` ) ;
348
212
349
213
try {
350
- if ( query . paymentTransactionId ) {
351
- const baseCost = await query . getCost ( this . clientMain ) ;
352
- const res = await this . increaseCostAndRetryExecution ( query , baseCost , client , 3 , 0 , requestDetails ) ;
353
- queryResponse = res . resp ;
354
- queryCost = res . cost . toTinybars ( ) . toNumber ( ) ;
355
- } else {
356
- queryResponse = await query . execute ( client ) ;
357
- queryCost = query . _queryPayment ?. toTinybars ( ) . toNumber ( ) ;
358
- }
359
-
214
+ queryResponse = await query . execute ( client ) ;
215
+ queryCost = query . _queryPayment ?. toTinybars ( ) . toNumber ( ) ;
360
216
status = Status . Success . toString ( ) ;
361
-
362
217
this . logger . info (
363
- `${ requestIdPrefix } Successfully execute ${ queryConstructorName } query: paymentTransactionId= ${ query . paymentTransactionId } , callerName=${ callerName } , cost=${ queryCost } tinybars` ,
218
+ `${ requestIdPrefix } Successfully execute ${ queryConstructorName } query: callerName=${ callerName } , cost=${ queryCost } tinybars` ,
364
219
) ;
365
220
return queryResponse ;
366
221
} catch ( e : any ) {
@@ -369,16 +224,13 @@ export class SDKClient {
369
224
queryCost = query . _queryPayment ?. toTinybars ( ) . toNumber ( ) ;
370
225
status = sdkClientError . status . toString ( ) ;
371
226
372
- if ( e instanceof PrecheckStatusError && e . contractFunctionResult ?. errorMessage ) {
373
- throw predefined . CONTRACT_REVERT ( e . contractFunctionResult . errorMessage ) ;
374
- }
375
227
if ( sdkClientError . isGrpcTimeout ( ) ) {
376
228
throw predefined . REQUEST_TIMEOUT ;
377
229
}
378
230
379
231
if ( this . logger . isLevelEnabled ( 'debug' ) ) {
380
232
this . logger . debug (
381
- `${ requestIdPrefix } Fail to execute ${ queryConstructorName } query: paymentTransactionId= ${ query . paymentTransactionId } , callerName=${ callerName } , status=${ sdkClientError . status } (${ sdkClientError . status . _code } ), cost=${ queryCost } tinybars` ,
233
+ `${ requestIdPrefix } Fail to execute ${ queryConstructorName } callerName=${ callerName } , status=${ sdkClientError . status } (${ sdkClientError . status . _code } ), cost=${ queryCost } tinybars` ,
382
234
) ;
383
235
}
384
236
@@ -412,7 +264,7 @@ export class SDKClient {
412
264
* @returns {Promise<TransactionResponse> } - A promise that resolves to the transaction response.
413
265
* @throws {SDKClientError } - Throws if an error occurs during transaction execution.
414
266
*/
415
- async executeTransaction (
267
+ private async executeTransaction (
416
268
transaction : Transaction ,
417
269
callerName : string ,
418
270
interactingEntity : string ,
@@ -509,7 +361,7 @@ export class SDKClient {
509
361
* @returns {Promise<void> } - A promise that resolves when the batch execution is complete.
510
362
* @throws {SDKClientError } - Throws if an error occurs during batch transaction execution.
511
363
*/
512
- async executeAllTransaction (
364
+ private async executeAllTransaction (
513
365
transaction : FileAppendTransaction ,
514
366
callerName : string ,
515
367
interactingEntity : string ,
@@ -579,7 +431,7 @@ export class SDKClient {
579
431
* @returns {Promise<FileId | null> } A promise that resolves to the created file ID or null if the creation failed.
580
432
* @throws Will throw an error if the created file is empty or if any transaction fails during execution.
581
433
*/
582
- async createFile (
434
+ private async createFile (
583
435
callData : Uint8Array ,
584
436
client : Client ,
585
437
requestDetails : RequestDetails ,
@@ -646,7 +498,6 @@ export class SDKClient {
646
498
new FileInfoQuery ( ) . setFileId ( fileId ) ,
647
499
this . clientMain ,
648
500
callerName ,
649
- interactingEntity ,
650
501
requestDetails ,
651
502
originalCallerAddress ,
652
503
) ;
@@ -676,7 +527,7 @@ export class SDKClient {
676
527
* @returns {Promise<void> } - A promise that resolves when the operation is complete.
677
528
* @throws {any } - Throws an error if the file deletion fails.
678
529
*/
679
- async deleteFile (
530
+ public async deleteFile (
680
531
fileId : FileId ,
681
532
requestDetails : RequestDetails ,
682
533
callerName : string ,
@@ -702,7 +553,6 @@ export class SDKClient {
702
553
new FileInfoQuery ( ) . setFileId ( fileId ) ,
703
554
this . clientMain ,
704
555
callerName ,
705
- interactingEntity ,
706
556
requestDetails ,
707
557
originalCallerAddress ,
708
558
) ;
@@ -779,7 +629,7 @@ export class SDKClient {
779
629
* @param {string } accountId - The ID of the account for which the transfer sum is to be calculated.
780
630
* @returns {number } The total sum of transfer amounts for the specified account, in tinybars.
781
631
*/
782
- public getTransferAmountSumForAccount ( transactionRecord : TransactionRecord , accountId : string ) : number {
632
+ private getTransferAmountSumForAccount ( transactionRecord : TransactionRecord , accountId : string ) : number {
783
633
return transactionRecord . transfers
784
634
. filter ( ( transfer ) => transfer . accountId . toString ( ) === accountId && transfer . amount . isNegative ( ) )
785
635
. reduce ( ( acc , transfer ) => {
@@ -793,19 +643,9 @@ export class SDKClient {
793
643
* @param {number } exchangeRate - The exchange rate in cents used to convert the transaction query cost.
794
644
* @returns {number } - The transaction record query cost in tinybars.
795
645
*/
796
- public calculateTxRecordChargeAmount ( exchangeRate : ExchangeRate ) : number {
646
+ private calculateTxRecordChargeAmount ( exchangeRate : ExchangeRate ) : number {
797
647
const exchangeRateInCents = exchangeRate . exchangeRateInCents ;
798
648
const hbarToTinybar = Hbar . from ( 1 , HbarUnit . Hbar ) . toTinybars ( ) . toNumber ( ) ;
799
649
return Math . round ( ( constants . NETWORK_FEES_IN_CENTS . TRANSACTION_GET_RECORD / exchangeRateInCents ) * hbarToTinybar ) ;
800
650
}
801
-
802
- /**
803
- * Removes the '0x' prefix from a string if it exists.
804
- * @private
805
- * @param {string } input - The input string to be pruned.
806
- * @returns {string } The input string without the '0x' prefix.
807
- */
808
- private static prune0x ( input : string ) : string {
809
- return input . startsWith ( '0x' ) ? input . substring ( 2 ) : input ;
810
- }
811
651
}
0 commit comments