From 462a7021e17b2ef303dddbc7df2b682094923fdf Mon Sep 17 00:00:00 2001 From: Hardhat Chad Date: Sat, 29 Jun 2024 00:59:55 +0000 Subject: [PATCH] refactor --- Cargo.lock | 22 ++++- Cargo.toml | 35 ++----- core/api/Cargo.toml | 25 +++++ {src => core/api/src}/consts.rs | 0 {src => core/api/src}/error.rs | 0 {src => core/api/src}/instruction.rs | 95 +++++++++---------- core/api/src/lib.rs | 9 ++ {src => core/api/src}/state/bus.rs | 0 {src => core/api/src}/state/config.rs | 0 {src => core/api/src}/state/mod.rs | 0 {src => core/api/src}/state/proof.rs | 0 {src => core/api/src}/state/treasury.rs | 0 {src => core/api/src}/utils.rs | 20 ++-- core/program/Cargo.toml | 34 +++++++ {src => core/program/src}/lib.rs | 18 +--- {src => core/program/src}/loaders.rs | 25 +++-- {src => core/program/src}/processor/claim.rs | 8 +- {src => core/program/src}/processor/close.rs | 3 +- {src => core/program/src}/processor/crown.rs | 10 +- .../program/src}/processor/initialize.rs | 52 +++++----- {src => core/program/src}/processor/mine.rs | 31 +++--- {src => core/program/src}/processor/mod.rs | 0 {src => core/program/src}/processor/open.rs | 19 ++-- {src => core/program/src}/processor/reset.rs | 18 ++-- {src => core/program/src}/processor/stake.rs | 6 +- {src => core/program/src}/processor/update.rs | 3 +- .../program/src}/processor/upgrade.rs | 6 +- stake/api/Cargo.toml | 0 stake/program/Cargo.toml | 0 29 files changed, 245 insertions(+), 194 deletions(-) create mode 100644 core/api/Cargo.toml rename {src => core/api/src}/consts.rs (100%) rename {src => core/api/src}/error.rs (100%) rename {src => core/api/src}/instruction.rs (98%) create mode 100644 core/api/src/lib.rs rename {src => core/api/src}/state/bus.rs (100%) rename {src => core/api/src}/state/config.rs (100%) rename {src => core/api/src}/state/mod.rs (100%) rename {src => core/api/src}/state/proof.rs (100%) rename {src => core/api/src}/state/treasury.rs (100%) rename {src => core/api/src}/utils.rs (98%) create mode 100644 core/program/Cargo.toml rename {src => core/program/src}/lib.rs (78%) rename {src => core/program/src}/loaders.rs (97%) rename {src => core/program/src}/processor/claim.rs (93%) rename {src => core/program/src}/processor/close.rs (94%) rename {src => core/program/src}/processor/crown.rs (97%) rename {src => core/program/src}/processor/initialize.rs (92%) rename {src => core/program/src}/processor/mine.rs (96%) rename {src => core/program/src}/processor/mod.rs (100%) rename {src => core/program/src}/processor/open.rs (93%) rename {src => core/program/src}/processor/reset.rs (96%) rename {src => core/program/src}/processor/stake.rs (94%) rename {src => core/program/src}/processor/update.rs (90%) rename {src => core/program/src}/processor/upgrade.rs (95%) create mode 100644 stake/api/Cargo.toml create mode 100644 stake/program/Cargo.toml diff --git a/Cargo.lock b/Cargo.lock index 412b136..1dd4c24 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2591,26 +2591,38 @@ dependencies = [ ] [[package]] -name = "ore-program" +name = "ore-api" version = "2.0.0" dependencies = [ "array-const-fn-init", "bs58 0.5.0", - "bs64", "bytemuck", "const-crypto", "drillx", "mpl-token-metadata", "num_enum 0.7.2", - "rand 0.8.5", "shank", "solana-program", + "spl-associated-token-account", + "spl-token", + "static_assertions", + "thiserror", +] + +[[package]] +name = "ore-program" +version = "2.0.0" +dependencies = [ + "bs64", + "drillx", + "mpl-token-metadata", + "ore-api", + "rand 0.8.5", + "solana-program", "solana-program-test", "solana-sdk", "spl-associated-token-account", "spl-token", - "static_assertions", - "thiserror", "tokio", ] diff --git a/Cargo.toml b/Cargo.toml index fa1ecd3..2b46f9e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,41 +1,20 @@ -[package] -name = "ore-program" +[workspace] +resolver = "2" +members = ["core/*"] + +[workspace.package] version = "2.0.0" -description = "Ore is a digital currency you can mine from anywhere, at home or on your phone." edition = "2021" license = "Apache-2.0" homepage = "https://ore.supply" documentation = "https://ore.supply" -repository = "https://github.com/hardhatchad/ore" +repository = "https://github.com/regolith-labs/ore" readme = "./README.md" keywords = ["solana", "crypto", "mining"] -[lib] -crate-type = ["cdylib", "lib"] -name = "ore" - -[features] -no-entrypoint = [] -default = [] - -[dependencies] -array-const-fn-init = "0.1.1" -bs58 = "0.5.0" -bytemuck = "1.14.3" -const-crypto = "0.1.0" +[workspace.dependencies] drillx = { git = "https://github.com/regolith-labs/drillx", branch = "master", features = ["solana"] } mpl-token-metadata = "4.1.2" -num_enum = "0.7.2" -shank = "0.3.0" solana-program = "1.18" spl-token = { version = "^4", features = ["no-entrypoint"] } spl-associated-token-account = { version = "^2.2", features = [ "no-entrypoint" ] } -static_assertions = "1.1.0" -thiserror = "1.0.57" - -[dev-dependencies] -bs64 = "0.1.2" -rand = "0.8.5" -solana-program-test = "^1.18" -solana-sdk = "^1.18" -tokio = { version = "1.35", features = ["full"] } diff --git a/core/api/Cargo.toml b/core/api/Cargo.toml new file mode 100644 index 0000000..9477a8c --- /dev/null +++ b/core/api/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "ore-api" +description = "API for interacting with the ORE program" +version.workspace = true +edition.workspace = true +license.workspace = true +homepage.workspace = true +documentation.workspace = true +repository.workspace = true +keywords.workspace = true + +[dependencies] +array-const-fn-init = "0.1.1" +bs58 = "0.5.0" +bytemuck = "1.14.3" +const-crypto = "0.1.0" +drillx.workspace = true +mpl-token-metadata.workspace = true +num_enum = "0.7.2" +shank = "0.3.0" +solana-program.workspace = true +spl-token.workspace = true +spl-associated-token-account.workspace = true +static_assertions = "1.1.0" +thiserror = "1.0.57" \ No newline at end of file diff --git a/src/consts.rs b/core/api/src/consts.rs similarity index 100% rename from src/consts.rs rename to core/api/src/consts.rs diff --git a/src/error.rs b/core/api/src/error.rs similarity index 100% rename from src/error.rs rename to core/api/src/error.rs diff --git a/src/instruction.rs b/core/api/src/instruction.rs similarity index 98% rename from src/instruction.rs rename to core/api/src/instruction.rs index fdf9d2a..f87371f 100644 --- a/src/instruction.rs +++ b/core/api/src/instruction.rs @@ -8,10 +8,7 @@ use solana_program::{ system_program, sysvar, }; -use crate::{ - impl_instruction_from_bytes, impl_to_bytes, BUS, BUS_ADDRESSES, CONFIG, CONFIG_ADDRESS, - METADATA, MINT, MINT_ADDRESS, MINT_NOISE, MINT_V1_ADDRESS, PROOF, TREASURY, TREASURY_ADDRESS, -}; +use crate::{consts::*, impl_instruction_from_bytes, impl_to_bytes}; #[repr(u8)] #[derive(Clone, Copy, Debug, Eq, PartialEq, ShankInstruction, TryFromPrimitive)] @@ -185,8 +182,9 @@ impl_instruction_from_bytes!(ClaimArgs); impl_instruction_from_bytes!(StakeArgs); impl_instruction_from_bytes!(UpgradeArgs); -/// Builds a reset instruction. -pub fn reset(signer: Pubkey) -> Instruction { +/// Builds a claim instruction. +pub fn claim(signer: Pubkey, beneficiary: Pubkey, amount: u64) -> Instruction { + let proof = Pubkey::find_program_address(&[PROOF, signer.as_ref()], &crate::id()).0; let treasury_tokens = spl_associated_token_account::get_associated_token_address( &TREASURY_ADDRESS, &MINT_ADDRESS, @@ -195,39 +193,20 @@ pub fn reset(signer: Pubkey) -> Instruction { program_id: crate::id(), accounts: vec![ AccountMeta::new(signer, true), - AccountMeta::new(BUS_ADDRESSES[0], false), - AccountMeta::new(BUS_ADDRESSES[1], false), - AccountMeta::new(BUS_ADDRESSES[2], false), - AccountMeta::new(BUS_ADDRESSES[3], false), - AccountMeta::new(BUS_ADDRESSES[4], false), - AccountMeta::new(BUS_ADDRESSES[5], false), - AccountMeta::new(BUS_ADDRESSES[6], false), - AccountMeta::new(BUS_ADDRESSES[7], false), - AccountMeta::new(CONFIG_ADDRESS, false), + AccountMeta::new(beneficiary, false), AccountMeta::new(MINT_ADDRESS, false), - AccountMeta::new(TREASURY_ADDRESS, false), + AccountMeta::new(proof, false), + AccountMeta::new_readonly(TREASURY_ADDRESS, false), AccountMeta::new(treasury_tokens, false), AccountMeta::new_readonly(spl_token::id(), false), ], - data: OreInstruction::Reset.to_vec(), - } -} - -/// Builds an open instruction. -pub fn open(signer: Pubkey, miner: Pubkey) -> Instruction { - let proof_pda = Pubkey::find_program_address(&[PROOF, signer.as_ref()], &crate::id()); - Instruction { - program_id: crate::id(), - accounts: vec![ - AccountMeta::new(signer, true), - AccountMeta::new_readonly(miner, false), - AccountMeta::new(proof_pda.0, false), - AccountMeta::new_readonly(solana_program::system_program::id(), false), - AccountMeta::new_readonly(sysvar::slot_hashes::id(), false), - ], data: [ - OreInstruction::Open.to_vec(), - OpenArgs { bump: proof_pda.1 }.to_bytes().to_vec(), + OreInstruction::Claim.to_vec(), + ClaimArgs { + amount: amount.to_le_bytes(), + } + .to_bytes() + .to_vec(), ] .concat(), } @@ -273,9 +252,28 @@ pub fn mine(signer: Pubkey, bus: Pubkey, solution: Solution) -> Instruction { } } -/// Builds a claim instruction. -pub fn claim(signer: Pubkey, beneficiary: Pubkey, amount: u64) -> Instruction { - let proof = Pubkey::find_program_address(&[PROOF, signer.as_ref()], &crate::id()).0; +/// Builds an open instruction. +pub fn open(signer: Pubkey, miner: Pubkey) -> Instruction { + let proof_pda = Pubkey::find_program_address(&[PROOF, signer.as_ref()], &crate::id()); + Instruction { + program_id: crate::id(), + accounts: vec![ + AccountMeta::new(signer, true), + AccountMeta::new_readonly(miner, false), + AccountMeta::new(proof_pda.0, false), + AccountMeta::new_readonly(solana_program::system_program::id(), false), + AccountMeta::new_readonly(sysvar::slot_hashes::id(), false), + ], + data: [ + OreInstruction::Open.to_vec(), + OpenArgs { bump: proof_pda.1 }.to_bytes().to_vec(), + ] + .concat(), + } +} + +/// Builds a reset instruction. +pub fn reset(signer: Pubkey) -> Instruction { let treasury_tokens = spl_associated_token_account::get_associated_token_address( &TREASURY_ADDRESS, &MINT_ADDRESS, @@ -284,22 +282,21 @@ pub fn claim(signer: Pubkey, beneficiary: Pubkey, amount: u64) -> Instruction { program_id: crate::id(), accounts: vec![ AccountMeta::new(signer, true), - AccountMeta::new(beneficiary, false), + AccountMeta::new(BUS_ADDRESSES[0], false), + AccountMeta::new(BUS_ADDRESSES[1], false), + AccountMeta::new(BUS_ADDRESSES[2], false), + AccountMeta::new(BUS_ADDRESSES[3], false), + AccountMeta::new(BUS_ADDRESSES[4], false), + AccountMeta::new(BUS_ADDRESSES[5], false), + AccountMeta::new(BUS_ADDRESSES[6], false), + AccountMeta::new(BUS_ADDRESSES[7], false), + AccountMeta::new(CONFIG_ADDRESS, false), AccountMeta::new(MINT_ADDRESS, false), - AccountMeta::new(proof, false), - AccountMeta::new_readonly(TREASURY_ADDRESS, false), + AccountMeta::new(TREASURY_ADDRESS, false), AccountMeta::new(treasury_tokens, false), AccountMeta::new_readonly(spl_token::id(), false), ], - data: [ - OreInstruction::Claim.to_vec(), - ClaimArgs { - amount: amount.to_le_bytes(), - } - .to_bytes() - .to_vec(), - ] - .concat(), + data: OreInstruction::Reset.to_vec(), } } diff --git a/core/api/src/lib.rs b/core/api/src/lib.rs new file mode 100644 index 0000000..85782b2 --- /dev/null +++ b/core/api/src/lib.rs @@ -0,0 +1,9 @@ +pub mod consts; +pub mod error; +pub mod instruction; +pub mod state; +pub mod utils; + +use solana_program::declare_id; + +declare_id!("mineRHF5r6S7HyD9SppBfVMXMavDkJsxwGesEvxZr2A"); diff --git a/src/state/bus.rs b/core/api/src/state/bus.rs similarity index 100% rename from src/state/bus.rs rename to core/api/src/state/bus.rs diff --git a/src/state/config.rs b/core/api/src/state/config.rs similarity index 100% rename from src/state/config.rs rename to core/api/src/state/config.rs diff --git a/src/state/mod.rs b/core/api/src/state/mod.rs similarity index 100% rename from src/state/mod.rs rename to core/api/src/state/mod.rs diff --git a/src/state/proof.rs b/core/api/src/state/proof.rs similarity index 100% rename from src/state/proof.rs rename to core/api/src/state/proof.rs diff --git a/src/state/treasury.rs b/core/api/src/state/treasury.rs similarity index 100% rename from src/state/treasury.rs rename to core/api/src/state/treasury.rs diff --git a/src/utils.rs b/core/api/src/utils.rs similarity index 98% rename from src/utils.rs rename to core/api/src/utils.rs index cdb9779..669c7d9 100644 --- a/src/utils.rs +++ b/core/api/src/utils.rs @@ -7,7 +7,7 @@ use solana_program::{ /// Creates a new pda #[inline(always)] -pub(crate) fn create_pda<'a, 'info>( +pub fn create_pda<'a, 'info>( target_account: &'a AccountInfo<'info>, owner: &Pubkey, space: usize, @@ -73,14 +73,6 @@ pub(crate) fn create_pda<'a, 'info>( Ok(()) } -#[repr(C)] -#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)] -pub struct MineEvent { - pub difficulty: u64, - pub reward: u64, - pub timing: i64, -} - #[repr(u8)] #[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)] pub enum AccountDiscriminator { @@ -152,3 +144,13 @@ macro_rules! impl_instruction_from_bytes { } }; } + +#[repr(C)] +#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)] +pub struct MineEvent { + pub difficulty: u64, + pub reward: u64, + pub timing: i64, +} + +impl_to_bytes!(MineEvent); diff --git a/core/program/Cargo.toml b/core/program/Cargo.toml new file mode 100644 index 0000000..3ea6d1c --- /dev/null +++ b/core/program/Cargo.toml @@ -0,0 +1,34 @@ +[package] +name = "ore-program" +description = "ORE is a fair-launch, proof-of-work, digital currency everyone can mine" +version.workspace = true +edition.workspace = true +license.workspace = true +homepage.workspace = true +documentation.workspace = true +repository.workspace = true +readme.workspace = true +keywords.workspace = true + +[lib] +crate-type = ["cdylib", "lib"] +name = "ore" + +[features] +no-entrypoint = [] +default = [] + +[dependencies] +drillx.workspace = true +mpl-token-metadata.workspace = true +ore-api = { path = "../api" } +solana-program.workspace = true +spl-token.workspace = true +spl-associated-token-account.workspace = true + +[dev-dependencies] +bs64 = "0.1.2" +rand = "0.8.5" +solana-program-test = "^1.18" +solana-sdk = "^1.18" +tokio = { version = "1.35", features = ["full"] } diff --git a/src/lib.rs b/core/program/src/lib.rs similarity index 78% rename from src/lib.rs rename to core/program/src/lib.rs index 51ede90..2fcd002 100644 --- a/src/lib.rs +++ b/core/program/src/lib.rs @@ -1,21 +1,13 @@ -pub mod consts; -pub mod error; -pub mod instruction; -pub mod loaders; +mod loaders; mod processor; -pub mod state; -pub mod utils; -pub use consts::*; -use instruction::*; +use ore_api::instruction::*; use processor::*; use solana_program::{ - self, account_info::AccountInfo, declare_id, entrypoint::ProgramResult, - program_error::ProgramError, pubkey::Pubkey, + self, account_info::AccountInfo, entrypoint::ProgramResult, program_error::ProgramError, + pubkey::Pubkey, }; -declare_id!("oreFHcE6FvJTrsfaYca4mVeZn7J7T6oZS9FAvW9eg4q"); - #[cfg(not(feature = "no-entrypoint"))] solana_program::entrypoint!(process_instruction); @@ -24,7 +16,7 @@ pub fn process_instruction( accounts: &[AccountInfo], data: &[u8], ) -> ProgramResult { - if program_id.ne(&crate::id()) { + if program_id.ne(&ore_api::id()) { return Err(ProgramError::IncorrectProgramId); } diff --git a/src/loaders.rs b/core/program/src/loaders.rs similarity index 97% rename from src/loaders.rs rename to core/program/src/loaders.rs index c0b0692..61cc800 100644 --- a/src/loaders.rs +++ b/core/program/src/loaders.rs @@ -1,15 +1,14 @@ +use ore_api::{ + consts::*, + state::{Bus, Config, Proof, Treasury}, + utils::{AccountDeserialize, Discriminator}, +}; use solana_program::{ account_info::AccountInfo, program_error::ProgramError, program_pack::Pack, pubkey::Pubkey, system_program, sysvar, }; use spl_token::state::Mint; -use crate::{ - state::{Bus, Config, Proof, Treasury}, - utils::{AccountDeserialize, Discriminator}, - BUS_ADDRESSES, CONFIG_ADDRESS, TREASURY_ADDRESS, -}; - /// Errors if: /// - Account is not a signer. pub fn load_signer<'a, 'info>(info: &'a AccountInfo<'info>) -> Result<(), ProgramError> { @@ -32,7 +31,7 @@ pub fn load_bus<'a, 'info>( id: u64, is_writable: bool, ) -> Result<(), ProgramError> { - if info.owner.ne(&crate::id()) { + if info.owner.ne(&ore_api::id()) { return Err(ProgramError::InvalidAccountOwner); } @@ -69,7 +68,7 @@ pub fn load_any_bus<'a, 'info>( info: &'a AccountInfo<'info>, is_writable: bool, ) -> Result<(), ProgramError> { - if info.owner.ne(&crate::id()) { + if info.owner.ne(&ore_api::id()) { return Err(ProgramError::InvalidAccountOwner); } @@ -102,7 +101,7 @@ pub fn load_config<'a, 'info>( info: &'a AccountInfo<'info>, is_writable: bool, ) -> Result<(), ProgramError> { - if info.owner.ne(&crate::id()) { + if info.owner.ne(&ore_api::id()) { return Err(ProgramError::InvalidAccountOwner); } @@ -136,7 +135,7 @@ pub fn load_proof<'a, 'info>( authority: &Pubkey, is_writable: bool, ) -> Result<(), ProgramError> { - if info.owner.ne(&crate::id()) { + if info.owner.ne(&ore_api::id()) { return Err(ProgramError::InvalidAccountOwner); } @@ -169,7 +168,7 @@ pub fn load_proof_with_miner<'a, 'info>( miner: &Pubkey, is_writable: bool, ) -> Result<(), ProgramError> { - if info.owner.ne(&crate::id()) { + if info.owner.ne(&ore_api::id()) { return Err(ProgramError::InvalidAccountOwner); } @@ -200,7 +199,7 @@ pub fn load_any_proof<'a, 'info>( info: &'a AccountInfo<'info>, is_writable: bool, ) -> Result<(), ProgramError> { - if info.owner.ne(&crate::id()) { + if info.owner.ne(&ore_api::id()) { return Err(ProgramError::InvalidAccountOwner); } @@ -229,7 +228,7 @@ pub fn load_treasury<'a, 'info>( info: &'a AccountInfo<'info>, is_writable: bool, ) -> Result<(), ProgramError> { - if info.owner.ne(&crate::id()) { + if info.owner.ne(&ore_api::id()) { return Err(ProgramError::InvalidAccountOwner); } diff --git a/src/processor/claim.rs b/core/program/src/processor/claim.rs similarity index 93% rename from src/processor/claim.rs rename to core/program/src/processor/claim.rs index f41cf0d..7a688d2 100644 --- a/src/processor/claim.rs +++ b/core/program/src/processor/claim.rs @@ -1,12 +1,12 @@ +use ore_api::{ + consts::*, error::OreError, instruction::ClaimArgs, state::Proof, utils::AccountDeserialize, +}; use solana_program::{ account_info::AccountInfo, entrypoint::ProgramResult, program_error::ProgramError, pubkey::Pubkey, }; -use crate::{ - error::OreError, instruction::ClaimArgs, loaders::*, state::Proof, utils::AccountDeserialize, - MINT_ADDRESS, TREASURY, TREASURY_BUMP, -}; +use crate::loaders::*; /// Claim distributes Ore from the treasury to a miner. Its responsibilies include: /// 1. Decrement the miner's claimable balance. diff --git a/src/processor/close.rs b/core/program/src/processor/close.rs similarity index 94% rename from src/processor/close.rs rename to core/program/src/processor/close.rs index 0356341..4f6d0f6 100644 --- a/src/processor/close.rs +++ b/core/program/src/processor/close.rs @@ -1,9 +1,10 @@ +use ore_api::{state::Proof, utils::AccountDeserialize}; use solana_program::{ account_info::AccountInfo, entrypoint::ProgramResult, program_error::ProgramError, pubkey::Pubkey, system_program, }; -use crate::{loaders::*, state::Proof, utils::AccountDeserialize}; +use crate::loaders::*; /// Close closes a proof account and returns the rent to the owner. Its responsibilities include: /// 1. Realloc proof account size to 0. diff --git a/src/processor/crown.rs b/core/program/src/processor/crown.rs similarity index 97% rename from src/processor/crown.rs rename to core/program/src/processor/crown.rs index 674f114..e977d15 100644 --- a/src/processor/crown.rs +++ b/core/program/src/processor/crown.rs @@ -1,13 +1,13 @@ +use ore_api::{ + state::{Config, Proof}, + utils::AccountDeserialize, +}; use solana_program::{ account_info::AccountInfo, entrypoint::ProgramResult, program_error::ProgramError, pubkey::Pubkey, }; -use crate::{ - loaders::*, - state::{Config, Proof}, - utils::AccountDeserialize, -}; +use crate::loaders::*; /// Crown marks an account as the top staker if their balance is greater than the last known top staker. pub fn process_crown<'a, 'info>( diff --git a/src/processor/initialize.rs b/core/program/src/processor/initialize.rs similarity index 92% rename from src/processor/initialize.rs rename to core/program/src/processor/initialize.rs index fa614eb..d01110e 100644 --- a/src/processor/initialize.rs +++ b/core/program/src/processor/initialize.rs @@ -1,5 +1,13 @@ use std::mem::size_of; +use ore_api::{ + consts::*, + instruction::*, + state::{Bus, Config, Treasury}, + utils::create_pda, + utils::AccountDeserialize, + utils::Discriminator, +}; use solana_program::{ account_info::AccountInfo, entrypoint::ProgramResult, @@ -10,16 +18,7 @@ use solana_program::{ }; use spl_token::state::Mint; -use crate::{ - instruction::*, - loaders::*, - state::{Bus, Config, Treasury}, - utils::create_pda, - utils::AccountDeserialize, - utils::Discriminator, - BUS, BUS_COUNT, CONFIG, INITIAL_ADMIN, INITIAL_BASE_REWARD_RATE, METADATA, METADATA_NAME, - METADATA_SYMBOL, METADATA_URI, MINT, MINT_ADDRESS, MINT_NOISE, TOKEN_DECIMALS, TREASURY, -}; +use crate::loaders::*; /// Initialize sets up the Ore program. Its responsibilities include: /// 1. Initialize the 8 bus accounts. @@ -54,15 +53,15 @@ pub fn process_initialize<'a, 'info>( return Err(ProgramError::NotEnoughAccountKeys); }; load_signer(signer)?; - load_uninitialized_pda(bus_0_info, &[BUS, &[0]], args.bus_0_bump, &crate::id())?; - load_uninitialized_pda(bus_1_info, &[BUS, &[1]], args.bus_1_bump, &crate::id())?; - load_uninitialized_pda(bus_2_info, &[BUS, &[2]], args.bus_2_bump, &crate::id())?; - load_uninitialized_pda(bus_3_info, &[BUS, &[3]], args.bus_3_bump, &crate::id())?; - load_uninitialized_pda(bus_4_info, &[BUS, &[4]], args.bus_4_bump, &crate::id())?; - load_uninitialized_pda(bus_5_info, &[BUS, &[5]], args.bus_5_bump, &crate::id())?; - load_uninitialized_pda(bus_6_info, &[BUS, &[6]], args.bus_6_bump, &crate::id())?; - load_uninitialized_pda(bus_7_info, &[BUS, &[7]], args.bus_7_bump, &crate::id())?; - load_uninitialized_pda(config_info, &[CONFIG], args.config_bump, &crate::id())?; + 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, &[ @@ -77,9 +76,14 @@ pub fn process_initialize<'a, 'info>( mint_info, &[MINT, MINT_NOISE.as_slice()], args.mint_bump, - &crate::id(), + &ore_api::id(), + )?; + load_uninitialized_pda( + treasury_info, + &[TREASURY], + args.treasury_bump, + &ore_api::id(), )?; - load_uninitialized_pda(treasury_info, &[TREASURY], args.treasury_bump, &crate::id())?; load_system_account(treasury_tokens_info, true)?; load_program(system_program, system_program::id())?; load_program(token_program, spl_token::id())?; @@ -110,7 +114,7 @@ pub fn process_initialize<'a, 'info>( for i in 0..BUS_COUNT { create_pda( bus_infos[i], - &crate::id(), + &ore_api::id(), 8 + size_of::(), &[BUS, &[i as u8], &[bus_bumps[i]]], system_program, @@ -126,7 +130,7 @@ pub fn process_initialize<'a, 'info>( // Initialize config create_pda( config_info, - &crate::id(), + &ore_api::id(), 8 + size_of::(), &[CONFIG, &[args.config_bump]], system_program, @@ -144,7 +148,7 @@ pub fn process_initialize<'a, 'info>( // Initialize treasury create_pda( treasury_info, - &crate::id(), + &ore_api::id(), 8 + size_of::(), &[TREASURY, &[args.treasury_bump]], system_program, diff --git a/src/processor/mine.rs b/core/program/src/processor/mine.rs similarity index 96% rename from src/processor/mine.rs rename to core/program/src/processor/mine.rs index e66b8aa..20c0778 100644 --- a/src/processor/mine.rs +++ b/core/program/src/processor/mine.rs @@ -1,6 +1,13 @@ use std::mem::size_of; use drillx::Solution; +use ore_api::{ + consts::*, + error::OreError, + instruction::{MineArgs, OreInstruction}, + state::{Bus, Config, Proof}, + utils::{AccountDeserialize, MineEvent}, +}; use solana_program::program::set_return_data; #[allow(deprecated)] use solana_program::{ @@ -18,14 +25,7 @@ use solana_program::{ sysvar::{self, instructions::load_current_index, Sysvar}, }; -use crate::{ - error::OreError, - instruction::{MineArgs, OreInstruction}, - loaders::*, - state::{Bus, Config, Proof}, - utils::{AccountDeserialize, MineEvent}, - EPOCH_DURATION, MIN_DIFFICULTY, ONE_MINUTE, TOLERANCE, -}; +use crate::loaders::*; /// Mine is the primary workhorse instruction of the Ore program. Its responsibilities include: /// 1. Calculate the hash from the provided nonce. @@ -162,11 +162,14 @@ pub fn process_mine<'a, 'info>( proof.total_rewards = proof.total_rewards.saturating_add(reward); // Log the mined rewards - set_return_data(bytemuck::bytes_of(&MineEvent { - difficulty: difficulty as u64, - reward: reward_actual, - timing: t.saturating_sub(t_liveness), - })); + set_return_data( + MineEvent { + difficulty: difficulty as u64, + reward: reward_actual, + timing: t.saturating_sub(t_liveness), + } + .to_bytes(), + ); Ok(()) } @@ -193,7 +196,7 @@ fn validate_transaction(msg: &[u8]) -> Result { c += num_accounts * 33; // Only allow instructions to call ore and the compute budget program. match read_pubkey(&mut c, msg)? { - crate::ID => { + ore_api::ID => { c += 2; if let Ok(ix) = OreInstruction::try_from(read_u8(&mut c, msg)?) { if let OreInstruction::Mine = ix { diff --git a/src/processor/mod.rs b/core/program/src/processor/mod.rs similarity index 100% rename from src/processor/mod.rs rename to core/program/src/processor/mod.rs diff --git a/src/processor/open.rs b/core/program/src/processor/open.rs similarity index 93% rename from src/processor/open.rs rename to core/program/src/processor/open.rs index 3f03f61..782ceab 100644 --- a/src/processor/open.rs +++ b/core/program/src/processor/open.rs @@ -1,5 +1,11 @@ use std::mem::size_of; +use ore_api::{ + consts::*, + instruction::OpenArgs, + state::Proof, + utils::{create_pda, AccountDeserialize, Discriminator}, +}; use solana_program::{ account_info::AccountInfo, blake3::hashv, @@ -12,14 +18,7 @@ use solana_program::{ sysvar::{self, Sysvar}, }; -use crate::{ - instruction::OpenArgs, - loaders::*, - state::Proof, - utils::AccountDeserialize, - utils::{create_pda, Discriminator}, - PROOF, -}; +use crate::loaders::*; /// Register generates a new hash chain for a prospective miner. Its responsibilities include: /// 1. Initialize a new proof account. @@ -48,7 +47,7 @@ pub fn process_open<'a, 'info>( proof_info, &[PROOF, signer.key.as_ref()], args.bump, - &crate::id(), + &ore_api::id(), )?; load_program(system_program, system_program::id())?; load_sysvar(slot_hashes_info, sysvar::slot_hashes::id())?; @@ -56,7 +55,7 @@ pub fn process_open<'a, 'info>( // Initialize proof create_pda( proof_info, - &crate::id(), + &ore_api::id(), 8 + size_of::(), &[PROOF, signer.key.as_ref(), &[args.bump]], system_program, diff --git a/src/processor/reset.rs b/core/program/src/processor/reset.rs similarity index 96% rename from src/processor/reset.rs rename to core/program/src/processor/reset.rs index 3f2b924..ccc1528 100644 --- a/src/processor/reset.rs +++ b/core/program/src/processor/reset.rs @@ -1,19 +1,17 @@ +use ore_api::{ + consts::*, + error::OreError, + state::{Bus, Config}, + utils::AccountDeserialize, +}; use solana_program::{ account_info::AccountInfo, clock::Clock, entrypoint::ProgramResult, program_error::ProgramError, program_pack::Pack, pubkey::Pubkey, sysvar::Sysvar, }; use spl_token::state::Mint; -use crate::{ - error::OreError, - loaders::{ - load_bus, load_config, load_mint, load_program, load_signer, load_token_account, - load_treasury, - }, - state::{Bus, Config}, - utils::AccountDeserialize, - BUS_COUNT, BUS_EPOCH_REWARDS, EPOCH_DURATION, MAX_EPOCH_REWARDS, MAX_SUPPLY, MINT_ADDRESS, - SMOOTHING_FACTOR, TARGET_EPOCH_REWARDS, TREASURY, TREASURY_BUMP, +use crate::loaders::{ + load_bus, load_config, load_mint, load_program, load_signer, load_token_account, load_treasury, }; /// Reset sets up the Ore program for the next epoch. Its responsibilities include: diff --git a/src/processor/stake.rs b/core/program/src/processor/stake.rs similarity index 94% rename from src/processor/stake.rs rename to core/program/src/processor/stake.rs index 79be3dd..e948060 100644 --- a/src/processor/stake.rs +++ b/core/program/src/processor/stake.rs @@ -1,12 +1,10 @@ +use ore_api::{consts::*, instruction::StakeArgs, state::Proof, utils::AccountDeserialize}; use solana_program::{ account_info::AccountInfo, clock::Clock, entrypoint::ProgramResult, program_error::ProgramError, pubkey::Pubkey, sysvar::Sysvar, }; -use crate::{ - instruction::StakeArgs, loaders::*, state::Proof, utils::AccountDeserialize, MINT_ADDRESS, - TREASURY_ADDRESS, -}; +use crate::loaders::*; /// Stake deposits Ore into a miner's proof account to earn multiplier. Its responsibilies include: /// 1. Transfer tokens from the miner to the treasury account. diff --git a/src/processor/update.rs b/core/program/src/processor/update.rs similarity index 90% rename from src/processor/update.rs rename to core/program/src/processor/update.rs index 246c6d8..ed5313e 100644 --- a/src/processor/update.rs +++ b/core/program/src/processor/update.rs @@ -1,9 +1,10 @@ +use ore_api::{state::Proof, utils::AccountDeserialize}; use solana_program::{ account_info::AccountInfo, entrypoint::ProgramResult, program_error::ProgramError, pubkey::Pubkey, }; -use crate::{loaders::*, state::Proof, utils::AccountDeserialize}; +use crate::loaders::*; /// Update changes the miner authority on a proof account. pub fn process_update<'a, 'info>( diff --git a/src/processor/upgrade.rs b/core/program/src/processor/upgrade.rs similarity index 95% rename from src/processor/upgrade.rs rename to core/program/src/processor/upgrade.rs index acfe18d..0f5a044 100644 --- a/src/processor/upgrade.rs +++ b/core/program/src/processor/upgrade.rs @@ -1,13 +1,11 @@ +use ore_api::{consts::*, error::OreError, instruction::StakeArgs}; use solana_program::{ account_info::AccountInfo, entrypoint::ProgramResult, program_error::ProgramError, program_pack::Pack, pubkey::Pubkey, }; use spl_token::state::Mint; -use crate::{ - error::OreError, instruction::StakeArgs, loaders::*, MAX_SUPPLY, MINT_ADDRESS, MINT_V1_ADDRESS, - TREASURY, TREASURY_BUMP, -}; +use crate::loaders::*; /// Upgrade allows a user to migrate a v1 token to a v2 token one-for-one. Its responsibilies include: /// 1. Burns the v1 tokens. diff --git a/stake/api/Cargo.toml b/stake/api/Cargo.toml new file mode 100644 index 0000000..e69de29 diff --git a/stake/program/Cargo.toml b/stake/program/Cargo.toml new file mode 100644 index 0000000..e69de29