mirror of
https://github.com/d0zingcat/ore.git
synced 2026-05-14 15:10:13 +00:00
log base reward after boost rewards
This commit is contained in:
@@ -1,8 +1,5 @@
|
||||
use ore_api::{consts::*, error::OreError, instruction::*, loaders::*, state::Proof};
|
||||
use ore_utils::*;
|
||||
use solana_program::{
|
||||
account_info::AccountInfo, entrypoint::ProgramResult, program_error::ProgramError,
|
||||
};
|
||||
use ore_api::prelude::*;
|
||||
use steel::*;
|
||||
|
||||
/// Claim distributes claimable ORE from the treasury to a miner.
|
||||
pub fn process_claim(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
|
||||
@@ -11,21 +8,24 @@ pub fn process_claim(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
let amount = u64::from_le_bytes(args.amount);
|
||||
|
||||
// Load accounts.
|
||||
let [signer, beneficiary_info, proof_info, treasury_info, treasury_tokens_info, token_program] =
|
||||
let [signer_info, beneficiary_info, proof_info, treasury_info, treasury_tokens_info, token_program] =
|
||||
accounts
|
||||
else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
};
|
||||
load_signer(signer)?;
|
||||
load_token_account(beneficiary_info, None, &MINT_ADDRESS, true)?;
|
||||
load_proof(proof_info, signer.key, true)?;
|
||||
load_treasury(treasury_info, false)?;
|
||||
load_treasury_tokens(treasury_tokens_info, true)?;
|
||||
load_program(token_program, spl_token::id())?;
|
||||
signer_info.is_signer()?;
|
||||
beneficiary_info
|
||||
.is_writable()?
|
||||
.to_token_account()?
|
||||
.check(|t| t.mint == MINT_ADDRESS)?;
|
||||
let proof = proof_info
|
||||
.to_account_mut::<Proof>(&ore_api::ID)?
|
||||
.check_mut(|p| p.authority == *signer_info.key)?;
|
||||
treasury_info.is_treasury()?;
|
||||
treasury_tokens_info.is_writable()?.is_treasury_tokens()?;
|
||||
token_program.is_program(&spl_token::ID)?;
|
||||
|
||||
// Update miner balance.
|
||||
let mut proof_data = proof_info.data.borrow_mut();
|
||||
let proof = Proof::try_from_bytes_mut(&mut proof_data)?;
|
||||
proof.balance = proof
|
||||
.balance
|
||||
.checked_sub(amount)
|
||||
|
||||
@@ -1,33 +1,25 @@
|
||||
use ore_api::{loaders::*, state::Proof};
|
||||
use ore_utils::*;
|
||||
use solana_program::{
|
||||
account_info::AccountInfo, entrypoint::ProgramResult, program_error::ProgramError,
|
||||
system_program,
|
||||
};
|
||||
use ore_api::prelude::*;
|
||||
use steel::*;
|
||||
|
||||
/// Close closes a proof account and returns the rent to the owner.
|
||||
pub fn process_close(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResult {
|
||||
// Load accounts.
|
||||
let [signer, proof_info, system_program] = accounts else {
|
||||
let [signer_info, proof_info, system_program] = accounts else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
};
|
||||
load_signer(signer)?;
|
||||
load_proof(proof_info, signer.key, true)?;
|
||||
load_program(system_program, system_program::id())?;
|
||||
|
||||
// Validate balance is zero.
|
||||
let proof_data = proof_info.data.borrow();
|
||||
let proof = Proof::try_from_bytes(&proof_data)?;
|
||||
if proof.balance.gt(&0) {
|
||||
return Err(ProgramError::InvalidAccountData);
|
||||
}
|
||||
drop(proof_data);
|
||||
signer_info.is_signer()?;
|
||||
proof_info
|
||||
.is_writable()?
|
||||
.to_account::<Proof>(&ore_api::ID)?
|
||||
.check(|p| p.authority == *signer_info.key)?
|
||||
.check(|p| p.balance == 0)?;
|
||||
system_program.is_program(&system_program::ID)?;
|
||||
|
||||
// Realloc data to zero.
|
||||
proof_info.realloc(0, true)?;
|
||||
|
||||
// Send remaining lamports to signer.
|
||||
**signer.lamports.borrow_mut() += proof_info.lamports();
|
||||
**signer_info.lamports.borrow_mut() += proof_info.lamports();
|
||||
**proof_info.lamports.borrow_mut() = 0;
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -1,16 +1,7 @@
|
||||
use std::mem::size_of;
|
||||
|
||||
use ore_api::{
|
||||
consts::*,
|
||||
instruction::*,
|
||||
state::{Bus, Config, Treasury},
|
||||
};
|
||||
use ore_utils::*;
|
||||
use solana_program::{
|
||||
self, account_info::AccountInfo, entrypoint::ProgramResult, program_error::ProgramError,
|
||||
program_pack::Pack, system_program, sysvar,
|
||||
};
|
||||
use ore_api::prelude::*;
|
||||
use solana_program::program_pack::Pack;
|
||||
use spl_token::state::Mint;
|
||||
use steel::*;
|
||||
|
||||
/// Initialize sets up the ORE program to begin mining.
|
||||
pub fn process_initialize(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
|
||||
@@ -18,23 +9,49 @@ pub fn process_initialize(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramR
|
||||
let args = Initialize::try_from_bytes(data)?;
|
||||
|
||||
// Load accounts.
|
||||
let [signer, bus_0_info, bus_1_info, bus_2_info, bus_3_info, bus_4_info, bus_5_info, bus_6_info, bus_7_info, config_info, metadata_info, mint_info, treasury_info, treasury_tokens_info, system_program, token_program, associated_token_program, metadata_program, rent_sysvar] =
|
||||
let [signer_info, bus_0_info, bus_1_info, bus_2_info, bus_3_info, bus_4_info, bus_5_info, bus_6_info, bus_7_info, config_info, metadata_info, mint_info, treasury_info, treasury_tokens_info, system_program, token_program, associated_token_program, metadata_program, rent_sysvar] =
|
||||
accounts
|
||||
else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
};
|
||||
load_signer(signer)?;
|
||||
load_uninitialized_pda(bus_0_info, &[BUS, &[0]], args.bus_0_bump, &ore_api::id())?;
|
||||
load_uninitialized_pda(bus_1_info, &[BUS, &[1]], args.bus_1_bump, &ore_api::id())?;
|
||||
load_uninitialized_pda(bus_2_info, &[BUS, &[2]], args.bus_2_bump, &ore_api::id())?;
|
||||
load_uninitialized_pda(bus_3_info, &[BUS, &[3]], args.bus_3_bump, &ore_api::id())?;
|
||||
load_uninitialized_pda(bus_4_info, &[BUS, &[4]], args.bus_4_bump, &ore_api::id())?;
|
||||
load_uninitialized_pda(bus_5_info, &[BUS, &[5]], args.bus_5_bump, &ore_api::id())?;
|
||||
load_uninitialized_pda(bus_6_info, &[BUS, &[6]], args.bus_6_bump, &ore_api::id())?;
|
||||
load_uninitialized_pda(bus_7_info, &[BUS, &[7]], args.bus_7_bump, &ore_api::id())?;
|
||||
load_uninitialized_pda(config_info, &[CONFIG], args.config_bump, &ore_api::id())?;
|
||||
load_uninitialized_pda(
|
||||
metadata_info,
|
||||
signer_info.is_signer()?.has_address(&INITIALIZER_ADDRESS)?;
|
||||
bus_0_info
|
||||
.is_empty()?
|
||||
.is_writable()?
|
||||
.has_seeds(&[BUS, &[0]], args.bus_0_bump, &ore_api::ID)?;
|
||||
bus_1_info
|
||||
.is_empty()?
|
||||
.is_writable()?
|
||||
.has_seeds(&[BUS, &[1]], args.bus_1_bump, &ore_api::ID)?;
|
||||
bus_2_info
|
||||
.is_empty()?
|
||||
.is_writable()?
|
||||
.has_seeds(&[BUS, &[2]], args.bus_2_bump, &ore_api::ID)?;
|
||||
bus_3_info
|
||||
.is_empty()?
|
||||
.is_writable()?
|
||||
.has_seeds(&[BUS, &[3]], args.bus_3_bump, &ore_api::ID)?;
|
||||
bus_4_info
|
||||
.is_empty()?
|
||||
.is_writable()?
|
||||
.has_seeds(&[BUS, &[4]], args.bus_4_bump, &ore_api::ID)?;
|
||||
bus_5_info
|
||||
.is_empty()?
|
||||
.is_writable()?
|
||||
.has_seeds(&[BUS, &[5]], args.bus_5_bump, &ore_api::ID)?;
|
||||
bus_6_info
|
||||
.is_empty()?
|
||||
.is_writable()?
|
||||
.has_seeds(&[BUS, &[6]], args.bus_6_bump, &ore_api::ID)?;
|
||||
bus_7_info
|
||||
.is_empty()?
|
||||
.is_writable()?
|
||||
.has_seeds(&[BUS, &[7]], args.bus_7_bump, &ore_api::ID)?;
|
||||
config_info
|
||||
.is_empty()?
|
||||
.is_writable()?
|
||||
.has_seeds(&[CONFIG], args.config_bump, &ore_api::ID)?;
|
||||
metadata_info.is_empty()?.is_writable()?.has_seeds(
|
||||
&[
|
||||
METADATA,
|
||||
mpl_token_metadata::ID.as_ref(),
|
||||
@@ -43,29 +60,22 @@ pub fn process_initialize(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramR
|
||||
args.metadata_bump,
|
||||
&mpl_token_metadata::ID,
|
||||
)?;
|
||||
load_uninitialized_pda(
|
||||
mint_info,
|
||||
mint_info.is_empty()?.is_writable()?.has_seeds(
|
||||
&[MINT, MINT_NOISE.as_slice()],
|
||||
args.mint_bump,
|
||||
&ore_api::id(),
|
||||
&ore_api::ID,
|
||||
)?;
|
||||
load_uninitialized_pda(
|
||||
treasury_info,
|
||||
treasury_info.is_empty()?.is_writable()?.has_seeds(
|
||||
&[TREASURY],
|
||||
args.treasury_bump,
|
||||
&ore_api::id(),
|
||||
&ore_api::ID,
|
||||
)?;
|
||||
load_system_account(treasury_tokens_info, true)?;
|
||||
load_program(system_program, system_program::id())?;
|
||||
load_program(token_program, spl_token::id())?;
|
||||
load_program(associated_token_program, spl_associated_token_account::id())?;
|
||||
load_program(metadata_program, mpl_token_metadata::ID)?;
|
||||
load_sysvar(rent_sysvar, sysvar::rent::id())?;
|
||||
|
||||
// Check signer.
|
||||
if signer.key.ne(&INITIALIZER_ADDRESS) {
|
||||
return Err(ProgramError::MissingRequiredSignature);
|
||||
}
|
||||
treasury_tokens_info.is_empty()?.is_writable()?;
|
||||
system_program.is_program(&system_program::ID)?;
|
||||
token_program.is_program(&spl_token::ID)?;
|
||||
associated_token_program.is_program(&spl_associated_token_account::ID)?;
|
||||
metadata_program.is_program(&mpl_token_metadata::ID)?;
|
||||
rent_sysvar.is_sysvar(&sysvar::rent::ID)?;
|
||||
|
||||
// Initialize bus accounts.
|
||||
let bus_infos = [
|
||||
@@ -83,17 +93,14 @@ pub fn process_initialize(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramR
|
||||
args.bus_7_bump,
|
||||
];
|
||||
for i in 0..BUS_COUNT {
|
||||
create_pda(
|
||||
create_account::<Bus>(
|
||||
bus_infos[i],
|
||||
&ore_api::id(),
|
||||
8 + size_of::<Bus>(),
|
||||
&ore_api::ID,
|
||||
&[BUS, &[i as u8], &[bus_bumps[i]]],
|
||||
system_program,
|
||||
signer,
|
||||
signer_info,
|
||||
)?;
|
||||
let mut bus_data = bus_infos[i].try_borrow_mut_data()?;
|
||||
bus_data[0] = Bus::discriminator() as u8;
|
||||
let bus = Bus::try_from_bytes_mut(&mut bus_data)?;
|
||||
let bus = bus_infos[i].to_account_mut::<Bus>(&ore_api::ID)?;
|
||||
bus.id = i as u64;
|
||||
bus.rewards = 0;
|
||||
bus.theoretical_rewards = 0;
|
||||
@@ -101,47 +108,40 @@ pub fn process_initialize(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramR
|
||||
}
|
||||
|
||||
// Initialize config.
|
||||
create_pda(
|
||||
create_account::<Config>(
|
||||
config_info,
|
||||
&ore_api::id(),
|
||||
8 + size_of::<Config>(),
|
||||
&ore_api::ID,
|
||||
&[CONFIG, &[args.config_bump]],
|
||||
system_program,
|
||||
signer,
|
||||
signer_info,
|
||||
)?;
|
||||
let mut config_data = config_info.data.borrow_mut();
|
||||
config_data[0] = Config::discriminator() as u8;
|
||||
let config = Config::try_from_bytes_mut(&mut config_data)?;
|
||||
let config = config_info.to_account_mut::<Config>(&ore_api::ID)?;
|
||||
config.base_reward_rate = INITIAL_BASE_REWARD_RATE;
|
||||
config.last_reset_at = 0;
|
||||
config.min_difficulty = INITIAL_MIN_DIFFICULTY as u64;
|
||||
config.top_balance = 0;
|
||||
|
||||
// Initialize treasury.
|
||||
create_pda(
|
||||
create_account::<Treasury>(
|
||||
treasury_info,
|
||||
&ore_api::id(),
|
||||
8 + size_of::<Treasury>(),
|
||||
&ore_api::ID,
|
||||
&[TREASURY, &[args.treasury_bump]],
|
||||
system_program,
|
||||
signer,
|
||||
signer_info,
|
||||
)?;
|
||||
let mut treasury_data = treasury_info.data.borrow_mut();
|
||||
treasury_data[0] = Treasury::discriminator() as u8;
|
||||
drop(treasury_data);
|
||||
|
||||
// Initialize mint.
|
||||
create_pda(
|
||||
allocate_account(
|
||||
mint_info,
|
||||
&spl_token::id(),
|
||||
&spl_token::ID,
|
||||
Mint::LEN,
|
||||
&[MINT, MINT_NOISE.as_slice(), &[args.mint_bump]],
|
||||
system_program,
|
||||
signer,
|
||||
signer_info,
|
||||
)?;
|
||||
solana_program::program::invoke_signed(
|
||||
&spl_token::instruction::initialize_mint(
|
||||
&spl_token::id(),
|
||||
&spl_token::ID,
|
||||
mint_info.key,
|
||||
treasury_info.key,
|
||||
None,
|
||||
@@ -162,8 +162,8 @@ pub fn process_initialize(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramR
|
||||
metadata: metadata_info,
|
||||
mint: mint_info,
|
||||
mint_authority: treasury_info,
|
||||
payer: signer,
|
||||
update_authority: (signer, true),
|
||||
payer: signer_info,
|
||||
update_authority: (signer_info, true),
|
||||
system_program,
|
||||
rent: Some(rent_sysvar),
|
||||
__args: mpl_token_metadata::instructions::CreateMetadataAccountV3InstructionArgs {
|
||||
@@ -183,8 +183,8 @@ pub fn process_initialize(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramR
|
||||
.invoke_signed(&[&[TREASURY, &[args.treasury_bump]]])?;
|
||||
|
||||
// Initialize treasury token account.
|
||||
create_ata(
|
||||
signer,
|
||||
create_associated_token_account(
|
||||
signer_info,
|
||||
treasury_info,
|
||||
treasury_tokens_info,
|
||||
mint_info,
|
||||
|
||||
@@ -19,27 +19,16 @@ use update::*;
|
||||
use upgrade::*;
|
||||
|
||||
use ore_api::instruction::*;
|
||||
use solana_program::{
|
||||
self, account_info::AccountInfo, entrypoint::ProgramResult, program_error::ProgramError,
|
||||
pubkey::Pubkey,
|
||||
};
|
||||
|
||||
solana_program::entrypoint!(process_instruction);
|
||||
use steel::*;
|
||||
|
||||
pub fn process_instruction(
|
||||
program_id: &Pubkey,
|
||||
accounts: &[AccountInfo],
|
||||
data: &[u8],
|
||||
) -> ProgramResult {
|
||||
if program_id.ne(&ore_api::id()) {
|
||||
return Err(ProgramError::IncorrectProgramId);
|
||||
}
|
||||
let (ix, data) = parse_instruction(&ore_api::ID, program_id, data)?;
|
||||
|
||||
let (tag, data) = data
|
||||
.split_first()
|
||||
.ok_or(ProgramError::InvalidInstructionData)?;
|
||||
|
||||
match OreInstruction::try_from(*tag).or(Err(ProgramError::InvalidInstructionData))? {
|
||||
match ix {
|
||||
OreInstruction::Claim => process_claim(accounts, data)?,
|
||||
OreInstruction::Close => process_close(accounts, data)?,
|
||||
OreInstruction::Mine => process_mine(accounts, data)?,
|
||||
@@ -53,3 +42,5 @@ pub fn process_instruction(
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
entrypoint!(process_instruction);
|
||||
|
||||
@@ -1,56 +1,43 @@
|
||||
use std::mem::size_of;
|
||||
|
||||
use base64::{prelude::BASE64_STANDARD, Engine};
|
||||
use drillx::Solution;
|
||||
use ore_api::{
|
||||
consts::*,
|
||||
error::OreError,
|
||||
event::{BoostEvent, MineEvent},
|
||||
instruction::Mine,
|
||||
loaders::*,
|
||||
state::{Bus, Config, Proof},
|
||||
};
|
||||
use ore_boost_api::{
|
||||
loaders::{load_any_boost, load_stake},
|
||||
state::{Boost, Stake},
|
||||
};
|
||||
use ore_utils::{load_signer, load_sysvar};
|
||||
use ore_api::prelude::*;
|
||||
use ore_boost_api::state::{Boost, Stake};
|
||||
#[allow(deprecated)]
|
||||
use solana_program::{
|
||||
account_info::AccountInfo,
|
||||
clock::Clock,
|
||||
entrypoint::ProgramResult,
|
||||
keccak::hashv,
|
||||
program_error::ProgramError,
|
||||
pubkey::Pubkey,
|
||||
sanitize::SanitizeError,
|
||||
serialize_utils::{read_pubkey, read_u16},
|
||||
slot_hashes::SlotHash,
|
||||
sysvar::{self, Sysvar},
|
||||
};
|
||||
use solana_program::{
|
||||
log::{self, sol_log},
|
||||
log::{sol_log, sol_log_data},
|
||||
program::set_return_data,
|
||||
};
|
||||
use steel::*;
|
||||
|
||||
/// Mine validates hashes and increments a miner's collectable balance.
|
||||
pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
|
||||
pub fn process_mine(accounts: &[AccountInfo], data: &[u8]) -> ProgramResult {
|
||||
// Parse args.
|
||||
let args = Mine::try_from_bytes(data)?;
|
||||
|
||||
// Load accounts.
|
||||
let (required_accounts, optional_accounts) = accounts.split_at(6);
|
||||
let [signer, bus_info, config_info, proof_info, instructions_sysvar, slot_hashes_sysvar] =
|
||||
let [signer_info, bus_info, config_info, proof_info, instructions_sysvar, slot_hashes_sysvar] =
|
||||
required_accounts
|
||||
else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
};
|
||||
load_signer(signer)?;
|
||||
load_any_bus(bus_info, true)?;
|
||||
load_config(config_info, false)?;
|
||||
load_proof_with_miner(proof_info, signer.key, true)?;
|
||||
load_sysvar(instructions_sysvar, sysvar::instructions::id())?;
|
||||
load_sysvar(slot_hashes_sysvar, sysvar::slot_hashes::id())?;
|
||||
signer_info.is_signer()?;
|
||||
let bus = bus_info.to_account_mut::<Bus>(&ore_api::ID)?;
|
||||
let config = config_info
|
||||
.is_config()?
|
||||
.to_account::<Config>(&ore_api::ID)?;
|
||||
let proof = proof_info
|
||||
.to_account_mut::<Proof>(&ore_api::ID)?
|
||||
.check_mut(|p| p.miner == *signer_info.key)?;
|
||||
instructions_sysvar.is_sysvar(&sysvar::instructions::ID)?;
|
||||
slot_hashes_sysvar.is_sysvar(&sysvar::slot_hashes::ID)?;
|
||||
|
||||
// Authenticate the proof account.
|
||||
//
|
||||
@@ -59,12 +46,7 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
authenticate(&instructions_sysvar.data.borrow(), proof_info.key)?;
|
||||
|
||||
// Validate epoch is active.
|
||||
let config_data = config_info.data.borrow();
|
||||
let config = {
|
||||
use ore_utils::AccountDeserialize;
|
||||
Config::try_from_bytes(&config_data)?
|
||||
};
|
||||
let clock = Clock::get().or(Err(ProgramError::InvalidAccountData))?;
|
||||
let clock = Clock::get()?;
|
||||
if config
|
||||
.last_reset_at
|
||||
.saturating_add(EPOCH_DURATION)
|
||||
@@ -77,11 +59,6 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
//
|
||||
// Here we use drillx to validate the provided solution is a valid hash of the challenge.
|
||||
// If invalid, we return an error.
|
||||
let mut proof_data = proof_info.data.borrow_mut();
|
||||
let proof = {
|
||||
use ore_utils::AccountDeserialize;
|
||||
Proof::try_from_bytes_mut(&mut proof_data)?
|
||||
};
|
||||
let solution = Solution::new(args.digest, args.nonce);
|
||||
if !solution.is_valid(&proof.challenge) {
|
||||
return Err(OreError::HashInvalid.into());
|
||||
@@ -125,11 +102,6 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
// If user has greater than or equal to the max stake on the network, they receive 2x multiplier.
|
||||
// Any stake less than this will receives between 1x and 2x multipler. The multipler is only active
|
||||
// if the miner's last stake deposit was more than one minute ago to protect against flash loan attacks.
|
||||
let mut bus_data = bus_info.data.borrow_mut();
|
||||
let bus = {
|
||||
use ore_utils::AccountDeserialize;
|
||||
Bus::try_from_bytes_mut(&mut bus_data)?
|
||||
};
|
||||
if proof.balance.gt(&0) && proof.last_stake_at.saturating_add(ONE_MINUTE).lt(&t) {
|
||||
// Calculate staking reward.
|
||||
if config.top_balance.gt(&0) {
|
||||
@@ -157,8 +129,11 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
// Load optional accounts.
|
||||
let boost_info = optional_accounts[i * 2].clone();
|
||||
let stake_info = optional_accounts[i * 2 + 1].clone();
|
||||
load_any_boost(&boost_info, false)?;
|
||||
load_stake(&stake_info, &proof.authority, boost_info.key, false)?;
|
||||
let boost = boost_info.to_account::<Boost>(&ore_boost_api::ID)?;
|
||||
let stake = stake_info
|
||||
.to_account::<Stake>(&ore_boost_api::ID)?
|
||||
.check(|s| s.authority == proof.authority)?
|
||||
.check(|s| s.boost == *boost_info.key)?;
|
||||
|
||||
// Skip if boost is applied twice.
|
||||
if applied_boosts.contains(boost_info.key) {
|
||||
@@ -168,22 +143,8 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
// Record this boost has been used.
|
||||
applied_boosts[i] = *boost_info.key;
|
||||
|
||||
// Parse account data.
|
||||
let boost_data = boost_info.data.borrow();
|
||||
let boost = {
|
||||
use ore_boost_api::ore_utils::AccountDeserialize;
|
||||
Boost::try_from_bytes(&boost_data)?
|
||||
};
|
||||
let stake_data = stake_info.data.borrow();
|
||||
let stake = {
|
||||
use ore_boost_api::ore_utils::AccountDeserialize;
|
||||
Stake::try_from_bytes(&stake_data)?
|
||||
};
|
||||
|
||||
let not_expired = boost.expires_at.gt(&t);
|
||||
let settled = stake.last_stake_at.saturating_add(ONE_MINUTE).le(&t);
|
||||
// Apply multiplier if boost is not expired and last stake at was more than one minute ago.
|
||||
if not_expired && settled {
|
||||
if boost.expires_at.gt(&t) && stake.last_stake_at.saturating_add(ONE_MINUTE).le(&t) {
|
||||
let multiplier = boost.multiplier.checked_sub(1).unwrap();
|
||||
let boost_reward = (reward as u128)
|
||||
.checked_mul(multiplier as u128)
|
||||
@@ -193,18 +154,19 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
.checked_div(boost.total_stake as u128)
|
||||
.unwrap() as u64;
|
||||
reward = reward.checked_add(boost_reward).unwrap();
|
||||
let boost_event = BoostEvent {
|
||||
|
||||
// Log event
|
||||
sol_log_data(&[(BoostEvent {
|
||||
mint: boost.mint,
|
||||
reward: boost_reward,
|
||||
};
|
||||
let boost_event = boost_event.to_bytes();
|
||||
let boost_event = BASE64_STANDARD.encode(boost_event);
|
||||
sol_log(format!("Boost event: {:}", boost_event).as_str());
|
||||
})
|
||||
.to_bytes()]);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Log base reward after boost rewards for parsing.
|
||||
log::sol_log(&format!("Base: {}", reward));
|
||||
// Log base reward after boost rewards.
|
||||
// Parser looks for base reward first, and then for the variable number of boost rewards.
|
||||
sol_log(&format!("Base: {}", reward));
|
||||
|
||||
// Apply liveness penalty.
|
||||
//
|
||||
@@ -264,7 +226,7 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
|
||||
// Update lifetime stats.
|
||||
proof.total_hashes = proof.total_hashes.saturating_add(1);
|
||||
proof.total_rewards = proof.total_rewards.saturating_add(reward);
|
||||
proof.total_rewards = proof.total_rewards.saturating_add(reward_actual);
|
||||
|
||||
// Log the mined rewards.
|
||||
//
|
||||
|
||||
@@ -1,17 +1,8 @@
|
||||
use std::mem::size_of;
|
||||
|
||||
use ore_api::{consts::*, instruction::Open, state::Proof};
|
||||
use ore_utils::*;
|
||||
use solana_program::{
|
||||
account_info::AccountInfo,
|
||||
clock::Clock,
|
||||
entrypoint::ProgramResult,
|
||||
keccak::hashv,
|
||||
program_error::ProgramError,
|
||||
slot_hashes::SlotHash,
|
||||
system_program,
|
||||
sysvar::{self, Sysvar},
|
||||
};
|
||||
use ore_api::prelude::*;
|
||||
use solana_program::{keccak::hashv, slot_hashes::SlotHash};
|
||||
use steel::*;
|
||||
|
||||
/// Open creates a new proof account to track a miner's state.
|
||||
pub fn process_open(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
|
||||
@@ -19,39 +10,35 @@ pub fn process_open(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
let args = Open::try_from_bytes(data)?;
|
||||
|
||||
// Load accounts.
|
||||
let [signer, miner_info, payer_info, proof_info, system_program, slot_hashes_info] = accounts
|
||||
let [signer_info, miner_info, payer_info, proof_info, system_program, slot_hashes_info] =
|
||||
accounts
|
||||
else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
};
|
||||
load_signer(signer)?;
|
||||
load_any(miner_info, false)?;
|
||||
load_signer(payer_info)?;
|
||||
load_uninitialized_pda(
|
||||
proof_info,
|
||||
&[PROOF, signer.key.as_ref()],
|
||||
signer_info.is_signer()?;
|
||||
payer_info.is_signer()?;
|
||||
proof_info.is_empty()?.is_writable()?.has_seeds(
|
||||
&[PROOF, signer_info.key.as_ref()],
|
||||
args.bump,
|
||||
&ore_api::id(),
|
||||
&ore_api::ID,
|
||||
)?;
|
||||
load_program(system_program, system_program::id())?;
|
||||
load_sysvar(slot_hashes_info, sysvar::slot_hashes::id())?;
|
||||
system_program.is_program(&system_program::ID)?;
|
||||
slot_hashes_info.is_sysvar(&sysvar::slot_hashes::ID)?;
|
||||
|
||||
// Initialize proof.
|
||||
create_pda(
|
||||
create_account::<Proof>(
|
||||
proof_info,
|
||||
&ore_api::id(),
|
||||
8 + size_of::<Proof>(),
|
||||
&[PROOF, signer.key.as_ref(), &[args.bump]],
|
||||
&ore_api::ID,
|
||||
&[PROOF, signer_info.key.as_ref(), &[args.bump]],
|
||||
system_program,
|
||||
payer_info,
|
||||
)?;
|
||||
let clock = Clock::get().or(Err(ProgramError::InvalidAccountData))?;
|
||||
let mut proof_data = proof_info.data.borrow_mut();
|
||||
proof_data[0] = Proof::discriminator() as u8;
|
||||
let proof = Proof::try_from_bytes_mut(&mut proof_data)?;
|
||||
proof.authority = *signer.key;
|
||||
let clock = Clock::get()?;
|
||||
let proof = proof_info.to_account_mut::<Proof>(&ore_api::ID)?;
|
||||
proof.authority = *signer_info.key;
|
||||
proof.balance = 0;
|
||||
proof.challenge = hashv(&[
|
||||
signer.key.as_ref(),
|
||||
signer_info.key.as_ref(),
|
||||
&slot_hashes_info.data.borrow()[0..size_of::<SlotHash>()],
|
||||
])
|
||||
.0;
|
||||
|
||||
@@ -1,47 +1,52 @@
|
||||
use ore_api::{
|
||||
consts::*,
|
||||
error::OreError,
|
||||
loaders::*,
|
||||
state::{Bus, Config},
|
||||
};
|
||||
use ore_utils::*;
|
||||
use solana_program::{
|
||||
account_info::AccountInfo, clock::Clock, entrypoint::ProgramResult,
|
||||
program_error::ProgramError, program_pack::Pack, sysvar::Sysvar,
|
||||
};
|
||||
use spl_token::state::Mint;
|
||||
use ore_api::prelude::*;
|
||||
use steel::*;
|
||||
|
||||
/// Reset tops up the bus balances, updates the base reward rate, and sets up the ORE program for the next epoch.
|
||||
pub fn process_reset(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResult {
|
||||
// Load accounts.
|
||||
let [signer, bus_0_info, bus_1_info, bus_2_info, bus_3_info, bus_4_info, bus_5_info, bus_6_info, bus_7_info, config_info, mint_info, treasury_info, treasury_tokens_info, token_program] =
|
||||
let [signer_info, bus_0_info, bus_1_info, bus_2_info, bus_3_info, bus_4_info, bus_5_info, bus_6_info, bus_7_info, config_info, mint_info, treasury_info, treasury_tokens_info, token_program] =
|
||||
accounts
|
||||
else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
};
|
||||
load_signer(signer)?;
|
||||
load_bus(bus_0_info, 0, true)?;
|
||||
load_bus(bus_1_info, 1, true)?;
|
||||
load_bus(bus_2_info, 2, true)?;
|
||||
load_bus(bus_3_info, 3, true)?;
|
||||
load_bus(bus_4_info, 4, true)?;
|
||||
load_bus(bus_5_info, 5, true)?;
|
||||
load_bus(bus_6_info, 6, true)?;
|
||||
load_bus(bus_7_info, 7, true)?;
|
||||
load_config(config_info, true)?;
|
||||
load_mint(mint_info, MINT_ADDRESS, true)?;
|
||||
load_treasury(treasury_info, true)?;
|
||||
load_treasury_tokens(treasury_tokens_info, true)?;
|
||||
load_program(token_program, spl_token::id())?;
|
||||
let busses: [&AccountInfo; BUS_COUNT] = [
|
||||
bus_0_info, bus_1_info, bus_2_info, bus_3_info, bus_4_info, bus_5_info, bus_6_info,
|
||||
bus_7_info,
|
||||
];
|
||||
signer_info.is_signer()?;
|
||||
let bus_0 = bus_0_info
|
||||
.to_account_mut::<Bus>(&ore_api::ID)?
|
||||
.check_mut(|b| b.id == 0)?;
|
||||
let bus_1 = bus_1_info
|
||||
.to_account_mut::<Bus>(&ore_api::ID)?
|
||||
.check_mut(|b| b.id == 1)?;
|
||||
let bus_2 = bus_2_info
|
||||
.to_account_mut::<Bus>(&ore_api::ID)?
|
||||
.check_mut(|b| b.id == 2)?;
|
||||
let bus_3 = bus_3_info
|
||||
.to_account_mut::<Bus>(&ore_api::ID)?
|
||||
.check_mut(|b| b.id == 3)?;
|
||||
let bus_4 = bus_4_info
|
||||
.to_account_mut::<Bus>(&ore_api::ID)?
|
||||
.check_mut(|b| b.id == 4)?;
|
||||
let bus_5 = bus_5_info
|
||||
.to_account_mut::<Bus>(&ore_api::ID)?
|
||||
.check_mut(|b| b.id == 5)?;
|
||||
let bus_6 = bus_6_info
|
||||
.to_account_mut::<Bus>(&ore_api::ID)?
|
||||
.check_mut(|b| b.id == 6)?;
|
||||
let bus_7 = bus_7_info
|
||||
.to_account_mut::<Bus>(&ore_api::ID)?
|
||||
.check_mut(|b| b.id == 7)?;
|
||||
let config = config_info
|
||||
.is_config()?
|
||||
.to_account_mut::<Config>(&ore_api::ID)?;
|
||||
let mint = mint_info
|
||||
.has_address(&MINT_ADDRESS)?
|
||||
.is_writable()?
|
||||
.to_mint()?;
|
||||
treasury_info.is_treasury()?.is_writable()?;
|
||||
treasury_tokens_info.is_treasury_tokens()?.is_writable()?;
|
||||
token_program.is_program(&spl_token::ID)?;
|
||||
|
||||
// Validate enough time has passed since the last reset.
|
||||
let mut config_data = config_info.data.borrow_mut();
|
||||
let config = Config::try_from_bytes_mut(&mut config_data)?;
|
||||
let clock = Clock::get().or(Err(ProgramError::InvalidAccountData))?;
|
||||
let clock = Clock::get()?;
|
||||
if config
|
||||
.last_reset_at
|
||||
.saturating_add(EPOCH_DURATION)
|
||||
@@ -54,14 +59,11 @@ pub fn process_reset(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResul
|
||||
config.last_reset_at = clock.unix_timestamp;
|
||||
|
||||
// Reset bus accounts and calculate actual rewards mined since last reset.
|
||||
let busses = [bus_0, bus_1, bus_2, bus_3, bus_4, bus_5, bus_6, bus_7];
|
||||
let mut total_remaining_rewards = 0u64;
|
||||
let mut total_theoretical_rewards = 0u64;
|
||||
let mut top_balance = 0u64;
|
||||
for i in 0..BUS_COUNT {
|
||||
// Parse bus account.
|
||||
let mut bus_data = busses[i].data.borrow_mut();
|
||||
let bus = Bus::try_from_bytes_mut(&mut bus_data)?;
|
||||
|
||||
for bus in busses {
|
||||
// Track top balance.
|
||||
if bus.top_balance.gt(&top_balance) {
|
||||
top_balance = bus.top_balance;
|
||||
@@ -99,7 +101,6 @@ pub fn process_reset(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResul
|
||||
}
|
||||
|
||||
// Max supply check.
|
||||
let mint = Mint::unpack(&mint_info.data.borrow()).expect("Failed to parse mint");
|
||||
if mint.supply.ge(&MAX_SUPPLY) {
|
||||
return Err(OreError::MaxSupply.into());
|
||||
}
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
use ore_api::{consts::*, instruction::Stake, loaders::*, state::Proof};
|
||||
use ore_utils::*;
|
||||
use solana_program::{
|
||||
account_info::AccountInfo, clock::Clock, entrypoint::ProgramResult,
|
||||
program_error::ProgramError, sysvar::Sysvar,
|
||||
};
|
||||
use ore_api::prelude::*;
|
||||
use steel::*;
|
||||
|
||||
/// Stake deposits ORE into a proof account to earn multiplier.
|
||||
pub fn process_stake(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
|
||||
@@ -12,27 +8,32 @@ pub fn process_stake(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|
||||
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_info, proof_info, sender_info, treasury_tokens_info, token_program] = accounts
|
||||
else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
};
|
||||
load_signer(signer)?;
|
||||
load_proof(proof_info, signer.key, true)?;
|
||||
load_token_account(sender_info, Some(signer.key), &MINT_ADDRESS, true)?;
|
||||
load_treasury_tokens(treasury_tokens_info, true)?;
|
||||
load_program(token_program, spl_token::id())?;
|
||||
signer_info.is_signer()?;
|
||||
let proof = proof_info
|
||||
.to_account_mut::<Proof>(&ore_api::ID)?
|
||||
.check_mut(|p| p.authority == *signer_info.key)?;
|
||||
sender_info
|
||||
.is_writable()?
|
||||
.to_token_account()?
|
||||
.check(|t| t.owner == *signer_info.key)?
|
||||
.check(|t| t.mint == MINT_ADDRESS)?;
|
||||
treasury_tokens_info.is_writable()?.is_treasury_tokens()?;
|
||||
token_program.is_program(&spl_token::ID)?;
|
||||
|
||||
// Update the proof balance.
|
||||
let mut proof_data = proof_info.data.borrow_mut();
|
||||
let proof = Proof::try_from_bytes_mut(&mut proof_data)?;
|
||||
proof.balance = proof.balance.checked_add(amount).unwrap();
|
||||
|
||||
// Update deposit timestamp.
|
||||
let clock = Clock::get().or(Err(ProgramError::InvalidAccountData))?;
|
||||
let clock = Clock::get()?;
|
||||
proof.last_stake_at = clock.unix_timestamp;
|
||||
|
||||
// Transfer tokens from signer to treasury.
|
||||
transfer(
|
||||
signer,
|
||||
signer_info,
|
||||
sender_info,
|
||||
treasury_tokens_info,
|
||||
token_program,
|
||||
|
||||
@@ -1,22 +1,18 @@
|
||||
use ore_api::{loaders::*, state::Proof};
|
||||
use ore_utils::*;
|
||||
use solana_program::{
|
||||
account_info::AccountInfo, entrypoint::ProgramResult, program_error::ProgramError,
|
||||
};
|
||||
use ore_api::prelude::*;
|
||||
use steel::*;
|
||||
|
||||
/// Update changes the miner authority on a proof account.
|
||||
pub fn process_update(accounts: &[AccountInfo<'_>], _data: &[u8]) -> ProgramResult {
|
||||
// Load accounts.
|
||||
let [signer, miner_info, proof_info] = accounts else {
|
||||
let [signer_info, miner_info, proof_info] = accounts else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
};
|
||||
load_signer(signer)?;
|
||||
load_any(miner_info, false)?;
|
||||
load_proof(proof_info, signer.key, true)?;
|
||||
signer_info.is_signer()?;
|
||||
let proof = proof_info
|
||||
.to_account_mut::<Proof>(&ore_api::ID)?
|
||||
.check_mut(|p| p.authority == *signer_info.key)?;
|
||||
|
||||
// Update the proof's miner authority.
|
||||
let mut proof_data = proof_info.data.borrow_mut();
|
||||
let proof = Proof::try_from_bytes_mut(&mut proof_data)?;
|
||||
proof.miner = *miner_info.key;
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -1,10 +1,5 @@
|
||||
use ore_api::{consts::*, error::OreError, instruction::Stake};
|
||||
use ore_utils::*;
|
||||
use solana_program::{
|
||||
account_info::AccountInfo, entrypoint::ProgramResult, program_error::ProgramError,
|
||||
program_pack::Pack,
|
||||
};
|
||||
use spl_token::state::Mint;
|
||||
use ore_api::prelude::*;
|
||||
use steel::*;
|
||||
|
||||
/// Upgrade allows a user to migrate a v1 token to a v2 token at a 1:1 exchange rate.
|
||||
pub fn process_upgrade(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult {
|
||||
@@ -13,17 +8,32 @@ pub fn process_upgrade(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResu
|
||||
let amount = u64::from_le_bytes(args.amount);
|
||||
|
||||
// Load accounts
|
||||
let [signer, beneficiary_info, mint_info, mint_v1_info, sender_info, treasury_info, token_program] =
|
||||
let [signer_info, beneficiary_info, mint_info, mint_v1_info, sender_info, treasury_info, token_program] =
|
||||
accounts
|
||||
else {
|
||||
return Err(ProgramError::NotEnoughAccountKeys);
|
||||
};
|
||||
load_signer(signer)?;
|
||||
load_token_account(beneficiary_info, Some(&signer.key), &MINT_ADDRESS, true)?;
|
||||
load_mint(mint_info, MINT_ADDRESS, true)?;
|
||||
load_mint(mint_v1_info, MINT_V1_ADDRESS, true)?;
|
||||
load_token_account(sender_info, Some(signer.key), &MINT_V1_ADDRESS, true)?;
|
||||
load_program(token_program, spl_token::id())?;
|
||||
signer_info.is_signer()?;
|
||||
beneficiary_info
|
||||
.is_writable()?
|
||||
.to_token_account()?
|
||||
.check(|t| t.owner == *signer_info.key)?
|
||||
.check(|t| t.mint == MINT_ADDRESS)?;
|
||||
let mint = mint_info
|
||||
.is_writable()?
|
||||
.has_address(&MINT_ADDRESS)?
|
||||
.to_mint()?;
|
||||
mint_v1_info
|
||||
.is_writable()?
|
||||
.has_address(&MINT_V1_ADDRESS)?
|
||||
.to_mint()?;
|
||||
sender_info
|
||||
.is_writable()?
|
||||
.to_token_account()?
|
||||
.check(|t| t.owner == *signer_info.key)?
|
||||
.check(|t| t.mint == MINT_V1_ADDRESS)?;
|
||||
treasury_info.is_treasury()?;
|
||||
token_program.is_program(&spl_token::ID)?;
|
||||
|
||||
// Burn v1 tokens
|
||||
solana_program::program::invoke(
|
||||
@@ -31,15 +41,15 @@ pub fn process_upgrade(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResu
|
||||
&spl_token::id(),
|
||||
sender_info.key,
|
||||
mint_v1_info.key,
|
||||
signer.key,
|
||||
&[signer.key],
|
||||
signer_info.key,
|
||||
&[signer_info.key],
|
||||
amount,
|
||||
)?,
|
||||
&[
|
||||
token_program.clone(),
|
||||
sender_info.clone(),
|
||||
mint_v1_info.clone(),
|
||||
signer.clone(),
|
||||
signer_info.clone(),
|
||||
],
|
||||
)?;
|
||||
|
||||
@@ -48,14 +58,11 @@ pub fn process_upgrade(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResu
|
||||
let amount_to_mint = amount.saturating_mul(100);
|
||||
|
||||
// Cap at max supply.
|
||||
let mint_data = mint_info.data.borrow();
|
||||
let mint = Mint::unpack(&mint_data)?;
|
||||
if mint.supply.saturating_add(amount_to_mint).gt(&MAX_SUPPLY) {
|
||||
return Err(OreError::MaxSupply.into());
|
||||
}
|
||||
|
||||
// Mint to the beneficiary account
|
||||
drop(mint_data);
|
||||
mint_to_signed(
|
||||
mint_info,
|
||||
beneficiary_info,
|
||||
|
||||
Reference in New Issue
Block a user