From 295b1ee5e1febc00888f34e09bfdcee6aece2246 Mon Sep 17 00:00:00 2001 From: luisburigo Date: Mon, 26 Feb 2024 20:05:42 -0300 Subject: [PATCH] fix: get gas used of transaction --- src/modules/transaction/services.ts | 31 ++++++++++------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/src/modules/transaction/services.ts b/src/modules/transaction/services.ts index fec0db9f3..f8b46deb3 100644 --- a/src/modules/transaction/services.ts +++ b/src/modules/transaction/services.ts @@ -370,23 +370,21 @@ export class TransactionService implements ITransactionService { this.checkInvalidConditions(api_transaction); const tx_est = await provider.estimatePredicates(tx); - const coast = await provider.getTransactionCost(tx_est); const encodedTransaction = hexlify(tx_est.toTransactionBytes()); return await provider.operations .submit({ encodedTransaction }) - .then(({ submit: { id: transactionId } }) => { + .then(async ({ submit: { id: transactionId } }) => { + const transaction = await new TransactionResponse( + transactionId, + provider, + ).waitForResult(); const resume: ITransactionResume = { ...api_transaction.resume, witnesses: _witnesses, hash: transactionId.substring(2), // max_fee * gasUsed - gasUsed: ( - parseFloat(coast.maxFee.format({ precision: 12 })) * - parseFloat(coast.gasPrice.format({ precision: 12 })) - ) - .toFixed(12) - .toString(), + gasUsed: transaction.fee.format({ precision: 9 }), status: TransactionStatus.PROCESS_ON_CHAIN, }; return resume; @@ -418,17 +416,8 @@ export class TransactionService implements ITransactionService { result.status.type === TransactionProcessStatus.SUCCESS || result.status.type === TransactionProcessStatus.FAILED ) { - const transactionCoast = ( - parseFloat(api_transaction.resume.gasUsed) * - parseFloat(result.gasPrice) * - result.receipts - .map(item => - item.receiptType === 'SCRIPT_RESULT' ? parseFloat(item.gasUsed) : 0, - ) - .reduce((a, b) => a + b, 0) - ) - .toFixed(9) - .toString(); + const { fee } = await sender.waitForResult(); + const gasUsed = fee.format({ precision: 9 }); const resume = { ...api_transaction.resume, @@ -440,10 +429,10 @@ export class TransactionService implements ITransactionService { const _api_transaction: IUpdateTransactionPayload = { status: resume.status, sendTime: new Date(), - gasUsed: transactionCoast, + gasUsed, resume: { ...resume, - gasUsed: transactionCoast, + gasUsed, }, };