proof of wager

This commit is contained in:
Hardhat Chad
2025-05-23 16:29:31 -07:00
parent 379ebae250
commit b5a50622a2
23 changed files with 312 additions and 805 deletions

View File

@@ -52,6 +52,12 @@ pub const SMOOTHING_FACTOR: u64 = 2;
/// The seed of the bus account PDA.
pub const BUS: &[u8] = b"bus";
/// The seed of the block account PDA.
pub const BLOCK: &[u8] = b"block";
/// The seed of the wager account PDA.
pub const WAGER: &[u8] = b"wager";
/// The seed of the config account PDA.
pub const CONFIG: &[u8] = b"config";

View File

@@ -4,59 +4,40 @@ use steel::*;
#[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)]
pub enum OreInstruction {
// User
Claim = 0,
Bet = 0,
Close = 1,
Mine = 2,
Open = 3,
Reset = 4,
Update = 5,
Payout = 2,
Reset = 3,
// Admin
Initialize = 100,
Migrate = 101,
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct Claim {
pub struct Bet {
pub amount: [u8; 8],
pub seed: [u8; 32],
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct Close {}
#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct Mine {
pub nonce: [u8; 8],
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct Open {}
#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct Reset {}
#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct Update {}
pub struct Payout {}
#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct Initialize {}
#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct Migrate {}
instruction!(OreInstruction, Claim);
instruction!(OreInstruction, Bet);
instruction!(OreInstruction, Close);
instruction!(OreInstruction, Mine);
instruction!(OreInstruction, Open);
instruction!(OreInstruction, Payout);
instruction!(OreInstruction, Reset);
instruction!(OreInstruction, Update);
instruction!(OreInstruction, Initialize);
instruction!(OreInstruction, Migrate);

View File

@@ -2,7 +2,6 @@ pub mod consts;
pub mod error;
pub mod event;
pub mod instruction;
pub mod loaders;
pub mod sdk;
pub mod state;
@@ -11,8 +10,6 @@ pub mod prelude {
pub use crate::error::*;
pub use crate::event::*;
pub use crate::instruction::*;
pub use crate::loaders::*;
pub use crate::sdk::*;
pub use crate::state::*;
}

View File

@@ -1,36 +0,0 @@
use steel::*;
use crate::{
consts::*,
state::{Config, Treasury},
};
pub trait OreAccountInfoValidation {
fn is_bus(&self) -> Result<&Self, ProgramError>;
fn is_config(&self) -> Result<&Self, ProgramError>;
fn is_treasury(&self) -> Result<&Self, ProgramError>;
fn is_treasury_tokens(&self) -> Result<&Self, ProgramError>;
}
impl OreAccountInfoValidation for AccountInfo<'_> {
fn is_bus(&self) -> Result<&Self, ProgramError> {
if !BUS_ADDRESSES.contains(self.key) {
return Err(ProgramError::InvalidSeeds);
}
Ok(self)
}
fn is_config(&self) -> Result<&Self, ProgramError> {
self.has_address(&CONFIG_ADDRESS)?
.is_type::<Config>(&crate::ID)
}
fn is_treasury(&self) -> Result<&Self, ProgramError> {
self.has_address(&TREASURY_ADDRESS)?
.is_type::<Treasury>(&crate::ID)
}
fn is_treasury_tokens(&self) -> Result<&Self, ProgramError> {
self.has_address(&TREASURY_TOKENS_ADDRESS)
}
}

View File

@@ -1,136 +1,3 @@
use steel::*;
use crate::{consts::*, instruction::*, state::proof_pda};
/// Builds a claim instruction.
pub fn claim(signer: Pubkey, beneficiary: Pubkey, amount: u64) -> Instruction {
let proof = proof_pda(signer).0;
Instruction {
program_id: crate::ID,
accounts: vec![
AccountMeta::new(signer, true),
AccountMeta::new(beneficiary, false),
AccountMeta::new(proof, false),
AccountMeta::new_readonly(TREASURY_ADDRESS, false),
AccountMeta::new(TREASURY_TOKENS_ADDRESS, false),
AccountMeta::new_readonly(spl_token::ID, false),
],
data: Claim {
amount: amount.to_le_bytes(),
}
.to_bytes(),
}
}
/// Builds a close instruction.
pub fn close(signer: Pubkey) -> Instruction {
let proof = proof_pda(signer).0;
Instruction {
program_id: crate::ID,
accounts: vec![
AccountMeta::new(signer, true),
AccountMeta::new(proof, false),
AccountMeta::new_readonly(solana_program::system_program::ID, false),
],
data: Close {}.to_bytes(),
}
}
/// Builds a mine instruction.
pub fn mine(
signer: Pubkey,
authority: Pubkey,
bus: Pubkey,
nonce: u64,
boost_config: Pubkey,
) -> Instruction {
let proof = proof_pda(authority).0;
let accounts = vec![
AccountMeta::new(signer, true),
AccountMeta::new(bus, false),
AccountMeta::new_readonly(CONFIG_ADDRESS, false),
AccountMeta::new(proof, false),
AccountMeta::new_readonly(sysvar::instructions::ID, false),
AccountMeta::new_readonly(sysvar::slot_hashes::ID, false),
AccountMeta::new_readonly(boost_config, false),
AccountMeta::new(proof_pda(boost_config).0, false),
];
Instruction {
program_id: crate::ID,
accounts,
data: Mine {
// digest: solution.d,
nonce: nonce.to_le_bytes(),
}
.to_bytes(),
}
}
/// Builds an open instruction.
pub fn open(signer: Pubkey, miner: Pubkey, payer: Pubkey) -> Instruction {
let proof_pda = proof_pda(signer);
Instruction {
program_id: crate::ID,
accounts: vec![
AccountMeta::new(signer, true),
AccountMeta::new_readonly(miner, false),
AccountMeta::new(payer, true),
AccountMeta::new(proof_pda.0, false),
AccountMeta::new_readonly(solana_program::system_program::ID, false),
AccountMeta::new_readonly(sysvar::slot_hashes::ID, false),
],
data: Open {}.to_bytes(),
}
}
/// Builds a reset instruction.
pub fn reset(signer: Pubkey, best_proof: Pubkey) -> Instruction {
Instruction {
program_id: crate::ID,
accounts: vec![
AccountMeta::new(signer, true),
AccountMeta::new(CONFIG_ADDRESS, false),
AccountMeta::new(MINT_ADDRESS, false),
AccountMeta::new(best_proof, false),
AccountMeta::new(TREASURY_ADDRESS, false),
AccountMeta::new(TREASURY_TOKENS_ADDRESS, false),
AccountMeta::new_readonly(spl_token::ID, false),
],
data: Reset {}.to_bytes(),
}
}
// Build an update instruction.
pub fn update(signer: Pubkey, miner: Pubkey) -> Instruction {
let proof = proof_pda(signer).0;
Instruction {
program_id: crate::ID,
accounts: vec![
AccountMeta::new(signer, true),
AccountMeta::new_readonly(miner, false),
AccountMeta::new(proof, false),
],
data: Update {}.to_bytes(),
}
}
// Builds an initialize instruction.
pub fn initialize(signer: Pubkey) -> Instruction {
Instruction {
program_id: crate::ID,
accounts: vec![
AccountMeta::new(signer, true),
AccountMeta::new(CONFIG_ADDRESS, false),
AccountMeta::new(METADATA_ADDRESS, false),
AccountMeta::new(MINT_ADDRESS, false),
AccountMeta::new(TREASURY_ADDRESS, false),
AccountMeta::new(TREASURY_TOKENS_ADDRESS, false),
AccountMeta::new_readonly(system_program::ID, false),
AccountMeta::new_readonly(spl_token::ID, false),
AccountMeta::new_readonly(spl_associated_token_account::ID, false),
AccountMeta::new_readonly(mpl_token_metadata::ID, false),
AccountMeta::new_readonly(sysvar::rent::ID, false),
],
data: Initialize {}.to_bytes(),
}
}
use crate::{consts::*, instruction::*, state::*};

36
api/src/state/block.rs Normal file
View File

@@ -0,0 +1,36 @@
use steel::*;
use super::OreAccount;
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
pub struct Block {
/// The current round.
pub current_round: u64,
/// The cumulative amount of SOL risked in the current round, up to and including this bet.
pub total_bets: u64,
/// The number of bets made in the current round.
pub bet_count: u64,
/// The time time the current round started.
pub started_at: u64,
/// The slot at which the current round ends.
pub ends_at: u64,
/// Whether or not the current round has ended.
pub payed_out: u64,
/// The mint used to track wagers of the current round.
pub mint: Pubkey,
/// The amount of ORE to distribute to the winner.
pub reward: u64,
/// The noise used for the current round.
pub noise: [u8; 32],
}
account!(OreAccount, Block);

View File

@@ -1,25 +0,0 @@
use steel::*;
use super::OreAccount;
/// Bus accounts are responsible for distributing mining rewards. There are 8 busses total
/// to minimize write-lock contention and allow Solana to process mine instructions in parallel.
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
pub struct Bus {
/// The ID of the bus account.
pub id: u64,
/// The remaining rewards this bus has left to payout in the current epoch.
pub rewards: u64,
/// The rewards this bus would have paid out in the current epoch if there no limit.
/// This is used to calculate the updated reward rate.
pub theoretical_rewards: u64,
/// The largest known stake balance seen by the bus this epoch.
#[deprecated(since = "2.8.0", note = "Top balance is no longer tracked or used")]
pub top_balance: u64,
}
account!(OreAccount, Bus);

View File

@@ -1,43 +0,0 @@
use steel::*;
use super::{OldOreAccount, OreAccount};
/// Config is a singleton account which manages program global variables.
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
pub struct Config {
/// The timestamp of the last reset.
pub last_reset_at: i64,
/// The best difficulty score of this epoch.
pub best_hash: [u8; 32],
/// The proof of the best submitted hash of this epoch.
pub best_proof: Pubkey,
/// The challenge of this epoch.
pub challenge: [u8; 32],
/// The target emissions rate in ORE/min.
pub block_reward: u64,
}
/// Config is a singleton account which manages program global variables.
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
pub struct OldConfig {
/// The base reward rate paid out for a hash of minimum difficulty.
pub base_reward_rate: u64,
/// The timestamp of the last reset.
pub last_reset_at: i64,
/// The minimum accepted difficulty.
pub min_difficulty: u64,
/// The target emissions rate in ORE/min.
pub target_emmissions_rate: u64,
}
account!(OreAccount, Config);
account!(OldOreAccount, OldConfig);

View File

@@ -1,12 +1,12 @@
mod bus;
mod config;
mod block;
mod proof;
mod treasury;
mod wager;
pub use bus::*;
pub use config::*;
pub use block::*;
pub use proof::*;
pub use treasury::*;
pub use wager::*;
use steel::*;
@@ -15,29 +15,27 @@ use crate::consts::*;
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
pub enum OreAccount {
Bus = 100,
Config = 101,
Proof = 102,
Treasury = 103,
Block = 104,
Wager = 105,
}
/// Derive the PDA of the config account.
pub fn config_pda() -> (Pubkey, u8) {
Pubkey::find_program_address(&[CONFIG], &crate::id())
pub fn block_pda() -> (Pubkey, u8) {
Pubkey::find_program_address(&[BLOCK], &crate::ID)
}
/// Derive the PDA of a proof account.
pub fn proof_pda(authority: Pubkey) -> (Pubkey, u8) {
Pubkey::find_program_address(&[PROOF, authority.as_ref()], &crate::id())
}
/// Derive the PDA of the treasury account.
pub fn treasury_pda() -> (Pubkey, u8) {
Pubkey::find_program_address(&[TREASURY], &crate::id())
pub fn wager_pda(round: u64, id: u64) -> (Pubkey, u8) {
Pubkey::find_program_address(
&[WAGER, &round.to_le_bytes(), &id.to_le_bytes()],
&crate::ID,
)
}
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
pub enum OldOreAccount {
OldConfig = 101,
pub fn treasury_pda() -> (Pubkey, u8) {
Pubkey::find_program_address(&[TREASURY], &crate::ID)
}

27
api/src/state/wager.rs Normal file
View File

@@ -0,0 +1,27 @@
use steel::*;
use super::OreAccount;
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
pub struct Wager {
/// The signer authorized to use this wager.
pub authority: Pubkey,
/// The current round this miner is betting in.
pub round: u64,
/// The ID of the bet.
pub id: u64,
/// The quantity of SOL this miner has bet in the current round.
pub amount: u64,
/// The cumulative amount of SOL bet in the current round, up to and including this wager.
pub cumulative_bets: u64,
/// The timestamp of the wager.
pub timestamp: u64,
}
account!(OreAccount, Wager);