mirror of
https://github.com/d0zingcat/ore.git
synced 2026-05-13 23:16:52 +00:00
variable rewards
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user