ore-types 0.0.51

Types for interacting with ORE backend infrasturcture
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
use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};
use solana_sdk::pubkey::Pubkey;

#[cfg(feature = "redis")]
use redis_derive::{FromRedisValue, ToRedisArgs};

/// Response for a successful login, containing the JWT.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct AuthResponse {
    pub token: String,
}

/// Response for a successful supply endpoint.
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
pub struct SupplyResponse {
    pub result: String,
}

/// Response after successfully sending a chat message.
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq)]
pub struct ChatSendMessageResponse {
    pub status: String,
    pub message: String,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct User {
    pub authority: String,
    pub username: String,
    pub profile_photo_url: Option<String>,
    pub discord_user: Option<DiscordUser>,
    pub updated_at: NaiveDateTime,
    pub risk_score: i64,
    pub is_banned: bool,
    pub role: Option<String>,
}

// Notifications
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "redis", derive(FromRedisValue, ToRedisArgs))]
pub struct ChatNotification {
    pub authority: String,
    pub username: String,
    pub text: String,
    pub id: u64,
    pub ts: i64,
    pub profile_photo_url: Option<String>,
    pub role: Option<String>,
    pub discord_user_id: Option<String>,
    // Reply fields (optional - null if not a reply)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub reply_to_id: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub reply_to_text: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub reply_to_username: Option<String>,
    // Reaction counts (optional - for backwards compatibility)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub reactions: Option<ChatReactions>,
}

/// Reaction counts for a chat message.
/// Uses named fields instead of emoji keys for type safety and Redis compatibility.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
#[cfg_attr(feature = "redis", derive(FromRedisValue, ToRedisArgs))]
pub struct ChatReactions {
    #[serde(default, skip_serializing_if = "is_zero")]
    pub thumbs_up: u32, // 👍
    #[serde(default, skip_serializing_if = "is_zero")]
    pub heart: u32, // ❤️
    #[serde(default, skip_serializing_if = "is_zero")]
    pub laughing: u32, // 😂
    #[serde(default, skip_serializing_if = "is_zero")]
    pub surprised: u32, // 😮
    #[serde(default, skip_serializing_if = "is_zero")]
    pub sad: u32, // 😢
    #[serde(default, skip_serializing_if = "is_zero")]
    pub fire: u32, // 🔥
}

fn is_zero(n: &u32) -> bool {
    *n == 0
}

impl ChatReactions {
    /// Check if all reaction counts are zero.
    pub fn is_empty(&self) -> bool {
        self.thumbs_up == 0
            && self.heart == 0
            && self.laughing == 0
            && self.surprised == 0
            && self.sad == 0
            && self.fire == 0
    }

    /// Get the count for a specific emoji.
    pub fn get(&self, emoji: &str) -> u32 {
        match emoji {
            "👍" => self.thumbs_up,
            "❤️" | "" => self.heart,
            "😂" => self.laughing,
            "😮" => self.surprised,
            "😢" => self.sad,
            "🔥" => self.fire,
            _ => 0,
        }
    }

    /// Set the count for a specific emoji.
    pub fn set(&mut self, emoji: &str, count: u32) {
        match emoji {
            "👍" => self.thumbs_up = count,
            "❤️" | "" => self.heart = count,
            "😂" => self.laughing = count,
            "😮" => self.surprised = count,
            "😢" => self.sad = count,
            "🔥" => self.fire = count,
            _ => {}
        }
    }
}

/// Allowed reaction emoji.
pub const ALLOWED_REACTION_EMOJI: [&str; 6] = ["👍", "❤️", "😂", "😮", "😢", "🔥"];

/// Check if an emoji is in the allowed reaction set.
pub fn is_valid_reaction_emoji(emoji: &str) -> bool {
    // Handle heart emoji variant (with and without variation selector)
    if emoji == "" {
        return true;
    }
    ALLOWED_REACTION_EMOJI.contains(&emoji)
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct ResetNotification {
    pub block_id: u64,
}

/// Notification for a deploy event, containing all relevant data for clients.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "redis", derive(FromRedisValue, ToRedisArgs))]
pub struct DeployNotification {
    /// The authority (miner) who deployed.
    pub authority: String,
    /// The amount of SOL deployed (in lamports).
    pub amount: u64,
    /// The mask (bitmask of selected squares).
    pub mask: u64,
    /// The round ID this deployment is for.
    pub round_id: u64,
    /// The signer of the transaction.
    pub signer: String,
    /// The strategy used for deployment.
    pub strategy: u64,
    /// Total squares selected.
    pub total_squares: u64,
    /// Timestamp of the deployment.
    pub ts: i64,
    /// Transaction signature.
    pub sig: String,
}

/// Notification for a reaction being added or removed from a chat message.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct ReactionNotification {
    pub message_id: u64,
    pub emoji: String,
    pub count: u32,
    pub action: String, // "added" or "removed"
}

/// A user currently typing in chat.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct TypingUser {
    pub authority: String,
    pub username: String,
}

/// Notification for typing indicator updates.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct TypingNotification {
    pub users: Vec<TypingUser>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub enum Notification {
    Chat(ChatNotification),
    ChatBatch(Vec<ChatNotification>),
    Reset(ResetNotification),
    Deploy(DeployNotification),
    Reaction(ReactionNotification),
    Typing(TypingNotification),
}

