begin testing new staking mechanism

This commit is contained in:
Hardhat Chad
2024-06-27 13:17:53 +00:00
parent f580f7fd58
commit 77bf195139
4 changed files with 29 additions and 16 deletions

View File

@@ -102,23 +102,23 @@ pub fn process_mine<'a, 'info>(
sol_log(&format!("Base {}", reward));
// Apply staking multiplier.
// The multiplier can range 1x to 2x. To receive the maximum multiplier, the stake balance must be
// greater than or equal to two years worth of rewards at the selected difficulty. Miners are only
// eligable for a multipler if their last stake deposit was more than one minute ago.
if proof
.last_stake_at
.saturating_add(ONE_MINUTE)
.le(&clock.unix_timestamp)
// If user has greater than or equal to the max stake on the network, they will receive 2x multiplier.
// Any less than this, and they will receive between 1x and 2x. Miners are only eligable for a multipler
// if their last stake deposit was more than one minute ago.
if config.max_stake.gt(&0)
&& proof
.last_stake_at
.saturating_add(ONE_MINUTE)
.le(&clock.unix_timestamp)
{
let upper_bound = reward.saturating_mul(ONE_YEAR);
let staking_reward = proof
.balance
.min(upper_bound)
.min(config.max_stake)
.saturating_mul(reward)
.saturating_div(upper_bound);
.saturating_div(config.max_stake);
reward = reward.saturating_add(staking_reward);
sol_log(&format!("Staking {}", staking_reward));
};
}
// Apply spam penalty
let t = clock.unix_timestamp;

View File

@@ -4,8 +4,11 @@ use solana_program::{
};
use crate::{
instruction::StakeArgs, loaders::*, state::Proof, utils::AccountDeserialize, MINT_ADDRESS,
TREASURY_ADDRESS,
instruction::StakeArgs,
loaders::*,
state::{Config, Proof},
utils::AccountDeserialize,
MINT_ADDRESS, TREASURY_ADDRESS,
};
/// Stake deposits Ore into a miner's proof account to earn multiplier. Its responsibilies include:
@@ -26,10 +29,13 @@ pub fn process_stake<'a, 'info>(
let amount = u64::from_le_bytes(args.amount);
// Load accounts
let [signer, proof_info, sender_info, treasury_tokens_info, token_program] = accounts else {
let [signer, config_info, 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(
@@ -49,6 +55,11 @@ 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(