variable rewards

This commit is contained in:
Hardhat Chad
2025-06-12 15:38:59 -07:00
parent a4719853f2
commit ea415c36e6
12 changed files with 148 additions and 21 deletions

View File

@@ -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;

View File

@@ -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);

View File

@@ -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)

View File

@@ -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,

View File

@@ -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,

View File

@@ -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,

View File

@@ -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,

View File

@@ -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);
}

View File

@@ -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,

View File

@@ -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;
}
}

View File

@@ -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::<Block>(&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>(
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);
}

View File

@@ -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();