This commit is contained in:
Hardhat Chad
2025-05-29 09:58:05 -07:00
parent 6507372413
commit 79a9ac3b40
17 changed files with 182 additions and 167 deletions

View File

@@ -2,25 +2,29 @@ use meteora_pools_sdk::instructions::{SwapCpi, SwapCpiAccounts, SwapInstructionA
use ore_api::prelude::*;
use steel::*;
/// Swap wagers into ORE and bury the ORE.
pub fn process_bury(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResult {
/// Swap commits into ORE and bury the ORE.
pub fn process_bury(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
// Parse data.
let args = Bury::try_from_bytes(data)?;
let amount = u64::from_le_bytes(args.amount);
// Load accounts.
let clock = Clock::get()?;
let (required_accounts, meteora_accounts) = accounts.split_at(6);
let [signer_info, block_info, block_bets_info, block_ore_info, bet_mint_info, ore_mint_info] =
let [signer_info, block_info, block_commits_info, block_ore_info, mint_info, ore_mint_info] =
required_accounts
else {
return Err(ProgramError::NotEnoughAccountKeys);
};
signer_info.is_signer()?.has_address(&ADMIN_ADDRESS)?;
signer_info.is_signer()?; // .has_address(&ADMIN_ADDRESS)?;
block_info.as_account::<Block>(&ore_api::ID)?;
let block_bets = block_bets_info
let block_commits = block_commits_info
.is_writable()?
.as_associated_token_account(block_info.key, bet_mint_info.key)?;
.as_associated_token_account(block_info.key, mint_info.key)?;
block_ore_info
.is_writable()?
.as_associated_token_account(block_info.key, &MINT_ADDRESS)?;
bet_mint_info.as_mint()?;
mint_info.as_mint()?;
ore_mint_info.has_address(&MINT_ADDRESS)?.as_mint()?;
// Load meteora accounts.
@@ -36,7 +40,7 @@ pub fn process_bury(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResult
&meteora_pools_program,
SwapCpiAccounts {
pool: pool_info,
user_source_token: block_bets_info,
user_source_token: block_commits_info,
user_destination_token: block_ore_info,
a_vault: a_vault_info,
b_vault: b_vault_info,
@@ -52,7 +56,7 @@ pub fn process_bury(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResult
token_program: token_program_info,
},
SwapInstructionArgs {
in_amount: block_bets.amount(),
in_amount: block_commits.amount().min(amount),
minimum_out_amount: 0, // TODO: Calculate minimum out amount with slippage
},
);

View File

@@ -1,22 +1,22 @@
use ore_api::prelude::*;
use steel::*;
/// Close a wager account.
/// Close a commit account.
pub fn process_close(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResult {
// Load accounts.
let [signer_info, block_info, wager_info, system_program] = accounts else {
let [signer_info, block_info, commit_info, system_program] = accounts else {
return Err(ProgramError::NotEnoughAccountKeys);
};
signer_info.is_signer()?;
let block = block_info.as_account::<Block>(&ore_api::ID)?;
wager_info
.as_account_mut::<Wager>(&ore_api::ID)?
.assert_mut(|w| w.authority == *signer_info.key)?
.assert_mut(|w| w.round < block.current_round)?;
commit_info
.as_account_mut::<Commit>(&ore_api::ID)?
.assert_mut(|c| c.authority == *signer_info.key)?
.assert_mut(|c| c.round < block.current_round)?;
system_program.is_program(&system_program::ID)?;
// Close the wager account
wager_info.close(&signer_info)?;
// Close the commit account
commit_info.close(&signer_info)?;
Ok(())
}

View File

@@ -2,16 +2,16 @@ use ore_api::prelude::*;
use solana_program::keccak::hashv;
use steel::*;
/// Open a wager.
pub fn process_bet(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
/// Deploy capital to mine the current block.
pub fn process_deploy(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
// Parse data.
let args = Bet::try_from_bytes(data)?;
let args = Deploy::try_from_bytes(data)?;
let amount = u64::from_le_bytes(args.amount);
let seed = args.seed;
// Load accounts.
let clock = Clock::get()?;
let [signer_info, block_info, block_bets_info, sender_info, wager_info, system_program, token_program, slot_hashes_sysvar] =
let [signer_info, block_info, block_commits_info, commit_info, sender_info, system_program, token_program, slot_hashes_sysvar] =
accounts
else {
return Err(ProgramError::NotEnoughAccountKeys);
@@ -21,54 +21,54 @@ pub fn process_bet(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
.as_account_mut::<Block>(&ore_api::ID)?
.assert_mut(|b| b.ends_at > clock.slot)?
.assert_mut(|b| b.paid == 0)?;
block_bets_info
block_commits_info
.is_writable()?
.as_associated_token_account(block_info.key, &block.mint)?;
commit_info.is_writable()?.is_empty()?.has_seeds(
&[COMMIT, &block.current_round.to_le_bytes(), &seed],
&ore_api::ID,
)?;
sender_info
.is_writable()?
.as_associated_token_account(signer_info.key, &block.mint)?;
wager_info.is_writable()?.is_empty()?.has_seeds(
&[WAGER, &block.current_round.to_le_bytes(), &seed],
&ore_api::ID,
)?;
system_program.is_program(&system_program::ID)?;
token_program.is_program(&spl_token::ID)?;
slot_hashes_sysvar.is_sysvar(&sysvar::slot_hashes::ID)?;
// Create wager account.
create_program_account::<Wager>(
&wager_info,
// Create commit account.
create_program_account::<Commit>(
&commit_info,
&system_program,
&signer_info,
&ore_api::ID,
&[WAGER, &block.current_round.to_le_bytes(), &seed],
&[COMMIT, &block.current_round.to_le_bytes(), &seed],
)?;
let wager = wager_info.as_account_mut::<Wager>(&ore_api::ID)?;
wager.amount = amount;
wager.authority = *signer_info.key;
wager.cumulative_sum = block.cumulative_sum;
wager.round = block.current_round;
wager.seed = seed;
wager.timestamp = clock.unix_timestamp as u64;
let commit = commit_info.as_account_mut::<Commit>(&ore_api::ID)?;
commit.amount = amount;
commit.authority = *signer_info.key;
commit.cumulative_sum = block.cumulative_sum;
commit.round = block.current_round;
commit.seed = seed;
commit.timestamp = clock.unix_timestamp as u64;
// Update block stats.
block.cumulative_sum += amount;
block.total_wagers += 1;
block.total_commits += 1;
// Hash client seed into block noise for provably fair randomness.
block.noise = hashv(&[&block.noise, &seed]).to_bytes();
// Transfer wagers.
// Transfer commits.
transfer(
&signer_info,
&sender_info,
&block_bets_info,
&block_commits_info,
&token_program,
amount,
)?;
// Emit an event.
BetEvent {
DeployEvent {
authority: *signer_info.key,
amount,
ts: clock.unix_timestamp as u64,

View File

@@ -4,17 +4,17 @@ use steel::*;
/// 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] =
let [signer_info, block_info, block_commits_info, block_ore_info, ore_mint_info, sol_mint_info, system_program, token_program, associated_token_program] =
accounts
else {
return Err(ProgramError::NotEnoughAccountKeys);
};
signer_info.is_signer()?.has_address(&ADMIN_ADDRESS)?;
signer_info.is_signer()?; // .has_address(&ADMIN_ADDRESS)?;
block_info
.is_empty()?
.is_writable()?
.has_seeds(&[BLOCK], &ore_api::ID)?;
block_bets_info.is_empty()?.is_writable()?;
block_commits_info.is_empty()?.is_writable()?;
block_ore_info.is_empty()?.is_writable()?;
ore_mint_info.has_address(&MINT_ADDRESS)?;
sol_mint_info.has_address(&spl_token::native_mint::ID)?;
@@ -39,13 +39,13 @@ pub fn process_initialize(accounts: &[AccountInfo<'_>], _data: &[u8]) -> Program
block.paid = 0;
block.reward = 0;
block.started_at = 0;
block.total_wagers = 0;
block.total_commits = 0;
// Initialize block token accounts.
create_associated_token_account(
signer_info,
block_info,
block_bets_info,
block_commits_info,
sol_mint_info,
system_program,
token_program,

View File

@@ -1,15 +1,15 @@
mod bet;
mod bury;
mod claim;
mod close;
mod deploy;
mod initialize;
mod payout;
mod reset;
use bet::*;
use bury::*;
use claim::*;
use close::*;
use deploy::*;
use initialize::*;
use payout::*;
use reset::*;
@@ -26,9 +26,9 @@ pub fn process_instruction(
match ix {
// User
OreInstruction::Bet => process_bet(accounts, data)?,
OreInstruction::Claim => process_claim(accounts, data)?,
OreInstruction::Close => process_close(accounts, data)?,
OreInstruction::Deploy => process_deploy(accounts, data)?,
OreInstruction::Payout => process_payout(accounts, data)?,
OreInstruction::Reset => process_reset(accounts, data)?,

View File

@@ -3,11 +3,11 @@ use solana_program::keccak::hashv;
use steel::*;
use sysvar::slot_hashes::SlotHashes;
/// Payout block reward to the winning wager.
/// Payout block reward to the winning commit.
pub fn process_payout(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResult {
// Load accounts.
let clock = Clock::get()?;
let [signer_info, block_info, mint_info, wager_info, recipient_info, treasury_info, treasury_tokens_info, system_program, token_program, slot_hashes_sysvar] =
let [signer_info, block_info, commit_info, mint_info, recipient_info, treasury_info, treasury_tokens_info, system_program, token_program, slot_hashes_sysvar] =
accounts
else {
return Err(ProgramError::NotEnoughAccountKeys);
@@ -37,7 +37,7 @@ pub fn process_payout(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResu
return Ok(());
}
// Skip payout if no bets were placed.
// Skip payout if no commits were placed.
if block.cumulative_sum == 0 {
burn_signed(
&treasury_tokens_info,
@@ -55,7 +55,7 @@ pub fn process_payout(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResu
bincode::deserialize::<SlotHashes>(slot_hashes_sysvar.data.borrow().as_ref()).unwrap();
let Some(slot_hash) = slot_hashes.get(&block.ends_at) else {
// If payout is not called within 2.5 minutes of the block ending,
// then the slot hash will be unavailable and the winning wager cannot be determined.
// then the slot hash will be unavailable and the winning commit cannot be determined.
burn_signed(
&treasury_tokens_info,
&mint_info,
@@ -75,12 +75,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.cumulative_sum;
// Validate the wager account.
let wager = wager_info
.as_account_mut::<Wager>(&ore_api::ID)?
.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)?;
// Validate the commit account.
let commit = commit_info
.as_account_mut::<Commit>(&ore_api::ID)?
.assert_mut(|c| roll >= c.cumulative_sum)?
.assert_mut(|c| roll < c.cumulative_sum + c.amount)?;
recipient_info.as_associated_token_account(&commit.authority, &MINT_ADDRESS)?;
// Transfer the winnings to the recipient.
transfer_signed(
@@ -94,7 +94,7 @@ pub fn process_payout(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResu
// Emit an event.
PayoutEvent {
authority: wager.authority,
authority: commit.authority,
amount: block.reward,
ts: clock.unix_timestamp as u64,
}

View File

@@ -49,7 +49,7 @@ pub fn process_reset(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResul
block.paid = 0;
block.reward = net_emissions - boost_reward;
block.started_at = clock.slot;
block.total_wagers = 0;
block.total_commits = 0;
// Fund the treasury.
mint_to_signed(