From 60b0b023fef3e14126dfe556deb5f0771bcd7efd Mon Sep 17 00:00:00 2001 From: Hardhat Chad Date: Fri, 6 Jun 2025 08:12:15 -0700 Subject: [PATCH] cleanup --- api/src/consts.rs | 61 +----------------------------------------- api/src/state/miner.rs | 6 ----- program/src/mine.rs | 42 ++++++++++++++++++++++++----- program/src/swap.rs | 16 +---------- 4 files changed, 38 insertions(+), 87 deletions(-) diff --git a/api/src/consts.rs b/api/src/consts.rs index 224e1da..1b7cc64 100644 --- a/api/src/consts.rs +++ b/api/src/consts.rs @@ -20,9 +20,6 @@ pub const MAX_SUPPLY: u64 = ONE_ORE * 5_000_000; /// The seed of the block account PDA. pub const BLOCK: &[u8] = b"block"; -/// The seed of the commit account PDA. -pub const COMMIT: &[u8] = b"commit"; - /// The seed of the config account PDA. pub const CONFIG: &[u8] = b"config"; @@ -32,35 +29,12 @@ pub const MARKET: &[u8] = b"market"; /// The seed of the miner account PDA. pub const MINER: &[u8] = b"miner"; -/// The seed of the receipt account PDA. -pub const RECEIPT: &[u8] = b"receipt"; - -/// The seed of the stake account PDA. -pub const STAKE: &[u8] = b"stake"; - -/// The seed of the metadata account PDA. -pub const METADATA: &[u8] = b"metadata"; - /// The seed of the mint account PDA. pub const MINT: &[u8] = b"mint"; /// The seed of the treasury account PDA. pub const TREASURY: &[u8] = b"treasury"; -/// Noise for deriving the mint pda -pub const MINT_NOISE: [u8; 16] = [ - 89, 157, 88, 232, 243, 249, 197, 132, 199, 49, 19, 234, 91, 94, 150, 41, -]; - -/// The name for token metadata. -pub const METADATA_NAME: &str = "ORE"; - -/// The ticker symbol for token metadata. -pub const METADATA_SYMBOL: &str = "ORE"; - -/// The uri for token metdata. -pub const METADATA_URI: &str = "https://ore.supply/assets/metadata.json"; - /// Program id for const pda derivations const PROGRAM_ID: [u8; 32] = unsafe { *(&crate::id() as *const Pubkey as *const [u8; 32]) }; @@ -68,46 +42,13 @@ const PROGRAM_ID: [u8; 32] = unsafe { *(&crate::id() as *const Pubkey as *const pub const CONFIG_ADDRESS: Pubkey = Pubkey::new_from_array(ed25519::derive_program_address(&[CONFIG], &PROGRAM_ID).0); -/// The address of the mint metadata account. -pub const METADATA_ADDRESS: Pubkey = Pubkey::new_from_array( - ed25519::derive_program_address( - &[ - METADATA, - unsafe { &*(&mpl_token_metadata::ID as *const Pubkey as *const [u8; 32]) }, - unsafe { &*(&MINT_ADDRESS as *const Pubkey as *const [u8; 32]) }, - ], - unsafe { &*(&mpl_token_metadata::ID as *const Pubkey as *const [u8; 32]) }, - ) - .0, -); - /// The address of the mint account. -pub const MINT_ADDRESS: Pubkey = - Pubkey::new_from_array(ed25519::derive_program_address(&[MINT, &MINT_NOISE], &PROGRAM_ID).0); - -/// The bump of the mint account. -pub const MINT_BUMP: u8 = ed25519::derive_program_address(&[MINT, &MINT_NOISE], &PROGRAM_ID).1; +pub const MINT_ADDRESS: Pubkey = pubkey!("oreoU2P8bN6jkk3jbaiVxYnG1dCXcYxwhwyK9jSybcp"); /// The address of the treasury account. pub const TREASURY_ADDRESS: Pubkey = Pubkey::new_from_array(ed25519::derive_program_address(&[TREASURY], &PROGRAM_ID).0); -/// The bump of the treasury account, for cpis. -pub const TREASURY_BUMP: u8 = ed25519::derive_program_address(&[TREASURY], &PROGRAM_ID).1; - -/// The address of the treasury token account. -pub const TREASURY_TOKENS_ADDRESS: Pubkey = Pubkey::new_from_array( - ed25519::derive_program_address( - &[ - unsafe { &*(&TREASURY_ADDRESS as *const Pubkey as *const [u8; 32]) }, - unsafe { &*(&spl_token::id() as *const Pubkey as *const [u8; 32]) }, - unsafe { &*(&MINT_ADDRESS as *const Pubkey as *const [u8; 32]) }, - ], - unsafe { &*(&spl_associated_token_account::id() as *const Pubkey as *const [u8; 32]) }, - ) - .0, -); - /// Denominator for protocol fee calculations. pub const FEE_RATE_BPS: u64 = 100; diff --git a/api/src/state/miner.rs b/api/src/state/miner.rs index 4bfec1a..b8be78f 100644 --- a/api/src/state/miner.rs +++ b/api/src/state/miner.rs @@ -11,15 +11,9 @@ pub struct Miner { /// The ID of the last block this miner mined in. pub block_id: u64, - /// The amount of ORE this miner has deployed into hashpower markets. - pub deployed: u64, - /// The hash of the last block this miner mined in. pub hash: [u8; 32], - /// The amount of ORE this miner has staked. - pub stake: u64, - /// The total number of hashes this miner has submitted. pub total_hashes: u64, diff --git a/program/src/mine.rs b/program/src/mine.rs index db5863f..37f60dc 100644 --- a/program/src/mine.rs +++ b/program/src/mine.rs @@ -10,7 +10,9 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult // Load accounts. let clock = Clock::get()?; - let [signer_info, block_info, miner_info, system_program, token_program] = accounts else { + let [signer_info, block_info, market_info, miner_info, mint_info, sender_info, system_program, token_program] = + accounts + else { return Err(ProgramError::NotEnoughAccountKeys); }; signer_info.is_signer()?; @@ -18,14 +20,38 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult .as_account_mut::(&ore_api::ID)? .assert_mut(|b| clock.slot >= b.start_slot)? .assert_mut(|b| clock.slot < b.start_slot + 1500)?; - let miner = miner_info - .as_account_mut::(&ore_api::ID)? - .assert_mut(|m| m.authority == *signer_info.key)?; + let market = market_info + .as_account::(&ore_api::ID)? + .assert(|m| m.id == block.id)?; + mint_info.has_address(&market.base.mint)?.as_mint()?; + sender_info + .is_writable()? + .as_associated_token_account(signer_info.key, &mint_info.key)? + .assert(|t| t.amount() >= amount)?; system_program.is_program(&system_program::ID)?; token_program.is_program(&spl_token::ID)?; - // TODO Open miner account if it doesn't exist. - // TODO Burn hash tokens + // Load miner account. + let miner = if miner_info.data_is_empty() { + create_program_account::( + miner_info, + system_program, + signer_info, + &ore_api::ID, + &[MINER, &signer_info.key.to_bytes()], + )?; + let miner = miner_info.as_account_mut::(&ore_api::ID)?; + miner.authority = *signer_info.key; + miner.block_id = 0; + miner.hash = [0; 32]; + miner.total_hashes = 0; + miner.total_rewards = 0; + miner + } else { + miner_info + .as_account_mut::(&ore_api::ID)? + .assert_mut(|m| m.authority == *signer_info.key)? + }; // Reset miner hash if mining new block. if miner.block_id != block.id { @@ -34,6 +60,7 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult keccak::hashv(&[block.slot_hash.as_ref(), miner.authority.as_ref()]).to_bytes(); } + // Mine. for _ in 0..amount { miner.hash = keccak::hashv(&[miner.hash.as_ref()]).to_bytes(); if miner.hash < block.best_hash { @@ -45,5 +72,8 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult // Update miner stats. miner.total_hashes += amount; + // Burn hash tokens. + burn(sender_info, mint_info, signer_info, token_program, amount)?; + Ok(()) } diff --git a/program/src/swap.rs b/program/src/swap.rs index 5cfc754..e3ae253 100644 --- a/program/src/swap.rs +++ b/program/src/swap.rs @@ -11,7 +11,7 @@ pub fn process_swap(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult // Load accounts. let clock = Clock::get()?; - let [signer_info, block_info, market_info, miner_info, mint_base_info, mint_quote_info, tokens_base_info, tokens_quote_info, vault_base_info, vault_quote_info, system_program, token_program] = + let [signer_info, block_info, market_info, mint_base_info, mint_quote_info, tokens_base_info, tokens_quote_info, vault_base_info, vault_quote_info, system_program, token_program] = accounts else { return Err(ProgramError::NotEnoughAccountKeys); @@ -31,9 +31,6 @@ pub fn process_swap(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult |m| m.quote.reserves() > 0, OreError::InsufficientLiquidity.into(), )?; - let miner = miner_info - .as_account_mut::(&ore_api::ID)? - .assert_mut(|m| m.authority == *signer_info.key)?; mint_base_info.has_address(&market.base.mint)?.as_mint()?; mint_quote_info.has_address(&market.quote.mint)?.as_mint()?; tokens_base_info @@ -75,17 +72,6 @@ pub fn process_swap(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult ), }; - // Update miner state. - match direction { - SwapDirection::Buy => { - miner.deployed += in_amount; - assert!(miner.deployed <= miner.stake); - } - SwapDirection::Sell => { - miner.deployed = miner.deployed.saturating_sub(out_amount); - } - } - // Transfer tokens. transfer(signer_info, in_from, in_to, token_program, in_amount)?; transfer_signed(