Skip to content

Commit

Permalink
fixed comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rwwwx committed Aug 26, 2024
1 parent c6de19f commit 92eea0e
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 79 deletions.
18 changes: 9 additions & 9 deletions programs/rewards/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub enum RewardsInstruction {
lockup_period: LockupPeriod,
/// Specifies the owner of the Mining Account
mining_owner: Pubkey,
delegate_wallet_addr: Pubkey,
delegate: Pubkey,
},

/// Withdraws amount of supply to the mining account
Expand All @@ -78,7 +78,7 @@ pub enum RewardsInstruction {
amount: u64,
/// Specifies the owner of the Mining Account
mining_owner: Pubkey,
delegate_wallet_addr: Pubkey,
delegate: Pubkey,
},

/// Claims amount of rewards
Expand Down Expand Up @@ -116,7 +116,7 @@ pub enum RewardsInstruction {
/// The wallet who owns the mining account
mining_owner: Pubkey,
/// Wallet addres of delegate
delegate_wallet_addr: Pubkey,
delegate: Pubkey,
},

/// Distributes tokens among mining owners
Expand Down Expand Up @@ -244,7 +244,7 @@ pub fn deposit_mining(
amount: u64,
lockup_period: LockupPeriod,
mining_owner: &Pubkey,
delegate_wallet_addr: &Pubkey,
delegate: &Pubkey,
) -> Instruction {
let accounts = vec![
AccountMeta::new(*reward_pool, false),
Expand All @@ -259,7 +259,7 @@ pub fn deposit_mining(
amount,
lockup_period,
mining_owner: *mining_owner,
delegate_wallet_addr: *delegate_wallet_addr,
delegate: *delegate,
},
accounts,
)
Expand All @@ -275,7 +275,7 @@ pub fn withdraw_mining(
delegate_mining: &Pubkey,
amount: u64,
mining_owner: &Pubkey,
delegate_wallet_addr: &Pubkey,
delegate: &Pubkey,
) -> Instruction {
let accounts = vec![
AccountMeta::new(*reward_pool, false),
Expand All @@ -289,7 +289,7 @@ pub fn withdraw_mining(
&RewardsInstruction::WithdrawMining {
amount,
mining_owner: *mining_owner,
delegate_wallet_addr: *delegate_wallet_addr,
delegate: *delegate,
},
accounts,
)
Expand Down Expand Up @@ -335,7 +335,7 @@ pub fn extend_stake(
base_amount: u64,
additional_amount: u64,
mining_owner: &Pubkey,
delegate_wallet_addr: &Pubkey,
delegate: &Pubkey,
) -> Instruction {
let accounts = vec![
AccountMeta::new(*reward_pool, false),
Expand All @@ -353,7 +353,7 @@ pub fn extend_stake(
base_amount,
additional_amount,
mining_owner: *mining_owner,
delegate_wallet_addr: *delegate_wallet_addr,
delegate: *delegate,
},
accounts,
)
Expand Down
21 changes: 3 additions & 18 deletions programs/rewards/src/instructions/deposit_mining.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use crate::{
asserts::assert_and_get_pool_and_mining,
error::MplxRewardsError,
state::WrappedMining,
utils::{get_delegate_mining, verify_mining_address, AccountLoader, LockupPeriod},
utils::{get_delegate_mining, verify_delegate_mining_address, AccountLoader, LockupPeriod},
};
use solana_program::{account_info::AccountInfo, entrypoint::ProgramResult, pubkey::Pubkey};

Expand All @@ -12,7 +10,7 @@ pub fn process_deposit_mining<'a>(
amount: u64,
lockup_period: LockupPeriod,
mining_owner: &Pubkey,
delegate_wallet_addr: &Pubkey,
delegate: &Pubkey,
) -> ProgramResult {
let account_info_iter = &mut accounts.iter().enumerate();

Expand All @@ -35,21 +33,8 @@ pub fn process_deposit_mining<'a>(
)?;

let delegate_mining = get_delegate_mining(delegate_mining, mining)?;

if let Some(delegate_mining) = delegate_mining {
if *delegate_mining.key
!= verify_mining_address(
program_id,
delegate_wallet_addr,
reward_pool.key,
WrappedMining::from_bytes_mut(&mut delegate_mining.data.borrow_mut())?
.mining
.bump,
)
.map_err(|_| MplxRewardsError::DerivationError)?
{
return Err(MplxRewardsError::InvalidMining.into());
};
verify_delegate_mining_address(program_id, delegate_mining, delegate, reward_pool.key)?
}

wrapped_reward_pool.deposit(&mut wrapped_mining, amount, lockup_period, delegate_mining)?;
Expand Down
25 changes: 3 additions & 22 deletions programs/rewards/src/instructions/extend_stake.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use crate::{
asserts::assert_and_get_pool_and_mining,
error::MplxRewardsError,
state::WrappedMining,
utils::{get_delegate_mining, AccountLoader, LockupPeriod},
utils::{get_delegate_mining, verify_delegate_mining_address, AccountLoader, LockupPeriod},
};
use solana_program::{account_info::AccountInfo, entrypoint::ProgramResult, pubkey::Pubkey};

Expand All @@ -16,7 +14,7 @@ pub fn process_extend_stake<'a>(
base_amount: u64,
additional_amount: u64,
mining_owner: &Pubkey,
delegate_wallet_addr: &Pubkey,
delegate: &Pubkey,
) -> ProgramResult {
let account_info_iter = &mut accounts.iter().enumerate();

Expand All @@ -41,24 +39,7 @@ pub fn process_extend_stake<'a>(
let delegate_mining = get_delegate_mining(delegate_mining, mining)?;

if let Some(delegate_mining) = delegate_mining {
if *delegate_mining.key
!= Pubkey::create_program_address(
&[
"mining".as_bytes(),
&delegate_wallet_addr.to_bytes(),
&reward_pool.key.to_bytes(),
&[
WrappedMining::from_bytes_mut(&mut delegate_mining.data.borrow_mut())?
.mining
.bump,
],
],
program_id,
)
.map_err(|_| MplxRewardsError::DerivationError)?
{
return Err(MplxRewardsError::InvalidMining.into());
};
verify_delegate_mining_address(program_id, delegate_mining, delegate, reward_pool.key)?
}

wrapped_reward_pool.extend(
Expand Down
18 changes: 6 additions & 12 deletions programs/rewards/src/instructions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn process_instruction<'a>(
amount,
lockup_period,
mining_owner,
delegate_wallet_addr,
delegate,
} => {
msg!("RewardsInstruction: DepositMining");
process_deposit_mining(
Expand All @@ -64,22 +64,16 @@ pub fn process_instruction<'a>(
amount,
lockup_period,
&mining_owner,
&delegate_wallet_addr,
&delegate,
)
}
RewardsInstruction::WithdrawMining {
amount,
mining_owner,
delegate_wallet_addr,
delegate,
} => {
msg!("RewardsInstruction: WithdrawMining");
process_withdraw_mining(
program_id,
accounts,
amount,
&mining_owner,
&delegate_wallet_addr,
)
process_withdraw_mining(program_id, accounts, amount, &mining_owner, &delegate)
}
RewardsInstruction::Claim => {
msg!("RewardsInstruction: Claim");
Expand All @@ -92,7 +86,7 @@ pub fn process_instruction<'a>(
base_amount,
additional_amount,
mining_owner,
delegate_wallet_addr,
delegate,
} => {
msg!("RewardsInstruction: ExtendStake");
process_extend_stake(
Expand All @@ -104,7 +98,7 @@ pub fn process_instruction<'a>(
base_amount,
additional_amount,
&mining_owner,
&delegate_wallet_addr,
&delegate,
)
}
RewardsInstruction::DistributeRewards => {
Expand Down
19 changes: 3 additions & 16 deletions programs/rewards/src/instructions/withdraw_mining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ use crate::{
utils::{get_delegate_mining, AccountLoader},
};

use crate::{error::MplxRewardsError, state::WrappedMining, utils::verify_mining_address};
use crate::utils::verify_delegate_mining_address;
use solana_program::{account_info::AccountInfo, entrypoint::ProgramResult, pubkey::Pubkey};

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

Expand All @@ -34,21 +34,8 @@ pub fn process_withdraw_mining<'a>(
)?;

let delegate_mining = get_delegate_mining(delegate_mining, mining)?;

if let Some(delegate_mining) = delegate_mining {
if *delegate_mining.key
!= verify_mining_address(
program_id,
delegate_wallet_addr,
reward_pool.key,
WrappedMining::from_bytes_mut(&mut delegate_mining.data.borrow_mut())?
.mining
.bump,
)
.map_err(|_| MplxRewardsError::DerivationError)?
{
return Err(MplxRewardsError::InvalidMining.into());
};
verify_delegate_mining_address(program_id, delegate_mining, delegate, reward_pool.key)?
}

wrapped_reward_pool.withdraw(&mut wrapped_mining, amount, delegate_mining)?;
Expand Down
27 changes: 25 additions & 2 deletions programs/rewards/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Arbitrary auxilliary functions
use std::iter::Enumerate;

use crate::error::MplxRewardsError;
use crate::{error::MplxRewardsError, state::WrappedImmutableMining};
use borsh::{BorshDeserialize, BorshSerialize};
use solana_program::{
account_info::AccountInfo,
Expand Down Expand Up @@ -118,6 +118,29 @@ pub fn get_delegate_mining<'a, 'b>(
}
}

pub fn verify_delegate_mining_address(
program_id: &Pubkey,
delegate_mining: &AccountInfo<'_>,
delegate: &Pubkey,
reward_pool_key: &Pubkey,
) -> Result<(), ProgramError> {
if *delegate_mining.key
!= create_mining_address(
program_id,
delegate,
reward_pool_key,
WrappedImmutableMining::from_bytes(&delegate_mining.data.borrow())?
.mining
.bump,
)
.map_err(|_| MplxRewardsError::DerivationError)?
{
return Err(MplxRewardsError::InvalidMining.into());
}

Ok(())
}

/// Helper for parsing accounts with arbitrary input conditions
pub struct AccountLoader {}

Expand Down Expand Up @@ -322,7 +345,7 @@ impl SafeArithmeticOperations for u128 {
}
}

pub fn verify_mining_address(
pub fn create_mining_address(
program_id: &Pubkey,
mining_owner: &Pubkey,
reward_pool: &Pubkey,
Expand Down

0 comments on commit 92eea0e

Please sign in to comment.