Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace delegate_mining validation to rewards #26

Merged
merged 8 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion programs/voter-stake-registry/src/cpi_instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -155,6 +161,7 @@ pub enum RewardsInstruction {
ChangeDelegate {
/// Amount of staked tokens
staked_amount: u64,
new_delegate: Pubkey,
},
}

Expand Down Expand Up @@ -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),
Expand All @@ -269,6 +277,7 @@ pub fn deposit_mining<'a>(
amount,
lockup_period,
owner: *owner,
delegate: *delegate_wallet_addr,
},
accounts,
);
Expand Down Expand Up @@ -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),
Expand All @@ -318,6 +328,7 @@ pub fn extend_stake<'a>(
base_amount,
additional_amount,
mining_owner: *mining_owner,
delegate: *delegate_wallet_addr,
},
accounts,
);
Expand Down Expand Up @@ -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),
Expand All @@ -361,6 +373,7 @@ pub fn withdraw_mining<'a>(
&RewardsInstruction::WithdrawMining {
amount,
owner: *owner,
delegate: *delegate_wallet_addr,
},
accounts,
);
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
);

Expand Down
14 changes: 3 additions & 11 deletions programs/voter-stake-registry/src/instructions/change_delegate.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -105,16 +105,6 @@ pub fn change_delegate(ctx: Context<ChangeDelegate>, 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;
}
Expand All @@ -132,6 +122,7 @@ pub fn change_delegate(ctx: Context<ChangeDelegate>, 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(),
Expand All @@ -141,6 +132,7 @@ pub fn change_delegate(ctx: Context<ChangeDelegate>, deposit_entry_index: u8) ->
mining_owner,
old_delegate_mining,
new_delegate_mining,
new_delegate,
staked_amount,
signers_seeds,
)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(),
Expand All @@ -109,6 +110,7 @@ pub fn extend_stake(
additional_amount,
mining_owner,
signers_seeds,
delegate_wallet_addr,
)?;

Ok(())
Expand Down
18 changes: 1 addition & 17 deletions programs/voter-stake-registry/src/instructions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand All @@ -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,
)
}
4 changes: 3 additions & 1 deletion programs/voter-stake-registry/src/instructions/stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -72,6 +72,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(),
Expand All @@ -83,6 +84,7 @@ pub fn stake(
target.lockup.period,
owner,
signers_seeds,
delegate_wallet_addr,
)?;

Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn unlock_tokens(ctx: Context<Stake>, 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
Expand All @@ -44,6 +44,7 @@ pub fn unlock_tokens(ctx: Context<Stake>, 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,
Expand All @@ -54,6 +55,7 @@ pub fn unlock_tokens(ctx: Context<Stake>, deposit_entry_index: u8) -> Result<()>
deposit_entry.amount_deposited_native,
owner.key,
signers_seeds,
delegate_wallet_addr,
)?;

Ok(())
Expand Down
13 changes: 1 addition & 12 deletions programs/voter-stake-registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,25 +226,14 @@ 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,
*self.delegate.to_account_info().key,
MplStakingError::InvalidDelegate
);

rwwwx marked this conversation as resolved.
Show resolved Hide resolved
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(())
}
}
Binary file modified programs/voter-stake-registry/tests/fixtures/mplx_rewards.so
Binary file not shown.
3 changes: 3 additions & 0 deletions programs/voter-stake-registry/tests/program_test/rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ impl RewardsCookie {
Ok(mining)
}

// TODO: Maybe we need to delete this function. It is not used anywhere
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's unused, then kill it.

#[allow(clippy::too_many_arguments)]
pub async fn deposit_mining<'a>(
&self,
Expand Down Expand Up @@ -159,6 +160,8 @@ impl RewardsCookie {
amount,
lockup_period,
owner: *owner,
// TODO: ?????
delegate: Default::default(),
},
accounts,
);
Expand Down
Loading