diff --git a/programs/rewards/src/instruction.rs b/programs/rewards/src/instruction.rs index e51c925e..bfaa7772 100644 --- a/programs/rewards/src/instruction.rs +++ b/programs/rewards/src/instruction.rs @@ -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 @@ -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 @@ -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 @@ -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), @@ -259,7 +259,7 @@ pub fn deposit_mining( amount, lockup_period, mining_owner: *mining_owner, - delegate_wallet_addr: *delegate_wallet_addr, + delegate: *delegate, }, accounts, ) @@ -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), @@ -289,7 +289,7 @@ pub fn withdraw_mining( &RewardsInstruction::WithdrawMining { amount, mining_owner: *mining_owner, - delegate_wallet_addr: *delegate_wallet_addr, + delegate: *delegate, }, accounts, ) @@ -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), @@ -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, ) diff --git a/programs/rewards/src/instructions/deposit_mining.rs b/programs/rewards/src/instructions/deposit_mining.rs index 2697689e..be3d3d8d 100644 --- a/programs/rewards/src/instructions/deposit_mining.rs +++ b/programs/rewards/src/instructions/deposit_mining.rs @@ -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}; @@ -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(); @@ -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)?; diff --git a/programs/rewards/src/instructions/extend_stake.rs b/programs/rewards/src/instructions/extend_stake.rs index b784c1fa..a1640dd2 100644 --- a/programs/rewards/src/instructions/extend_stake.rs +++ b/programs/rewards/src/instructions/extend_stake.rs @@ -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}; @@ -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(); @@ -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( diff --git a/programs/rewards/src/instructions/mod.rs b/programs/rewards/src/instructions/mod.rs index 2192023d..ee9877ec 100644 --- a/programs/rewards/src/instructions/mod.rs +++ b/programs/rewards/src/instructions/mod.rs @@ -55,7 +55,7 @@ pub fn process_instruction<'a>( amount, lockup_period, mining_owner, - delegate_wallet_addr, + delegate, } => { msg!("RewardsInstruction: DepositMining"); process_deposit_mining( @@ -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"); @@ -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( @@ -104,7 +98,7 @@ pub fn process_instruction<'a>( base_amount, additional_amount, &mining_owner, - &delegate_wallet_addr, + &delegate, ) } RewardsInstruction::DistributeRewards => { diff --git a/programs/rewards/src/instructions/withdraw_mining.rs b/programs/rewards/src/instructions/withdraw_mining.rs index 83867874..d9f88993 100644 --- a/programs/rewards/src/instructions/withdraw_mining.rs +++ b/programs/rewards/src/instructions/withdraw_mining.rs @@ -3,7 +3,7 @@ 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>( @@ -11,7 +11,7 @@ pub fn process_withdraw_mining<'a>( accounts: &'a [AccountInfo<'a>], amount: u64, mining_owner: &Pubkey, - delegate_wallet_addr: &Pubkey, + delegate: &Pubkey, ) -> ProgramResult { let account_info_iter = &mut accounts.iter().enumerate(); @@ -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)?; diff --git a/programs/rewards/src/utils.rs b/programs/rewards/src/utils.rs index e9374def..5bd2c415 100644 --- a/programs/rewards/src/utils.rs +++ b/programs/rewards/src/utils.rs @@ -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, @@ -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 {} @@ -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,