From 96bc317c9694ccd1f0996e3393d071225527b3c5 Mon Sep 17 00:00:00 2001 From: Hardhat Chad Date: Sun, 28 Apr 2024 17:20:14 +0000 Subject: [PATCH] ix builders --- src/instruction.rs | 34 +++++++++++++++++++++++++++++++--- src/lib.rs | 1 - src/processor/claim.rs | 21 ++++++--------------- src/processor/stake.rs | 4 ++-- 4 files changed, 39 insertions(+), 21 deletions(-) diff --git a/src/instruction.rs b/src/instruction.rs index 955ca1c..6674f6c 100644 --- a/src/instruction.rs +++ b/src/instruction.rs @@ -57,8 +57,8 @@ pub enum OreInstruction { #[account(0, name = "ore_program", desc = "Ore program")] #[account(1, name = "signer", desc = "Signer", signer)] - #[account(2, name = "sender", desc = "Signer token account", writable)] - #[account(3, name = "proof", desc = "Ore proof account", writable)] + #[account(2, name = "proof", desc = "Ore proof account", writable)] + #[account(3, name = "sender", desc = "Signer token account", writable)] #[account(4, name = "treasury_tokens", desc = "Ore treasury token account", writable)] #[account(5, name = "token_program", desc = "SPL token program")] Stake = 4, @@ -271,7 +271,7 @@ pub fn claim(signer: Pubkey, beneficiary: Pubkey, amount: u64) -> Instruction { AccountMeta::new(signer, true), AccountMeta::new(beneficiary, false), AccountMeta::new(proof, false), - AccountMeta::new(TREASURY_ADDRESS, false), + AccountMeta::new_readonly(TREASURY_ADDRESS, false), AccountMeta::new(treasury_tokens, false), AccountMeta::new_readonly(spl_token::id(), false), ], @@ -287,6 +287,34 @@ pub fn claim(signer: Pubkey, beneficiary: Pubkey, amount: u64) -> Instruction { } } +/// Build a stake instruction. +pub fn stake(signer: Pubkey, sender: 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, + ); + Instruction { + program_id: crate::id(), + accounts: vec![ + AccountMeta::new(signer, true), + AccountMeta::new(proof, false), + AccountMeta::new(sender, false), + AccountMeta::new(treasury_tokens, false), + AccountMeta::new_readonly(spl_token::id(), false), + ], + data: [ + OreInstruction::Stake.to_vec(), + StakeArgs { + amount: amount.to_le_bytes(), + } + .to_bytes() + .to_vec(), + ] + .concat(), + } +} + /// Builds an initialize instruction. pub fn initialize(signer: Pubkey) -> Instruction { let bus_pdas = [ diff --git a/src/lib.rs b/src/lib.rs index a8316e4..724c880 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,7 +14,6 @@ use solana_program::{ program_error::ProgramError, pubkey::Pubkey, }; -// TODO Admin fn for pause // TODO u128 for internal rewards representation? // TODO Admin fn for min difficulty? What if this were set automatically by u128 base reward rate? // TODO Increase bus count? diff --git a/src/processor/claim.rs b/src/processor/claim.rs index 9287b12..69ae1fe 100644 --- a/src/processor/claim.rs +++ b/src/processor/claim.rs @@ -4,18 +4,13 @@ use solana_program::{ }; use crate::{ - error::OreError, - instruction::ClaimArgs, - loaders::*, - state::{Proof, Treasury}, - utils::AccountDeserialize, - MINT_ADDRESS, TREASURY, + error::OreError, instruction::ClaimArgs, loaders::*, state::Proof, utils::AccountDeserialize, + MINT_ADDRESS, TREASURY, TREASURY_BUMP, }; /// Claim distributes owed token rewards from the treasury to the miner. Its responsibilies include: -/// 1. Transfer tokens from the treasury to the miner. -/// 2. Decrement the miner's claimable rewards counter by an appropriate amount. -/// 3. Update the program's lifetime stats. +/// 1. Decrement the miner's claimable rewards counter. +/// 2. Transfer tokens from the treasury to the miner. /// /// Safety requirements: /// - Claim is a permissionless instruction and can be called by any miner. @@ -39,7 +34,7 @@ pub fn process_claim<'a, 'info>( load_signer(signer)?; load_token_account(beneficiary_info, None, &MINT_ADDRESS, true)?; load_proof(proof_info, signer.key, true)?; - load_treasury(treasury_info, true)?; + load_treasury(treasury_info, false)?; load_token_account( treasury_tokens_info, Some(treasury_info.key), @@ -57,10 +52,6 @@ pub fn process_claim<'a, 'info>( .ok_or(OreError::ClaimTooLarge)?; // Distribute tokens from treasury to beneficiary - let mut treasury_data = treasury_info.data.borrow_mut(); - let treasury = Treasury::try_from_bytes_mut(&mut treasury_data)?; - let treasury_bump = treasury.bump; - drop(treasury_data); solana_program::program::invoke_signed( &spl_token::instruction::transfer( &spl_token::id(), @@ -76,7 +67,7 @@ pub fn process_claim<'a, 'info>( beneficiary_info.clone(), treasury_info.clone(), ], - &[&[TREASURY, &[treasury_bump as u8]]], + &[&[TREASURY, &[TREASURY_BUMP]]], )?; Ok(()) diff --git a/src/processor/stake.rs b/src/processor/stake.rs index e3390a3..68405ed 100644 --- a/src/processor/stake.rs +++ b/src/processor/stake.rs @@ -18,13 +18,13 @@ pub fn process_stake<'a, 'info>( let amount = u64::from_le_bytes(args.amount); // Load accounts - let [signer_info, sender_info, proof_info, treasury_tokens_info, token_program] = accounts + let [signer_info, proof_info, sender_info, treasury_tokens_info, token_program] = accounts else { return Err(ProgramError::NotEnoughAccountKeys); }; load_signer(signer_info)?; - load_token_account(sender_info, Some(signer_info.key), &MINT_ADDRESS, true)?; load_proof(proof_info, signer_info.key, true)?; + load_token_account(sender_info, Some(signer_info.key), &MINT_ADDRESS, true)?; load_token_account( treasury_tokens_info, Some(&TREASURY_ADDRESS),