Skip to content

Commit

Permalink
change naming && add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kstepanovdev committed Sep 2, 2024
1 parent 8a5b238 commit a638638
Show file tree
Hide file tree
Showing 14 changed files with 252 additions and 95 deletions.
26 changes: 20 additions & 6 deletions programs/rewards/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,16 @@ pub enum RewardsInstruction {
#[account(0, signer, name = "deposit_authority", desc = "The address of the Staking program's Registrar, which is PDA and is responsible for signing CPIs")]
#[account(1, name = "reward_pool", desc = "The address of the reward pool")]
#[account(2, writable, name = "mining", desc = "The address of the mining account which belongs to the user and stores info about user's rewards")]
RestrictClaiming {},
RestrictTokenFlow {
mining_owner: Pubkey,
},

#[account(0, signer, name = "deposit_authority", desc = "The address of the Staking program's Registrar, which is PDA and is responsible for signing CPIs")]
#[account(1, name = "reward_pool", desc = "The address of the reward pool")]
#[account(2, writable, name = "mining", desc = "The address of the mining account which belongs to the user and stores info about user's rewards")]
AllowClaiming {},
AllowTokenFlow {
mining_owner: Pubkey,
},
}

/// Creates 'InitializePool' instruction.
Expand Down Expand Up @@ -443,11 +447,12 @@ pub fn change_delegate(
)
}

pub fn restrict_claiming(
pub fn restrict_tokenflow(
program_id: &Pubkey,
deposit_authority: &Pubkey,
reward_pool: &Pubkey,
mining: &Pubkey,
mining_owner: &Pubkey,
) -> Instruction {
let accounts = vec![
AccountMeta::new_readonly(*deposit_authority, true),
Expand All @@ -457,22 +462,31 @@ pub fn restrict_claiming(

Instruction::new_with_borsh(
*program_id,
&RewardsInstruction::RestrictClaiming {},
&RewardsInstruction::RestrictTokenFlow {
mining_owner: *mining_owner,
},
accounts,
)
}

pub fn allow_claiming(
pub fn allow_tokenflow(
program_id: &Pubkey,
deposit_authority: &Pubkey,
reward_pool: &Pubkey,
mining: &Pubkey,
mining_owner: &Pubkey,
) -> Instruction {
let accounts = vec![
AccountMeta::new_readonly(*deposit_authority, true),
AccountMeta::new_readonly(*reward_pool, false),
AccountMeta::new(*mining, false),
];

Instruction::new_with_borsh(*program_id, &RewardsInstruction::AllowClaiming {}, accounts)
Instruction::new_with_borsh(
*program_id,
&RewardsInstruction::AllowTokenFlow {
mining_owner: *mining_owner,
},
accounts,
)
}
2 changes: 1 addition & 1 deletion programs/rewards/src/instructions/claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn process_claim<'a>(program_id: &Pubkey, accounts: &'a [AccountInfo<'a>]) -
let mining_data = &mut mining.data.borrow_mut();
let mut wrapped_mining = WrappedMining::from_bytes_mut(mining_data)?;

if wrapped_mining.mining.is_claiming_restricted() {
if wrapped_mining.mining.is_tokenflow_restricted() {
return Err(MplxRewardsError::ClaimingRestricted.into());
}

Expand Down
8 changes: 4 additions & 4 deletions programs/rewards/src/instructions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ pub fn process_instruction<'a>(
msg!("RewardsInstruction: ChangeDelegate");
process_change_delegate(program_id, accounts, staked_amount, &new_delegate)
}
RewardsInstruction::RestrictClaiming {} => {
RewardsInstruction::RestrictTokenFlow { mining_owner } => {
msg!("RewardsInstruction: RestrictClaiming");
process_restrict_claiming(program_id, accounts)
process_restrict_tokenflow(program_id, accounts, &mining_owner)
}
RewardsInstruction::AllowClaiming {} => {
RewardsInstruction::AllowTokenFlow { mining_owner } => {
msg!("RewardsInstruction: AllowClaiming");
process_allow_claiming(program_id, accounts)
process_allow_tokenflow(program_id, accounts, &mining_owner)
}
}
}
32 changes: 0 additions & 32 deletions programs/rewards/src/instructions/penalties/allow_claiming.rs

This file was deleted.

31 changes: 31 additions & 0 deletions programs/rewards/src/instructions/penalties/allow_tokenflow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use crate::{asserts::assert_and_get_pool_and_mining, utils::AccountLoader};
use solana_program::{account_info::AccountInfo, entrypoint::ProgramResult, pubkey::Pubkey};

pub fn process_allow_tokenflow<'a>(
program_id: &Pubkey,
accounts: &'a [AccountInfo<'a>],
mining_owner: &Pubkey,
) -> ProgramResult {
let account_info_iter = &mut accounts.iter().enumerate();

let deposit_authority = AccountLoader::next_signer(account_info_iter)?;
let reward_pool = AccountLoader::next_with_owner(account_info_iter, program_id)?;
let mining = AccountLoader::next_with_owner(account_info_iter, program_id)?;

let reward_pool_data = &mut reward_pool.data.borrow_mut();
let mining_data = &mut mining.data.borrow_mut();

let (_, wrapped_mining) = assert_and_get_pool_and_mining(
program_id,
mining_owner,
mining,
reward_pool,
deposit_authority,
reward_pool_data,
mining_data,
)?;

wrapped_mining.mining.allow_tokenflow()?;

Ok(())
}
8 changes: 4 additions & 4 deletions programs/rewards/src/instructions/penalties/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub(crate) use allow_claiming::*;
pub(crate) use restrict_claiming::*;
pub(crate) use allow_tokenflow::*;
pub(crate) use restrict_tokenflow::*;

mod allow_claiming;
mod restrict_claiming;
mod allow_tokenflow;
mod restrict_tokenflow;
32 changes: 0 additions & 32 deletions programs/rewards/src/instructions/penalties/restrict_claiming.rs

This file was deleted.

31 changes: 31 additions & 0 deletions programs/rewards/src/instructions/penalties/restrict_tokenflow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use crate::{asserts::assert_and_get_pool_and_mining, utils::AccountLoader};
use solana_program::{account_info::AccountInfo, entrypoint::ProgramResult, pubkey::Pubkey};

pub fn process_restrict_tokenflow<'a>(
program_id: &Pubkey,
accounts: &'a [AccountInfo<'a>],
mining_owner: &Pubkey,
) -> ProgramResult {
let account_info_iter = &mut accounts.iter().enumerate();

let deposit_authority = AccountLoader::next_signer(account_info_iter)?;
let reward_pool = AccountLoader::next_with_owner(account_info_iter, program_id)?;
let mining = AccountLoader::next_with_owner(account_info_iter, program_id)?;

let reward_pool_data = &mut reward_pool.data.borrow_mut();
let mining_data = &mut mining.data.borrow_mut();

let (_, wrapped_mining) = assert_and_get_pool_and_mining(
program_id,
mining_owner,
mining,
reward_pool,
deposit_authority,
reward_pool_data,
mining_data,
)?;

wrapped_mining.mining.restrict_tokenflow()?;

Ok(())
}
2 changes: 1 addition & 1 deletion programs/rewards/src/instructions/withdraw_mining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn process_withdraw_mining<'a>(
mining_data,
)?;

