kaccy-core 0.2.0

Core business logic for Kaccy Protocol - batching, fee optimization, and transaction management
Documentation
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
//! Position management and tracking

use chrono::{DateTime, Utc};
use rust_decimal::Decimal;
use rust_decimal_macros::dec;
use serde::{Deserialize, Serialize};
use sqlx::FromRow;
use std::fmt;
use uuid::Uuid;

use super::user::ValidationError;

/// User's trading position for a token
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct Position {
    /// Unique identifier for this position
    pub position_id: Uuid,
    /// Owner of this position
    pub user_id: Uuid,
    /// Token held in this position
    pub token_id: Uuid,
    /// Total tokens held
    pub quantity: Decimal,
    /// Average entry price
    pub average_entry_price: Decimal,
    /// Total cost basis
    pub cost_basis: Decimal,
    /// Realized profit/loss from closed trades
    pub realized_pnl: Decimal,
    /// Position status
    pub status: PositionStatus,
    /// First entry time
    pub opened_at: DateTime<Utc>,
    /// Last modification time
    pub updated_at: DateTime<Utc>,
    /// Close time (if closed)
    pub closed_at: Option<DateTime<Utc>>,
}

/// Status of a trading position
#[derive(Debug, Clone, Copy, Serialize, Deserialize, sqlx::Type, PartialEq, Eq)]
#[sqlx(type_name = "varchar", rename_all = "lowercase")]
#[derive(Default)]
pub enum PositionStatus {
    /// Position is currently open
    #[default]
    Open,
    /// Position has been fully closed
    Closed,
}

impl fmt::Display for PositionStatus {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            PositionStatus::Open => write!(f, "open"),
            PositionStatus::Closed => write!(f, "closed"),
        }
    }
}

impl Position {
    /// Calculate unrealized PnL based on current price
    pub fn unrealized_pnl(&self, current_price: Decimal) -> Decimal {
        let current_value = self.quantity * current_price;
        current_value - self.cost_basis
    }

    /// Calculate total PnL (realized + unrealized)
    pub fn total_pnl(&self, current_price: Decimal) -> Decimal {
        self.realized_pnl + self.unrealized_pnl(current_price)
    }

    /// Calculate PnL percentage
    pub fn pnl_percentage(&self, current_price: Decimal) -> Decimal {
        if self.cost_basis == dec!(0) {
            return dec!(0);
        }
        (self.total_pnl(current_price) / self.cost_basis) * dec!(100)
    }

    /// Calculate return on investment (ROI)
    pub fn roi(&self, current_price: Decimal) -> Decimal {
        if self.cost_basis == dec!(0) {
            return dec!(0);
        }
        (self.unrealized_pnl(current_price) / self.cost_basis) * dec!(100)
    }

    /// Add to position (buy more)
    pub fn add(&mut self, quantity: Decimal, price: Decimal) {
        let additional_cost = quantity * price;
        let new_cost_basis = self.cost_basis + additional_cost;
        let new_quantity = self.quantity + quantity;

        self.average_entry_price = if new_quantity > dec!(0) {
            new_cost_basis / new_quantity
        } else {
            dec!(0)
        };

        self.quantity = new_quantity;
        self.cost_basis = new_cost_basis;
        self.updated_at = Utc::now();
    }

    /// Reduce position (sell some)
    pub fn reduce(&mut self, quantity: Decimal, price: Decimal) -> Decimal {
        if quantity > self.quantity {
            return dec!(0); // Can't reduce more than we have
        }

        // Calculate realized PnL from this sale
        let sale_proceeds = quantity * price;
        let proportional_cost = (quantity / self.quantity) * self.cost_basis;
        let realized_pnl = sale_proceeds - proportional_cost;

        self.quantity -= quantity;
        self.cost_basis -= proportional_cost;
        self.realized_pnl += realized_pnl;
        self.updated_at = Utc::now();

        // If position is now zero, mark as closed
        if self.quantity == dec!(0) {
            self.status = PositionStatus::Closed;
            self.closed_at = Some(Utc::now());
        }

        realized_pnl
    }

    /// Check if position is profitable at current price
    pub fn is_profitable(&self, current_price: Decimal) -> bool {
        self.total_pnl(current_price) > dec!(0)
    }
}

impl fmt::Display for Position {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Position({}, qty={}, avg_price={}, status={})",
            self.position_id, self.quantity, self.average_entry_price, self.status
        )
    }
}

/// Position limits and risk controls
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct PositionLimits {
    /// Owner of these limits
    pub user_id: Uuid,
    /// Maximum position size per token
    pub max_position_size: Option<Decimal>,
    /// Maximum total portfolio value
    pub max_portfolio_value: Option<Decimal>,
    /// Maximum leverage (if applicable)
    pub max_leverage: Option<Decimal>,
    /// Maximum number of open positions
    pub max_open_positions: Option<i32>,
    /// Daily loss limit
    pub daily_loss_limit: Option<Decimal>,
    /// Current daily loss
    pub current_daily_loss: Decimal,
    /// Last reset time for daily limits
    pub last_reset_at: DateTime<Utc>,
    /// Timestamp when these limits were created
    pub created_at: DateTime<Utc>,
    /// Timestamp when these limits were last modified
    pub updated_at: DateTime<Utc>,
}

