diff --git a/src/utils/__tests__/activityTimeline.utils.test.ts b/src/utils/__tests__/activityTimeline.utils.test.ts new file mode 100644 index 0000000..7febfce --- /dev/null +++ b/src/utils/__tests__/activityTimeline.utils.test.ts @@ -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)}`); + }); +}); diff --git a/src/utils/activityTimeline.utils.ts b/src/utils/activityTimeline.utils.ts index 6102ca1..7c09c49 100644 --- a/src/utils/activityTimeline.utils.ts +++ b/src/utils/activityTimeline.utils.ts @@ -1,3 +1,5 @@ +import { formatKeyPrice } from '@/utils/keyPriceDisplay.utils'; + interface TimelineEntryWithTimestamp { timestamp?: number; } @@ -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}`; +}