-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6c76333
commit 38af58e
Showing
5 changed files
with
163 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use crate::{ | ||
asserts::get_delegate_mining, | ||
state::{Mining, RewardPool}, | ||
utils::{assert_and_deserialize_pool_and_mining, AccountLoader}, | ||
}; | ||
use solana_program::{ | ||
account_info::AccountInfo, entrypoint::ProgramResult, program_error::ProgramError, | ||
program_pack::Pack, pubkey::Pubkey, | ||
}; | ||
|
||
/// Instruction context | ||
pub struct ChangeDelegateContext<'a, 'b> { | ||
reward_pool: &'a AccountInfo<'b>, | ||
mining: &'a AccountInfo<'b>, | ||
deposit_authority: &'a AccountInfo<'b>, | ||
mining_owner: &'a AccountInfo<'b>, | ||
old_delegate_mining: &'a AccountInfo<'b>, | ||
new_delegate_mining: &'a AccountInfo<'b>, | ||
} | ||
|
||
impl<'a, 'b> ChangeDelegateContext<'a, 'b> { | ||
/// New instruction context | ||
pub fn new( | ||
program_id: &Pubkey, | ||
accounts: &'a [AccountInfo<'b>], | ||
) -> Result<ChangeDelegateContext<'a, 'b>, ProgramError> { | ||
let account_info_iter = &mut accounts.iter().enumerate(); | ||
|
||
let reward_pool = AccountLoader::next_with_owner(account_info_iter, program_id)?; | ||
let mining = AccountLoader::next_with_owner(account_info_iter, program_id)?; | ||
let deposit_authority = AccountLoader::next_signer(account_info_iter)?; | ||
let mining_owner = AccountLoader::next_signer(account_info_iter)?; | ||
let old_delegate_mining = AccountLoader::next_with_owner(account_info_iter, program_id)?; | ||
let new_delegate_mining = AccountLoader::next_with_owner(account_info_iter, program_id)?; | ||
|
||
Ok(ChangeDelegateContext { | ||
reward_pool, | ||
mining, | ||
deposit_authority, | ||
mining_owner, | ||
old_delegate_mining, | ||
new_delegate_mining, | ||
}) | ||
} | ||
|
||
/// Process instruction | ||
#[allow(clippy::too_many_arguments)] | ||
pub fn process(&self, program_id: &Pubkey, staked_amount: u64) -> ProgramResult { | ||
let (mut reward_pool, mut mining) = assert_and_deserialize_pool_and_mining( | ||
program_id, | ||
self.mining_owner.key, | ||
self.reward_pool, | ||
self.mining, | ||
self.deposit_authority, | ||
)?; | ||
|
||
// if new_delegate_mining.is_none that means that new_delegate == self | ||
let mut new_delegate_mining = get_delegate_mining(self.new_delegate_mining, self.mining)?; | ||
// if old_delegate_mining.is_none that means that old_delegate == self | ||
let mut old_delegate_mining = get_delegate_mining(self.old_delegate_mining, self.mining)?; | ||
|
||
reward_pool.change_delegate( | ||
&mut mining, | ||
new_delegate_mining.as_mut(), | ||
old_delegate_mining.as_mut(), | ||
staked_amount, | ||
)?; | ||
|
||
RewardPool::pack(reward_pool, *self.reward_pool.data.borrow_mut())?; | ||
Mining::pack(mining, *self.mining.data.borrow_mut())?; | ||
|
||
if let Some(new_delegate_mining) = new_delegate_mining { | ||
Mining::pack( | ||
new_delegate_mining, | ||
*self.new_delegate_mining.data.borrow_mut(), | ||
)?; | ||
} | ||
|
||
if let Some(old_delegate_mining) = old_delegate_mining { | ||
Mining::pack( | ||
old_delegate_mining, | ||
*self.old_delegate_mining.data.borrow_mut(), | ||
)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters