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

View File

@@ -24,12 +24,6 @@ pub const ONE_ORE: u64 = 10u64.pow(TOKEN_DECIMALS as u32);
/// The duration of one minute, in seconds.
pub const ONE_MINUTE: i64 = 60;
/// The duration of one day, in seconds.
pub const ONE_DAY: i64 = 86400;
/// The duration of one year, in minutes.
pub const ONE_YEAR: u64 = 525600;
/// The number of minutes in an Ore epoch.
pub const EPOCH_MINUTES: i64 = 1;

View File

@@ -72,6 +72,13 @@ pub enum OreInstruction {
#[account(5, name = "token_program", desc = "SPL token program")]
Stake = 5,
#[account(0, name = "ore_program", desc = "Ore program")]
#[account(1, name = "signer", desc = "Signer", signer)]
#[account(2, name = "config", desc = "Ore config account", writable)]
#[account(3, name = "proof", desc = "Ore proof account current top staker")]
#[account(4, name = "proof_new", desc = "Ore proof account new top staker")]
Crown = 6,
#[account(0, name = "ore_program", desc = "Ore program")]
#[account(1, name = "signer", desc = "Signer", signer)]
#[account(2, name = "beneficiary", desc = "Beneficiary token account", writable)]
@@ -80,7 +87,7 @@ pub enum OreInstruction {
#[account(5, name = "mint", desc = "Ore token mint account", writable)]
#[account(6, name = "mint_v1", desc = "Ore v1 token mint account", writable)]
#[account(7, name = "token_program", desc = "SPL token program")]
Upgrade = 6,
Upgrade = 7,
#[account(0, name = "ore_program", desc = "Ore program")]
#[account(1, name = "signer", desc = "Admin signer", signer)]

View File

@@ -35,6 +35,7 @@ pub fn process_instruction(
match OreInstruction::try_from(*tag).or(Err(ProgramError::InvalidInstructionData))? {
OreInstruction::Open => process_open(program_id, accounts, data)?,
OreInstruction::Close => process_close(program_id, accounts, data)?,
OreInstruction::Crown => process_crown(program_id, accounts, data)?,
OreInstruction::Mine => process_mine(program_id, accounts, data)?,
OreInstruction::Claim => process_claim(program_id, accounts, data)?,
OreInstruction::Stake => process_stake(program_id, accounts, data)?,

View File

@@ -158,6 +158,34 @@ pub fn load_proof<'a, 'info>(
Ok(())
}
/// Errors if:
/// - Owner is not Ore program.
/// - Data is empty.
/// - Data cannot deserialize into a proof account.
/// - Expected to be writable, but is not.
pub fn load_any_proof<'a, 'info>(
info: &'a AccountInfo<'info>,
is_writable: bool,
) -> Result<(), ProgramError> {
if info.owner.ne(&crate::id()) {
return Err(ProgramError::InvalidAccountOwner);
}
if info.data_is_empty() {
return Err(ProgramError::UninitializedAccount);
}
if info.data.borrow()[0].ne(&(Proof::discriminator() as u8)) {
return Err(solana_program::program_error::ProgramError::InvalidAccountData);
}
if is_writable && !info.is_writable {
return Err(ProgramError::InvalidAccountData);
}
Ok(())
}
/// Errors if:
/// - Owner is not Ore program.
/// - Address does not match the expected address.

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(

View File

@@ -20,8 +20,11 @@ pub struct Config {
/// The timestamp of the last reset.
pub last_reset_at: i64,
/// The largest stake account on the network.
/// The largest known stake balance on the network.
pub max_stake: u64,
/// The address of the proof account with the highest stake balance.
pub top_staker: Pubkey,
}
impl Discriminator for Config {