if wrapped_mining.mining.is_claiming_restricted() {
if wrapped_mining.mining.is_tokenflow_restricted() {
return Err(MplxRewardsError::WithdrawalRestricted.into());
}

Expand Down
6 changes: 3 additions & 3 deletions programs/rewards/src/state/mining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ impl Mining {
Ok(())
}

pub fn restrict_claiming(&mut self) -> ProgramResult {
pub fn restrict_tokenflow(&mut self) -> ProgramResult {
if self.data[CLAIMING_RESTRICTION_BYTE] == 1 {
Err(MplxRewardsError::MiningAlreadyRestricted.into())
} else {
Expand All @@ -207,7 +207,7 @@ impl Mining {
}
}

pub fn allow_claiming(&mut self) -> ProgramResult {
pub fn allow_tokenflow(&mut self) -> ProgramResult {
if self.data[CLAIMING_RESTRICTION_BYTE] == 0 {
Err(MplxRewardsError::MiningNotRestricted.into())
} else {
Expand All @@ -216,7 +216,7 @@ impl Mining {
}
}

pub fn is_claiming_restricted(&self) -> bool {
pub fn is_tokenflow_restricted(&self) -> bool {
self.data[CLAIMING_RESTRICTION_BYTE] == 1
}
}
Expand Down
Binary file modified programs/rewards/tests/rewards/fixtures/mplx_rewards.so
Binary file not shown.
2 changes: 1 addition & 1 deletion programs/rewards/tests/rewards/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ mod precision;
mod utils;
mod withdraw_mining;

mod claim_restrictions;
mod tokenflow_restrictions;

mod extend_stake;
Loading

0 comments on commit a638638

Please sign in to comment.