mirror of
https://github.com/d0zingcat/ore.git
synced 2026-05-14 07:26:51 +00:00
scaffolding
This commit is contained in:
@@ -18,6 +18,7 @@ name = "ore"
|
||||
default = []
|
||||
|
||||
[dependencies]
|
||||
bincode.workspace = true
|
||||
drillx.workspace = true
|
||||
meteora-pools-sdk.workspace = true
|
||||
mpl-token-metadata.workspace = true
|
||||
|
||||
@@ -4,10 +4,12 @@ use ore_api::prelude::*;
|
||||
use solana_program::{keccak::hashv, slot_hashes::SlotHash};
|
||||
use steel::*;
|
||||
|
||||
/// Places a bet.
|
||||
pub fn process_bet(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
|
||||
// Parse data.
|
||||
let args = Bet::try_from_bytes(data)?;
|
||||
let amount = u64::from_le_bytes(args.amount);
|
||||
let seed = args.seed;
|
||||
|
||||
// Load accounts.
|
||||
let clock = Clock::get()?;
|
||||
@@ -24,11 +26,7 @@ pub fn process_bet(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
|
||||
block_bets_info.as_associated_token_account(block_info.key, &block.mint)?;
|
||||
sender_info.as_associated_token_account(signer_info.key, &block.mint)?;
|
||||
wager_info.is_writable()?.is_empty()?.has_seeds(
|
||||
&[
|
||||
WAGER,
|
||||
&block.current_round.to_le_bytes(),
|
||||
&block.bet_count.to_le_bytes(),
|
||||
],
|
||||
&[WAGER, &block.current_round.to_le_bytes(), &seed],
|
||||
&ore_api::ID,
|
||||
)?;
|
||||
system_program.is_program(&system_program::ID)?;
|
||||
@@ -55,18 +53,12 @@ pub fn process_bet(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
|
||||
wager.timestamp = clock.unix_timestamp as u64;
|
||||
wager.cumulative_bets = block.total_bets;
|
||||
|
||||
// Update block.
|
||||
// Update block stats.
|
||||
block.total_bets += amount;
|
||||
block.bet_count += 1;
|
||||
|
||||
// Hash client seed into block noise. Use a recent slot hash if no seed is provided.
|
||||
// This follows the scheme for provable randomness.
|
||||
let seed: &[u8] = if args.seed == [0; 32] {
|
||||
&slot_hashes_sysvar.data.borrow()[0..size_of::<SlotHash>()]
|
||||
} else {
|
||||
args.seed.as_slice()
|
||||
};
|
||||
block.noise = hashv(&[&block.noise, seed]).to_bytes();
|
||||
// Hash client seed into block noise. This follows the scheme for provable randomness.
|
||||
block.noise = hashv(&[&block.noise, &seed]).to_bytes();
|
||||
|
||||
// Transfer wagers.
|
||||
transfer(
|
||||
|
||||
@@ -2,6 +2,7 @@ use meteora_pools_sdk::instructions::{SwapCpi, SwapCpiAccounts, SwapInstructionA
|
||||
use ore_api::prelude::*;
|
||||
use steel::*;
|
||||
|
||||
/// Swaps bets into ORE and buries the ORE.
|
||||
pub fn process_bury(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResult {
|
||||
// Load accounts.
|
||||
let (required_accounts, meteora_accounts) = accounts.split_at(5);
|
||||
@@ -56,13 +57,14 @@ 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)?;
|
||||
burn_signed(
|
||||
burn_signed_with_bump(
|
||||
block_ore_info,
|
||||
ore_mint_info,
|
||||
block_info,
|
||||
token_program_info,
|
||||
block_ore.amount(),
|
||||
&[BLOCK, &[block_bump]],
|
||||
&[BLOCK],
|
||||
block_bump,
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use ore_api::prelude::*;
|
||||
use steel::*;
|
||||
|
||||
/// Closes 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 {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use ore_api::prelude::*;
|
||||
use steel::*;
|
||||
|
||||
/// Initialize sets up the ORE program to begin mining.
|
||||
/// Initialize the program.
|
||||
pub fn process_initialize(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResult {
|
||||
// Load accounts.
|
||||
let [signer_info, block_info, block_bets_info, block_ore_info, ore_mint_info, sol_mint_info, system_program, token_program, associated_token_program] =
|
||||
|
||||
@@ -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],
|
||||
)?;
|
||||
|
||||
|
||||
@@ -6,8 +6,8 @@ use steel::*;
|
||||
pub fn process_reset(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResult {
|
||||
// Load accounts.
|
||||
let clock = Clock::get()?;
|
||||
let (required_accounts, boost_accounts) = accounts.split_at(6);
|
||||
let [signer_info, block_info, mint_info, treasury_info, treasury_tokens_info, token_program, slot_hashes_sysvar] =
|
||||
let (required_accounts, boost_accounts) = accounts.split_at(5);
|
||||
let [signer_info, block_info, mint_info, treasury_info, treasury_tokens_info, token_program] =
|
||||
required_accounts
|
||||
else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
@@ -24,7 +24,6 @@ pub fn process_reset(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResul
|
||||
treasury_info.has_address(&TREASURY_ADDRESS)?;
|
||||
treasury_tokens_info.has_address(&TREASURY_TOKENS_ADDRESS)?;
|
||||
token_program.is_program(&spl_token::ID)?;
|
||||
slot_hashes_sysvar.is_sysvar(&sysvar::slot_hashes::ID)?;
|
||||
|
||||
// Load boost accounts.
|
||||
let [boost_config_info, boost_proof_info] = boost_accounts else {
|
||||
|
||||
Reference in New Issue
Block a user