This commit is contained in:
Hardhat Chad
2025-06-06 08:12:15 -07:00
parent 2b9c917bf4
commit 60b0b023fe
4 changed files with 38 additions and 87 deletions

View File

@@ -20,9 +20,6 @@ pub const MAX_SUPPLY: u64 = ONE_ORE * 5_000_000;
/// The seed of the block account PDA. /// The seed of the block account PDA.
pub const BLOCK: &[u8] = b"block"; 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. /// The seed of the config account PDA.
pub const CONFIG: &[u8] = b"config"; pub const CONFIG: &[u8] = b"config";
@@ -32,35 +29,12 @@ pub const MARKET: &[u8] = b"market";
/// The seed of the miner account PDA. /// The seed of the miner account PDA.
pub const MINER: &[u8] = b"miner"; 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. /// The seed of the mint account PDA.
pub const MINT: &[u8] = b"mint"; pub const MINT: &[u8] = b"mint";
/// The seed of the treasury account PDA. /// The seed of the treasury account PDA.
pub const TREASURY: &[u8] = b"treasury"; 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 /// Program id for const pda derivations
const PROGRAM_ID: [u8; 32] = unsafe { *(&crate::id() as *const Pubkey as *const [u8; 32]) }; 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 = pub const CONFIG_ADDRESS: Pubkey =
Pubkey::new_from_array(ed25519::derive_program_address(&[CONFIG], &PROGRAM_ID).0); 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. /// The address of the mint account.
pub const MINT_ADDRESS: Pubkey = pub const MINT_ADDRESS: Pubkey = pubkey!("oreoU2P8bN6jkk3jbaiVxYnG1dCXcYxwhwyK9jSybcp");
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;
/// The address of the treasury account. /// The address of the treasury account.
pub const TREASURY_ADDRESS: Pubkey = pub const TREASURY_ADDRESS: Pubkey =
Pubkey::new_from_array(ed25519::derive_program_address(&[TREASURY], &PROGRAM_ID).0); 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. /// Denominator for protocol fee calculations.
pub const FEE_RATE_BPS: u64 = 100; pub const FEE_RATE_BPS: u64 = 100;

View File

@@ -11,15 +11,9 @@ pub struct Miner {
/// The ID of the last block this miner mined in. /// The ID of the last block this miner mined in.
pub block_id: u64, 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. /// The hash of the last block this miner mined in.
pub hash: [u8; 32], pub hash: [u8; 32],
/// The amount of ORE this miner has staked.
pub stake: u64,
/// The total number of hashes this miner has submitted. /// The total number of hashes this miner has submitted.
pub total_hashes: u64, pub total_hashes: u64,

View File

@@ -10,7 +10,9 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
// Load accounts. // Load accounts.
let clock = Clock::get()?; 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); return Err(ProgramError::NotEnoughAccountKeys);
}; };
signer_info.is_signer()?; signer_info.is_signer()?;
@@ -18,14 +20,38 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
.as_account_mut::<Block>(&ore_api::ID)? .as_account_mut::<Block>(&ore_api::ID)?
.assert_mut(|b| clock.slot >= b.start_slot)? .assert_mut(|b| clock.slot >= b.start_slot)?
.assert_mut(|b| clock.slot < b.start_slot + 1500)?; .assert_mut(|b| clock.slot < b.start_slot + 1500)?;
let miner = miner_info let market = market_info
.as_account_mut::<Miner>(&ore_api::ID)? .as_account::<Market>(&ore_api::ID)?
.assert_mut(|m| m.authority == *signer_info.key)?; .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)?; system_program.is_program(&system_program::ID)?;
token_program.is_program(&spl_token::ID)?; token_program.is_program(&spl_token::ID)?;
// TODO Open miner account if it doesn't exist. // Load miner account.
// TODO Burn hash tokens let miner = if miner_info.data_is_empty() {
create_program_account::<Miner>(
miner_info,
system_program,
signer_info,
&ore_api::ID,
&[MINER, &signer_info.key.to_bytes()],
)?;
let miner = miner_info.as_account_mut::<Miner>(&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::<Miner>(&ore_api::ID)?
.assert_mut(|m| m.authority == *signer_info.key)?
};
// Reset miner hash if mining new block. // Reset miner hash if mining new block.
if miner.block_id != block.id { 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(); keccak::hashv(&[block.slot_hash.as_ref(), miner.authority.as_ref()]).to_bytes();
} }
// Mine.
for _ in 0..amount { for _ in 0..amount {
miner.hash = keccak::hashv(&[miner.hash.as_ref()]).to_bytes(); miner.hash = keccak::hashv(&[miner.hash.as_ref()]).to_bytes();
if miner.hash < block.best_hash { if miner.hash < block.best_hash {
@@ -45,5 +72,8 @@ pub fn process_mine(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
// Update miner stats. // Update miner stats.
miner.total_hashes += amount; miner.total_hashes += amount;
// Burn hash tokens.
burn(sender_info, mint_info, signer_info, token_program, amount)?;
Ok(()) Ok(())
} }

View File

@@ -11,7 +11,7 @@ pub fn process_swap(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
// Load accounts. // Load accounts.
let clock = Clock::get()?; 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 accounts
else { else {
return Err(ProgramError::NotEnoughAccountKeys); return Err(ProgramError::NotEnoughAccountKeys);
@@ -31,9 +31,6 @@ pub fn process_swap(accounts: &[AccountInfo<'_>], data: &[u8]) -> ProgramResult
|m| m.quote.reserves() > 0, |m| m.quote.reserves() > 0,
OreError::InsufficientLiquidity.into(), OreError::InsufficientLiquidity.into(),
)?; )?;
let miner = miner_info
.as_account_mut::<Miner>(&ore_api::ID)?
.assert_mut(|m| m.authority == *signer_info.key)?;
mint_base_info.has_address(&market.base.mint)?.as_mint()?; mint_base_info.has_address(&market.base.mint)?.as_mint()?;
mint_quote_info.has_address(&market.quote.mint)?.as_mint()?; mint_quote_info.has_address(&market.quote.mint)?.as_mint()?;
tokens_base_info 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 tokens.
transfer(signer_info, in_from, in_to, token_program, in_amount)?; transfer(signer_info, in_from, in_to, token_program, in_amount)?;
transfer_signed( transfer_signed(