From af59f4009389ccbc1627072704e895b1a5a030ff Mon Sep 17 00:00:00 2001 From: Pedro Figueiredo Date: Tue, 7 Jan 2025 13:32:27 +0000 Subject: [PATCH] Add new completion_time_onchain property to Transaction Finalized events --- app/scripts/lib/transaction/metrics.ts | 34 +++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/app/scripts/lib/transaction/metrics.ts b/app/scripts/lib/transaction/metrics.ts index ee09d60259cd..26d2f3d748a1 100644 --- a/app/scripts/lib/transaction/metrics.ts +++ b/app/scripts/lib/transaction/metrics.ts @@ -33,6 +33,7 @@ import { TRANSACTION_ENVELOPE_TYPE_NAMES, } from '../../../../shared/lib/transactions-controller-utils'; import { + hexToDecimal, hexWEIToDecETH, hexWEIToDecGWEI, } from '../../../../shared/modules/conversion.utils'; @@ -229,12 +230,19 @@ export const handleTransactionConfirmed = async ( extraParams.gas_used = txReceipt?.gasUsed; - const { submittedTime } = transactionMeta; + const { submittedTime, blockTimestamp } = transactionMeta; if (submittedTime) { extraParams.completion_time = getTransactionCompletionTime(submittedTime); } + if (submittedTime && blockTimestamp) { + extraParams.completion_time_onchain = getTransactionOnchainCompletionTime( + submittedTime, + blockTimestamp, + ); + } + if (txReceipt?.status === '0x0') { extraParams.status = METRICS_STATUS_FAILED; } @@ -1133,6 +1141,30 @@ function getTransactionCompletionTime(submittedTime: number) { return Math.round((Date.now() - submittedTime) / 1000).toString(); } +/** + * Returns number of seconds (rounded to the hundredths) between submitted time + * and the block timestamp. + * + * @param submittedTime - The UNIX timestamp in milliseconds in which the + * transaction has been submitted + * @param blockTimestamp - The UNIX timestamp in seconds in hexadecimal in which + * the transaction has been confirmed in a block + */ +function getTransactionOnchainCompletionTime( + submittedTime: number, + blockTimestamp: string, +): string { + const DECIMAL_DIGITS = 2; + + return ( + Math.round( + (Number(hexToDecimal(blockTimestamp)) - submittedTime / 1000) * + 10 ** DECIMAL_DIGITS, + ) / + 10 ** DECIMAL_DIGITS + ).toString(); +} + /** * The allowance amount in relation to the dapp proposed amount for specific token *