continue migration

This commit is contained in:
Hardhat Chad
2024-09-27 00:22:32 +00:00
parent 609349a5aa
commit be20034e57
20 changed files with 175 additions and 363 deletions

View File

@@ -1,6 +1,4 @@
use num_enum::IntoPrimitive;
use steel::*;
use thiserror::Error;
#[derive(Debug, Error, Clone, Copy, PartialEq, Eq, IntoPrimitive)]
#[repr(u32)]

View File

@@ -1,4 +1,3 @@
use bytemuck::{Pod, Zeroable};
use steel::*;
#[repr(C)]

View File

@@ -1,223 +1,28 @@
use solana_program::{account_info::AccountInfo, program_error::ProgramError, pubkey::Pubkey};
use steel::*;
use crate::{
consts::*,
state::{Bus, Config, Proof, Treasury},
state::{Config, Treasury},
};
/// Errors if:
/// - Owner is not Ore program.
/// - Address does not match the expected bus address.
/// - Data is empty.
/// - Data cannot deserialize into a bus account.
/// - Bus ID does not match the expected ID.
/// - Expected to be writable, but is not.
pub fn load_bus(info: &AccountInfo<'_>, id: u64, is_writable: bool) -> Result<(), ProgramError> {
if info.owner.ne(&crate::id()) {
return Err(ProgramError::InvalidAccountOwner);
}
if info.key.ne(&BUS_ADDRESSES[id as usize]) {
return Err(ProgramError::InvalidSeeds);
}
if info.data_is_empty() {
return Err(ProgramError::UninitializedAccount);
}
let bus_data = info.data.borrow();
let bus = Bus::try_from_bytes(&bus_data)?;
if bus.id.ne(&id) {
return Err(ProgramError::InvalidAccountData);
}
if is_writable && !info.is_writable {
return Err(ProgramError::InvalidAccountData);
}
Ok(())
}
/// Errors if:
/// - Owner is not Ore program.
/// - Data is empty.
/// - Data cannot deserialize into a bus account.
/// - Bus ID is not in the expected range.
/// - Address is not in set of valid bus address.
/// - Expected to be writable, but is not.
pub fn load_any_bus(info: &AccountInfo<'_>, is_writable: bool) -> Result<(), ProgramError> {
if info.owner.ne(&crate::id()) {
return Err(ProgramError::InvalidAccountOwner);
}
if info.data_is_empty() {
return Err(ProgramError::UninitializedAccount);
}
if info.data.borrow()[0].ne(&(Bus::discriminator() as u8)) {
return Err(solana_program::program_error::ProgramError::InvalidAccountData);
}
if !BUS_ADDRESSES.contains(info.key) {
return Err(ProgramError::InvalidSeeds);
}
if is_writable && !info.is_writable {
return Err(ProgramError::InvalidAccountData);
}
Ok(())
}
/// Errors if:
/// - Owner is not Ore program.
/// - Address does not match the expected address.
/// - Data is empty.
/// - Data cannot deserialize into a config account.
/// - Expected to be writable, but is not.
pub fn load_config(info: &AccountInfo<'_>, is_writable: bool) -> Result<(), ProgramError> {
if info.owner.ne(&crate::id()) {
return Err(ProgramError::InvalidAccountOwner);
}
if info.key.ne(&CONFIG_ADDRESS) {
return Err(ProgramError::InvalidSeeds);
}
if info.data_is_empty() {
return Err(ProgramError::UninitializedAccount);
}
if info.data.borrow()[0].ne(&(Config::discriminator() as u8)) {
return Err(solana_program::program_error::ProgramError::InvalidAccountData);
}
if is_writable && !info.is_writable {
return Err(ProgramError::InvalidAccountData);
}
Ok(())
}
/// Errors if:
/// - Owner is not Ore program.
/// - Data is empty.
/// - Data cannot deserialize into a proof account.
/// - Proof authority does not match the expected address.
/// - Expected to be writable, but is not.
pub fn load_proof(
info: &AccountInfo<'_>,
authority: &Pubkey,
is_writable: bool,
) -> Result<(), ProgramError> {
if info.owner.ne(&crate::id()) {
return Err(ProgramError::InvalidAccountOwner);
}
if info.data_is_empty() {
return Err(ProgramError::UninitializedAccount);
}
let proof_data = info.data.borrow();
let proof = Proof::try_from_bytes(&proof_data)?;
if proof.authority.ne(&authority) {
return Err(ProgramError::InvalidAccountData);
}
if is_writable && !info.is_writable {
return Err(ProgramError::InvalidAccountData);
}
Ok(())
}
/// Errors if:
/// - Owner is not Ore program.
/// - Data is empty.
/// - Data cannot deserialize into a proof account.
/// - Proof miner does not match the expected address.
/// - Expected to be writable, but is not.
pub fn load_proof_with_miner(
info: &AccountInfo<'_>,
miner: &Pubkey,
is_writable: bool,
) -> Result<(), ProgramError> {
if info.owner.ne(&crate::id()) {
return Err(ProgramError::InvalidAccountOwner);
}
if info.data_is_empty() {
return Err(ProgramError::UninitializedAccount);
}
let proof_data = info.data.borrow();
let proof = Proof::try_from_bytes(&proof_data)?;
if proof.miner.ne(&miner) {
return Err(ProgramError::InvalidAccountData);
}
if is_writable && !info.is_writable {
return Err(ProgramError::InvalidAccountData);
}
Ok(())
}
/// Errors if:
/// - Owner is not Ore program.
/// - Data is empty.
/// - Data cannot deserialize into a proof account.
/// - Expected to be writable, but is not.
pub fn load_any_proof(info: &AccountInfo<'_>, is_writable: bool) -> Result<(), ProgramError> {
if info.owner.ne(&crate::id()) {
return Err(ProgramError::InvalidAccountOwner);
}
if info.data_is_empty() {
return Err(ProgramError::UninitializedAccount);
}
if info.data.borrow()[0].ne(&(Proof::discriminator() as u8)) {
return Err(solana_program::program_error::ProgramError::InvalidAccountData);
}
if is_writable && !info.is_writable {
return Err(ProgramError::InvalidAccountData);
}
Ok(())
}
pub trait OreAccountInfoValidation {
fn is_config(&self) -> Result<&Self, ProgramError>;
fn is_treasury(&self) -> Result<&Self, ProgramError>;
fn is_treasury_tokens(&self) -> Result<&Self, ProgramError>;
}
impl<'a> OreAccountInfoValidation for AccountInfo<'a> {
impl OreAccountInfoValidation for AccountInfo<'_> {
fn is_config(&self) -> Result<&Self, ProgramError> {
self.has_address(&CONFIG_ADDRESS)?
.has_owner(&crate::ID)?
.is_type::<Treasury>()
.is_type::<Config>(&crate::ID)
}
fn is_treasury(&self) -> Result<&Self, ProgramError> {
self.has_address(&TREASURY_ADDRESS)?
.has_owner(&crate::ID)?
.is_type::<Treasury>()
}
}
/// Errors if:
/// - Address does not match the expected treasury tokens address.
/// - Cannot load as a token account
pub fn load_treasury_tokens(info: &AccountInfo<'_>, is_writable: bool) -> Result<(), ProgramError> {
if info.key.ne(&TREASURY_TOKENS_ADDRESS) {
return Err(ProgramError::InvalidSeeds);
.is_type::<Treasury>(&crate::ID)
}
load_token_account(info, Some(&TREASURY_ADDRESS), &MINT_ADDRESS, is_writable)
fn is_treasury_tokens(&self) -> Result<&Self, ProgramError> {
self.has_address(&TREASURY_TOKENS_ADDRESS)
}
}

