1use serde::{Deserialize, Serialize};
2use steel::*;
3
4use crate::state::{round_pda, OreAccountV4};
5
6use super::OreAccountV1;
7
8#[repr(C)]
9#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable, Serialize, Deserialize)]
10pub struct RoundV1 {
11 pub id: u64,
13
14 pub deployed: [u64; 25],
16
17 pub slot_hash: [u8; 32],
19
20 pub count: [u64; 25],
22
23 pub expires_at: u64,
25
26 pub motherlode: u64,
28
29 pub rent_payer: Pubkey,
31
32 pub top_miner: Pubkey,
34
35 pub top_miner_reward: u64,
37
38 pub total_deployed: u64,
40
41 pub total_miners: u64,
43
44 pub total_vaulted: u64,
46
47 pub total_winnings: u64,
49}
50
51#[repr(C)]
52#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable, Serialize, Deserialize)]
53pub struct RoundV4 {
54 pub id: u64,
56
57 pub sol: [u64; 25],
59
60 pub mass: [u64; 25],
62
63 pub miners: [u64; 25],
65
66 pub entropy: [u8; 32],
68
69 pub closes_at: u64,
71
72 pub motherlode: u64,
74
75 pub rent_payer: Pubkey,
77
78 pub rewards: [u64; 25],
80
81 pub protocol_fee: u64,
83
84 pub unique_miners: u64,
86
87 pub winner: Pubkey,
89}
90
91impl RoundV1 {
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 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 RoundV4 {
146 pub fn pda(&self) -> (Pubkey, u8) {
147 round_pda(self.id)
148 }
149
150 pub fn rng(&self) -> Option<u64> {
151 if self.entropy == [0; 32] || self.entropy == [u8::MAX; 32] {
152 return None;
153 }
154 let r1 = u64::from_le_bytes(self.entropy[0..8].try_into().unwrap());
155 let r2 = u64::from_le_bytes(self.entropy[8..16].try_into().unwrap());
156 let r3 = u64::from_le_bytes(self.entropy[16..24].try_into().unwrap());
157 let r4 = u64::from_le_bytes(self.entropy[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.sol[winning_square] == 0 {
168 return 0;
169 }
170 rng.reverse_bits() % self.sol[winning_square]
171 }
172
173 pub fn calculate_total_winnings(&self, winning_square: usize) -> u64 {
174 let mut total_winnings = 0;
175 for (i, &sol) in self.sol.iter().enumerate() {
176 if i != winning_square {
177 total_winnings += sol;
178 }
179 }
180 total_winnings
181 }
182
183 pub fn is_split_reward(&self, rng: u64) -> bool {
184 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!(OreAccountV1, RoundV1);
200account!(OreAccountV4, RoundV4);
201
202#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
203pub enum Round {
204 V1(RoundV1),
205 V4(RoundV4),
206}
207
208impl Round {
209 pub fn id(&self) -> u64 {
210 match self {
211 Round::V1(r) => r.id,
212 Round::V4(r) => r.id,
213 }
214 }
215
216 pub fn deployed(&self) -> [u64; 25] {
217 match self {
218 Round::V1(r) => r.deployed,
219 Round::V4(r) => r.sol,
220 }
221 }
222
223 pub fn slot_hash(&self) -> [u8; 32] {
224 match self {
225 Round::V1(r) => r.slot_hash,
226 Round::V4(r) => r.entropy,
227 }
228 }
229
230 pub fn count(&self) -> [u64; 25] {
231 match self {
232 Round::V1(r) => r.count,
233 Round::V4(r) => r.miners,
234 }
235 }
236
237 pub fn expires_at(&self) -> u64 {
238 match self {
239 Round::V1(r) => r.expires_at,
240 Round::V4(r) => r.closes_at,
241 }
242 }
243
244 pub fn motherlode(&self) -> u64 {
245 match self {
246 Round::V1(r) => r.motherlode,
247 Round::V4(r) => r.motherlode,
248 }
249 }
250
251 pub fn rent_payer(&self) -> Pubkey {
252 match self {
253 Round::V1(r) => r.rent_payer,
254 Round::V4(r) => r.rent_payer,
255 }
256 }
257
258 pub fn top_miner(&self) -> Pubkey {
259 match self {
260 Round::V1(r) => r.top_miner,
261 Round::V4(r) => r.winner,
262 }
263 }
264
265 pub fn top_miner_reward(&self) -> u64 {
266 match self {
267 Round::V1(r) => r.top_miner_reward,
268 Round::V4(_) => 0,
269 }
270 }
271
272 pub fn total_deployed(&self) -> u64 {
273 match self {
274 Round::V1(r) => r.total_deployed,
275 Round::V4(r) => r.sol.iter().sum(),
276 }
277 }
278
279 pub fn total_miners(&self) -> u64 {
280 match self {
281 Round::V1(r) => r.total_miners,
282 Round::V4(r) => r.unique_miners,
283 }
284 }
285
286 pub fn total_vaulted(&self) -> u64 {
287 match self {
288 Round::V1(r) => r.total_vaulted,
289 Round::V4(_) => 0,
290 }
291 }
292
293 pub fn total_winnings(&self) -> u64 {
294 match self {
295 Round::V1(r) => r.total_winnings,
296 Round::V4(_) => 0,
297 }
298 }
299
300 pub fn pda(&self) -> (Pubkey, u8) {
301 match self {
302 Round::V1(r) => r.pda(),
303 Round::V4(r) => r.pda(),
304 }
305 }
306
307 pub fn rng(&self) -> Option<u64> {
308 match self {
309 Round::V1(r) => r.rng(),
310 Round::V4(r) => r.rng(),
311 }
312 }
313
314 pub fn winning_square(&self, rng: u64) -> usize {
315 match self {
316 Round::V1(r) => r.winning_square(rng),
317 Round::V4(r) => r.winning_square(rng),
318 }
319 }
320
321 pub fn top_miner_sample(&self, rng: u64, winning_square: usize) -> u64 {
322 match self {
323 Round::V1(r) => r.top_miner_sample(rng, winning_square),
324 Round::V4(r) => r.top_miner_sample(rng, winning_square),
325 }
326 }
327
328 pub fn calculate_total_winnings(&self, winning_square: usize) -> u64 {
329 match self {
330 Round::V1(r) => r.calculate_total_winnings(winning_square),
331 Round::V4(r) => r.calculate_total_winnings(winning_square),
332 }
333 }
334
335 pub fn is_split_reward(&self, rng: u64) -> bool {
336 match self {
337 Round::V1(r) => r.is_split_reward(rng),
338 Round::V4(r) => r.is_split_reward(rng),
339 }
340 }
341
342 pub fn did_hit_motherlode(&self, rng: u64) -> bool {
343 match self {
344 Round::V1(r) => r.did_hit_motherlode(rng),
345 Round::V4(r) => r.did_hit_motherlode(rng),
346 }
347 }
348}