Skip to content

Commit

Permalink
NNS1-2987: Display block timestamp in transaction history
Browse files Browse the repository at this point in the history
  • Loading branch information
dskloetd committed Mar 26, 2024
1 parent d1f4d99 commit 9c12a2b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
7 changes: 5 additions & 2 deletions frontend/src/lib/utils/icp-transactions.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,14 @@ export const mapIcpTransaction = ({
});
const otherParty = isReceive ? txInfo.from : txInfo.to;

const blockTimestampNanos = fromNullable(transaction.transaction.timestamp)
?.timestamp_nanos;
const createdTimestampNanos = fromNullable(
transaction.transaction.created_at_time
)?.timestamp_nanos;
const timestampMilliseconds = nonNullish(createdTimestampNanos)
? Number(createdTimestampNanos) / NANO_SECONDS_IN_MILLISECOND
const timestampNanos = blockTimestampNanos ?? createdTimestampNanos;
const timestampMilliseconds = nonNullish(timestampNanos)
? Number(timestampNanos) / NANO_SECONDS_IN_MILLISECOND
: undefined;
const timestamp = nonNullish(timestampMilliseconds)
? new Date(timestampMilliseconds)
Expand Down
62 changes: 62 additions & 0 deletions frontend/src/tests/lib/utils/icp-transactions.utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
CREATE_CANISTER_MEMO,
TOP_UP_CANISTER_MEMO,
} from "$lib/constants/api.constants";
import { NANO_SECONDS_IN_MILLISECOND } from "$lib/constants/constants";
import type { UiTransaction } from "$lib/types/transaction";
import {
mapIcpTransaction,
Expand Down Expand Up @@ -253,6 +254,67 @@ describe("icp-transactions.utils", () => {
).toEqual(expectedUiTransaction);
});

describe("maps timestamps", () => {
const createdDate = new Date("2023-01-01T00:12:00.000Z");
const blockDate = new Date("2023-01-01T00:34:00.000Z");
const createTimestamps = {
timestamp_nanos:
BigInt(createdDate.getTime()) * BigInt(NANO_SECONDS_IN_MILLISECOND),
};
const blockTimestamps = {
timestamp_nanos:
BigInt(blockDate.getTime()) * BigInt(NANO_SECONDS_IN_MILLISECOND),
};

it("prefers block timestamp", () => {
const transaction = createTransaction({
operation: defaultTransferOperation,
});
transaction.transaction.created_at_time = [createTimestamps];
transaction.transaction.timestamp = [blockTimestamps];

const expectedUiTransaction: UiTransaction = {
...defaultUiTransaction,
timestamp: blockDate,
};

expect(
mapIcpTransaction({
transaction,
accountIdentifier: from,
toSelfTransaction: false,
neuronAccounts: new Set<string>(),
swapCanisterAccounts: new Set<string>(),
i18n: en,
})
).toEqual(expectedUiTransaction);
});

it("falls back on created timestamps", () => {
const transaction = createTransaction({
operation: defaultTransferOperation,
});
transaction.transaction.created_at_time = [createTimestamps];
transaction.transaction.timestamp = [];

const expectedUiTransaction: UiTransaction = {
...defaultUiTransaction,
timestamp: createdDate,
};

expect(
mapIcpTransaction({
transaction,
accountIdentifier: from,
toSelfTransaction: false,
neuronAccounts: new Set<string>(),
swapCanisterAccounts: new Set<string>(),
i18n: en,
})
).toEqual(expectedUiTransaction);
});
});

it("maps toSelf transaction as Received", () => {
const transaction = createTransaction({
operation: toSelfOperation,
Expand Down

0 comments on commit 9c12a2b

Please sign in to comment.