diff --git a/api/src/consts.rs b/api/src/consts.rs index fa763e1..468fd42 100644 --- a/api/src/consts.rs +++ b/api/src/consts.rs @@ -73,5 +73,5 @@ pub const VIRTUAL_LIQUIDITY: u64 = ONE_ORE * 5; /// The minimum difficulty required for payout. pub const MIN_DIFFICULTY: u64 = 10; -/// The reward rate per satisfying hash (0.002048 ORE). -pub const REWARD_RATE: u64 = 204_800_000; +// The reward rate per satisfying hash (0.002048 ORE). +// pub const REWARD_RATE: u64 = 204_800_000; diff --git a/api/src/event.rs b/api/src/event.rs index c97692d..7080b95 100644 --- a/api/src/event.rs +++ b/api/src/event.rs @@ -6,7 +6,7 @@ use crate::state::SwapDirection; #[derive(Clone, Copy, Debug, Default, PartialEq, Pod, Zeroable)] pub struct SwapEvent { /// The authority of the swap. - pub authority: [u8; 32], + pub authority: Pubkey, /// The block id. pub block_id: u64, @@ -42,4 +42,18 @@ impl SwapEvent { } } +#[repr(C)] +#[derive(Clone, Copy, Debug, Default, PartialEq, Pod, Zeroable)] +pub struct RewardEvent { + /// The authority who received the reward. + pub authority: Pubkey, + + /// The block id. + pub block_id: u64, + + /// The amount of ORE distributed as a reward. + pub amount: u64, +} + event!(SwapEvent); +event!(RewardEvent); diff --git a/api/src/state/block.rs b/api/src/state/block.rs index a65a4c3..c5ab4c0 100644 --- a/api/src/state/block.rs +++ b/api/src/state/block.rs @@ -4,6 +4,12 @@ use crate::state::block_pda; use super::OreAccount; +// What could be variable? +// - Payout style (winner take all / difficulty / both) +// - Payout skew (larger / neutral / smaller) +// - Jackpot possiblity (yes / no) +// - Known / unknown + #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)] pub struct Block { @@ -11,10 +17,11 @@ pub struct Block { pub id: u64, /// The minimum difficulty required for payout. - pub min_difficulty: u64, + // pub min_difficulty: u64, /// The reward rate per satisfying hash. - pub reward_rate: u64, + // pub reward_rate: u64, + pub reward: RewardConfig, /// The hash of the starting slot. pub slot_hash: [u8; 32], @@ -28,10 +35,36 @@ pub struct Block { /// The total amount of rewards paid out to miners. pub total_rewards: u64, - /// The total number of hashes submitted to the block. + /// The total number of hashes that resulted in a payout. pub winning_hashes: u64, } +/// Configuration specifying how rewards are paid out. +#[repr(C)] +#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)] +pub struct RewardConfig { + /// The reward paid to the submitter of the best hash. + pub best_hash_reward: u64, + + /// The authority of the miner who submitted the best hash. + pub best_hash_authority: Pubkey, + + /// The best hash. + pub best_hash: [u8; 32], + + /// The reward rate paid to hashes satisfying the difficulty threshold. + pub difficulty_reward: u64, + + /// The minimum difficulty required for payout. + pub difficulty_threshold: u64, + + /// Jackpot amount. + pub jackpot_amount: u64, + + /// The threshold difficulty for the jackpot payout. + pub jackpot_threshold: u64, +} + impl Block { pub fn pda(&self) -> (Pubkey, u8) { block_pda(self.id) diff --git a/api/src/state/market/buy_exact_in.rs b/api/src/state/market/buy_exact_in.rs index d2c878a..b2a66d2 100644 --- a/api/src/state/market/buy_exact_in.rs +++ b/api/src/state/market/buy_exact_in.rs @@ -54,7 +54,7 @@ impl Market { // Produce swap result. let base_out = base_via_ask + base_via_curve; let swap_event = SwapEvent { - authority: [0; 32], + authority: Pubkey::default(), block_id: 0, direction: SwapDirection::Buy as u64, base_to_transfer: base_out as u64, diff --git a/api/src/state/market/buy_exact_out.rs b/api/src/state/market/buy_exact_out.rs index 4414aff..4afed20 100644 --- a/api/src/state/market/buy_exact_out.rs +++ b/api/src/state/market/buy_exact_out.rs @@ -1,3 +1,5 @@ +use steel::Pubkey; + use crate::error::OreError; use super::{Market, SwapDirection, TokenType, VirtualLimitOrder}; @@ -57,7 +59,7 @@ impl Market { // Produce swap result. let swap_event = SwapEvent { - authority: [0; 32], + authority: Pubkey::default(), block_id: 0, direction: SwapDirection::Buy as u64, base_to_transfer: base_out as u64, diff --git a/api/src/state/market/sell_exact_in.rs b/api/src/state/market/sell_exact_in.rs index 70447de..3c212b0 100644 --- a/api/src/state/market/sell_exact_in.rs +++ b/api/src/state/market/sell_exact_in.rs @@ -1,3 +1,5 @@ +use steel::Pubkey; + use crate::error::OreError; use super::{Market, SwapDirection, TokenType, VirtualLimitOrder}; @@ -61,7 +63,7 @@ impl Market { // Produce swap result. let quote_out = quote_via_bid + quote_via_curve; let swap_event = SwapEvent { - authority: [0; 32], + authority: Pubkey::default(), block_id: 0, direction: SwapDirection::Sell as u64, base_to_transfer: base_in as u64, diff --git a/api/src/state/market/sell_exact_out.rs b/api/src/state/market/sell_exact_out.rs index e947641..023765b 100644 --- a/api/src/state/market/sell_exact_out.rs +++ b/api/src/state/market/sell_exact_out.rs @@ -1,3 +1,5 @@ +use steel::Pubkey; + use crate::error::OreError; use super::{Market, SwapDirection, TokenType, VirtualLimitOrder}; @@ -59,7 +61,7 @@ impl Market { // Produce swap result. let swap_event = SwapEvent { - authority: [0; 32], + authority: Pubkey::default(), block_id: 0, direction: SwapDirection::Sell as u64, base_to_transfer: base_in as u64, diff --git a/cli/src/main.rs b/cli/src/main.rs index f21b1e3..6ce85c9 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -87,12 +87,12 @@ fn print_block(block: Block) { println!("Address: {:?}", address); println!(" Id: {:?}", block.id); println!(" Start slot: {:?}", block.start_slot); - println!( - " Reward rate: {:?}", - amount_to_ui_amount(block.reward_rate, TOKEN_DECIMALS) - ); + // println!( + // " Reward rate: {:?}", + // amount_to_ui_amount(block.reward_rate, TOKEN_DECIMALS) + // ); println!(" Slot hash: {:?}", block.slot_hash); - println!(" Min difficulty: {:?}", block.min_difficulty); + // println!(" Min difficulty: {:?}", block.min_difficulty); println!(" Total hashes: {:?}", block.total_hashes); println!(" Winning hashes: {:?}", block.winning_hashes); } diff --git a/program/src/close.rs b/program/src/close.rs index 5b872d6..167c6fc 100644 --- a/program/src/close.rs +++ b/program/src/close.rs @@ -5,7 +5,7 @@ use steel::*; pub fn process_close(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResult { // Load accounts. let clock = Clock::get()?; - let [signer_info, block_info, market_info, mint_base_info, mint_quote_info, vault_base_info, vault_quote_info, system_program, token_program] = + let [signer_info, block_info, market_info, mint_base_info, mint_quote_info, recipient_info, treasury_info, vault_base_info, vault_quote_info, system_program, token_program] = accounts else { return Err(ProgramError::NotEnoughAccountKeys); @@ -19,6 +19,9 @@ pub fn process_close(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResul .assert_mut(|m| m.id == block.id)?; mint_base_info.has_address(&market.base.mint)?.as_mint()?; mint_quote_info.has_address(&market.quote.mint)?.as_mint()?; + treasury_info + .is_writable()? + .has_address(&TREASURY_ADDRESS)?; let vault_base = vault_base_info.as_associated_token_account(market_info.key, mint_base_info.key)?; let vault_quote = @@ -26,6 +29,20 @@ pub fn process_close(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResul system_program.is_program(&system_program::ID)?; token_program.is_program(&spl_token::ID)?; + // Payout block reward. + if block.reward.best_hash_reward > 0 && block.reward.best_hash_authority != Pubkey::default() { + recipient_info + .as_associated_token_account(&block.reward.best_hash_authority, &MINT_ADDRESS)?; + mint_to_signed( + mint_quote_info, + recipient_info, + treasury_info, + token_program, + block.reward.best_hash_reward, + &[TREASURY], + )?; + } + // Burn base liquidity. burn_signed( vault_base_info, diff --git a/program/src/mine.rs b/program/src/mine.rs index 1b7a39a..eddaf33 100644 --- a/program/src/mine.rs +++ b/program/src/mine.rs @@ -104,10 +104,16 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult // Score and increment rewards. let score = difficulty(miner.hash) as u64; - if score >= block.min_difficulty { + if score >= block.reward.difficulty_threshold { block.winning_hashes += 1; miner.winning_hashes += 1; - miner_reward += block.reward_rate; + miner_reward += block.reward.difficulty_reward; + } + + // If hash is best hash, update best hash. + if miner.hash < block.reward.best_hash { + block.reward.best_hash = miner.hash; + block.reward.best_hash_authority = miner.authority; } } diff --git a/program/src/open.rs b/program/src/open.rs index 7ac5645..91e3713 100644 --- a/program/src/open.rs +++ b/program/src/open.rs @@ -1,4 +1,5 @@ use ore_api::prelude::*; +use solana_nostd_keccak::hash; use solana_program::program_pack::Pack; use spl_token_2022::instruction::AuthorityType; use steel::*; @@ -53,13 +54,31 @@ pub fn process_open(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult )?; let block = block_info.as_account_mut::(&ore_api::ID)?; block.id = id; - block.min_difficulty = MIN_DIFFICULTY; - block.reward_rate = REWARD_RATE; + block.reward = RewardConfig { + best_hash: [0; 32], + best_hash_authority: Pubkey::default(), + best_hash_reward: 0, + difficulty_threshold: MIN_DIFFICULTY, + difficulty_reward: 0, + jackpot_amount: 0, + jackpot_threshold: 0, + }; block.slot_hash = [0; 32]; block.start_slot = start_slot; block.total_hashes = 0; block.winning_hashes = 0; + // Select reward strategy. + let noise_seed = block.id.to_le_bytes(); + let noise = hash(&noise_seed); + let best_hash_reward = ONE_ORE * generate_best_hash_reward(noise) as u64; + let target_block_reward = ONE_ORE * 10; + let expected_hashes_per_block = HASH_TOKEN_SUPPLY / 2; + let expected_qualifying_hashes = expected_hashes_per_block / 2u64.pow(MIN_DIFFICULTY as u32); + let difficulty_reward = (target_block_reward - best_hash_reward) / expected_qualifying_hashes; + block.reward.best_hash_reward = best_hash_reward; + block.reward.difficulty_reward = difficulty_reward; + // Initialize market. create_program_account::( market_info, @@ -194,3 +213,35 @@ pub fn process_open(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult Ok(()) } + +fn generate_best_hash_reward(hash: [u8; 32]) -> u8 { + // Extract the first byte (0 to 255) + let byte_value = hash[0]; + + // Map to 1-10 using integer division + let reward = (byte_value / 25) + 1; + + // Ensure the value doesn't exceed 10 + if reward > 10 { + 10 + } else { + reward + } +} + +#[test] +fn test_hash_rewards() { + for i in 0u64..1000 { + let noise_seed = i.to_le_bytes(); + let noise = hash(&noise_seed); + let best_hash_reward = ONE_ORE * generate_best_hash_reward(noise) as u64; + let target_block_reward = ONE_ORE * 10; + let expected_hashes_per_block = HASH_TOKEN_SUPPLY / 2; + let expected_qualifying_hashes = + expected_hashes_per_block / 2u64.pow(MIN_DIFFICULTY as u32); + let difficulty_reward = + (target_block_reward - best_hash_reward) / expected_qualifying_hashes; + println!("{}: {} {}", i, best_hash_reward, difficulty_reward); + } + // assert!(false); +} diff --git a/program/src/swap.rs b/program/src/swap.rs index a1a0a9b..bd2edc0 100644 --- a/program/src/swap.rs +++ b/program/src/swap.rs @@ -100,7 +100,7 @@ pub fn process_swap(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult // Update market state. let mut swap_result = market.swap(amount, direction, precision, clock)?; - swap_result.authority = signer_info.key.to_bytes(); + swap_result.authority = *signer_info.key; swap_result.block_id = block.id; swap_result.log_return();