Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit d536460

Browse files
authored
stake-pool: Fix stake split and withdraw token calculation (#1352)
* Fix stake split and withdraw token calculation * Cargo fmt
1 parent b389053 commit d536460

File tree

2 files changed

+15
-39
lines changed

2 files changed

+15
-39
lines changed

stake-pool/cli/src/main.rs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,9 @@ fn command_vsa_remove(
395395

396396
// Calculate amount of tokens to burn
397397
let stake_account = config.rpc_client.get_account(&stake)?;
398-
let tokens_to_burn = stake_amount_to_pool_tokens(&pool_data, stake_account.lamports);
398+
let tokens_to_burn = pool_data
399+
.calc_pool_withdraw_amount(stake_account.lamports)
400+
.unwrap();
399401

400402
// Check balance and mint
401403
let account_data = config.rpc_client.get_account_data(&burn_from)?;
@@ -736,22 +738,6 @@ fn command_update(config: &Config, pool: &Pubkey) -> CommandResult {
736738
}
737739
}
738740

739-
fn stake_amount_to_pool_tokens(pool_data: &StakePool, amount: u64) -> u64 {
740-
(amount as u128)
741-
.checked_mul(pool_data.pool_total as u128)
742-
.unwrap()
743-
.checked_div(pool_data.stake_total as u128)
744-
.unwrap() as u64
745-
}
746-
747-
fn pool_tokens_to_stake_amount(pool_data: &StakePool, tokens: u64) -> u64 {
748-
(tokens as u128)
749-
.checked_mul(pool_data.stake_total as u128)
750-
.unwrap()
751-
.checked_div(pool_data.pool_total as u128)
752-
.unwrap() as u64
753-
}
754-
755741
#[derive(PartialEq, Debug)]
756742
struct WithdrawAccount {
757743
pubkey: Pubkey,
@@ -859,7 +845,7 @@ fn command_withdraw(
859845
}
860846

861847
// Convert pool tokens amount to lamports
862-
let sol_withdraw_amount = pool_tokens_to_stake_amount(&pool_data, amount);
848+
let sol_withdraw_amount = pool_data.calc_lamports_amount(amount).unwrap();
863849

864850
// Get the list of accounts to withdraw from
865851
let withdraw_from: Vec<WithdrawAccount> =
@@ -870,8 +856,6 @@ fn command_withdraw(
870856
let mut signers = vec![config.fee_payer.as_ref(), config.owner.as_ref()];
871857
let stake_receiver_account = Keypair::new(); // Will be added to signers if creating new account
872858

873-
let mut total_rent_free_balances: u64 = 0;
874-
875859
instructions.push(
876860
// Approve spending token
877861
approve_token(
@@ -887,12 +871,19 @@ fn command_withdraw(
887871
// Use separate mutable variable because withdraw might create a new account
888872
let mut stake_receiver: Option<Pubkey> = *stake_receiver_param;
889873

874+
let mut total_rent_free_balances = 0;
875+
890876
// Go through prepared accounts and withdraw/claim them
891877
for withdraw_stake in withdraw_from {
878+
let withdraw_amount = pool_data
879+
.calc_pool_withdraw_amount(withdraw_stake.amount)
880+
.unwrap()
881+
+ 1;
892882
println!(
893-
"Withdrawing from account {}, amount {} SOL",
883+
"Withdrawing from account {}, amount {} SOL, {} pool tokens",
894884
withdraw_stake.pubkey,
895-
lamports_to_sol(withdraw_stake.amount)
885+
lamports_to_sol(withdraw_stake.amount),
886+
lamports_to_sol(withdraw_amount),
896887
);
897888

898889
if stake_receiver.is_none() {
@@ -936,7 +927,7 @@ fn command_withdraw(
936927
&pool_data.pool_mint,
937928
&spl_token::id(),
938929
&stake_program_id(),
939-
withdraw_stake.amount,
930+
withdraw_amount,
940931
)?);
941932
}
942933

stake-pool/program/src/processor.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ impl Processor {
131131
}
132132

133133
/// Issue a stake_split instruction.
134-
#[allow(clippy::too_many_arguments)]
135134
pub fn stake_split<'a>(
136135
stake_pool: &Pubkey,
137136
stake_account: AccountInfo<'a>,
@@ -140,26 +139,14 @@ impl Processor {
140139
bump_seed: u8,
141140
amount: u64,
142141
split_stake: AccountInfo<'a>,
143-
reserved: AccountInfo<'a>,
144-
stake_program_info: AccountInfo<'a>,
145142
) -> Result<(), ProgramError> {
146143
let me_bytes = stake_pool.to_bytes();
147144
let authority_signature_seeds = [&me_bytes[..32], authority_type, &[bump_seed]];
148145
let signers = &[&authority_signature_seeds[..]];
149146

150147
let ix = stake::split_only(stake_account.key, authority.key, amount, split_stake.key);
151148

152-
invoke_signed(
153-
&ix,
154-
&[
155-
stake_account,
156-
reserved,
157-
authority,
158-
split_stake,
159-
stake_program_info,
160-
],
161-
signers,
162-
)
149+
invoke_signed(&ix, &[stake_account, split_stake, authority], signers)
163150
}
164151

165152
/// Issue a stake_merge instruction.
@@ -1114,8 +1101,6 @@ impl Processor {
11141101
stake_pool.withdraw_bump_seed,
11151102
stake_amount,
11161103
stake_split_to.clone(),
1117-
clock_info.clone(),
1118-
stake_program_info.clone(),
11191104
)?;
11201105

11211106
Self::stake_authorize(

0 commit comments

Comments
 (0)