Skip to main content

hypercall_db/types/
competition.rs

1//! Competition domain types.
2//!
3//! Competitions, leaderboards, username requests, and profile data
4//! for the trading competition system.
5
6use chrono::{DateTime, Utc};
7use hypercall_types::WalletAddress;
8use rust_decimal::Decimal;
9use serde::{Deserialize, Serialize};
10
11/// A persisted competition row.
12#[derive(Debug, Clone, Serialize, Deserialize)]
13pub struct CompetitionRecord {
14    pub id: i64,
15    pub name: String,
16    pub description: Option<String>,
17    pub rules_url: Option<String>,
18    pub rules_content: Option<String>,
19    pub win_conditions: Vec<String>,
20    pub primary_win_condition: String,
21    pub start_ts_ms: i64,
22    pub end_ts_ms: i64,
23    pub created_at: DateTime<Utc>,
24    pub updated_at: DateTime<Utc>,
25}
26
27/// Input for creating a new competition.
28#[derive(Debug, Clone)]
29pub struct CompetitionUpsertInput {
30    pub name: String,
31    pub description: Option<String>,
32    pub rules_url: Option<String>,
33    pub rules_content: Option<String>,
34    pub win_conditions: Vec<String>,
35    pub primary_win_condition: String,
36    pub start_ts_ms: i64,
37    pub end_ts_ms: i64,
38}
39
40/// Input for updating an existing competition (all fields optional).
41#[derive(Debug, Clone, Default)]
42pub struct CompetitionUpdateInput {
43    pub name: Option<String>,
44    pub description: Option<Option<String>>,
45    pub rules_url: Option<Option<String>>,
46    pub rules_content: Option<Option<String>>,
47    pub win_conditions: Option<Vec<String>>,
48    pub primary_win_condition: Option<String>,
49    pub start_ts_ms: Option<i64>,
50    pub end_ts_ms: Option<i64>,
51}
52
53/// A fill event for competition PnL computation.
54#[derive(Debug, Clone, Serialize, Deserialize)]
55pub struct CompetitionFillRecord {
56    pub wallet: WalletAddress,
57    pub symbol: String,
58    pub side: String,
59    pub price: Decimal,
60    pub size: Decimal,
61    pub fee: Decimal,
62    pub timestamp_ms: i64,
63}
64
65/// Finalized stats for a wallet in a completed competition.
66#[derive(Debug, Clone, Serialize, Deserialize)]
67pub struct CompetitionFinalStatsRecord {
68    pub wallet: WalletAddress,
69    pub rank: i32,
70    pub pnl: Decimal,
71    pub volume: Decimal,
72    pub efficiency: Decimal,
73    pub medal: Option<i32>,
74}
75
76/// Aggregated ledger stats for a wallet (deposits, withdrawals, realized PnL, 24h PnL).
77#[derive(Debug, Clone)]
78pub struct WalletLedgerStats {
79    pub deposits: Decimal,
80    pub withdrawals: Decimal,
81    pub lifetime_realized_pnl: Decimal,
82    pub pnl_24h: Decimal,
83}
84
85/// Per-symbol realized PnL breakdown.
86#[derive(Debug, Clone, Serialize, Deserialize)]
87pub struct SymbolPnlRecord {
88    pub symbol: String,
89    pub realized_pnl: Decimal,
90    pub event_count: i64,
91}
92
93/// Per-symbol volume breakdown.
94#[derive(Debug, Clone, Serialize, Deserialize)]
95pub struct SymbolVolumeRecord {
96    pub symbol: String,
97    pub volume: Decimal,
98}
99
100/// Record for fill data used by the profile trade history endpoint.
101#[derive(Debug, Clone, Serialize, Deserialize)]
102pub struct ProfileFillRecord {
103    pub fill_id: Option<i64>,
104    pub trade_id: i64,
105    pub wallet_address: WalletAddress,
106    pub symbol: String,
107    pub price: Decimal,
108    pub size: Decimal,
109    pub fee: Decimal,
110    pub is_taker: bool,
111    pub timestamp: i64,
112    pub created_at: Option<DateTime<Utc>>,
113    pub builder_code_address: Option<WalletAddress>,
114    pub builder_code_fee: Option<Decimal>,
115    pub realized_pnl: Option<Decimal>,
116}
117
118/// Per-wallet realized PnL and volume (platform-wide metrics).
119#[derive(Debug, Clone)]
120pub struct PlatformWalletMetrics {
121    pub wallet: WalletAddress,
122    pub realized: Decimal,
123    pub volume: Decimal,
124}
125
126/// Historical theoretical mark price for a symbol.
127#[derive(Debug, Clone)]
128pub struct TheoMarkRecord {
129    pub symbol: String,
130    pub theoretical_price: Decimal,
131}
132
133/// Input for recording a competition fill event.
134#[derive(Debug, Clone)]
135pub struct CompetitionFillInput {
136    pub trade_id: i64,
137    pub wallet: WalletAddress,
138    pub symbol: String,
139    pub side: String,
140    pub price: Decimal,
141    pub size: Decimal,
142    pub fee: Decimal,
143    pub timestamp_ms: i64,
144}
145
146/// Input row for inserting finalized competition stats.
147#[derive(Debug, Clone)]
148pub struct CompetitionFinalStatsInput {
149    pub competition_id: i64,
150    pub wallet: WalletAddress,
151    pub rank: i32,
152    pub pnl: Decimal,
153    pub volume: Decimal,
154    pub efficiency: Decimal,
155    pub medal: Option<i32>,
156}
157
158/// Batch wallet-to-username mapping from the usernames table.
159#[derive(Debug, Clone)]
160pub struct WalletUsernameRecord {
161    pub wallet_address: String,
162    pub username: String,
163}