ix builders

This commit is contained in:
Hardhat Chad
2024-04-28 17:20:14 +00:00
parent e65aa9c385
commit 96bc317c96
4 changed files with 39 additions and 21 deletions

View File

@@ -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 = [

View File

@@ -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?

View File

@@ -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(())

View File

@@ -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),