Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronfigueiredo committed Jan 9, 2025
1 parent af59f40 commit c372981
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 11 deletions.
62 changes: 62 additions & 0 deletions app/scripts/lib/transaction/metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
METRICS_STATUS_FAILED,
TransactionMetricsRequest,
} from './metrics';
import { decimalToHex } from '../../../../shared/modules/conversion.utils';

Check failure on line 39 in app/scripts/lib/transaction/metrics.test.ts

View workflow job for this annotation

GitHub Actions / Test lint / Test lint

`../../../../shared/modules/conversion.utils` import should occur before import of `./metrics`

const providerResultStub = {
eth_getCode: '0x123',
Expand Down Expand Up @@ -828,6 +829,67 @@ describe('Transaction metrics', () => {
mockTransactionMetricsRequest.finalizeEventFragment,
).toHaveBeenCalledWith(expectedUniqueId);
});

it.only('should create, update, finalize event fragment with completion_time_onchain', async () => {
mockTransactionMeta.txReceipt = {
gasUsed: '0x123',
status: '0x0',
};
mockTransactionMeta.blockTimestamp = decimalToHex(124);
mockTransactionMeta.submittedTime = 123123;

await handleTransactionConfirmed(mockTransactionMetricsRequest, {
...mockTransactionMeta,
actionId: mockActionId,
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any);

const expectedUniqueId = 'transaction-submitted-1';

expect(mockTransactionMetricsRequest.createEventFragment).toBeCalledTimes(
1,
);
expect(mockTransactionMetricsRequest.createEventFragment).toBeCalledWith({
actionId: mockActionId,
category: MetaMetricsEventCategory.Transactions,
successEvent: TransactionMetaMetricsEvent.finalized,
uniqueIdentifier: expectedUniqueId,
persist: true,
properties: expectedProperties,
sensitiveProperties: {
...expectedSensitiveProperties,
completion_time: expect.any(String),
completion_time_onchain: '0.88',
gas_used: '0.000000291',
status: METRICS_STATUS_FAILED,
},
});

expect(mockTransactionMetricsRequest.updateEventFragment).toBeCalledTimes(
1,
);
expect(mockTransactionMetricsRequest.updateEventFragment).toBeCalledWith(
expectedUniqueId,
{
properties: expectedProperties,
sensitiveProperties: {
...expectedSensitiveProperties,
completion_time: expect.any(String),
completion_time_onchain: '0.88',
gas_used: '0.000000291',
status: METRICS_STATUS_FAILED,
},
},
);

expect(
mockTransactionMetricsRequest.finalizeEventFragment,
).toBeCalledTimes(1);
expect(
mockTransactionMetricsRequest.finalizeEventFragment,
).toBeCalledWith(expectedUniqueId);
});
});

describe('handleTransactionDropped', () => {
Expand Down
22 changes: 11 additions & 11 deletions app/scripts/lib/transaction/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1145,24 +1145,24 @@ function getTransactionCompletionTime(submittedTime: number) {
* 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
* @param submittedTimeMs - The UNIX timestamp in milliseconds in which the
* transaction has been submitted
* @param blockTimestamp - The UNIX timestamp in seconds in hexadecimal in which
* @param blockTimestampHex - The UNIX timestamp in seconds in hexadecimal in which
* the transaction has been confirmed in a block
*/
function getTransactionOnchainCompletionTime(
submittedTime: number,
blockTimestamp: string,
submittedTimeMs: number,
blockTimestampHex: string,
): string {
const DECIMAL_DIGITS = 2;

return (
Math.round(
(Number(hexToDecimal(blockTimestamp)) - submittedTime / 1000) *
10 ** DECIMAL_DIGITS,
) /
10 ** DECIMAL_DIGITS
).toString();
const blockTimestampSeconds = Number(hexToDecimal(blockTimestampHex));
const completionTimeSeconds = blockTimestampSeconds - submittedTimeMs / 1000;
const completionTimeSecondsRoundedToThousands =
Math.round(completionTimeSeconds * 10 ** DECIMAL_DIGITS) /
10 ** DECIMAL_DIGITS;

return completionTimeSecondsRoundedToThousands.toString();
}

/**
Expand Down

0 comments on commit c372981

Please sign in to comment.