mirror of
https://github.com/d0zingcat/ore.git
synced 2026-05-17 15:10:21 +00:00
revshare
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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
92
api/src/state/stake.rs
Normal 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);
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user