View File

@@ -1,9 +1,6 @@
use drillx::Solution;
use solana_program::{
instruction::{AccountMeta, Instruction},
pubkey::Pubkey,
system_program, sysvar,
};
use solana_program::{system_program, sysvar};
use steel::*;
use crate::{
consts::*,

View File

@@ -1,7 +1,5 @@
use steel::*;
use crate::consts::BUS;
use super::OreAccount;
/// Bus accounts are responsible for distributing mining rewards. There are 8 busses total
@@ -23,30 +21,4 @@ pub struct Bus {
pub top_balance: u64,
}
/// Fetch the PDA of a bus account.
pub fn bus_pda(id: u8) -> (Pubkey, u8) {
Pubkey::find_program_address(&[BUS, &[id]], &crate::id())
}
impl<'a> From<&'a [u8]> for &'a Bus {
fn from(value: &'a [u8]) -> &'a Bus {
Bus::try_from_bytes(value).unwrap()
}
}
impl<'a> From<*const u8> for &'a Bus {
fn from(value: *const u8) -> &'a Bus {
unsafe {
if Bus::discriminator().ne(&value.add(0).read()) {
panic!("");
}
bytemuck::try_from_bytes::<Bus>(std::slice::from_raw_parts(
value.add(8),
std::mem::size_of::<Bus>(),
))
.expect("")
}
}
}
account!(OreAccount, Bus);

