Skip to content

Commit

Permalink
replace delegate_mining validation to rewards
Browse files Browse the repository at this point in the history
  • Loading branch information
rwwwx committed Aug 20, 2024
1 parent 61855c5 commit f4aafdb
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 21 deletions.
14 changes: 14 additions & 0 deletions programs/voter-stake-registry/src/cpi_instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ pub enum RewardsInstruction {
lockup_period: LockupPeriod,
/// Specifies the owner of the Mining Account
owner: Pubkey,
/// Wallet addres of delegate
delegate_wallet_addr: Pubkey,

},

/// Withdraws amount of supply to the mining account
Expand All @@ -89,6 +92,8 @@ pub enum RewardsInstruction {
amount: u64,
/// Specifies the owner of the Mining Account
owner: Pubkey,
/// Wallet addres of delegate
delegate_wallet_addr: Pubkey,
},

/// Claims amount of rewards
Expand Down Expand Up @@ -129,6 +134,8 @@ pub enum RewardsInstruction {
additional_amount: u64,
/// The wallet who owns the mining account
mining_owner: Pubkey,
/// Wallet addres of delegate
delegate_wallet_addr: Pubkey,
},

/// Distributes tokens among mining owners
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_wallet_addr: *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_wallet_addr: *delegate_wallet_addr,
},
accounts,
);
Expand Down Expand Up @@ -348,7 +359,9 @@ pub fn withdraw_mining<'a>(
amount: u64,
owner: &Pubkey,
signers_seeds: &[&[u8]],
delegate_wallet_addr: &Pubkey
) -> ProgramResult {
// TODO: Maybe it necessary to put `delegate_wallet_addr` into metadata
let accounts = vec![
AccountMeta::new(reward_pool.key(), false),
AccountMeta::new(mining.key(), false),
Expand All @@ -361,6 +374,7 @@ pub fn withdraw_mining<'a>(
&RewardsInstruction::WithdrawMining {
amount,
owner: *owner,
delegate_wallet_addr: *delegate_wallet_addr,
},
accounts,
);
Expand Down
10 changes: 0 additions & 10 deletions programs/voter-stake-registry/src/instructions/change_delegate.rs
Original file line number Diff line number Diff line change
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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions programs/voter-stake-registry/src/instructions/stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -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
11 changes: 0 additions & 11 deletions programs/voter-stake-registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
}
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
#[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_wallet_addr: Default::default(),
},
accounts,
);
Expand Down

0 comments on commit f4aafdb

Please sign in to comment.