Skip to content

Commit 5d0e576

Browse files
authored
Add TransactionRecordQuery calls to capture failed transaction costs (0.9) (#596)
Cherry pick #593 Add TransactionRecordQuery calls to capture fail transaction costs - update executeTransaction catch with TransactionRecordQuery logic - update executeGetTransactionRecord catch with TransactionRecordQuery logic Signed-off-by: Nana Essilfie-Conduah <[email protected]>
1 parent a81e52f commit 5d0e576

File tree

4 files changed

+110
-48
lines changed

4 files changed

+110
-48
lines changed

package-lock.json

Lines changed: 47 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/relay/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"test": "nyc ts-mocha --recursive ./tests/**/*.spec.ts --exit"
2828
},
2929
"dependencies": {
30-
"@hashgraph/sdk": "^2.18.0",
30+
"@hashgraph/sdk": "^2.18.3",
3131
"@keyvhq/core": "^1.6.9",
3232
"axios": "^0.26.1",
3333
"buffer": "^6.0.3",

packages/relay/src/lib/clients/sdkClient.ts

Lines changed: 61 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ import {
4444
FileInfoQuery,
4545
EthereumTransaction,
4646
EthereumTransactionData,
47+
TransactionRecordQuery,
48+
Hbar,
4749
} from '@hashgraph/sdk';
4850
import { BigNumber } from '@hashgraph/sdk/lib/Transfer';
4951
import { Logger } from "pino";
@@ -284,9 +286,7 @@ export class SDKClient {
284286
this.hbarLimiter.addExpense(cost, currentDateNow);
285287
}
286288

287-
this.logger.info(`${requestIdPrefix} Consensus Node ${query.constructor.name} response: ${query.paymentTransactionId} ${Status.Success._code}`);
288-
// local free queries will have a '0.0.0' accountId on transactionId
289-
this.logger.trace(`${requestIdPrefix} ${callerName} query cost: ${query._queryPayment}`);
289+
this.logger.info(`${requestIdPrefix} ${query.paymentTransactionId} ${callerName} ${query.constructor.name} status: ${Status.Success} (${Status.Success._code}), cost: ${query._queryPayment}`);
290290
this.captureMetrics(
291291
SDKClient.queryMode,
292292
query.constructor.name,
@@ -298,14 +298,13 @@ export class SDKClient {
298298
catch (e: any) {
299299
const cost = query._queryPayment?.toTinybars().toNumber();
300300
const sdkClientError = new SDKClientError(e);
301-
this.logger.debug(`${requestIdPrefix} Consensus Node query response: ${query.constructor.name} ${sdkClientError.status}`);
302301
this.captureMetrics(
303302
SDKClient.queryMode,
304303
query.constructor.name,
305304
sdkClientError.status,
306305
cost,
307306
callerName);
308-
this.logger.trace(`${requestIdPrefix} ${query.paymentTransactionId} ${callerName} query cost: ${query._queryPayment}`);
307+
this.logger.trace(`${requestIdPrefix} ${query.paymentTransactionId} ${callerName} ${query.constructor.name} status: ${sdkClientError.status} (${sdkClientError.status._code}), cost: ${query._queryPayment}`);
309308
if (cost) {
310309
this.hbarLimiter.addExpense(cost, currentDateNow);
311310
}
@@ -329,19 +328,39 @@ export class SDKClient {
329328

330329
this.logger.info(`${requestIdPrefix} Execute ${transactionType} transaction`);
331330
const resp = await transaction.execute(this.clientMain);
332-
this.logger.info(`${requestIdPrefix} Consensus Node ${transactionType} transaction response: ${resp.transactionId.toString()} ${Status.Success._code}`);
331+
this.logger.info(`${requestIdPrefix} ${resp.transactionId} ${callerName} ${transactionType} status: ${Status.Success} (${Status.Success._code})`);
333332
return resp;
334333
}
335334
catch (e: any) {
336335
const sdkClientError = new SDKClientError(e);
337-
this.logger.info(`${requestIdPrefix} Consensus Node ${transactionType} transaction response: ${sdkClientError.status}`);
338-
this.captureMetrics(
339-
SDKClient.transactionMode,
340-
transactionType,
341-
sdkClientError.status,
342-
0,
343-
callerName);
336+
let transactionFee: number | Hbar = 0;
337+
338+
// if valid network error utilize transaction id
339+
if (sdkClientError.isValidNetworkError()) {
340+
341+
try {
342+
const transctionRecord = await new TransactionRecordQuery()
343+
.setTransactionId(transaction.transactionId!)
344+
.setNodeAccountIds(transaction.nodeAccountIds!)
345+
.setValidateReceiptStatus(false)
346+
.execute(this.clientMain);
347+
transactionFee = transctionRecord.transactionFee;
348+
349+
this.captureMetrics(
350+
SDKClient.transactionMode,
351+
transactionType,
352+
sdkClientError.status,
353+
transactionFee.toTinybars().toNumber(),
354+
callerName);
355+
356+
this.hbarLimiter.addExpense(transactionFee.toTinybars().toNumber(), currentDateNow);
357+
} catch (err: any) {
358+
const recordQueryError = new SDKClientError(e);
359+
this.logger.error(recordQueryError, `${requestIdPrefix} Error raised during TransactionRecordQuery for ${transaction.transactionId}`);
360+
}
361+
}
344362

363+
this.logger.trace(`${requestIdPrefix} ${transaction.transactionId} ${callerName} ${transactionType} status: ${sdkClientError.status} (${sdkClientError.status._code}), cost: ${transactionFee}`);
345364
if (e instanceof JsonRpcError){
346365
throw predefined.HBAR_RATE_LIMIT_EXCEEDED;
347366
}
@@ -364,27 +383,47 @@ export class SDKClient {
364383
const transactionRecord: TransactionRecord = await resp.getRecord(this.clientMain);
365384
const cost = transactionRecord.transactionFee.toTinybars().toNumber();
366385
this.hbarLimiter.addExpense(cost, currentDateNow);
367-
this.logger.info(`${requestIdPrefix} Consensus Node ${transactionName} record response: ${resp.transactionId.toString()} ${Status.Success._code}`);
368-
this.logger.trace(`${requestIdPrefix} ${resp.transactionId.toString()} ${callerName} transaction cost: ${transactionRecord.transactionFee}`);
386+
this.logger.info(`${requestIdPrefix} ${resp.transactionId} ${callerName} ${transactionName} record status: ${Status.Success} (${Status.Success._code}), cost: ${transactionRecord.transactionFee}`);
369387
this.captureMetrics(
370388
SDKClient.transactionMode,
371389
transactionName,
372390
transactionRecord.receipt.status,
373391
cost,
374392
callerName);
393+
394+
this.hbarLimiter.addExpense(cost, currentDateNow);
395+
375396
return transactionRecord;
376397
}
377398
catch (e: any) {
378399
// capture sdk record retrieval errors and shorten familiar stack trace
379400
const sdkClientError = new SDKClientError(e);
380-
this.captureMetrics(
381-
SDKClient.transactionMode,
382-
transactionName,
383-
sdkClientError.status,
384-
0,
385-
callerName);
401+
let transactionFee: number | Hbar = 0;
402+
if (sdkClientError.isValidNetworkError()) {
403+
try {
404+
// pull transaction record for fee
405+
const transctionRecord = await new TransactionRecordQuery()
406+
.setTransactionId(resp.transactionId!)
407+
.setNodeAccountIds([resp.nodeId])
408+
.setValidateReceiptStatus(false)
409+
.execute(this.clientMain);
410+
transactionFee = transctionRecord.transactionFee;
411+
412+
this.captureMetrics(
413+
SDKClient.transactionMode,
414+
transactionName,
415+
sdkClientError.status,
416+
transactionFee.toTinybars().toNumber(),
417+
callerName);
418+
419+
this.hbarLimiter.addExpense(transactionFee.toTinybars().toNumber(), currentDateNow);
420+
} catch (err: any) {
421+
const recordQueryError = new SDKClientError(e);
422+
this.logger.error(recordQueryError, `${requestIdPrefix} Error raised during TransactionRecordQuery for ${resp.transactionId}`);
423+
}
424+
}
386425

387-
this.logger.debug(`${requestIdPrefix} Consensus Node ${transactionName} record response: ${resp.transactionId.toString()} ${sdkClientError.status}`);
426+
this.logger.debug(`${requestIdPrefix} ${resp.transactionId} ${callerName} ${transactionName} record status: ${sdkClientError.status} (${sdkClientError.status._code}), cost: ${transactionFee}`);
388427

389428
if (e instanceof JsonRpcError){
390429
throw predefined.HBAR_RATE_LIMIT_EXCEEDED;

packages/server/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
},
2222
"devDependencies": {
2323
"@hashgraph/hedera-local": "^1.1.0",
24-
"@hashgraph/sdk": "^2.18.0",
24+
"@hashgraph/sdk": "^2.18.3",
2525
"@koa/cors": "^3.1.0",
2626
"@types/chai": "^4.3.0",
2727
"@types/cors": "^2.8.12",

0 commit comments

Comments
 (0)