flip/
flip_demo.rs

1use hyperstack_sdk::HyperStackClient;
2use serde::{Deserialize, Serialize};
3use tokio::time::{sleep, Duration};
4
5#[derive(Serialize, Deserialize, Debug, Clone, Default)]
6struct GameId {
7    global_count: Option<u64>,
8    account_id: Option<u64>,
9}
10
11#[derive(Serialize, Deserialize, Debug, Clone, Default)]
12struct GameStatus {
13    current: Option<String>,
14    created_at: Option<i64>,
15    activated_at: Option<i64>,
16    settled_at: Option<i64>,
17}
18
19#[derive(Serialize, Deserialize, Debug, Clone, Default)]
20struct GameMetrics {
21    total_volume: Option<i64>,
22    total_ev: Option<i64>,
23    bet_count: Option<i64>,
24    unique_players: Option<i64>,
25    total_fees_collected: Option<i64>,
26    total_payouts_distributed: Option<i64>,
27    house_profit_loss: Option<i64>,
28    claim_rate: Option<f64>,
29}
30
31#[derive(Serialize, Deserialize, Debug, Clone)]
32struct GameEvent {
33    timestamp: i64,
34    #[serde(flatten)]
35    data: serde_json::Value,
36}
37
38#[derive(Serialize, Deserialize, Debug, Clone, Default)]
39struct GameEvents {
40    created: Option<GameEvent>,
41    activated: Option<GameEvent>,
42    #[serde(default)]
43    bets_placed: Vec<GameEvent>,
44    betting_closed: Option<GameEvent>,
45    settled: Option<GameEvent>,
46    #[serde(default)]
47    payouts_claimed: Vec<GameEvent>,
48}
49
50#[derive(Serialize, Deserialize, Debug, Clone, Default)]
51struct SettlementGame {
52    #[serde(skip_serializing_if = "Option::is_none")]
53    id: Option<GameId>,
54    #[serde(skip_serializing_if = "Option::is_none")]
55    status: Option<GameStatus>,
56    #[serde(skip_serializing_if = "Option::is_none")]
57    metrics: Option<GameMetrics>,
58    #[serde(skip_serializing_if = "Option::is_none")]
59    events: Option<GameEvents>,
60}
61
62#[tokio::main]
63async fn main() -> anyhow::Result<()> {
64    let url = std::env::args().nth(1).unwrap_or_else(|| "ws://127.0.0.1:8080".to_string());
65    
66    let view = std::env::var("VIEW").unwrap_or_else(|_| "SettlementGame/kv".to_string());
67    
68    let mut client = HyperStackClient::<SettlementGame>::new(url, &view);
69    
70    if let Ok(key) = std::env::var("KEY") {
71        client = client.with_key(key);
72    }
73    
74    let store = client.connect().await?;
75    
76    println!("connected, watching {}...\n", view);
77    
78    let mut updates = store.subscribe();
79    
80    loop {
81        tokio::select! {
82            Ok((key, game)) = updates.recv() => {
83                println!("\n=== Game {} ===", key);
84                println!("{}", serde_json::to_string_pretty(&game).unwrap_or_default());
85            }
86            _ = sleep(Duration::from_secs(30)) => {
87                println!("No updates for 30s...");
88            }
89        }
90    }
91}