This commit is contained in:
Hardhat Chad
2025-09-28 18:19:19 -07:00
parent 9d0e096edd
commit 8af79a8d2f
16 changed files with 442 additions and 332 deletions

View File

@@ -38,6 +38,9 @@ pub const SEEKER: &[u8] = b"seeker";
/// The seed of the square account PDA.
pub const SQUARE: &[u8] = b"square";
/// The seed of the stake account PDA.
pub const STAKE: &[u8] = b"stake";
/// The seed of the treasury account PDA.
pub const TREASURY: &[u8] = b"treasury";

View File

@@ -53,7 +53,10 @@ pub struct BuryEvent {
pub disc: u64,
/// The amount of ORE buried.
pub ore_amount: u64,
pub ore_buried: u64,
/// The amount of ORE shared with stakers.
pub ore_shared: u64,
/// The amount of SOL swapped.
pub sol_amount: u64,

View File

@@ -3,7 +3,7 @@ use steel::*;
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)]
pub enum OreInstruction {
// User
// Miner
Automate = 0,
Boost = 1,
ClaimSOL = 2,
@@ -13,11 +13,16 @@ pub enum OreInstruction {
Log = 6,
Reset = 7,
// Staker
Deposit = 8,
Withdraw = 9,
ClaimYield = 10,
// Admin
Bury = 9,
Wrap = 10,
SetAdmin = 11,
SetFeeCollector = 12,
Bury = 11,
Wrap = 12,
SetAdmin = 13,
SetFeeCollector = 14,
// Seeker
ClaimSeeker = 15,
@@ -118,6 +123,24 @@ pub struct Bury {
pub min_amount_out: [u8; 8],
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct Deposit {
pub amount: [u8; 8],
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct Withdraw {
pub amount: [u8; 8],
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct ClaimYield {
pub amount: [u8; 8],
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct ClaimSeeker {}
@@ -138,5 +161,8 @@ instruction!(OreInstruction, Bury);
instruction!(OreInstruction, Reset);
instruction!(OreInstruction, SetAdmin);
instruction!(OreInstruction, SetFeeCollector);
instruction!(OreInstruction, Deposit);
instruction!(OreInstruction, Withdraw);
instruction!(OreInstruction, ClaimYield);
instruction!(OreInstruction, ClaimSeeker);
instruction!(OreInstruction, MigrateMiner);

View File

@@ -15,10 +15,7 @@ pub struct Miner {
/// Unused buffer.
#[deprecated(note = "No longer used")]
pub buffer: [u8; 24],
/// Whether this miner is associated with a Solana Seeker.
pub is_seeker: u64,
pub buffer: [u8; 32],
/// The amount of SOL this miner has had refunded and may claim.
pub refund_sol: u64,

View File

@@ -4,6 +4,7 @@ mod config;
mod miner;
mod seeker;
mod square;
mod stake;
mod treasury;
pub use automation::*;
@@ -12,6 +13,7 @@ pub use config::*;
pub use miner::*;
pub use seeker::*;
pub use square::*;
pub use stake::*;
pub use treasury::*;
use crate::consts::*;
@@ -30,6 +32,7 @@ pub enum OreAccount {
Board = 105,
Square = 106,
Seeker = 107,
Stake = 108,
}
pub fn automation_pda(authority: Pubkey) -> (Pubkey, u8) {
@@ -56,6 +59,10 @@ pub fn square_pda() -> (Pubkey, u8) {
Pubkey::find_program_address(&[SQUARE], &crate::ID)
}
pub fn stake_pda(authority: Pubkey) -> (Pubkey, u8) {
Pubkey::find_program_address(&[STAKE, &authority.to_bytes()], &crate::ID)
}
pub fn treasury_pda() -> (Pubkey, u8) {
Pubkey::find_program_address(&[TREASURY], &crate::ID)
}

92
api/src/state/stake.rs Normal file
View File

@@ -0,0 +1,92 @@
use steel::*;
use crate::state::{stake_pda, Treasury};
use super::OreAccount;
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
pub struct Stake {
/// The authority of this miner account.
pub authority: Pubkey,
/// The balance of this stake account.
pub balance: u64,
/// The timestamp of last claim.
pub last_claim_at: i64,
/// The timestamp the last time this staker deposited.
pub last_deposit_at: i64,
/// The timestamp the last time this staker withdrew.
pub last_withdraw_at: i64,
/// The rewards factor last time rewards were updated on this stake account.
pub rewards_factor: Numeric,
/// The amount of ORE this staker can claim.
pub rewards: u64,
/// The total amount of ORE this staker has earned over its lifetime.
pub lifetime_rewards: u64,
/// Flag indicating whether this staker is associated with a Solana Seeker.
pub is_seeker: u64,
}
impl Stake {
pub fn pda(&self) -> (Pubkey, u8) {
stake_pda(self.authority)
}
pub fn claim(&mut self, amount: u64, clock: &Clock, treasury: &Treasury) -> u64 {
self.update_rewards(treasury);
let amount = self.rewards.min(amount);
self.rewards -= amount;
self.last_claim_at = clock.unix_timestamp;
amount
}
pub fn deposit(
&mut self,
amount: u64,
clock: &Clock,
treasury: &mut Treasury,
sender: &TokenAccount,
) -> u64 {
self.update_rewards(treasury);
let amount = sender.amount().min(amount);
self.balance += amount;
self.last_deposit_at = clock.unix_timestamp;
treasury.total_staked += amount;
amount
}
pub fn withdraw(&mut self, amount: u64, clock: &Clock, treasury: &mut Treasury) -> u64 {
self.update_rewards(treasury);
let amount = self.balance.min(amount);
self.balance -= amount;
self.last_withdraw_at = clock.unix_timestamp;
treasury.total_staked -= amount;
amount
}
fn update_rewards(&mut self, treasury: &Treasury) {
// Accumulate rewards, weighted by stake balance.
if treasury.rewards_factor > self.rewards_factor {
let accumulated_rewards = treasury.rewards_factor - self.rewards_factor;
if accumulated_rewards < Numeric::ZERO {
panic!("Accumulated rewards is negative");
}
let personal_rewards = accumulated_rewards * Numeric::from_u64(self.balance);
self.rewards += personal_rewards.to_u64();
self.lifetime_rewards += personal_rewards.to_u64();
}
// Update this stake account's last seen rewards factor.
self.rewards_factor = treasury.rewards_factor;
}
}
account!(OreAccount, Stake);

View File

@@ -10,8 +10,14 @@ pub struct Treasury {
// The amount of SOL collected for buy-bury operations.
pub balance: u64,
/// The amount of ORE in the motherlode.
/// The amount of ORE in the motherlode rewards pool.
pub motherlode: u64,
/// The cumulative ORE distributed to stakers, divided by the total stake at the time of distribution.
pub rewards_factor: Numeric,
/// The current total amount of ORE staked.
pub total_staked: u64,
}
account!(OreAccount, Treasury);