This commit is contained in:
Hardhat Chad
2025-10-08 16:34:53 -07:00
parent d4b4db8927
commit 44f4021834
15 changed files with 393 additions and 49 deletions

View File

@@ -28,6 +28,8 @@ pub enum OreInstruction {
// Seeker
ClaimSeeker = 17,
MigrateTreasury = 18,
MigrateMiner = 19,
}
#[repr(C)]
@@ -46,15 +48,11 @@ pub struct Boost {}
#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct ClaimSOL {
pub amount: [u8; 8],
}
pub struct ClaimSOL {}
#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct ClaimORE {
pub amount: [u8; 8],
}
pub struct ClaimORE {}
#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
@@ -154,6 +152,14 @@ pub struct Checkpoint {}
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct Close {}
#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct MigrateTreasury {}
#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct MigrateMiner {}
instruction!(OreInstruction, Automate);
instruction!(OreInstruction, Boost);
instruction!(OreInstruction, Close);
@@ -172,3 +178,5 @@ instruction!(OreInstruction, Deposit);
instruction!(OreInstruction, Withdraw);
instruction!(OreInstruction, ClaimYield);
instruction!(OreInstruction, ClaimSeeker);
instruction!(OreInstruction, MigrateTreasury);
instruction!(OreInstruction, MigrateMiner);

View File

@@ -105,7 +105,7 @@ pub fn initialize(signer: Pubkey) -> Instruction {
}
}
pub fn claim_sol(signer: Pubkey, amount: u64) -> Instruction {
pub fn claim_sol(signer: Pubkey) -> Instruction {
let miner_address = miner_pda(signer).0;
Instruction {
program_id: crate::ID,
@@ -114,16 +114,13 @@ pub fn claim_sol(signer: Pubkey, amount: u64) -> Instruction {
AccountMeta::new(miner_address, false),
AccountMeta::new_readonly(system_program::ID, false),
],
data: ClaimSOL {
amount: amount.to_le_bytes(),
}
.to_bytes(),
data: ClaimSOL {}.to_bytes(),
}
}
// let [signer_info, miner_info, mint_info, recipient_info, treasury_info, treasury_tokens_info, system_program, token_program, associated_token_program] =
pub fn claim_ore(signer: Pubkey, amount: u64) -> Instruction {
pub fn claim_ore(signer: Pubkey) -> Instruction {
let miner_address = miner_pda(signer).0;
let treasury_address = treasury_pda().0;
let treasury_tokens_address = get_associated_token_address(&treasury_address, &MINT_ADDRESS);
@@ -141,10 +138,7 @@ pub fn claim_ore(signer: Pubkey, amount: u64) -> Instruction {
AccountMeta::new_readonly(spl_token::ID, false),
AccountMeta::new_readonly(spl_associated_token_account::ID, false),
],
data: ClaimORE {
amount: amount.to_le_bytes(),
}
.to_bytes(),
data: ClaimORE {}.to_bytes(),
}
}
@@ -190,6 +184,35 @@ pub fn deploy(
}
}
pub fn migrate_miner(signer: Pubkey, address: Pubkey) -> Instruction {
let config_address = config_pda().0;
Instruction {
program_id: crate::ID,
accounts: vec![
AccountMeta::new(signer, true),
AccountMeta::new(config_address, false),
AccountMeta::new(address, false),
AccountMeta::new_readonly(system_program::ID, false),
],
data: MigrateMiner {}.to_bytes(),
}
}
pub fn migrate_treasury(signer: Pubkey) -> Instruction {
let config_address = config_pda().0;
let treasury_address = treasury_pda().0;
Instruction {
program_id: crate::ID,
accounts: vec![
AccountMeta::new(signer, true),
AccountMeta::new(config_address, false),
AccountMeta::new(treasury_address, false),
AccountMeta::new_readonly(system_program::ID, false),
],
data: MigrateTreasury {}.to_bytes(),
}
}
const POOL_ADDRESS: Pubkey = pubkey!("GgaDTFbqdgjoZz3FP7zrtofGwnRS4E6MCzmmD5Ni1Mxj");
const TOKEN_A_MINT: Pubkey = MINT_ADDRESS; // pubkey!("oreoU2P8bN6jkk3jbaiVxYnG1dCXcYxwhwyK9jSybcp");
const TOKEN_B_MINT: Pubkey = SOL_MINT; //pubkey!("So11111111111111111111111111111111111111112");

View File

