|
1 | 1 | import { Trade, Transaction } from 'ccxt'; |
2 | 2 | import { ExchangeTxDto } from '../dto/exchange-tx.dto'; |
| 3 | +import { |
| 4 | + ScryptBalanceTransaction, |
| 5 | + ScryptTrade, |
| 6 | + ScryptTradeSide, |
| 7 | + ScryptTradeStatus, |
| 8 | + ScryptTransactionStatus, |
| 9 | + ScryptTransactionType, |
| 10 | +} from '../dto/scrypt.dto'; |
3 | 11 | import { ExchangeTxType } from '../entities/exchange-tx.entity'; |
4 | 12 | import { ExchangeName } from '../enums/exchange.enum'; |
5 | 13 |
|
6 | 14 | export class ExchangeTxMapper { |
| 15 | + // --- CCXT TRANSACTIONS --- // |
7 | 16 | static mapDeposits(transactions: Transaction[], exchange: ExchangeName): ExchangeTxDto[] { |
8 | 17 | return transactions |
9 | 18 | .filter((d) => d.type === 'deposit') |
@@ -70,4 +79,76 @@ export class ExchangeTxMapper { |
70 | 79 | side: t.side, |
71 | 80 | })); |
72 | 81 | } |
| 82 | + |
| 83 | + // --- SCRYPT TRANSACTIONS --- // |
| 84 | + static mapScryptTransactions(transactions: ScryptBalanceTransaction[], exchange: ExchangeName): ExchangeTxDto[] { |
| 85 | + return transactions.map((t) => ({ |
| 86 | + exchange, |
| 87 | + type: this.mapScryptTransactionType(t.TransactionType), |
| 88 | + externalId: t.TransactionID, |
| 89 | + externalCreated: t.TransactTime ? new Date(t.TransactTime) : null, |
| 90 | + externalUpdated: t.Timestamp ? new Date(t.Timestamp) : null, |
| 91 | + status: this.mapScryptStatus(t.Status), |
| 92 | + amount: parseFloat(t.Quantity) || 0, |
| 93 | + feeAmount: t.Fee ? parseFloat(t.Fee) : 0, |
| 94 | + feeCurrency: t.Currency, |
| 95 | + currency: t.Currency, |
| 96 | + txId: t.TxHash, |
| 97 | + })); |
| 98 | + } |
| 99 | + |
| 100 | + private static mapScryptTransactionType(type: ScryptTransactionType): ExchangeTxType { |
| 101 | + switch (type) { |
| 102 | + case ScryptTransactionType.DEPOSIT: |
| 103 | + return ExchangeTxType.DEPOSIT; |
| 104 | + case ScryptTransactionType.WITHDRAWAL: |
| 105 | + return ExchangeTxType.WITHDRAWAL; |
| 106 | + default: |
| 107 | + throw new Error(`Unknown Scrypt transaction type: ${type}`); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private static mapScryptStatus(status: ScryptTransactionStatus): string { |
| 112 | + switch (status) { |
| 113 | + case ScryptTransactionStatus.COMPLETED: |
| 114 | + return 'ok'; |
| 115 | + case ScryptTransactionStatus.FAILED: |
| 116 | + case ScryptTransactionStatus.REJECTED: |
| 117 | + return 'failed'; |
| 118 | + default: |
| 119 | + return 'pending'; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + // --- SCRYPT TRADES --- // |
| 124 | + static mapScryptTrades(trades: ScryptTrade[], exchange: ExchangeName): ExchangeTxDto[] { |
| 125 | + return trades.map((t) => ({ |
| 126 | + exchange, |
| 127 | + type: ExchangeTxType.TRADE, |
| 128 | + externalId: t.TradeID, |
| 129 | + externalCreated: new Date(t.TransactTime), |
| 130 | + externalUpdated: new Date(t.Timestamp), |
| 131 | + status: this.mapScryptTradeStatus(t.TradeStatus), |
| 132 | + amount: parseFloat(t.Quantity) || 0, |
| 133 | + feeAmount: parseFloat(t.Fee) || 0, |
| 134 | + feeCurrency: t.FeeCurrency ?? t.Currency, |
| 135 | + symbol: t.Symbol.replace('-', '/'), |
| 136 | + side: t.Side === ScryptTradeSide.BUY ? 'buy' : 'sell', |
| 137 | + price: t.Price ? parseFloat(t.Price) : undefined, |
| 138 | + cost: parseFloat(t.Amount) || 0, |
| 139 | + order: t.OrderID, |
| 140 | + })); |
| 141 | + } |
| 142 | + |
| 143 | + private static mapScryptTradeStatus(status: ScryptTradeStatus): string { |
| 144 | + switch (status) { |
| 145 | + case ScryptTradeStatus.CONFIRMED: |
| 146 | + return 'ok'; |
| 147 | + case ScryptTradeStatus.CANCELED: |
| 148 | + return 'canceled'; |
| 149 | + case ScryptTradeStatus.PENDING: |
| 150 | + default: |
| 151 | + return 'pending'; |
| 152 | + } |
| 153 | + } |
73 | 154 | } |
0 commit comments