impl Notification {
    pub fn id(&self) -> String {
        match self {
            Notification::Chat(chat) => chat.id.to_string(),
            Notification::ChatBatch(_) => "chat_batch".to_string(),
            Notification::Reset(reset) => reset.block_id.to_string(),
            Notification::Deploy(deploy) => deploy.sig.clone(),
            Notification::Reaction(reaction) => {
                format!("{}:{}", reaction.message_id, reaction.emoji)
            }
            Notification::Typing(_) => "typing".to_string(),
        }
    }
}

/// Response after adding/removing a reaction.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct ChatReactResponse {
    pub status: String,
    pub action: String,     // "added" or "removed"
    pub message_id: u64,
    pub emoji: String,
    pub count: u32,         // new count for this emoji on this message
}

/// Response for typing indicator endpoint.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct ChatTypingResponse {
    pub status: String,
}

/// Response for chat history endpoint with cursor-based pagination.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct ChatHistoryResponse {
    pub messages: Vec<ChatNotification>,
    pub has_more: bool,
    pub oldest_id: Option<u64>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct DailyRevenue {
    pub day: String,
    pub revenue: i64,
}

// Username validation

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct UsernameValidationResponse {
    pub valid: bool,
    pub error: Option<String>,
}

impl UsernameValidationResponse {
    pub fn valid() -> Self {
        Self {
            valid: true,
            error: None,
        }
    }

    pub fn invalid(error: String) -> Self {
        Self {
            valid: false,
            error: Some(error),
        }
    }
}

/// Response for a successful Discord authentication.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct DiscordAuthResponse {
    pub access_token: String,
}

/// Google user information returned after authentication.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct GoogleUser {
    pub email: String,
    pub name: String,
    pub picture: Option<String>,
}

/// Response for a successful Google authentication.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct GoogleAuthResponse {
    pub jwt: String,
    pub user: GoogleUser,
}

#[derive(Deserialize, Serialize, Debug, Clone, PartialEq)]
pub struct DiscordUser {
    pub id: String,
    pub username: String,
    pub discriminator: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub global_name: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub avatar: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub verified: Option<bool>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub email: Option<String>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct OreBalance {
    pub wallet: u64,
    pub staked: u64,
    pub unrefined: u64,
    pub refined: u64,
    pub lifetime_deployed_sol: u64,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct LeaderboardEntry {
    pub authority: String,
    pub amount: u64,
    pub username: Option<String>,
    pub profile_picture_url: Option<String>,
}

/// Response type for reset events with enriched top miner user info.
/// Maintains field order matching ore_api::event::ResetEvent for backwards compatibility.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct ResetEventResponse {
    pub disc: u8,
    pub round_id: u64,
    pub start_slot: u64,
    pub end_slot: u64,
    pub winning_square: u64,
    pub top_miner: Pubkey,
    pub num_winners: u64,
    pub motherlode: u64,
    pub total_deployed: u64,
    pub total_vaulted: u64,
    pub total_winnings: u64,
    pub total_minted: u64,
    pub ts: i64,
    pub rng: u64,
    pub deployed_winning_square: u64,
    pub top_miner_username: Option<String>,
    pub top_miner_profile_photo: Option<String>,
}

/// Response type for a user's deploy history event.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct DeployHistoryEvent {
    pub sig: String,
    pub authority: String,
    pub signer: String,
    pub amount: u64,
    pub mask: i64,
    pub round_id: i64,
    pub total_squares: i64,
    pub ts: i64,
    pub winning_square: i64,
    pub top_miner: String,
    pub rewards_sol: u64,
    pub rewards_ore: u64,
    pub total_winnings_sol: u64,
    pub deployed_winning_square: u64,
    pub motherlode: u64,
}

/// Response type for a winner in a round, with user data and betting pattern.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct RoundWinner {
    /// The miner's public key.
    pub authority: String,
    /// User's display name (if set).
    pub username: Option<String>,
    /// User's profile photo URL (if set).
    pub profile_photo_url: Option<String>,
    /// Amount deployed on the winning square (in lamports).
    pub deployed_on_winning: u64,
    /// Combined mask of all squares the user deployed to (bitmask).
    pub combined_mask: u64,
}

/// Response type for listing winners in a round.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct RoundWinnersResponse {
    /// The round ID.
    pub round_id: u64,
    /// The winning square (0-24).
    pub winning_square: u64,
    /// Total amount deployed on the winning square by all miners.
    pub total_on_winning: u64,
    /// Total winnings for the round (in lamports).
    pub total_winnings: u64,
    /// List of winners sorted by deployed_on_winning descending.
    pub winners: Vec<RoundWinner>,
}

/// A miner who participated in a round.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct RoundMiner {
    pub authority: String,
    pub username: Option<String>,
    pub profile_photo_url: Option<String>,
    /// Total SOL deployed in the round (lamports), summed across all deploy events.
    pub total_deployed: u64,
    /// Combined bitmask of all squares the user deployed to.
    pub combined_mask: u64,
    /// ORE rewards won in the round (in token base units, 11 decimals).
    /// Includes base reward (1 ORE, split or top-miner) plus motherlode share if applicable.
    /// Zero if the round is in progress or the miner didn't deploy on the winning square.
    pub rewards: u64,
}

/// Response type for listing miners in a round.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct RoundMinersResponse {
    pub round_id: u64,
    /// Miners sorted by total_deployed descending, paginated.
    pub miners: Vec<RoundMiner>,
    /// The pinned authority's data (included regardless of pagination rank).
    /// Null if no authority was provided or they didn't deploy in this round.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub authority: Option<RoundMiner>,
    /// The pinned authority's 1-based rank by total_deployed descending.
    /// Null if no authority was provided or they didn't deploy in this round.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub authority_rank: Option<u64>,
}