From bb54cf0dc6271fce8ea28ad4fa93179be5908ac0 Mon Sep 17 00:00:00 2001 From: Yuriy Savchenko Date: Wed, 21 Oct 2020 15:21:54 +0300 Subject: [PATCH] Added stake pool instruction interface methods (#684) * Added address type in programm address generation for the stake pool, renamed nonce to bump seed * Formatting fixed * Bump seed calculation moved to the smart contract, test for fee > 1 added, state length public constant added * Added claim method to stake pool, fixed program address generation in token mint and burn calls * Refactored signers management when calling other contracts * Signers formation put back into calling functions, deposit/withdraw/claim method reworked, state serialization bug fixed * Added instruction interface methods * Fixed signer flags for accounts in stake pool instruction creation --- stake-pool/program/src/instruction.rs | 141 ++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) diff --git a/stake-pool/program/src/instruction.rs b/stake-pool/program/src/instruction.rs index 296f32e5676bd7..f9d0e0eb469778 100644 --- a/stake-pool/program/src/instruction.rs +++ b/stake-pool/program/src/instruction.rs @@ -190,3 +190,144 @@ pub fn initialize( data, }) } + +/// Creates a 'deposit' instruction. +pub fn deposit( + program_id: &Pubkey, + stake_pool: &Pubkey, + stake_pool_deposit: &Pubkey, + stake_pool_withdraw: &Pubkey, + stake_to_join: &Pubkey, + pool_tokens_to: &Pubkey, + pool_fee_to: &Pubkey, + pool_mint: &Pubkey, + token_program_id: &Pubkey, +) -> Result { + let args = StakePoolInstruction::Deposit; + let data = args.serialize()?; + let accounts = vec![ + AccountMeta::new(*stake_pool, false), + AccountMeta::new(*stake_pool_deposit, false), + AccountMeta::new(*stake_pool_withdraw, false), + AccountMeta::new(*stake_to_join, false), + AccountMeta::new(*pool_tokens_to, false), + AccountMeta::new(*pool_fee_to, false), + AccountMeta::new(*pool_mint, false), + AccountMeta::new(*token_program_id, false), + ]; + Ok(Instruction { + program_id: *program_id, + accounts, + data, + }) +} + +/// Creates a 'withdraw' instruction. +pub fn withdraw( + program_id: &Pubkey, + stake_pool: &Pubkey, + stake_pool_withdraw: &Pubkey, + stake_to_split: &Pubkey, + stake_to_receive: &Pubkey, + user_withdrawer: &Pubkey, + burn_from: &Pubkey, + pool_mint: &Pubkey, + token_program_id: &Pubkey, + amount: u64, +) -> Result { + let args = StakePoolInstruction::Withdraw(amount); + let data = args.serialize()?; + let accounts = vec![ + AccountMeta::new(*stake_pool, false), + AccountMeta::new(*stake_pool_withdraw, false), + AccountMeta::new(*stake_to_split, false), + AccountMeta::new(*stake_to_receive, false), + AccountMeta::new(*user_withdrawer, false), + AccountMeta::new(*burn_from, true), + AccountMeta::new(*pool_mint, false), + AccountMeta::new(*token_program_id, false), + ]; + Ok(Instruction { + program_id: *program_id, + accounts, + data, + }) +} + +/// Creates a 'claim' instruction. +pub fn claim( + program_id: &Pubkey, + stake_pool: &Pubkey, + stake_pool_withdraw: &Pubkey, + stake_to_claim: &Pubkey, + user_withdrawer: &Pubkey, + burn_from: &Pubkey, + pool_mint: &Pubkey, + token_program_id: &Pubkey, + amount: u64, +) -> Result { + let args = StakePoolInstruction::Withdraw(amount); + let data = args.serialize()?; + let accounts = vec![ + AccountMeta::new(*stake_pool, false), + AccountMeta::new(*stake_pool_withdraw, false), + AccountMeta::new(*stake_to_claim, false), + AccountMeta::new(*user_withdrawer, false), + AccountMeta::new(*burn_from, true), + AccountMeta::new(*pool_mint, false), + AccountMeta::new(*token_program_id, false), + ]; + Ok(Instruction { + program_id: *program_id, + accounts, + data, + }) +} + +/// Creates a 'set staking authority' instruction. +pub fn set_staking_authority( + program_id: &Pubkey, + stake_pool: &Pubkey, + stake_pool_owner: &Pubkey, + stake_pool_withdraw: &Pubkey, + stake_account_to_update: &Pubkey, + stake_account_new_authority: &Pubkey, +) -> Result { + let args = StakePoolInstruction::SetStakingAuthority; + let data = args.serialize()?; + let accounts = vec![ + AccountMeta::new(*stake_pool, false), + AccountMeta::new(*stake_pool_owner, true), + AccountMeta::new(*stake_pool_withdraw, false), + AccountMeta::new(*stake_account_to_update, false), + AccountMeta::new(*stake_account_new_authority, false), + ]; + Ok(Instruction { + program_id: *program_id, + accounts, + data, + }) +} + +/// Creates a 'set owner' instruction. +pub fn set_owner( + program_id: &Pubkey, + stake_pool: &Pubkey, + stake_pool_owner: &Pubkey, + stake_pool_new_owner: &Pubkey, + stake_pool_new_fee_receiver: &Pubkey, +) -> Result { + let args = StakePoolInstruction::SetStakingAuthority; + let data = args.serialize()?; + let accounts = vec![ + AccountMeta::new(*stake_pool, false), + AccountMeta::new(*stake_pool_owner, true), + AccountMeta::new(*stake_pool_new_owner, false), + AccountMeta::new(*stake_pool_new_fee_receiver, false), + ]; + Ok(Instruction { + program_id: *program_id, + accounts, + data, + }) +}