Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/utils/__tests__/activityTimeline.utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { formatActivityAmount } from '@/utils/activityTimeline.utils';
import { formatKeyPrice } from '@/utils/keyPriceDisplay.utils';

describe('formatActivityAmount', () => {
it('formats a buy amount with a negative sign', () => {
const amount = 10_000_000n; // 1 XLM
expect(formatActivityAmount(amount, 'buy')).toBe(`-${formatKeyPrice(amount)}`);
});

it('formats a sell amount with a positive sign', () => {
const amount = 10_000_000n; // 1 XLM
expect(formatActivityAmount(amount, 'sell')).toBe(`+${formatKeyPrice(amount)}`);
});

it('formats a zero amount with a positive sign', () => {
const amount = 0n;
expect(formatActivityAmount(amount, 'sell')).toBe('+0.00 XLM');
});

it('formats a zero amount for buy type with a positive sign', () => {
const amount = 0n;
expect(formatActivityAmount(amount, 'buy')).toBe('+0.00 XLM');
});

it('formats a small amount with 4 decimal places', () => {
const amount = 5_000_000n; // 0.5 XLM
expect(formatActivityAmount(amount, 'sell')).toBe(`+${formatKeyPrice(amount)}`);
});
});
15 changes: 15 additions & 0 deletions src/utils/activityTimeline.utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { formatKeyPrice } from '@/utils/keyPriceDisplay.utils';

interface TimelineEntryWithTimestamp {
timestamp?: number;
}
Expand Down Expand Up @@ -35,3 +37,16 @@ export const formatDateHeader = (date: Date) => {
});
}
};

/**
* Formats an activity amount (buy/sell) with a sign prefix and XLM suffix.
* Buys are prefixed with '-', sells with '+'.
*/
export function formatActivityAmount(amount: bigint, type: 'buy' | 'sell'): string {
if (amount === 0n) {
return '+0.00 XLM';
}
const sign = type === 'buy' ? '-' : '+';
const formatted = formatKeyPrice(amount);
return `${sign}${formatted}`;
}
Loading