-
Notifications
You must be signed in to change notification settings - Fork 289
Expand file tree
/
Copy pathreset.rs
More file actions
294 lines (265 loc) · 10.7 KB
/
reset.rs
File metadata and controls
294 lines (265 loc) · 10.7 KB
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
use entropy_api::state::Var;
use ore_api::prelude::*;
use solana_program::{keccak, log::sol_log};
use steel::*;
// TODO Integrate admin fee
/// Pays out the winners and block reward.
pub fn process_reset(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResult {
// Load accounts.
let clock = Clock::get()?;
let (ore_accounts, other_accounts) = accounts.split_at(14);
sol_log(&format!("Ore accounts: {:?}", ore_accounts.len()).to_string());
sol_log(&format!("Other accounts: {:?}", other_accounts.len()).to_string());
let [signer_info, board_info, _config_info, fee_collector_info, mint_info, round_info, round_next_info, top_miner_info, treasury_info, treasury_tokens_info, system_program, token_program, ore_program, slot_hashes_sysvar] =
ore_accounts
else {
return Err(ProgramError::NotEnoughAccountKeys);
};
signer_info.is_signer()?;
let board = board_info
.as_account_mut::<Board>(&ore_api::ID)?
.assert_mut(|b| clock.slot >= b.end_slot + INTERMISSION_SLOTS)?;
fee_collector_info
.is_writable()?
.has_address(&ADMIN_FEE_COLLECTOR)?;
let round = round_info
.as_account_mut::<Round>(&ore_api::ID)?
.assert_mut(|r| r.id == board.round_id)?;
round_next_info
.is_empty()?
.is_writable()?
.has_seeds(&[ROUND, &(board.round_id + 1).to_le_bytes()], &ore_api::ID)?;
let mint = mint_info.has_address(&MINT_ADDRESS)?.as_mint()?;
let treasury = treasury_info.as_account_mut::<Treasury>(&ore_api::ID)?;
treasury_tokens_info.as_associated_token_account(&treasury_info.key, &mint_info.key)?;
system_program.is_program(&system_program::ID)?;
token_program.is_program(&spl_token::ID)?;
ore_program.is_program(&ore_api::ID)?;
slot_hashes_sysvar.is_sysvar(&sysvar::slot_hashes::ID)?;
// Open next round account.
create_program_account::<Round>(
round_next_info,
ore_program,
signer_info,
&ore_api::ID,
&[ROUND, &(board.round_id + 1).to_le_bytes()],
)?;
let round_next = round_next_info.as_account_mut::<Round>(&ore_api::ID)?;
round_next.id = board.round_id + 1;
round_next.deployed = [0; 25];
round_next.slot_hash = [0; 32];
round_next.count = [0; 25];
round_next.expires_at = u64::MAX; // Set to max, to indicate round is waiting for first deploy to begin.
round_next.rent_payer = *signer_info.key;
round_next.motherlode = 0;
round_next.top_miner = Pubkey::default();
round_next.top_miner_reward = 0;
round_next.total_deployed = 0;
round_next.total_vaulted = 0;
round_next.total_winnings = 0;
// Sample random variable
let (entropy_accounts, mint_accounts) = other_accounts.split_at(2);
sol_log(&format!("Entropy accounts: {:?}", entropy_accounts.len()).to_string());
sol_log(&format!("Mint accounts: {:?}", mint_accounts.len()).to_string());
let [var_info, entropy_program] = entropy_accounts else {
return Err(ProgramError::NotEnoughAccountKeys);
};
let var = var_info
.has_address(&VAR_ADDRESS)?
.as_account::<Var>(&entropy_api::ID)?
.assert(|v| v.authority == *board_info.key)?
.assert(|v| v.slot_hash != [0; 32])?
.assert(|v| v.seed != [0; 32])?
.assert(|v| v.value != [0; 32])?;
entropy_program.is_program(&entropy_api::ID)?;
// Print the seed and slot hash.
let seed = keccak::Hash::new_from_array(var.seed);
let slot_hash = keccak::Hash::new_from_array(var.slot_hash);
sol_log(&format!("var slothash: {:?}", slot_hash).to_string());
sol_log(&format!("var seed: {:?}", seed).to_string());
// Read the finalized value from the var.
let value = keccak::Hash::new_from_array(var.value);
sol_log(&format!("var value: {:?}", value).to_string());
round.slot_hash = var.value;
// Exit early if no slot hash was found.
let Some(r) = round.rng() else {
// Slot hash could not be found, refund all SOL.
round.total_vaulted = 0;
round.total_winnings = 0;
round.total_deployed = 0;
// Emit event.
program_log(
&[board_info.clone(), ore_program.clone()],
ResetEvent {
disc: 0,
round_id: round.id,
start_slot: board.start_slot,
end_slot: board.end_slot,
winning_square: u64::MAX,
top_miner: Pubkey::default(),
num_winners: 0,
motherlode: 0,
total_deployed: round.total_deployed,
total_vaulted: round.total_vaulted,
total_winnings: round.total_winnings,
total_minted: 0,
ts: clock.unix_timestamp,
rng: 0,
deployed_winning_square: 0,
}
.to_bytes(),
)?;
// Update board for next round.
board.round_id += 1;
board.start_slot = clock.slot + 1;
board.end_slot = u64::MAX;
return Ok(());
};
// Caculate admin fees.
let total_admin_fee = round.total_deployed / 100;
// Get the winning square.
let winning_square = round.winning_square(r);
// If no one deployed on the winning square, vault all deployed.
if round.deployed[winning_square] == 0 {
// Vault all deployed.
round.total_vaulted = round.total_deployed - total_admin_fee;
treasury.balance += round.total_deployed - total_admin_fee;
// Emit event.
program_log(
&[board_info.clone(), ore_program.clone()],
ResetEvent {
disc: 0,
round_id: round.id,
start_slot: board.start_slot,
end_slot: board.end_slot,
winning_square: winning_square as u64,
top_miner: Pubkey::default(),
num_winners: 0,
motherlode: 0,
total_deployed: round.total_deployed,
total_vaulted: round.total_vaulted,
total_winnings: round.total_winnings,
total_minted: 0,
ts: clock.unix_timestamp,
rng: r,
deployed_winning_square: round.deployed[winning_square],
}
.to_bytes(),
)?;
// Update board for next round.
board.round_id += 1;
board.start_slot = clock.slot + 1;
board.end_slot = u64::MAX;
// Do SOL transfers.
round_info.send(total_admin_fee, &fee_collector_info);
round_info.send(round.total_deployed - total_admin_fee, &treasury_info);
return Ok(());
}
// Get winnings amount (total deployed on all non-winning squares, minus admin fee).
let winnings = round.calculate_total_winnings(winning_square);
let winnings_admin_fee = winnings / 100; // 1% admin fee.
let winnings = winnings - winnings_admin_fee;
// Subtract vault amount from winnings.
let vault_amount = winnings / 10; // 10% of winnings.
let winnings = winnings - vault_amount;
round.total_winnings = winnings;
round.total_vaulted = vault_amount;
treasury.balance += vault_amount;
// Sanity check.
assert!(
round.total_deployed
>= round.total_vaulted
+ round.total_winnings
+ round.deployed[winning_square]
+ winnings_admin_fee
);
// Calculate mint amounts.
let mut mint_supply = mint.supply();
let mint_amount = MAX_SUPPLY.saturating_sub(mint_supply).min(ONE_ORE);
mint_supply += mint_amount;
let motherlode_mint_amount = MAX_SUPPLY.saturating_sub(mint_supply).min(ONE_ORE / 5);
let total_mint_amount = mint_amount + motherlode_mint_amount;
// Reward +1 ORE for the winning miner(s).
round.top_miner_reward = mint_amount;
// With 1 in 2 odds, split the +1 ORE reward.
if round.is_split_reward(r) {
round.top_miner = SPLIT_ADDRESS;
}
// Payout the motherlode if it was activated.
if round.did_hit_motherlode(r) {
round.motherlode = treasury.motherlode;
treasury.motherlode = 0;
}
// Mint +0.2 ORE to the motherlode rewards pool.
treasury.motherlode += motherlode_mint_amount;
// Mint ORE to the treasury.
let [mint_authority_info, mint_program] = mint_accounts else {
return Err(ProgramError::NotEnoughAccountKeys);
};
mint_authority_info.as_account::<ore_mint_api::state::Authority>(&ore_mint_api::ID)?;
mint_program.is_program(&ore_mint_api::ID)?;
invoke_signed(
&ore_mint_api::sdk::mint_ore(total_mint_amount),
&[
treasury_info.clone(),
mint_authority_info.clone(),
mint_info.clone(),
treasury_tokens_info.clone(),
token_program.clone(),
],
&ore_api::ID,
&[TREASURY],
)?;
// Validate top miner (dry-run - no errors on failure).
if round.top_miner != SPLIT_ADDRESS {
if let Ok(miner) = top_miner_info.as_account::<Miner>(&ore_api::ID) {
if miner.round_id == round.id {
let top_miner_sample = round.top_miner_sample(r, winning_square);
if top_miner_sample >= miner.cumulative[winning_square]
&& top_miner_sample
< miner.cumulative[winning_square] + miner.deployed[winning_square]
{
sol_log("Top miner verified");
} else {
sol_log("Top miner verification failed");
}
} else {
sol_log("Top miner round id mismatch");
}
} else {
sol_log("Top miner account cannot be parsed");
}
} else {
sol_log("Split round");
}
// Emit event.
program_log(
&[board_info.clone(), ore_program.clone()],
ResetEvent {
disc: 0,
round_id: round.id,
start_slot: board.start_slot,
end_slot: board.end_slot,
winning_square: winning_square as u64,
top_miner: round.top_miner,
motherlode: round.motherlode,
num_winners: round.count[winning_square],
total_deployed: round.total_deployed,
total_vaulted: round.total_vaulted,
total_winnings: round.total_winnings,
total_minted: total_mint_amount,
ts: clock.unix_timestamp,
rng: r,
deployed_winning_square: round.deployed[winning_square],
}
.to_bytes(),
)?;
// Reset board.
board.round_id += 1;
board.start_slot = clock.slot + 1;
board.end_slot = u64::MAX; // board.start_slot + 150;
// Do SOL transfers.
round_info.send(total_admin_fee, &fee_collector_info);
round_info.send(vault_amount, &treasury_info);
Ok(())
}