From 85b82222876412e199563e40d7c25df6b8961aa4 Mon Sep 17 00:00:00 2001 From: Hardhat Chad Date: Mon, 26 May 2025 13:57:53 -0700 Subject: [PATCH] backwards compatability --- api/src/instruction.rs | 18 ++++++++++---- program/src/claim.rs | 54 ++++++++++++++++++++++++++++++++++++++++++ program/src/lib.rs | 3 +++ 3 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 program/src/claim.rs diff --git a/api/src/instruction.rs b/api/src/instruction.rs index c150769..7476324 100644 --- a/api/src/instruction.rs +++ b/api/src/instruction.rs @@ -4,11 +4,12 @@ use steel::*; #[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)] pub enum OreInstruction { // User - Bet = 0, - Bury = 1, - Close = 2, - Payout = 3, - Reset = 4, + Claim = 0, + Bet = 1, + Bury = 2, + Close = 3, + Payout = 4, + Reset = 5, // Admin Initialize = 100, @@ -25,6 +26,12 @@ pub struct Bet { #[derive(Clone, Copy, Debug, Pod, Zeroable)] pub struct Bury {} +#[repr(C)] +#[derive(Clone, Copy, Debug, Pod, Zeroable)] +pub struct Claim { + pub amount: [u8; 8], +} + #[repr(C)] #[derive(Clone, Copy, Debug, Pod, Zeroable)] pub struct Close {} @@ -41,6 +48,7 @@ pub struct Payout {} #[derive(Clone, Copy, Debug, Pod, Zeroable)] pub struct Initialize {} +instruction!(OreInstruction, Claim); instruction!(OreInstruction, Bet); instruction!(OreInstruction, Bury); instruction!(OreInstruction, Close); diff --git a/program/src/claim.rs b/program/src/claim.rs new file mode 100644 index 0000000..1dd52b6 --- /dev/null +++ b/program/src/claim.rs @@ -0,0 +1,54 @@ +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 { + // Parse args. + let args = Claim::try_from_bytes(data)?; + let amount = u64::from_le_bytes(args.amount); + + // Load accounts. + let clock = Clock::get()?; + let [signer_info, beneficiary_info, proof_info, treasury_info, treasury_tokens_info, token_program] = + accounts + else { + return Err(ProgramError::NotEnoughAccountKeys); + }; + signer_info.is_signer()?; + beneficiary_info + .is_writable()? + .as_token_account()? + .assert(|t| t.mint() == MINT_ADDRESS)?; + let proof = proof_info + .as_account_mut::(&ore_api::ID)? + .assert_mut_err( + |p| p.authority == *signer_info.key, + ProgramError::MissingRequiredSignature, + )?; + treasury_info.has_address(&TREASURY_ADDRESS)?; + treasury_tokens_info + .is_writable()? + .has_address(&TREASURY_TOKENS_ADDRESS)?; + token_program.is_program(&spl_token::ID)?; + + // Update miner balance. + proof.balance = proof + .balance + .checked_sub(amount) + .ok_or(ProgramError::InsufficientFunds)?; + + // Update last claim timestamp. + proof.last_claim_at = clock.unix_timestamp; + + // Transfer tokens from treasury to beneficiary. + transfer_signed( + treasury_info, + treasury_tokens_info, + beneficiary_info, + token_program, + amount, + &[TREASURY], + )?; + + Ok(()) +} diff --git a/program/src/lib.rs b/program/src/lib.rs index 4d7fcd4..de76c3f 100644 --- a/program/src/lib.rs +++ b/program/src/lib.rs @@ -1,5 +1,6 @@ mod bet; mod bury; +mod claim; mod close; mod initialize; mod payout; @@ -7,6 +8,7 @@ mod reset; use bet::*; use bury::*; +use claim::*; use close::*; use initialize::*; use payout::*; @@ -25,6 +27,7 @@ pub fn process_instruction( match ix { // User OreInstruction::Bet => process_bet(accounts, data)?, + OreInstruction::Claim => process_claim(accounts, data)?, OreInstruction::Close => process_close(accounts, data)?, OreInstruction::Payout => process_payout(accounts, data)?, OreInstruction::Reset => process_reset(accounts, data)?,