impl PositionLimits {
    /// Create default limits for a user
    pub fn default_for_user(user_id: Uuid) -> Self {
        let now = Utc::now();
        Self {
            user_id,
            max_position_size: None,
            max_portfolio_value: None,
            max_leverage: Some(dec!(1)), // No leverage by default
            max_open_positions: Some(10),
            daily_loss_limit: None,
            current_daily_loss: dec!(0),
            last_reset_at: now,
            created_at: now,
            updated_at: now,
        }
    }

    /// Check if user can open new position
    pub fn can_open_position(
        &self,
        current_positions: i32,
        position_size: Decimal,
        _portfolio_value: Decimal,
    ) -> Result<(), ValidationError> {
        // Check max open positions
        if let Some(max_positions) = self.max_open_positions {
            if current_positions >= max_positions {
                return Err(ValidationError(format!(
                    "Maximum open positions reached: {}",
                    max_positions
                )));
            }
        }

        // Check max position size
        if let Some(max_size) = self.max_position_size {
            if position_size > max_size {
                return Err(ValidationError(format!(
                    "Position size {} exceeds limit: {}",
                    position_size, max_size
                )));
            }
        }

        Ok(())
    }

    /// Check if daily loss limit is exceeded
    pub fn is_daily_loss_limit_exceeded(&mut self) -> bool {
        // Reset daily loss if it's a new day
        let now = Utc::now();
        if (now - self.last_reset_at).num_hours() >= 24 {
            self.current_daily_loss = dec!(0);
            self.last_reset_at = now;
        }

        if let Some(limit) = self.daily_loss_limit {
            self.current_daily_loss.abs() >= limit
        } else {
            false
        }
    }

    /// Record a loss
    pub fn record_loss(&mut self, loss: Decimal) {
        if loss < dec!(0) {
            self.current_daily_loss += loss.abs();
            self.updated_at = Utc::now();
        }
    }
}

/// Position history entry
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct PositionHistory {
    /// Unique identifier for this history record
    pub history_id: Uuid,
    /// Position this record belongs to
    pub position_id: Uuid,
    /// User who owns the position
    pub user_id: Uuid,
    /// Token involved in the action
    pub token_id: Uuid,
    /// Action type (open, add, reduce, close)
    pub action: PositionAction,
    /// Quantity changed
    pub quantity: Decimal,
    /// Price at which action occurred
    pub price: Decimal,
    /// PnL realized from this action (if any)
    pub realized_pnl: Option<Decimal>,
    /// When this action occurred
    pub occurred_at: DateTime<Utc>,
}

/// Type of action taken on a position
#[derive(Debug, Clone, Copy, Serialize, Deserialize, sqlx::Type, PartialEq, Eq)]
#[sqlx(type_name = "varchar", rename_all = "lowercase")]
#[derive(Default)]
pub enum PositionAction {
    /// Position was opened
    #[default]
    Open,
    /// Quantity was added to the position
    Add,
    /// Quantity was removed from the position
    Reduce,
    /// Position was fully closed
    Close,
}

impl fmt::Display for PositionAction {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            PositionAction::Open => write!(f, "open"),
            PositionAction::Add => write!(f, "add"),
            PositionAction::Reduce => write!(f, "reduce"),
            PositionAction::Close => write!(f, "close"),
        }
    }
}

/// Trading portfolio summary
#[derive(Debug, Serialize)]
pub struct TradingPortfolio {
    /// Portfolio owner
    pub user_id: Uuid,
    /// Total number of positions (open + closed)
    pub total_positions: i32,
    /// Number of currently open positions
    pub open_positions: i32,
    /// Total cost basis across all open positions
    pub total_cost_basis: Decimal,
    /// Total current market value across all open positions
    pub total_market_value: Decimal,
    /// Total unrealized profit/loss
    pub total_unrealized_pnl: Decimal,
    /// Total realized profit/loss
    pub total_realized_pnl: Decimal,
    /// Combined realized and unrealized PnL
    pub total_pnl: Decimal,
    /// Portfolio return on investment as a percentage
    pub portfolio_roi: Decimal,
    /// Individual positions with current prices
    pub positions: Vec<PositionWithPrice>,
}

