mirror of
https://github.com/d0zingcat/ore.git
synced 2026-05-13 23:16:52 +00:00
cleanup
This commit is contained in:
@@ -1,13 +1,5 @@
|
||||
use steel::*;
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
|
||||
pub struct PayoutEvent {
|
||||
pub authority: Pubkey,
|
||||
pub amount: u64,
|
||||
pub ts: u64,
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
|
||||
pub struct BetEvent {
|
||||
@@ -23,6 +15,14 @@ pub struct BuryEvent {
|
||||
pub ts: u64,
|
||||
}
|
||||
|
||||
event!(PayoutEvent);
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
|
||||
pub struct PayoutEvent {
|
||||
pub authority: Pubkey,
|
||||
pub amount: u64,
|
||||
pub ts: u64,
|
||||
}
|
||||
|
||||
event!(BetEvent);
|
||||
event!(BuryEvent);
|
||||
event!(PayoutEvent);
|
||||
|
||||
@@ -5,8 +5,8 @@ use super::OreAccount;
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
|
||||
pub struct Block {
|
||||
/// The number of bets made in the current round.
|
||||
pub bet_count: u64,
|
||||
/// The cumulative amount risked in the current round.
|
||||
pub cumulative_sum: u64,
|
||||
|
||||
/// The current round.
|
||||
pub current_round: u64,
|
||||
@@ -14,23 +14,23 @@ pub struct Block {
|
||||
/// The slot at which the current round ends.
|
||||
pub ends_at: u64,
|
||||
|
||||
/// The mint used to track wagers of the current round.
|
||||
/// The mint used for wagers of the current round.
|
||||
pub mint: Pubkey,
|
||||
|
||||
/// The noise used for the current round.
|
||||
/// The noise used for the current round for provably fair randomness.
|
||||
pub noise: [u8; 32],
|
||||
|
||||
/// Whether or not the current round has ended.
|
||||
/// Whether or not the current round has paid out.
|
||||
pub payed_out: u64,
|
||||
|
||||
/// The amount of ORE to distribute to the winner.
|
||||
pub reward: u64,
|
||||
|
||||
/// The time time the current round started.
|
||||
/// The time the current round started at.
|
||||
pub started_at: u64,
|
||||
|
||||
/// The cumulative amount of SOL risked in the current round, up to and including this bet.
|
||||
pub total_bets: u64,
|
||||
/// The number of wagers made in the current round.
|
||||
pub total_wagers: u64,
|
||||
}
|
||||
|
||||
account!(OreAccount, Block);
|
||||
|
||||
@@ -11,8 +11,8 @@ pub struct Wager {
|
||||
/// The signer authorized to use this wager.
|
||||
pub authority: Pubkey,
|
||||
|
||||
/// The cumulative amount of SOL bet in the current round, up to and including this wager.
|
||||
pub cumulative_bets: u64,
|
||||
/// The cumulative amount risked in the current round, up to and including this wager.
|
||||
pub cumulative_sum: u64,
|
||||
|
||||
/// The current round this miner is betting in.
|
||||
pub round: u64,
|
||||
|
||||
@@ -88,8 +88,8 @@ async fn crank(
|
||||
"Time until payout: {:.1} seconds ({} slots) – {} wagers – {} SOL",
|
||||
seconds_remaining,
|
||||
slots_remaining,
|
||||
block.bet_count,
|
||||
lamports_to_sol(block.total_bets)
|
||||
block.total_wagers,
|
||||
lamports_to_sol(block.cumulative_sum)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -195,7 +195,7 @@ async fn build_payout_ix(
|
||||
let wagers = get_block_wagers(rpc).await?;
|
||||
|
||||
// Return early if no wagers
|
||||
if block.total_bets == 0 {
|
||||
if block.cumulative_sum == 0 || block.reward == 0 {
|
||||
return Ok(payout(
|
||||
payer.pubkey(),
|
||||
Pubkey::new_unique(),
|
||||
@@ -213,12 +213,12 @@ async fn build_payout_ix(
|
||||
let y = u64::from_le_bytes(noise[8..16].try_into().unwrap());
|
||||
let z = u64::from_le_bytes(noise[16..24].try_into().unwrap());
|
||||
let w = u64::from_le_bytes(noise[24..32].try_into().unwrap());
|
||||
let roll = (x ^ y ^ z ^ w) % block.total_bets;
|
||||
let roll = (x ^ y ^ z ^ w) % block.cumulative_sum;
|
||||
|
||||
// Find the winning wager
|
||||
let mut winner = None;
|
||||
for (pubkey, wager) in wagers {
|
||||
if roll >= wager.cumulative_bets && roll < wager.cumulative_bets + wager.amount {
|
||||
if roll >= wager.cumulative_sum && roll < wager.cumulative_sum + wager.amount {
|
||||
println!("Roll: {}, Winner: {:?}", roll, pubkey);
|
||||
winner = Some((pubkey, wager));
|
||||
break;
|
||||
|
||||
@@ -2,7 +2,7 @@ use ore_api::prelude::*;
|
||||
use solana_program::keccak::hashv;
|
||||
use steel::*;
|
||||
|
||||
/// Places a bet.
|
||||
/// Open a wager.
|
||||
pub fn process_bet(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
|
||||
// Parse data.
|
||||
let args = Bet::try_from_bytes(data)?;
|
||||
@@ -46,14 +46,14 @@ pub fn process_bet(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
|
||||
let wager = wager_info.as_account_mut::<Wager>(&ore_api::ID)?;
|
||||
wager.amount = amount;
|
||||
wager.authority = *signer_info.key;
|
||||
wager.cumulative_bets = block.total_bets;
|
||||
wager.cumulative_sum = block.cumulative_sum;
|
||||
wager.round = block.current_round;
|
||||
wager.seed = seed;
|
||||
wager.timestamp = clock.unix_timestamp as u64;
|
||||
|
||||
// Update block stats.
|
||||
block.bet_count += 1;
|
||||
block.total_bets += amount;
|
||||
block.cumulative_sum += amount;
|
||||
block.total_wagers += 1;
|
||||
|
||||
// Hash client seed into block noise for provably fair randomness.
|
||||
block.noise = hashv(&[&block.noise, &seed]).to_bytes();
|
||||
|
||||
@@ -2,7 +2,7 @@ use meteora_pools_sdk::instructions::{SwapCpi, SwapCpiAccounts, SwapInstructionA
|
||||
use ore_api::prelude::*;
|
||||
use steel::*;
|
||||
|
||||
/// Swaps bets into ORE and buries the ORE.
|
||||
/// Swap wagers into ORE and bury the ORE.
|
||||
pub fn process_bury(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResult {
|
||||
// Load accounts.
|
||||
let clock = Clock::get()?;
|
||||
@@ -61,19 +61,20 @@ pub fn process_bury(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResult
|
||||
|
||||
// Burn (bury) the purchased ORE.
|
||||
let block_ore = block_ore_info.as_associated_token_account(block_info.key, &MINT_ADDRESS)?;
|
||||
let burn_amount = block_ore.amount();
|
||||
burn_signed_with_bump(
|
||||
block_ore_info,
|
||||
ore_mint_info,
|
||||
block_info,
|
||||
token_program_info,
|
||||
block_ore.amount(),
|
||||
burn_amount,
|
||||
&[BLOCK],
|
||||
block_bump,
|
||||
)?;
|
||||
|
||||
// Emit an event.
|
||||
BuryEvent {
|
||||
amount: block_ore.amount(),
|
||||
amount: burn_amount,
|
||||
ts: clock.unix_timestamp as u64,
|
||||
}
|
||||
.log();
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use ore_api::prelude::*;
|
||||
use steel::*;
|
||||
|
||||
/// Closes a wager account.
|
||||
/// Close a wager account.
|
||||
pub fn process_close(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResult {
|
||||
// Load accounts.
|
||||
let [signer_info, block_info, wager_info, system_program] = accounts else {
|
||||
|
||||
@@ -31,7 +31,7 @@ pub fn process_initialize(accounts: &[AccountInfo<'_>], _data: &[u8]) -> Program
|
||||
&[BLOCK],
|
||||
)?;
|
||||
let block = block_info.as_account_mut::<Block>(&ore_api::ID)?;
|
||||
block.bet_count = 0;
|
||||
block.cumulative_sum = 0;
|
||||
block.current_round = 0;
|
||||
block.ends_at = 0;
|
||||
block.mint = spl_token::native_mint::ID;
|
||||
@@ -39,7 +39,7 @@ pub fn process_initialize(accounts: &[AccountInfo<'_>], _data: &[u8]) -> Program
|
||||
block.payed_out = 0;
|
||||
block.reward = 0;
|
||||
block.started_at = 0;
|
||||
block.total_bets = 0;
|
||||
block.total_wagers = 0;
|
||||
|
||||
// Initialize block token accounts.
|
||||
create_associated_token_account(
|
||||
|
||||
@@ -3,7 +3,7 @@ use solana_program::keccak::hashv;
|
||||
use steel::*;
|
||||
use sysvar::slot_hashes::SlotHashes;
|
||||
|
||||
/// Pays out a block reward to the winning.
|
||||
/// Payout block reward to the winning wager.
|
||||
pub fn process_payout(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResult {
|
||||
// Load accounts.
|
||||
let clock = Clock::get()?;
|
||||
@@ -29,7 +29,7 @@ pub fn process_payout(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResu
|
||||
block.payed_out = 1;
|
||||
|
||||
// Skip payout if no bets were placed.
|
||||
if block.total_bets == 0 || block.reward == 0 {
|
||||
if block.cumulative_sum == 0 || block.reward == 0 {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
@@ -44,13 +44,13 @@ pub fn process_payout(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResu
|
||||
let y = u64::from_le_bytes(block.noise[8..16].try_into().unwrap());
|
||||
let z = u64::from_le_bytes(block.noise[16..24].try_into().unwrap());
|
||||
let w = u64::from_le_bytes(block.noise[24..32].try_into().unwrap());
|
||||
let roll = (x ^ y ^ z ^ w) % block.total_bets;
|
||||
let roll = (x ^ y ^ z ^ w) % block.cumulative_sum;
|
||||
|
||||
// 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)?;
|
||||
.assert_mut(|w| roll >= w.cumulative_sum)?
|
||||
.assert_mut(|w| roll < w.cumulative_sum + w.amount)?;
|
||||
recipient_info.as_associated_token_account(&wager.authority, &MINT_ADDRESS)?;
|
||||
|
||||
// Transfer the winnings to the recipient.
|
||||
|
||||
@@ -2,7 +2,7 @@ use ore_api::prelude::*;
|
||||
use ore_boost_api::{consts::DENOMINATOR_BPS, prelude::Config as BoostConfig};
|
||||
use steel::*;
|
||||
|
||||
/// Reset tops up the bus balances and updates the emissions and reward rates.
|
||||
/// Start the next block.
|
||||
pub fn process_reset(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResult {
|
||||
// Load accounts.
|
||||
let clock = Clock::get()?;
|
||||
@@ -42,14 +42,14 @@ pub fn process_reset(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResul
|
||||
boost_proof.total_rewards += boost_reward;
|
||||
|
||||
// Reset the block.
|
||||
block.bet_count = 0;
|
||||
block.cumulative_sum = 0;
|
||||
block.current_round += 1;
|
||||
block.ends_at = clock.slot + 150; // 60 seconds
|
||||
block.noise = [0; 32];
|
||||
block.payed_out = 0;
|
||||
block.reward = net_emissions - boost_reward;
|
||||
block.started_at = clock.slot;
|
||||
block.total_bets = 0;
|
||||
block.total_wagers = 0;
|
||||
|
||||
// Fund the treasury.
|
||||
mint_to_signed(
|
||||
|
||||
Reference in New Issue
Block a user