mirror of
https://github.com/d0zingcat/ore.git
synced 2026-05-13 23:16:52 +00:00
square
This commit is contained in:
@@ -1,37 +0,0 @@
|
||||
use ore_api::prelude::*;
|
||||
use steel::*;
|
||||
|
||||
/// Initializes the program.
|
||||
pub fn process_initialize_squares(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResult {
|
||||
// Load accounts.
|
||||
let (required_accounts, square_accounts) = accounts.split_at(2);
|
||||
let [signer_info, system_program] = required_accounts else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
};
|
||||
signer_info.is_signer()?.has_address(&ADMIN_ADDRESS)?;
|
||||
system_program.is_program(&system_program::ID)?;
|
||||
|
||||
// Create config account.
|
||||
for i in 0..25 {
|
||||
let square_info = &square_accounts[i];
|
||||
if square_info.data_is_empty() {
|
||||
create_program_account::<Square>(
|
||||
square_info,
|
||||
system_program,
|
||||
signer_info,
|
||||
&ore_api::ID,
|
||||
&[SQUARE, &(i as u64).to_le_bytes()],
|
||||
)?;
|
||||
let square = square_info.as_account_mut::<Square>(&ore_api::ID)?;
|
||||
square.id = i as u64;
|
||||
square.round_id = 0;
|
||||
square.miners = [Pubkey::default(); 16];
|
||||
} else {
|
||||
square_info
|
||||
.as_account_mut::<Square>(&ore_api::ID)?
|
||||
.assert_mut(|s| s.id == i as u64)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -3,7 +3,6 @@ mod claim_ore;
|
||||
mod claim_seeker;
|
||||
mod claim_sol;
|
||||
mod initialize;
|
||||
mod initialize_squares;
|
||||
mod prospect;
|
||||
mod redeem;
|
||||
mod reset;
|
||||
@@ -16,7 +15,6 @@ use claim_ore::*;
|
||||
use claim_seeker::*;
|
||||
use claim_sol::*;
|
||||
use initialize::*;
|
||||
use initialize_squares::*;
|
||||
use prospect::*;
|
||||
use redeem::*;
|
||||
use reset::*;
|
||||
@@ -39,9 +37,7 @@ pub fn process_instruction(
|
||||
OreInstruction::ClaimSOL => process_claim_sol(accounts, data)?,
|
||||
OreInstruction::ClaimORE => process_claim_ore(accounts, data)?,
|
||||
OreInstruction::Initialize => process_initialize(accounts, data)?,
|
||||
OreInstruction::InitializeSquares => process_initialize_squares(accounts, data)?,
|
||||
OreInstruction::Prospect => process_prospect(accounts, data)?,
|
||||
OreInstruction::Redeem => panic!("not allowed"),
|
||||
OreInstruction::Reset => process_reset(accounts, data)?,
|
||||
|
||||
// Admin
|
||||
@@ -50,6 +46,8 @@ pub fn process_instruction(
|
||||
|
||||
// Seeker
|
||||
OreInstruction::ClaimSeeker => process_claim_seeker(accounts, data)?,
|
||||
|
||||
_ => return Err(ProgramError::InvalidInstructionData),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -8,7 +8,7 @@ pub fn process_prospect(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramRes
|
||||
// Parse data.
|
||||
let args = Prospect::try_from_bytes(data)?;
|
||||
let amount = u64::from_le_bytes(args.amount);
|
||||
let square_id = u64::from_le_bytes(args.square_id);
|
||||
let square_id = usize::from_le_bytes(args.square_id);
|
||||
|
||||
// Load accounts.
|
||||
let clock = Clock::get()?;
|
||||
@@ -29,12 +29,10 @@ pub fn process_prospect(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramRes
|
||||
.has_address(&config.fee_collector)?
|
||||
.is_writable()?;
|
||||
miner_info.is_writable()?;
|
||||
let square = square_info
|
||||
.as_account_mut::<Square>(&ore_api::ID)?
|
||||
.assert_mut(|s| s.id == square_id)?;
|
||||
let square = square_info.as_account_mut::<Square>(&ore_api::ID)?;
|
||||
system_program.is_program(&system_program::ID)?;
|
||||
|
||||
// Chekc whitelist
|
||||
// Check whitelist
|
||||
if !AUTHORIZED_ACCOUNTS.contains(&signer_info.key) {
|
||||
return Err(ProgramError::InvalidAccountData);
|
||||
}
|
||||
@@ -65,6 +63,7 @@ pub fn process_prospect(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramRes
|
||||
|
||||
// Reset board.
|
||||
if board.slot_hash != [0; 32] {
|
||||
// Reset board
|
||||
board.prospects = [0; 25];
|
||||
board.id += 1;
|
||||
board.slot_hash = [0; 32];
|
||||
@@ -74,6 +73,10 @@ pub fn process_prospect(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramRes
|
||||
board.total_prospects = 0;
|
||||
board.total_vaulted = 0;
|
||||
board.total_winnings = 0;
|
||||
|
||||
// Reset squares
|
||||
square.count = [0; 25];
|
||||
square.miners = [[Pubkey::default(); 16]; 25];
|
||||
}
|
||||
|
||||
// Reset miner
|
||||
@@ -82,29 +85,22 @@ pub fn process_prospect(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramRes
|
||||
miner.round_id = board.id;
|
||||
}
|
||||
|
||||
// Reset square
|
||||
if square.round_id != board.id {
|
||||
square.count = 0;
|
||||
square.miners = [Pubkey::default(); 16];
|
||||
square.round_id = board.id;
|
||||
}
|
||||
|
||||
// Normalize amount.
|
||||
let fee = amount / 100;
|
||||
let amount = amount - fee;
|
||||
|
||||
// Update miner
|
||||
let is_first_move = miner.prospects[square_id as usize] == 0;
|
||||
miner.prospects[square_id as usize] += amount;
|
||||
let is_first_move = miner.prospects[square_id] == 0;
|
||||
miner.prospects[square_id] += amount;
|
||||
|
||||
// Update square
|
||||
if is_first_move {
|
||||
square.miners[square.count as usize] = *signer_info.key;
|
||||
square.count += 1;
|
||||
square.miners[square_id][square.count[square_id]] = *signer_info.key;
|
||||
square.count[square_id] += 1;
|
||||
}
|
||||
|
||||
// Update board
|
||||
board.prospects[square_id as usize] += amount;
|
||||
board.prospects[square_id] += amount;
|
||||
board.total_prospects += amount;
|
||||
|
||||
// Transfer prospects.
|
||||
|
||||
@@ -7,7 +7,7 @@ pub fn process_reset(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResul
|
||||
// Load accounts.
|
||||
let clock = Clock::get()?;
|
||||
let (required_accounts, miner_accounts) = accounts.split_at(8);
|
||||
let [signer_info, board_info, mint_info, treasury_info, treasury_tokens_info, system_program, token_program, slot_hashes_sysvar] =
|
||||
let [signer_info, board_info, mint_info, square_info, treasury_info, treasury_tokens_info, system_program, token_program, slot_hashes_sysvar] =
|
||||
required_accounts
|
||||
else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
@@ -18,6 +18,7 @@ pub fn process_reset(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResul
|
||||
.assert_mut(|b| b.slot_hash == [0; 32])?
|
||||
.assert_mut(|b| clock.slot >= b.end_slot)?;
|
||||
let mint = mint_info.has_address(&MINT_ADDRESS)?.as_mint()?;
|
||||
let square = square_info.as_account_mut::<Square>(&ore_api::ID)?;
|
||||
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)?;
|
||||
@@ -29,7 +30,7 @@ pub fn process_reset(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResul
|
||||
if let Ok(slot_hash) = get_slot_hash(board.end_slot, slot_hashes_sysvar) {
|
||||
board.slot_hash = slot_hash;
|
||||
let winning_square = get_winning_square(&slot_hash);
|
||||
let square_prospects = board.prospects[winning_square as usize];
|
||||
let square_prospects = board.prospects[winning_square];
|
||||
(winning_square, square_prospects)
|
||||
} else {
|
||||
// Cannot get slot hash. No one wins.
|
||||
@@ -68,13 +69,18 @@ pub fn process_reset(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResul
|
||||
let miner = miner_info
|
||||
.as_account_mut::<Miner>(&ore_api::ID)?
|
||||
.assert_mut(|m| m.round_id == board.id)?;
|
||||
let miner_prospects = miner.prospects[winning_square as usize];
|
||||
let miner_prospects = miner.prospects[winning_square];
|
||||
let rewards = miner_prospects + (winnings * miner_prospects / square_prospects); // Winners get their own prospect back plus their share of the winnings.
|
||||
checksum += miner_prospects;
|
||||
miner.rewards_sol += rewards;
|
||||
miner.lifetime_rewards_sol += rewards;
|
||||
rewards_sol[i] = rewards;
|
||||
|
||||
// Check if miner was provided in correct order.
|
||||
if miner.authority != square.miners[winning_square][i] {
|
||||
return Err(ProgramError::InvalidAccountData);
|
||||
}
|
||||
|
||||
// Find the top winner.
|
||||
if miner_prospects > top_miner_prospects {
|
||||
top_miner_prospects = miner_prospects;
|
||||
@@ -134,7 +140,7 @@ pub fn get_slot_hash(
|
||||
Ok(slot_hash)
|
||||
}
|
||||
|
||||
fn get_winning_square(slot_hash: &[u8]) -> u64 {
|
||||
fn get_winning_square(slot_hash: &[u8]) -> usize {
|
||||
// Use slot hash to generate a random u64
|
||||
let r1 = u64::from_le_bytes(slot_hash[0..8].try_into().unwrap());
|
||||
let r2 = u64::from_le_bytes(slot_hash[8..16].try_into().unwrap());
|
||||
@@ -143,5 +149,5 @@ fn get_winning_square(slot_hash: &[u8]) -> u64 {
|
||||
let r = r1 ^ r2 ^ r3 ^ r4;
|
||||
|
||||
// Returns a value in the range [0, 24] inclusive
|
||||
r % 25
|
||||
(r % 25) as usize
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user