-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathtransactionTransforms.ts
More file actions
858 lines (780 loc) · 28 KB
/
transactionTransforms.ts
File metadata and controls
858 lines (780 loc) · 28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
import { BigNumber } from 'bignumber.js';
import {
TransactionMeta,
TransactionType,
} from '@metamask/transaction-controller';
import { strings } from '../../../../../locales/i18n';
import {
Funding,
Order,
OrderFill,
UserHistoryItem,
getPerpsDisplaySymbol,
} from '@metamask/perps-controller';
import {
FillType,
PerpsOrderTransactionStatus,
PerpsOrderTransactionStatusType,
PerpsTransaction,
} from '../types/transactionHistory';
import { formatOrderLabel } from './orderUtils';
import { getTokenTransferData } from '../../../Views/confirmations/utils/transaction-pay';
import { parseStandardTokenTransactionData } from '../../../Views/confirmations/utils/transaction';
import { calcTokenAmount } from '../../../../util/transactions';
import { ARBITRUM_USDC } from '../../../Views/confirmations/constants/perps';
/**
* Determines the close direction category for aggregation purposes.
* Returns a normalized direction string for grouping fills that should be aggregated together.
*
* @param direction - The fill direction string (e.g., "Close Long", "Close Short", "Sell")
* @returns A normalized close direction for grouping, or null if not a close fill
*/
function getCloseDirectionForAggregation(
direction: string | undefined,
): string | null {
if (!direction) return null;
const [part1, part2] = direction.split(' ');
// Handle standard close directions
if (part1 === 'Close') {
return `Close ${part2}`; // "Close Long" or "Close Short"
}
// Handle spot-perps and prelaunch markets that use "Sell" for closing
if (direction === 'Sell') {
return 'Sell';
}
// Handle auto-deleveraging as a closeable position
if (direction === 'Auto-Deleveraging') {
return 'Auto-Deleveraging';
}
// Not a close fill - don't aggregate
return null;
}
/**
* Aggregates fills that occur at the same timestamp for the same asset when closing positions.
* This handles cases where a stop loss or take profit order is split into multiple fills
* by HyperLiquid, ensuring users see the aggregate PnL instead of partial amounts.
*
* Aggregation criteria:
* - Same asset symbol
* - Same timestamp (truncated to the same second)
* - Same close direction (Close Long, Close Short, Sell, or Auto-Deleveraging)
*
* For aggregated fills:
* - Sizes are summed
* - PnLs are summed
* - Fees are summed
* - Price is calculated as VWAP (Volume Weighted Average Price)
* - First fill's orderId and metadata are preserved
* - detailedOrderType (Stop Loss, Take Profit) is preserved from any grouped fill
* - liquidation info is preserved from any grouped fill
*
* @param fills - Array of OrderFill objects to aggregate
* @returns Array of OrderFill objects with close fills aggregated by timestamp
*/
export function aggregateFillsByTimestamp(fills: OrderFill[]): OrderFill[] {
// Map to group fills by aggregation key
const aggregationMap = new Map<string, OrderFill[]>();
// Array to preserve non-aggregatable fills in order
const nonAggregatableFills: OrderFill[] = [];
// Group fills by asset + timestamp (truncated to second) + close direction
for (const fill of fills) {
const closeDirection = getCloseDirectionForAggregation(fill.direction);
if (closeDirection === null) {
// Not a close fill - don't aggregate, preserve as-is
nonAggregatableFills.push(fill);
continue;
}
// Create aggregation key: asset + timestamp (truncated to second) + close direction
const timestampSecond = Math.floor(fill.timestamp / 1000);
const aggregationKey = `${fill.symbol}-${timestampSecond}-${closeDirection}`;
const existingGroup = aggregationMap.get(aggregationKey);
if (existingGroup) {
existingGroup.push(fill);
} else {
aggregationMap.set(aggregationKey, [fill]);
}
}
// Build aggregated fills
const aggregatedFills: OrderFill[] = [];
for (const groupedFills of aggregationMap.values()) {
if (groupedFills.length === 1) {
// Only one fill in the group - no aggregation needed
aggregatedFills.push(groupedFills[0]);
continue;
}
// Aggregate multiple fills
const firstFill = groupedFills[0];
// Sum sizes, PnLs, and fees
let totalSize = BigNumber(0);
let totalPnl = BigNumber(0);
let totalFee = BigNumber(0);
let totalNotional = BigNumber(0); // For VWAP calculation: sum of (size * price)
// Preserve detailedOrderType and liquidation from any fill in the group
let aggregatedDetailedOrderType: string | undefined;
let aggregatedLiquidation: OrderFill['liquidation'];
let aggregatedStartPosition: string | undefined;
for (const fill of groupedFills) {
const size = BigNumber(fill.size);
const price = BigNumber(fill.price);
const pnl = BigNumber(fill.pnl || '0');
const fee = BigNumber(fill.fee || '0');
totalSize = totalSize.plus(size);
totalPnl = totalPnl.plus(pnl);
totalFee = totalFee.plus(fee);
totalNotional = totalNotional.plus(size.times(price));
// Preserve detailedOrderType from any fill that has it
if (fill.detailedOrderType && !aggregatedDetailedOrderType) {
aggregatedDetailedOrderType = fill.detailedOrderType;
}
// Preserve liquidation info from any fill that has it
if (fill.liquidation && !aggregatedLiquidation) {
aggregatedLiquidation = fill.liquidation;
}
// Use the startPosition from the first fill (represents position before any fills)
if (fill.startPosition && !aggregatedStartPosition) {
aggregatedStartPosition = fill.startPosition;
}
}
// Calculate VWAP: totalNotional / totalSize
const vwapPrice = totalSize.isZero()
? BigNumber(firstFill.price)
: totalNotional.dividedBy(totalSize);
// Create aggregated fill
const aggregatedFill: OrderFill = {
orderId: firstFill.orderId, // Use first fill's orderId
symbol: firstFill.symbol,
side: firstFill.side,
size: totalSize.toString(),
price: vwapPrice.toString(),
pnl: totalPnl.toString(),
direction: firstFill.direction,
fee: totalFee.toString(),
feeToken: firstFill.feeToken,
timestamp: firstFill.timestamp, // Use first fill's timestamp
startPosition: aggregatedStartPosition,
success: firstFill.success,
liquidation: aggregatedLiquidation,
orderType: firstFill.orderType,
detailedOrderType: aggregatedDetailedOrderType,
};
aggregatedFills.push(aggregatedFill);
}
// Combine aggregated and non-aggregatable fills, then sort by timestamp descending
const allFills = [...aggregatedFills, ...nonAggregatableFills];
allFills.sort((a, b) => b.timestamp - a.timestamp);
return allFills;
}
/**
* Merges REST and WebSocket fill arrays into a single deduplicated, sorted array.
*
* REST fills are added first; WS fills overwrite duplicates (fresher data).
* When a WS fill lacks `detailedOrderType` or `liquidation` that the REST fill has,
* the REST metadata is preserved so TP/SL pills remain visible on all screens.
*
* Dedup key: `orderId-timestamp-size-price`
*
* @param restFills - Historical fills from the REST API
* @param liveFills - Real-time fills from the WebSocket
* @returns Merged, deduplicated fills sorted by timestamp descending
*/
export function mergeOrderFills(
restFills: OrderFill[],
liveFills: OrderFill[],
): OrderFill[] {
const fillsMap = new Map<string, OrderFill>();
for (const fill of restFills) {
const key = `${fill.orderId}-${fill.timestamp}-${fill.size}-${fill.price}`;
fillsMap.set(key, fill);
}
for (const fill of liveFills) {
const key = `${fill.orderId}-${fill.timestamp}-${fill.size}-${fill.price}`;
const existing = fillsMap.get(key);
if (existing?.detailedOrderType && !fill.detailedOrderType) {
fillsMap.set(key, {
...fill,
detailedOrderType: existing.detailedOrderType,
...(existing.liquidation &&
!fill.liquidation && { liquidation: existing.liquidation }),
});
} else {
fillsMap.set(key, fill);
}
}
return Array.from(fillsMap.values()).sort(
(a, b) => b.timestamp - a.timestamp,
);
}
export interface WithdrawalRequest {
id: string;
timestamp: number;
amount: string;
asset: string;
txHash?: string;
status: 'pending' | 'bridging' | 'completed' | 'failed';
destination?: string;
withdrawalId?: string;
}
export interface DepositRequest {
id: string;
timestamp: number;
amount: string;
asset: string;
txHash?: string;
status: 'pending' | 'bridging' | 'completed' | 'failed';
source?: string;
depositId?: string;
}
/**
* Transform abstract OrderFill objects to PerpsTransaction format.
* Close fills that occur at the same timestamp for the same asset are automatically
* aggregated to show combined PnL (handles split stop loss/take profit orders).
*
* @param fills - Array of abstract OrderFill objects
* @returns Array of PerpsTransaction objects
*/
export function transformFillsToTransactions(
fills: OrderFill[],
): PerpsTransaction[] {
// Aggregate close fills that occur at the same timestamp for the same asset
// This handles split stop loss/take profit orders that execute as multiple fills
const aggregatedFills = aggregateFillsByTimestamp(fills);
return aggregatedFills.reduce((acc: PerpsTransaction[], fill) => {
const {
direction,
orderId,
symbol,
size,
price,
fee,
timestamp,
feeToken,
pnl,
liquidation,
detailedOrderType,
} = fill;
const [part1, part2] = direction ? direction.split(' ') : [];
const isOpened = part1 === 'Open';
const isClosed = part1 === 'Close';
const isFlipped = part2 === '>';
const isAutoDeleveraging = direction === 'Auto-Deleveraging';
// Handle spot-perps and prelaunch markets that use "Buy"/"Sell" instead of "Open Long"/"Close Short"
const isBuy = direction === 'Buy';
const isSell = direction === 'Sell';
let action = '';
let isPositive = false;
if (isOpened || isBuy) {
action = isBuy ? 'Bought' : 'Opened';
// Will be set based on fee calculation below
} else if (isClosed || isSell || isAutoDeleveraging) {
action = isSell ? 'Sold' : 'Closed';
// Will be set based on PnL calculation below
} else if (isFlipped) {
action = 'Flipped';
// Will be set based on calculation below
} else if (!direction) {
console.warn('Unknown fill direction', fill);
return acc;
} else if (direction === 'Spot Dust Conversion') {
// HL housekeeping — auto-conversion of spot dust to USDC, not a perps trade
return acc;
} else {
console.warn('Unhandled fill direction', direction);
return acc;
}
let amountBN = BigNumber(0);
let displayAmount = '';
let fillSize = size;
if (isFlipped) {
fillSize = BigNumber(fill.startPosition || '0')
.minus(fill.size)
.absoluteValue()
.toString();
}
// Calculate display amount based on action type
if (isOpened || isBuy) {
// For opening positions or buying: show fee paid (negative)
amountBN = BigNumber(fill.fee || 0);
displayAmount = `-$${Math.abs(amountBN.toNumber()).toFixed(2)}`;
isPositive = false; // Fee is always a cost
} else if (isClosed || isSell || isFlipped || isAutoDeleveraging) {
// For closing positions: show PnL minus fee
const pnlValue = BigNumber(fill.pnl || 0);
const feeValue = BigNumber(fill.fee || 0);
amountBN = pnlValue.minus(feeValue);
const netPnL = amountBN.toNumber();
// For display, show + for positive, - for negative, nothing for 0
if (netPnL > 0) {
displayAmount = `+$${Math.abs(netPnL).toFixed(2)}`;
isPositive = true;
} else if (netPnL < 0) {
displayAmount = `-$${Math.abs(netPnL).toFixed(2)}`;
isPositive = false;
} else {
displayAmount = `$${Math.abs(netPnL).toFixed(2)}`;
isPositive = true; // Treat break-even as positive (green)
}
} else {
// Fallback: show order size value
amountBN = BigNumber(fill.size).times(fill.price);
displayAmount = `$${Math.abs(amountBN.toNumber()).toFixed(2)}`;
isPositive = false; // Default to false for unknown cases
}
const isLiquidation = Boolean(liquidation);
const isTakeProfit = Boolean(detailedOrderType?.includes('Take Profit'));
const isStopLoss = Boolean(detailedOrderType?.includes('Stop'));
let title = '';
if (isBuy || isSell) {
// For Buy/Sell directions, just use the action ("Bought" or "Sold")
title = action;
} else if (isFlipped) {
title = `${action} ${direction?.toLowerCase() || ''}`;
} else if (isAutoDeleveraging) {
const startPositionNum = Number(fill.startPosition);
if (Number.isNaN(startPositionNum)) return acc;
const directionLabel =
Number(fill.startPosition) > 0
? strings('perps.market.long')
: strings('perps.market.short');
title = `${action} ${directionLabel?.toLowerCase() || ''}`;
} else {
title = `${action} ${part2?.toLowerCase() || ''}`;
}
let fillType = FillType.Standard;
if (isAutoDeleveraging) {
fillType = FillType.AutoDeleveraging;
} else if (isLiquidation) {
fillType = FillType.Liquidation;
} else if (isTakeProfit) {
fillType = FillType.TakeProfit;
} else if (isStopLoss) {
fillType = FillType.StopLoss;
}
acc.push({
id: `${orderId || 'fill'}-${timestamp}-${acc.length}`,
type: 'trade',
category: isOpened || isBuy ? 'position_open' : 'position_close',
title,
subtitle: `${size} ${getPerpsDisplaySymbol(symbol)}`,
timestamp,
asset: symbol,
fill: {
shortTitle:
isBuy || isSell
? action
: `${action} ${
isFlipped
? direction?.toLowerCase() || ''
: part2?.toLowerCase() || ''
}`,
// this is the amount that is displayed in the transaction view for what has been spent/gained
// it may be the fee spent or the pnl depending on the case
amount: displayAmount,
amountNumber: parseFloat(amountBN.toFixed(2)),
isPositive,
size: fillSize,
entryPrice: price,
pnl,
fee,
points: '0', // Points feature not activated yet
feeToken,
action,
liquidation,
fillType,
},
});
return acc;
}, []);
}
/**
* Transform abstract Order objects to PerpsTransaction format
* @param orders - Array of abstract Order objects
* @param fillSizeByOrderId - Optional map of orderId to total filled size (from actual fills).
* When provided, uses actual fill data to calculate accurate filled percentages.
* This is important because HyperLiquid's historical orders API returns sz=0 for all
* completed orders, making it impossible to calculate partial fill percentages without fill data.
* @returns Array of PerpsTransaction objects
*/
export function transformOrdersToTransactions(
orders: Order[],
fillSizeByOrderId?: Map<string, BigNumber>,
): PerpsTransaction[] {
return orders.map((order) => {
const {
orderId,
symbol,
orderType,
size,
originalSize,
price,
status,
timestamp,
} = order;
const isCancelled = status === 'canceled';
const isCompleted = status === 'filled';
const isOpened = status === 'open';
const isRejected = status === 'rejected';
const isTriggered = status === 'triggered';
// Use centralized order label formatting
const title = formatOrderLabel(order);
const subtitle = `${originalSize || '0'} ${getPerpsDisplaySymbol(symbol)}`;
const orderTypeSlug = orderType.toLowerCase().split(' ').join('_');
let orderStatusType: PerpsOrderTransactionStatusType =
PerpsOrderTransactionStatusType.Pending;
let statusText = PerpsOrderTransactionStatus.Queued;
if (isCompleted) {
orderStatusType = PerpsOrderTransactionStatusType.Filled;
statusText = PerpsOrderTransactionStatus.Filled;
} else if (isCancelled) {
orderStatusType = PerpsOrderTransactionStatusType.Canceled;
statusText = PerpsOrderTransactionStatus.Canceled;
} else if (isRejected) {
orderStatusType = PerpsOrderTransactionStatusType.Canceled; // Map rejected to canceled
statusText = PerpsOrderTransactionStatus.Rejected;
} else if (isTriggered) {
orderStatusType = PerpsOrderTransactionStatusType.Filled; // Map triggered to filled
statusText = PerpsOrderTransactionStatus.Triggered;
} else {
orderStatusType = PerpsOrderTransactionStatusType.Pending;
statusText = isOpened
? PerpsOrderTransactionStatus.Open
: PerpsOrderTransactionStatus.Queued;
}
// Calculate filled percentage - prefer actual fill data when available
let filledPercent: string;
const actualFilledSize = fillSizeByOrderId?.get(orderId);
if (actualFilledSize !== undefined) {
// Use actual fill data for accurate percentage
const origSize = BigNumber(originalSize);
if (origSize.isZero()) {
filledPercent = '0';
} else {
filledPercent = actualFilledSize
.dividedBy(origSize)
.multipliedBy(100)
.toFixed(0); // Round to whole number
}
} else if (isCompleted || isTriggered) {
// Filled/triggered orders are 100% filled
filledPercent = '100';
} else if (isCancelled || isRejected) {
// Canceled/rejected orders without fills = 0% filled
filledPercent = '0';
} else {
// Open/pending orders - use the order's size fields
const sizeIsZero = BigNumber(size).isEqualTo(0);
const originalSizeIsZero = BigNumber(originalSize).isZero();
if (sizeIsZero && originalSizeIsZero) {
// Position-bound TP/SL orders have no fixed size (both are 0)
// They're not filled yet - they're just tied to the position size
filledPercent = '0';
} else if (sizeIsZero) {
// Regular order with 0 remaining size = fully filled
filledPercent = '100';
} else {
// Partially filled order
filledPercent = BigNumber(originalSize)
.minus(size)
.dividedBy(originalSize)
.absoluteValue()
.multipliedBy(100)
.toString();
}
}
return {
id: `${orderId}-${timestamp}`,
type: 'order',
category: 'limit_order',
title,
subtitle,
timestamp,
asset: symbol,
order: {
text: statusText,
statusType: orderStatusType,
type: orderTypeSlug.includes('limit') ? 'limit' : 'market',
size: BigNumber(originalSize).multipliedBy(price).toString(),
limitPrice: price,
filled: `${filledPercent}%`,
},
};
});
}
/**
* Transform abstract Funding objects to PerpsTransaction format
* @param funding - Array of abstract Funding objects
* @returns Array of PerpsTransaction objects sorted by timestamp (newest first)
*/
export function transformFundingToTransactions(
funding: Funding[],
): PerpsTransaction[] {
return funding.map((fundingItem) => {
const { symbol, amountUsd, rate, timestamp } = fundingItem;
// Create safe amount strings
const isPositive = BigNumber(amountUsd).isGreaterThan(0);
const amountUSDC = `${isPositive ? '+' : '-'}$${BigNumber(amountUsd)
.absoluteValue()
.toString()}`;
return {
id: `funding-${timestamp}-${symbol}`,
type: 'funding',
category: 'funding_fee',
title: `${isPositive ? 'Received' : 'Paid'} funding fee`,
subtitle: getPerpsDisplaySymbol(symbol),
timestamp,
asset: symbol,
fundingAmount: {
isPositive,
fee: amountUSDC,
feeNumber: parseFloat(amountUsd),
rate: `${BigNumber(rate ?? '0')
.multipliedBy(100)
.toString()}%`,
},
};
});
}
/**
* Transform UserHistoryItem objects to PerpsTransaction format
* Only shows completed deposits/withdrawals (txHash not displayed in UI)
* @param userHistory - Array of UserHistoryItem objects (deposits/withdrawals)
* @returns Array of PerpsTransaction objects
*/
export function transformUserHistoryToTransactions(
userHistory: UserHistoryItem[],
): PerpsTransaction[] {
return userHistory
.filter((item) => item.status === 'completed')
.map((item) => {
const { id, timestamp, type, amount, asset, txHash, status } = item;
const isDeposit = type === 'deposit';
// Format amount with appropriate sign
const amountBN = BigNumber(amount);
const displayAmount = `${isDeposit ? '+' : '-'}$${amountBN.toFixed(2)}`;
// For completed transactions, status is always positive (green)
const statusText = strings(
'perps.transactions.activity.status_completed',
);
const title = isDeposit
? strings('perps.transactions.activity.deposited_amount', {
amount,
symbol: asset,
})
: strings('perps.transactions.activity.withdrew_amount', {
amount,
symbol: asset,
});
return {
id: `${type}-${id}`,
type: isDeposit ? 'deposit' : 'withdrawal',
category: isDeposit ? 'deposit' : 'withdrawal',
title,
subtitle: statusText,
timestamp,
asset,
depositWithdrawal: {
amount: displayAmount,
amountNumber: amountBN.toNumber(),
isPositive: isDeposit,
asset,
txHash: txHash || '',
status,
type: isDeposit ? 'deposit' : 'withdrawal',
},
};
});
}
/** Wallet transaction status to perps deposit/withdrawal status */
const WALLET_STATUS_TO_DEPOSIT_STATUS: Record<
string,
'completed' | 'failed' | 'pending' | 'bridging'
> = {
confirmed: 'completed',
failed: 'failed',
rejected: 'failed',
dropped: 'failed',
signed: 'pending',
submitted: 'pending',
approved: 'pending',
unapproved: 'pending',
pending: 'pending',
};
/**
* Transform wallet TransactionMeta (perpsDeposit / perpsDepositAndOrder) to PerpsTransaction format.
* Ensures wallet-originated perps deposits appear in the Perps activity Deposits tab.
* @param transactions - Array of TransactionMeta with type perpsDeposit or perpsDepositAndOrder
* @returns Array of PerpsTransaction objects with type 'deposit'
*/
export function transformWalletPerpsDepositsToTransactions(
transactions: TransactionMeta[],
): PerpsTransaction[] {
return transactions.map((tx) => {
const tokenData = getTokenTransferData(tx);
const decoded = tokenData?.data
? parseStandardTokenTransactionData(tokenData.data)
: undefined;
const amountWei = decoded?.args?._value?.toString?.();
const amountBN =
amountWei !== undefined
? new BigNumber(
calcTokenAmount(amountWei, ARBITRUM_USDC.decimals).toString(),
)
: new BigNumber(0);
const displayAmount = `+$${amountBN.toFixed(2)}`;
const status = WALLET_STATUS_TO_DEPOSIT_STATUS[tx.status] ?? 'pending';
const statusText =
status === 'completed'
? strings('perps.transactions.activity.status_completed')
: status === 'failed'
? strings('perps.transactions.activity.status_failed')
: strings('perps.transactions.activity.status_pending');
const title =
amountBN.isZero() || !amountWei
? strings('perps.transactions.activity.deposit_title')
: strings('perps.transactions.activity.deposited_amount', {
amount: amountBN.toFixed(2),
symbol: ARBITRUM_USDC.symbol,
});
return {
id: `wallet-deposit-${tx.id}`,
type: 'deposit' as const,
category: 'deposit' as const,
title,
subtitle: statusText,
timestamp: tx.time ?? 0,
asset: ARBITRUM_USDC.symbol,
depositWithdrawal: {
amount: displayAmount,
amountNumber: amountBN.toNumber(),
isPositive: true,
asset: ARBITRUM_USDC.symbol,
txHash: tx.hash ?? '',
status,
type: 'deposit' as const,
},
};
});
}
/**
* Transform WithdrawalRequest objects to PerpsTransaction format
* @param withdrawalRequests - Array of WithdrawalRequest objects
* @returns Array of PerpsTransaction objects
*/
export function transformWithdrawalRequestsToTransactions(
withdrawalRequests: WithdrawalRequest[],
): PerpsTransaction[] {
return withdrawalRequests.map((request) => {
const { id, timestamp, amount, asset, txHash, status } = request;
const amountBN = BigNumber(amount);
const displayAmount = `-$${amountBN.toFixed(2)}`;
const statusText =
status === 'completed'
? strings('perps.transactions.activity.status_completed')
: status === 'failed'
? strings('perps.transactions.activity.status_failed')
: strings('perps.transactions.activity.status_pending');
const title = amountBN.isZero()
? strings('perps.transactions.activity.withdrawal_title')
: strings('perps.transactions.activity.withdrew_amount', {
amount,
symbol: asset,
});
return {
id,
type: 'withdrawal' as const,
category: 'withdrawal' as const,
title,
subtitle: statusText,
timestamp,
asset,
depositWithdrawal: {
amount: displayAmount,
amountNumber: -amountBN.toNumber(),
isPositive: false,
asset,
txHash: txHash || '',
status,
type: 'withdrawal' as const,
},
};
});
}
/**
* Convert wallet TransactionMeta (perpsWithdraw) to WithdrawalRequest format
* so it can be passed to transformWithdrawalRequestsToTransactions.
* @param transactions - Array of TransactionMeta with type perpsWithdraw
* @returns Array of WithdrawalRequest objects
*/
export function walletPerpsWithdrawalsToRequests(
transactions: TransactionMeta[],
): WithdrawalRequest[] {
return transactions.map((tx) => {
const tokenData = getTokenTransferData(tx);
const decoded = tokenData?.data
? parseStandardTokenTransactionData(tokenData.data)
: undefined;
const amountWei = decoded?.args?._value?.toString?.();
const amountBN =
amountWei !== undefined
? new BigNumber(
calcTokenAmount(amountWei, ARBITRUM_USDC.decimals).toString(),
)
: new BigNumber(0);
return {
id: `wallet-withdrawal-${tx.id}`,
timestamp: tx.time ?? 0,
amount: amountBN.toFixed(2),
asset: ARBITRUM_USDC.symbol,
txHash: tx.hash,
status: WALLET_STATUS_TO_DEPOSIT_STATUS[tx.status] ?? 'pending',
};
});
}
/**
* Transform DepositRequest objects to PerpsTransaction format
* Only shows completed deposits (txHash not displayed in UI)
* @param depositRequests - Array of DepositRequest objects
* @returns Array of PerpsTransaction objects
*/
export function transformDepositRequestsToTransactions(
depositRequests: DepositRequest[],
): PerpsTransaction[] {
return depositRequests
.filter((request) => request.status === 'completed')
.map((request) => {
const { id, timestamp, amount, asset, txHash, status } = request;
// Format amount with positive sign for deposits
const amountBN = BigNumber(amount);
const displayAmount = `+$${amountBN.toFixed(2)}`;
// For completed deposits, status is always positive (green)
const statusText = strings(
'perps.transactions.activity.status_completed',
);
const isPositive = true;
// Create title based on whether we have the actual amount
const title =
amount === '0' || amount === '0.00'
? strings('perps.transactions.activity.deposit_title')
: strings('perps.transactions.activity.deposited_amount', {
amount,
symbol: asset,
});
return {
id: `deposit-${id}`,
type: 'deposit' as const,
category: 'deposit' as const,
title,
subtitle: statusText,
timestamp,
asset,
depositWithdrawal: {
amount: displayAmount,
amountNumber: amountBN.toNumber(),
isPositive,
asset,
txHash: txHash || '',
status,
type: 'deposit' as const,
},
};
});
}