crown new top staker

This commit is contained in:
Hardhat Chad
2024-06-27 14:01:52 +00:00
parent 77bf195139
commit 3608071f1c
10 changed files with 106 additions and 23 deletions

57
src/processor/crown.rs Normal file
View File

@@ -0,0 +1,57 @@
use solana_program::{
account_info::AccountInfo, entrypoint::ProgramResult, program_error::ProgramError,
pubkey::Pubkey,
};
use crate::{
loaders::*,
state::{Config, Proof},
utils::AccountDeserialize,
};
/// Crown marks an account as the top staker if their balance is greater than the last known top staker.
pub fn process_crown<'a, 'info>(
_program_id: &Pubkey,
accounts: &'a [AccountInfo<'info>],
_data: &[u8],
) -> ProgramResult {
// Load accounts
let [signer, config_info, proof_info, proof_new_info] = accounts else {
return Err(ProgramError::NotEnoughAccountKeys);
};
load_signer(signer)?;
load_config(config_info, true)?;
load_any_proof(proof_new_info, false)?;
// Load config
let mut config_data = config_info.data.borrow_mut();
let config = Config::try_from_bytes_mut(&mut config_data)?;
// Load proposed new top staker
let proof_new_data = proof_new_info.data.borrow();
let proof_new = Proof::try_from_bytes(&proof_new_data)?;
// If top staker is not the default null address, then compare balances
if config.top_staker.ne(&Pubkey::new_from_array([0; 32])) {
// Load current top staker
load_any_proof(proof_info, false)?;
let proof_data = proof_info.data.borrow();
let proof = Proof::try_from_bytes(&proof_data)?;
// Require the provided proof account is the current top staker
if proof_info.key.ne(&config.top_staker) {
return Err(ProgramError::InvalidAccountData);
}
// Compare balances
if proof_new.balance.lt(&proof.balance) {
return Err(ProgramError::InvalidAccountData);
}
}
// Crown the new top staker
config.max_stake = proof_new.balance;
config.top_staker = *proof_new_info.key;
Ok(())
}

View File

@@ -138,6 +138,8 @@ pub fn process_initialize<'a, 'info>(
config.admin = *signer.key;
config.base_reward_rate = INITIAL_BASE_REWARD_RATE;
config.last_reset_at = 0;
config.max_stake = 0;
config.top_staker = Pubkey::new_from_array([0; 32]);
// Initialize treasury
create_pda(

View File

@@ -24,7 +24,7 @@ use crate::{
loaders::*,
state::{Bus, Config, Proof},
utils::{AccountDeserialize, MineEvent},
EPOCH_DURATION, MIN_DIFFICULTY, ONE_MINUTE, ONE_YEAR, TOLERANCE,
EPOCH_DURATION, MIN_DIFFICULTY, ONE_MINUTE, TOLERANCE,
};
/// Mine is the primary workhorse instruction of the Ore program. Its responsibilities include:

View File

@@ -1,5 +1,6 @@
mod claim;
mod close;
mod crown;
mod initialize;
mod mine;
mod open;
@@ -9,6 +10,7 @@ mod upgrade;
pub use claim::*;
pub use close::*;
pub use crown::*;
pub use initialize::*;
pub use mine::*;
pub use open::*;

View File

@@ -4,11 +4,8 @@ use solana_program::{
};
use crate::{
instruction::StakeArgs,
loaders::*,
state::{Config, Proof},
utils::AccountDeserialize,
MINT_ADDRESS, TREASURY_ADDRESS,
instruction::StakeArgs, loaders::*, state::Proof, utils::AccountDeserialize, MINT_ADDRESS,
TREASURY_ADDRESS,
};
/// Stake deposits Ore into a miner's proof account to earn multiplier. Its responsibilies include:
@@ -29,13 +26,10 @@ pub fn process_stake<'a, 'info>(
let amount = u64::from_le_bytes(args.amount);
// Load accounts
let [signer, config_info, proof_info, sender_info, treasury_tokens_info, token_program] =
accounts
else {
let [signer, proof_info, sender_info, treasury_tokens_info, token_program] = accounts else {
return Err(ProgramError::NotEnoughAccountKeys);
};
load_signer(signer)?;
load_config(config_info, true)?;
load_proof(proof_info, signer.key, true)?;
load_token_account(sender_info, Some(signer.key), &MINT_ADDRESS, true)?;
load_token_account(
@@ -55,11 +49,6 @@ pub fn process_stake<'a, 'info>(
let clock = Clock::get().or(Err(ProgramError::InvalidAccountData))?;
proof.last_stake_at = clock.unix_timestamp;
// Update the max stake tracker
let mut config_data = config_info.data.borrow_mut();
let config = Config::try_from_bytes_mut(&mut config_data)?;
config.max_stake = config.max_stake.max(proof.balance);
// Distribute tokens from signer to treasury
solana_program::program::invoke(
&spl_token::instruction::transfer(