diff --git a/programs/mpl-staking/src/cpi_instructions.rs b/programs/mpl-staking/src/cpi_instructions.rs index 45fb1377..fec5d219 100644 --- a/programs/mpl-staking/src/cpi_instructions.rs +++ b/programs/mpl-staking/src/cpi_instructions.rs @@ -75,6 +75,8 @@ pub enum RewardsInstruction { lockup_period: LockupPeriod, /// Specifies the owner of the Mining Account owner: Pubkey, + /// Wallet addres of delegate + delegate: Pubkey, }, /// Withdraws amount of supply to the mining account @@ -89,6 +91,8 @@ pub enum RewardsInstruction { amount: u64, /// Specifies the owner of the Mining Account owner: Pubkey, + /// Wallet addres of delegate + delegate: Pubkey, }, /// Claims amount of rewards @@ -129,6 +133,8 @@ pub enum RewardsInstruction { additional_amount: u64, /// The wallet who owns the mining account mining_owner: Pubkey, + /// Wallet addres of delegate + delegate: Pubkey, }, /// Distributes tokens among mining owners @@ -155,6 +161,7 @@ pub enum RewardsInstruction { ChangeDelegate { /// Amount of staked tokens staked_amount: u64, + new_delegate: Pubkey, }, } @@ -255,6 +262,7 @@ pub fn deposit_mining<'a>( lockup_period: LockupPeriod, owner: &Pubkey, signers_seeds: &[&[u8]], + delegate_wallet_addr: &Pubkey, ) -> ProgramResult { let accounts = vec![ AccountMeta::new(reward_pool.key(), false), @@ -269,6 +277,7 @@ pub fn deposit_mining<'a>( amount, lockup_period, owner: *owner, + delegate: *delegate_wallet_addr, }, accounts, ); @@ -301,6 +310,7 @@ pub fn extend_stake<'a>( additional_amount: u64, mining_owner: &Pubkey, signers_seeds: &[&[u8]], + delegate_wallet_addr: &Pubkey, ) -> ProgramResult { let accounts = vec![ AccountMeta::new(reward_pool.key(), false), @@ -318,6 +328,7 @@ pub fn extend_stake<'a>( base_amount, additional_amount, mining_owner: *mining_owner, + delegate: *delegate_wallet_addr, }, accounts, ); @@ -348,6 +359,7 @@ pub fn withdraw_mining<'a>( amount: u64, owner: &Pubkey, signers_seeds: &[&[u8]], + delegate_wallet_addr: &Pubkey, ) -> ProgramResult { let accounts = vec![ AccountMeta::new(reward_pool.key(), false), @@ -361,6 +373,7 @@ pub fn withdraw_mining<'a>( &RewardsInstruction::WithdrawMining { amount, owner: *owner, + delegate: *delegate_wallet_addr, }, accounts, ); @@ -467,6 +480,7 @@ pub fn change_delegate<'a>( mining_owner: AccountInfo<'a>, old_delegate_mining: AccountInfo<'a>, new_delegate_mining: AccountInfo<'a>, + new_delegate: Pubkey, staked_amount: u64, signers_seeds: &[&[u8]], ) -> ProgramResult { @@ -481,7 +495,10 @@ pub fn change_delegate<'a>( let ix = Instruction::new_with_borsh( program_id.key(), - &RewardsInstruction::ChangeDelegate { staked_amount }, + &RewardsInstruction::ChangeDelegate { + staked_amount, + new_delegate, + }, accounts, ); diff --git a/programs/mpl-staking/src/instructions/change_delegate.rs b/programs/mpl-staking/src/instructions/change_delegate.rs index b3c45364..69e42193 100644 --- a/programs/mpl-staking/src/instructions/change_delegate.rs +++ b/programs/mpl-staking/src/instructions/change_delegate.rs @@ -1,4 +1,4 @@ -use crate::{clock_unix_timestamp, cpi_instructions, find_mining_address}; +use crate::{clock_unix_timestamp, cpi_instructions}; use anchor_lang::prelude::*; use mplx_staking_states::{ error::MplStakingError, @@ -105,16 +105,6 @@ pub fn change_delegate(ctx: Context, deposit_entry_index: u8) -> MplStakingError::InsufficientWeightedStake ); - let (delegate_mining, _) = find_mining_address( - &ctx.accounts.rewards_program.key(), - &delegate_voter.voter_authority, - &ctx.accounts.reward_pool.key(), - ); - - require!( - delegate_mining == ctx.accounts.new_delegate_mining.key(), - MplStakingError::InvalidMining - ); target.delegate = delegate_voter.voter_authority; } target.delegate_last_update_ts = curr_ts; @@ -132,6 +122,7 @@ pub fn change_delegate(ctx: Context, deposit_entry_index: u8) -> ]; let staked_amount = target.amount_deposited_native; let mining_owner = ctx.accounts.voter_authority.to_account_info(); + let new_delegate = target.delegate; cpi_instructions::change_delegate( ctx.accounts.rewards_program.to_account_info(), @@ -141,6 +132,7 @@ pub fn change_delegate(ctx: Context, deposit_entry_index: u8) -> mining_owner, old_delegate_mining, new_delegate_mining, + new_delegate, staked_amount, signers_seeds, )?; diff --git a/programs/mpl-staking/src/instructions/extend_stake.rs b/programs/mpl-staking/src/instructions/extend_stake.rs index 65ffb499..613497ec 100644 --- a/programs/mpl-staking/src/instructions/extend_stake.rs +++ b/programs/mpl-staking/src/instructions/extend_stake.rs @@ -72,7 +72,7 @@ pub fn extend_stake( source_mint_idx, MplStakingError::InvalidMint ); - ctx.accounts.verify_delegate_and_its_mining(target)?; + ctx.accounts.verify_delegate(target)?; target.amount_deposited_native = target .amount_deposited_native @@ -95,6 +95,7 @@ pub fn extend_stake( &[registrar.bump][..], ]; let mining_owner = &ctx.accounts.voter_authority.key(); + let delegate_wallet_addr = &ctx.accounts.delegate.key(); cpi_instructions::extend_stake( ctx.accounts.rewards_program.to_account_info(), @@ -109,6 +110,7 @@ pub fn extend_stake( additional_amount, mining_owner, signers_seeds, + delegate_wallet_addr, )?; Ok(()) diff --git a/programs/mpl-staking/src/instructions/mod.rs b/programs/mpl-staking/src/instructions/mod.rs index 1ec88a62..a59d155d 100644 --- a/programs/mpl-staking/src/instructions/mod.rs +++ b/programs/mpl-staking/src/instructions/mod.rs @@ -9,7 +9,7 @@ pub use create_voter::*; pub use deposit::*; pub use extend_stake::*; pub use log_voter_info::*; -use solana_program::{clock::Clock, pubkey::Pubkey, sysvar::Sysvar}; +use solana_program::{clock::Clock, sysvar::Sysvar}; pub use stake::*; pub use unlock_tokens::*; pub use update_voter_weight_record::*; @@ -34,19 +34,3 @@ mod withdraw; pub fn clock_unix_timestamp() -> u64 { Clock::get().unwrap().unix_timestamp as u64 } - -/// Generates mining address -pub fn find_mining_address( - program_id: &Pubkey, - mining_owner: &Pubkey, - reward_pool: &Pubkey, -) -> (Pubkey, u8) { - Pubkey::find_program_address( - &[ - "mining".as_bytes(), - &mining_owner.to_bytes(), - &reward_pool.to_bytes(), - ], - program_id, - ) -} diff --git a/programs/mpl-staking/src/instructions/stake.rs b/programs/mpl-staking/src/instructions/stake.rs index 02211d13..70a86ce4 100644 --- a/programs/mpl-staking/src/instructions/stake.rs +++ b/programs/mpl-staking/src/instructions/stake.rs @@ -53,7 +53,7 @@ pub fn stake( target.amount_deposited_native == 0, MplStakingError::DepositEntryIsOld ); - ctx.accounts.verify_delegate_and_its_mining(target)?; + ctx.accounts.verify_delegate(target)?; // Add target amounts target.amount_deposited_native = target @@ -73,6 +73,7 @@ pub fn stake( &[registrar.bump][..], ]; let owner = &ctx.accounts.voter_authority.key(); + let delegate_wallet_addr = &ctx.accounts.delegate.key(); cpi_instructions::deposit_mining( ctx.accounts.rewards_program.to_account_info(), @@ -84,6 +85,7 @@ pub fn stake( target.lockup.period, owner, signers_seeds, + delegate_wallet_addr, )?; Ok(()) diff --git a/programs/mpl-staking/src/instructions/unlock_tokens.rs b/programs/mpl-staking/src/instructions/unlock_tokens.rs index 7fab4b0b..a44038a1 100644 --- a/programs/mpl-staking/src/instructions/unlock_tokens.rs +++ b/programs/mpl-staking/src/instructions/unlock_tokens.rs @@ -25,7 +25,7 @@ pub fn unlock_tokens(ctx: Context, deposit_entry_index: u8) -> Result<()> MplStakingError::DepositStillLocked ); - ctx.accounts.verify_delegate_and_its_mining(deposit_entry)?; + ctx.accounts.verify_delegate(deposit_entry)?; deposit_entry.lockup.cooldown_requested = true; deposit_entry.lockup.cooldown_ends_at = curr_ts @@ -44,6 +44,7 @@ pub fn unlock_tokens(ctx: Context, deposit_entry_index: u8) -> Result<()> (registrar.realm_governing_token_mint.as_ref()), &[registrar.bump][..], ]; + let delegate_wallet_addr = &ctx.accounts.delegate.key(); withdraw_mining( rewards_program, @@ -54,6 +55,7 @@ pub fn unlock_tokens(ctx: Context, deposit_entry_index: u8) -> Result<()> deposit_entry.amount_deposited_native, owner.key, signers_seeds, + delegate_wallet_addr, )?; Ok(()) diff --git a/programs/mpl-staking/src/lib.rs b/programs/mpl-staking/src/lib.rs index 8aef4e45..7ae1f535 100644 --- a/programs/mpl-staking/src/lib.rs +++ b/programs/mpl-staking/src/lib.rs @@ -226,7 +226,7 @@ pub struct Stake<'info> { } impl Stake<'_> { - pub fn verify_delegate_and_its_mining(&self, deposit_entry: &DepositEntry) -> Result<()> { + pub fn verify_delegate(&self, deposit_entry: &DepositEntry) -> Result<()> { // check whether target delegate mining is the same as delegate mining from passed context require_eq!( deposit_entry.delegate, @@ -234,17 +234,6 @@ impl Stake<'_> { MplStakingError::InvalidDelegate ); - let (calculated_delegate_mining, _) = find_mining_address( - &self.rewards_program.to_account_info().key(), - &self.delegate.to_account_info().key(), - &self.reward_pool.key(), - ); - require_eq!( - calculated_delegate_mining, - self.delegate_mining.to_account_info().key(), - MplStakingError::InvalidMining - ); - Ok(()) } } diff --git a/programs/mpl-staking/tests/fixtures/mplx_rewards.so b/programs/mpl-staking/tests/fixtures/mplx_rewards.so index 4d6cd572..cdc905d5 100755 Binary files a/programs/mpl-staking/tests/fixtures/mplx_rewards.so and b/programs/mpl-staking/tests/fixtures/mplx_rewards.so differ diff --git a/programs/mpl-staking/tests/program_test/rewards.rs b/programs/mpl-staking/tests/program_test/rewards.rs index ee3f8883..e0263c93 100644 --- a/programs/mpl-staking/tests/program_test/rewards.rs +++ b/programs/mpl-staking/tests/program_test/rewards.rs @@ -1,7 +1,6 @@ use crate::SolanaCookie; use anchor_lang::{prelude::*, AnchorDeserialize}; use mpl_staking::cpi_instructions::RewardsInstruction; -use mplx_staking_states::state::LockupPeriod; use solana_program_test::*; use solana_sdk::{ instruction::{AccountMeta, Instruction}, @@ -128,49 +127,6 @@ impl RewardsCookie { Ok(mining) } - - #[allow(clippy::too_many_arguments)] - pub async fn deposit_mining<'a>( - &self, - reward_pool: &Pubkey, - deposit_authority: &Keypair, - amount: u64, - lockup_period: LockupPeriod, - owner: &Pubkey, - ) -> std::result::Result<(), BanksClientError> { - let (mining, _bump) = Pubkey::find_program_address( - &[ - "mining".as_bytes(), - &owner.key().to_bytes(), - &reward_pool.key().to_bytes(), - ], - &self.program_id, - ); - - let accounts = vec![ - AccountMeta::new(*reward_pool, false), - AccountMeta::new(mining, false), - AccountMeta::new_readonly(deposit_authority.pubkey(), true), - ]; - - let ix = Instruction::new_with_borsh( - self.program_id, - &RewardsInstruction::DepositMining { - amount, - lockup_period, - owner: *owner, - }, - accounts, - ); - - let signers = vec![deposit_authority]; - - self.solana - .process_transaction(&[ix], Some(&signers)) - .await?; - - Ok(()) - } } #[derive(Clone, Debug, PartialEq, AnchorDeserialize, AnchorSerialize, Default)]