View File

@@ -1,7 +1,5 @@
use steel::*;
use crate::consts::CONFIG;
use super::OreAccount;
/// Config is a singleton account which manages program global variables.
@@ -21,9 +19,4 @@ pub struct Config {
pub top_balance: u64,
}
/// Derive the PDA of the config account.
pub fn config_pda() -> (Pubkey, u8) {
Pubkey::find_program_address(&[CONFIG], &crate::id())
}
account!(OreAccount, Config);

View File

@@ -8,7 +8,9 @@ pub use config::*;
pub use proof::*;
pub use treasury::*;
use num_enum::{IntoPrimitive, TryFromPrimitive};
use steel::*;
use crate::consts::*;
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
@@ -18,3 +20,23 @@ pub enum OreAccount {
Proof = 102,
Treasury = 103,
}
/// Fetch the PDA of a bus account.
pub fn bus_pda(id: u8) -> (Pubkey, u8) {
Pubkey::find_program_address(&[BUS, &[id]], &crate::id())
}
/// Derive the PDA of the config account.
pub fn config_pda() -> (Pubkey, u8) {
Pubkey::find_program_address(&[CONFIG], &crate::id())
}
/// Derive the PDA of a proof account.
pub fn proof_pda(authority: Pubkey) -> (Pubkey, u8) {
Pubkey::find_program_address(&[PROOF, authority.as_ref()], &crate::id())
}
/// Derive the PDA of the treasury account.
pub fn treasury_pda() -> (Pubkey, u8) {
Pubkey::find_program_address(&[TREASURY], &crate::id())
}

View File

@@ -1,9 +1,5 @@
use bytemuck::{Pod, Zeroable};
use solana_program::pubkey::Pubkey;
use steel::*;
use crate::consts::PROOF;
use super::OreAccount;
/// Proof accounts track a miner's current hash, claimable rewards, and lifetime stats.
@@ -39,9 +35,4 @@ pub struct Proof {
pub total_rewards: u64,
}
/// Derive the PDA of a proof account.
pub fn proof_pda(authority: Pubkey) -> (Pubkey, u8) {
Pubkey::find_program_address(&[PROOF, authority.as_ref()], &crate::id())
}
account!(OreAccount, Proof);

View File

@@ -1,9 +1,5 @@
use bytemuck::{Pod, Zeroable};
use solana_program::pubkey::Pubkey;
use steel::*;
use crate::consts::TREASURY;
use super::OreAccount;
/// Treasury is a singleton account which is the mint authority for the ORE token and the authority of
@@ -12,9 +8,4 @@ use super::OreAccount;
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
pub struct Treasury {}
/// Derive the PDA of the treasury account.
pub fn treasury_pda() -> (Pubkey, u8) {
Pubkey::find_program_address(&[TREASURY], &crate::id())
}
account!(OreAccount, Treasury);