ore_api/state/
round.rs

1use serde::{Deserialize, Serialize};
2use steel::*;
3
4use crate::state::round_pda;
5
6use super::{OreAccount, OreAccountOLD};
7
8#[repr(C)]
9#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable, Serialize, Deserialize)]
10pub struct Round {
11    /// The round number.
12    pub id: u64,
13
14    /// The amount of SOL deployed in each square.
15    pub deployed: [u64; 25],
16
17    /// The hash of the end slot, provided by solana, used for random number generation.
18    pub slot_hash: [u8; 32],
19
20    /// The count of miners on each square.
21    pub count: [u64; 25],
22
23    /// The slot at which claims for this round account end.
24    pub expires_at: u64,
25
26    /// The amount of ORE in the motherlode.
27    pub motherlode: u64,
28
29    /// The account to which rent should be returned when this account is closed.
30    pub rent_payer: Pubkey,
31
32    /// The top miner of the round.
33    pub top_miner: Pubkey,
34
35    /// The amount of ORE to distribute to the top miner.
36    pub top_miner_reward: u64,
37
38    /// The total amount of SOL deployed in the round.
39    pub total_deployed: u64,
40
41    /// The total number of miners on the board.
42    pub total_miners: u64,
43
44    /// The total amount of SOL put in the ORE vault.
45    pub total_vaulted: u64,
46
47    /// The total amount of SOL won by miners for the round.
48    pub total_winnings: u64,
49}
50
51#[repr(C)]
52#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable, Serialize, Deserialize)]
53pub struct RoundOLD {
54    /// The round number.
55    pub id: u64,
56
57    /// The amount of SOL deployed in each square.
58    pub deployed: [u64; 25],
59
60    /// The hash of the end slot, provided by solana, used for random number generation.
61    pub slot_hash: [u8; 32],
62
63    /// The count of miners on each square.
64    pub count: [u64; 25],
65
66    /// The slot at which claims for this round account end.
67    pub expires_at: u64,
68
69    /// The amount of ORE in the motherlode.
70    pub motherlode: u64,
71
72    /// The account to which rent should be returned when this account is closed.
73    pub rent_payer: Pubkey,
74
75    /// The top miner of the round.
76    pub top_miner: Pubkey,
77
78    /// The amount of ORE to distribute to the top miner.
79    pub top_miner_reward: u64,
80
81    /// The total amount of SOL deployed in the round.
82    pub total_deployed: u64,
83
84    /// The total amount of SOL put in the ORE vault.
85    pub total_vaulted: u64,
86
87    /// The total amount of SOL won by miners for the round.
88    pub total_winnings: u64,
89}
90
91impl Round {
92    pub fn pda(&self) -> (Pubkey, u8) {
93        round_pda(self.id)
94    }
95
96    pub fn rng(&self) -> Option<u64> {
97        if self.slot_hash == [0; 32] || self.slot_hash == [u8::MAX; 32] {
98            return None;
99        }
100        let r1 = u64::from_le_bytes(self.slot_hash[0..8].try_into().unwrap());
101        let r2 = u64::from_le_bytes(self.slot_hash[8..16].try_into().unwrap());
102        let r3 = u64::from_le_bytes(self.slot_hash[16..24].try_into().unwrap());
103        let r4 = u64::from_le_bytes(self.slot_hash[24..32].try_into().unwrap());
104        let r = r1 ^ r2 ^ r3 ^ r4;
105        Some(r)
106    }
107
108    pub fn winning_square(&self, rng: u64) -> usize {
109        (rng % 25) as usize
110    }
111
112    pub fn top_miner_sample(&self, rng: u64, winning_square: usize) -> u64 {
113        if self.deployed[winning_square] == 0 {
114            return 0;
115        }
116        rng.reverse_bits() % self.deployed[winning_square]
117    }
118
119    pub fn calculate_total_winnings(&self, winning_square: usize) -> u64 {
120        let mut total_winnings = 0;
121        for (i, &deployed) in self.deployed.iter().enumerate() {
122            if i != winning_square {
123                total_winnings += deployed;
124            }
125        }
126        total_winnings
127    }
128
129    pub fn is_split_reward(&self, rng: u64) -> bool {
130        // One out of four rounds get split rewards.
131        let rng = rng.reverse_bits().to_le_bytes();
132        let r1 = u16::from_le_bytes(rng[0..2].try_into().unwrap());
133        let r2 = u16::from_le_bytes(rng[2..4].try_into().unwrap());
134        let r3 = u16::from_le_bytes(rng[4..6].try_into().unwrap());
135        let r4 = u16::from_le_bytes(rng[6..8].try_into().unwrap());
136        let r = r1 ^ r2 ^ r3 ^ r4;
137        r % 2 == 0
138    }
139
140    pub fn did_hit_motherlode(&self, rng: u64) -> bool {
141        rng.reverse_bits() % 625 == 0
142    }
143}
144
145impl RoundOLD {
146    pub fn pda(&self) -> (Pubkey, u8) {
147        round_pda(self.id)
148    }
149
150    pub fn rng(&self) -> Option<u64> {
151        if self.slot_hash == [0; 32] || self.slot_hash == [u8::MAX; 32] {
152            return None;
153        }
154        let r1 = u64::from_le_bytes(self.slot_hash[0..8].try_into().unwrap());
155        let r2 = u64::from_le_bytes(self.slot_hash[8..16].try_into().unwrap());
156        let r3 = u64::from_le_bytes(self.slot_hash[16..24].try_into().unwrap());
157        let r4 = u64::from_le_bytes(self.slot_hash[24..32].try_into().unwrap());
158        let r = r1 ^ r2 ^ r3 ^ r4;
159        Some(r)
160    }
161
162    pub fn winning_square(&self, rng: u64) -> usize {
163        (rng % 25) as usize
164    }
165
166    pub fn top_miner_sample(&self, rng: u64, winning_square: usize) -> u64 {
167        if self.deployed[winning_square] == 0 {
168            return 0;
169        }
170        rng.reverse_bits() % self.deployed[winning_square]
171    }
172
173    pub fn calculate_total_winnings(&self, winning_square: usize) -> u64 {
174        let mut total_winnings = 0;
175        for (i, &deployed) in self.deployed.iter().enumerate() {
176            if i != winning_square {
177                total_winnings += deployed;
178            }
179        }
180        total_winnings
181    }
182
183    pub fn is_split_reward(&self, rng: u64) -> bool {
184        // One out of four rounds get split rewards.
185        let rng = rng.reverse_bits().to_le_bytes();
186        let r1 = u16::from_le_bytes(rng[0..2].try_into().unwrap());
187        let r2 = u16::from_le_bytes(rng[2..4].try_into().unwrap());
188        let r3 = u16::from_le_bytes(rng[4..6].try_into().unwrap());
189        let r4 = u16::from_le_bytes(rng[6..8].try_into().unwrap());
190        let r = r1 ^ r2 ^ r3 ^ r4;
191        r % 2 == 0
192    }
193
194    pub fn did_hit_motherlode(&self, rng: u64) -> bool {
195        rng.reverse_bits() % 625 == 0
196    }
197}
198
199account!(OreAccount, Round);
200account!(OreAccountOLD, RoundOLD);
201
202#[cfg(test)]
203mod tests {
204    use solana_program::rent::Rent;
205
206    use super::*;
207
208    #[test]
209    fn test_rent() {
210        let size_of_round = 8 + std::mem::size_of::<Round>();
211        let required_rent = Rent::default().minimum_balance(size_of_round);
212        println!("required_rent: {}", required_rent);
213        assert!(false);
214    }
215}