/// Position with current market data
#[derive(Debug, Serialize)]
pub struct PositionWithPrice {
    /// Underlying position record
    #[serde(flatten)]
    pub position: Position,
    /// Current market price of the token
    pub current_price: Decimal,
    /// Current market value of the position
    pub market_value: Decimal,
    /// Unrealized profit/loss at current price
    pub unrealized_pnl: Decimal,
    /// Unrealized PnL as a percentage of cost basis
    pub unrealized_pnl_percentage: Decimal,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_position_add() {
        let mut position = Position {
            position_id: Uuid::new_v4(),
            user_id: Uuid::new_v4(),
            token_id: Uuid::new_v4(),
            quantity: dec!(100),
            average_entry_price: dec!(10),
            cost_basis: dec!(1000),
            realized_pnl: dec!(0),
            status: PositionStatus::Open,
            opened_at: Utc::now(),
            updated_at: Utc::now(),
            closed_at: None,
        };

        // Add 100 more at $15
        position.add(dec!(100), dec!(15));

        assert_eq!(position.quantity, dec!(200));
        assert_eq!(position.cost_basis, dec!(2500)); // 1000 + 1500
        assert_eq!(position.average_entry_price, dec!(12.5)); // 2500 / 200
    }

    #[test]
    fn test_position_reduce() {
        let mut position = Position {
            position_id: Uuid::new_v4(),
            user_id: Uuid::new_v4(),
            token_id: Uuid::new_v4(),
            quantity: dec!(100),
            average_entry_price: dec!(10),
            cost_basis: dec!(1000),
            realized_pnl: dec!(0),
            status: PositionStatus::Open,
            opened_at: Utc::now(),
            updated_at: Utc::now(),
            closed_at: None,
        };

        // Sell 50 at $15
        let realized_pnl = position.reduce(dec!(50), dec!(15));

        assert_eq!(position.quantity, dec!(50));
        assert_eq!(position.cost_basis, dec!(500)); // Half of original
        assert_eq!(realized_pnl, dec!(250)); // (50 * 15) - 500
        assert_eq!(position.realized_pnl, dec!(250));
    }

    #[test]
    fn test_position_close() {
        let mut position = Position {
            position_id: Uuid::new_v4(),
            user_id: Uuid::new_v4(),
            token_id: Uuid::new_v4(),
            quantity: dec!(100),
            average_entry_price: dec!(10),
            cost_basis: dec!(1000),
            realized_pnl: dec!(0),
            status: PositionStatus::Open,
            opened_at: Utc::now(),
            updated_at: Utc::now(),
            closed_at: None,
        };

        // Sell all at $12
        position.reduce(dec!(100), dec!(12));

        assert_eq!(position.quantity, dec!(0));
        assert_eq!(position.status, PositionStatus::Closed);
        assert!(position.closed_at.is_some());
        assert_eq!(position.realized_pnl, dec!(200)); // (100 * 12) - 1000
    }

    #[test]
    fn test_unrealized_pnl() {
        let position = Position {
            position_id: Uuid::new_v4(),
            user_id: Uuid::new_v4(),
            token_id: Uuid::new_v4(),
            quantity: dec!(100),
            average_entry_price: dec!(10),
            cost_basis: dec!(1000),
            realized_pnl: dec!(0),
            status: PositionStatus::Open,
            opened_at: Utc::now(),
            updated_at: Utc::now(),
            closed_at: None,
        };

        // Current price $12
        let unrealized = position.unrealized_pnl(dec!(12));
        assert_eq!(unrealized, dec!(200)); // (100 * 12) - 1000

        // Current price $8
        let unrealized = position.unrealized_pnl(dec!(8));
        assert_eq!(unrealized, dec!(-200)); // (100 * 8) - 1000
    }

    #[test]
    fn test_position_limits() {
        let limits = PositionLimits {
            user_id: Uuid::new_v4(),
            max_position_size: Some(dec!(1000)),
            max_portfolio_value: Some(dec!(10000)),
            max_leverage: Some(dec!(2)),
            max_open_positions: Some(5),
            daily_loss_limit: Some(dec!(500)),
            current_daily_loss: dec!(0),
            last_reset_at: Utc::now(),
            created_at: Utc::now(),
            updated_at: Utc::now(),
        };

        // Should allow position within limits
        assert!(limits.can_open_position(3, dec!(500), dec!(5000)).is_ok());

        // Should reject position exceeding size limit
        assert!(limits.can_open_position(3, dec!(1500), dec!(5000)).is_err());

        // Should reject when max positions reached
        assert!(limits.can_open_position(5, dec!(500), dec!(5000)).is_err());
    }

    #[test]
    fn test_daily_loss_limit() {
        let mut limits = PositionLimits {
            user_id: Uuid::new_v4(),
            max_position_size: None,
            max_portfolio_value: None,
            max_leverage: None,
            max_open_positions: None,
            daily_loss_limit: Some(dec!(100)),
            current_daily_loss: dec!(0),
            last_reset_at: Utc::now(),
            created_at: Utc::now(),
            updated_at: Utc::now(),
        };

        // Record losses
        limits.record_loss(dec!(-50));
        assert!(!limits.is_daily_loss_limit_exceeded());

        limits.record_loss(dec!(-60));
        assert!(limits.is_daily_loss_limit_exceeded());
    }
}