scaffolding

This commit is contained in:
Hardhat Chad
2025-05-23 18:41:54 -07:00
parent e942e7ae5b
commit 495dd4ad4b
12 changed files with 169 additions and 54 deletions

View File

@@ -1,9 +1,9 @@
use std::mem::size_of;
use ore_api::prelude::*;
use solana_program::{keccak::hashv, slot_hashes::SlotHash};
use solana_program::keccak::hashv;
use steel::*;
use sysvar::slot_hashes::SlotHashes;
/// Pays out a block reward to the winning.
pub fn process_payout(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResult {
// Load accounts.
let clock = Clock::get()?;
@@ -17,8 +17,6 @@ pub fn process_payout(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResu
.as_account_mut::<Block>(&ore_api::ID)?
.assert_mut(|b| b.ends_at < clock.slot)?
.assert_mut(|b| b.payed_out == 0)?;
let wager = wager_info.as_account::<Wager>(&ore_api::ID)?;
recipient_info.as_associated_token_account(&wager.authority, &MINT_ADDRESS)?;
treasury_info.has_address(&TREASURY_ADDRESS)?;
treasury_tokens_info
.has_address(&TREASURY_TOKENS_ADDRESS)?
@@ -27,13 +25,20 @@ pub fn process_payout(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResu
token_program.is_program(&spl_token::ID)?;
slot_hashes_sysvar.is_sysvar(&sysvar::slot_hashes::ID)?;
// Mark the block as paid out.
block.payed_out = 1;
// Skip payout if no bets were placed.
if block.total_bets == 0 {
return Ok(());
}
// Select the slothash from the slot at when the round ended.
// The represents the server seed for the provably fair random number.
let offset = clock.slot - block.ends_at;
let size = size_of::<SlotHash>();
let i = offset as usize * size;
let slot_hash = &slot_hashes_sysvar.data.borrow()[i..i + size];
block.noise = hashv(&[&block.noise, slot_hash]).to_bytes();
// The represents the server seed for a provably fair random number.
let slot_hashes =
bincode::deserialize::<SlotHashes>(slot_hashes_sysvar.data.borrow().as_ref()).unwrap();
let slot_hash = slot_hashes.get(&block.ends_at).unwrap();
block.noise = hashv(&[&block.noise, slot_hash.as_ref()]).to_bytes();
// Calculate the random number.
let x = u64::from_le_bytes(block.noise[0..8].try_into().unwrap());
@@ -42,11 +47,12 @@ pub fn process_payout(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResu
let w = u64::from_le_bytes(block.noise[24..32].try_into().unwrap());
let roll = (x ^ y ^ z ^ w) % block.total_bets;
// Assert that the wager account passed in is the winner.
assert!(roll >= wager.cumulative_bets && roll < wager.cumulative_bets + wager.amount);
// Mark the block as paid out.
block.payed_out = 1;
// Validate the wager account.
let wager = wager_info
.as_account_mut::<Wager>(&ore_api::ID)?
.assert_mut(|w| roll >= w.cumulative_bets)?
.assert_mut(|w| roll < w.cumulative_bets + w.amount)?;
recipient_info.as_associated_token_account(&wager.authority, &MINT_ADDRESS)?;
// Transfer the winnings to the recipient.
transfer_signed(
@@ -54,7 +60,7 @@ pub fn process_payout(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResu
&treasury_tokens_info,
&recipient_info,
&token_program,
ONE_ORE / 2,
block.reward,
&[TREASURY],
)?;