@@ -1,6 +1,6 @@
use steel::*;
use crate::state::{miner_pda, Treasury};
use crate::state::{miner_pda, OreAccountOLD, Treasury};
use super::OreAccount;
@@ -22,6 +22,52 @@ pub struct Miner {
/// The last round that this miner checkpointed.
pub checkpoint_id: u64,
/// The last time this miner claimed ORE rewards.
pub last_claim_ore_at: i64,
/// The last time this miner claimed SOL rewards.
pub last_claim_sol_at: i64,
/// The rewards factor last time rewards were updated on this miner account.
pub rewards_factor: Numeric,
/// The amount of SOL this miner can claim.
pub rewards_sol: u64,
/// The amount of ORE this miner can claim.
pub rewards_ore: u64,
/// The amount of ORE this miner has earned from claim fees.
pub refined_ore: u64,
/// The ID of the round this miner last played in.
pub round_id: u64,
/// The total amount of SOL this miner has mined across all blocks.
pub lifetime_rewards_sol: u64,
/// The total amount of ORE this miner has mined across all blocks.
pub lifetime_rewards_ore: u64,
}
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
pub struct MinerOLD {
/// The authority of this miner account.
pub authority: Pubkey,
/// The miner's prospects in the current round.
pub deployed: [u64; 25],
/// The cumulative amount of SOL deployed on each square prior to this miner's move.
pub cumulative: [u64; 25],
/// SOL witheld in reserve to pay for checkpointing.
pub checkpoint_fee: u64,
/// The last round that this miner checkpointed.
pub checkpoint_id: u64,
/// The amount of SOL this miner can claim.
pub rewards_sol: u64,
@@ -43,18 +89,52 @@ impl Miner {
miner_pda(self.authority)
}
pub fn claim_ore(&mut self, amount: u64, treasury: &mut Treasury) -> u64 {
let amount = self.rewards_ore.min(amount);
self.rewards_ore -= amount;
treasury.total_unclaimed -= amount;
pub fn claim_ore(&mut self, clock: &Clock, treasury: &mut Treasury) -> u64 {
self.update_rewards(treasury);
let refined_ore = self.refined_ore;
let rewards_ore = self.rewards_ore;
let mut amount = refined_ore + rewards_ore;
self.refined_ore = 0;
self.rewards_ore = 0;
treasury.total_unclaimed -= rewards_ore;
treasury.total_refined -= refined_ore;
self.last_claim_ore_at = clock.unix_timestamp;
// Charge a 10% fee and share with miners who haven't claimed yet.
if treasury.total_unclaimed > 0 {
let fee = rewards_ore / 10;
amount -= fee;
treasury.miner_rewards_factor += Numeric::from_fraction(fee, treasury.total_unclaimed);
treasury.total_refined += fee;
self.lifetime_rewards_ore -= fee;
}
amount
}
pub fn claim_sol(&mut self, amount: u64) -> u64 {
let amount = self.rewards_sol.min(amount);
self.rewards_sol -= amount;
pub fn claim_sol(&mut self, clock: &Clock) -> u64 {
let amount = self.rewards_sol;
self.rewards_sol = 0;
self.last_claim_sol_at = clock.unix_timestamp;
amount
}
pub fn update_rewards(&mut self, treasury: &Treasury) {
// Accumulate rewards, weighted by stake balance.
if treasury.miner_rewards_factor > self.rewards_factor {
let accumulated_rewards = treasury.miner_rewards_factor - self.rewards_factor;
if accumulated_rewards < Numeric::ZERO {
panic!("Accumulated rewards is negative");
}
let personal_rewards = accumulated_rewards * Numeric::from_u64(self.rewards_ore);
self.refined_ore += personal_rewards.to_u64();
self.lifetime_rewards_ore += personal_rewards.to_u64();
}
// Update this miner account's last seen rewards factor.
self.rewards_factor = treasury.miner_rewards_factor;
}
}
account!(OreAccount, Miner);
account!(OreAccountOLD, MinerOLD);

View File

@@ -35,6 +35,13 @@ pub enum OreAccount {
Round = 109,
}
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
pub enum OreAccountOLD {
MinerOLD = 103,
TreasuryOLD = 104,
}
pub fn automation_pda(authority: Pubkey) -> (Pubkey, u8) {
Pubkey::find_program_address(&[AUTOMATION, &authority.to_bytes()], &crate::ID)
}

View File

@@ -74,8 +74,8 @@ impl Stake {
pub 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 treasury.stake_rewards_factor > self.rewards_factor {
let accumulated_rewards = treasury.stake_rewards_factor - self.rewards_factor;
if accumulated_rewards < Numeric::ZERO {
panic!("Accumulated rewards is negative");
}
@@ -85,7 +85,7 @@ impl Stake {
}
// Update this stake account's last seen rewards factor.
self.rewards_factor = treasury.rewards_factor;
self.rewards_factor = treasury.stake_rewards_factor;
}
}

View File

@@ -1,5 +1,7 @@
use steel::*;
use crate::state::OreAccountOLD;
use super::OreAccount;
/// Treasury is a singleton account which is the mint authority for the ORE token and the authority of
@@ -13,8 +15,36 @@ pub struct Treasury {
/// The amount of ORE in the motherlode rewards pool.
pub motherlode: u64,
/// The cumulative ORE distributed to miners, divided by the total unclaimed ORE at the time of distribution.
pub miner_rewards_factor: Numeric,
/// The cumulative ORE distributed to stakers, divided by the total stake at the time of distribution.
pub rewards_factor: Numeric,
pub stake_rewards_factor: Numeric,
/// The current total amount of ORE staking deposits.
pub total_staked: u64,
/// The current total amount of unclaimed ORE mining rewards.
pub total_unclaimed: u64,
/// The current total amount of refined ORE mining rewards.
pub total_refined: u64,
}
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
pub struct TreasuryOLD {
// The amount of SOL collected for buy-bury operations.
pub balance: u64,
/// The amount of ORE in the motherlode rewards pool.
pub motherlode: u64,
/// The cumulative ORE distributed to miners, divided by the total unclaimed ORE at the time of distribution.
pub miner_rewards_factor: Numeric,
/// The cumulative ORE distributed to stakers, divided by the total stake at the time of distribution.
pub stake_rewards_factor: Numeric,
/// The current total amount of ORE staking deposits.
pub total_staked: u64,
@@ -24,3 +54,4 @@ pub struct Treasury {
}
account!(OreAccount, Treasury);
account!(OreAccountOLD, TreasuryOLD);