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

Withdraw available amount from stake account #35160

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
31 changes: 28 additions & 3 deletions cli/src/spend_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use {
crate::{
checks::{check_account_for_balance_with_commitment, get_fee_for_messages},
cli::CliError,
stake,
},
clap::ArgMatches,
solana_clap_utils::{input_parsers::lamports_of_sol, offline::SIGN_ONLY_ARG},
Expand Down Expand Up @@ -96,15 +97,39 @@ where
)?;
Ok((message, spend))
} else {
let from_balance = rpc_client
.get_balance_with_commitment(from_pubkey, commitment)?
.value;
let from_rent_exempt_minimum = if amount == SpendAmount::RentExempt {
let data = rpc_client.get_account_data(from_pubkey)?;
rpc_client.get_minimum_balance_for_rent_exemption(data.len())?
} else {
0
};

let mut from_balance: u64 = 0;
if let Some(account) = rpc_client
.get_account_with_commitment(from_pubkey, commitment)?
.value
{
if account.owner == solana_sdk::stake::program::id() {
let state = stake::get_account_stake_state(
rpc_client,
from_pubkey,
account,
true,
None,
false,
)?;
from_balance = state.account_balance;
if let Some(active_stake) = state.active_stake {
from_balance = from_balance.saturating_sub(active_stake);
}
}
}
if from_balance == 0 {
from_balance = rpc_client
.get_balance_with_commitment(from_pubkey, commitment)?
.value;
}

let (message, SpendAndFee { spend, fee }) = resolve_spend_message(
rpc_client,
amount,
Expand Down
21 changes: 20 additions & 1 deletion cli/src/stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2529,6 +2529,25 @@ pub fn process_show_stake_account(
use_csv: bool,
) -> ProcessResult {
let stake_account = rpc_client.get_account(stake_account_address)?;
let state = get_account_stake_state(
rpc_client,
stake_account_address,
stake_account,
use_lamports_unit,
with_rewards,
use_csv,
)?;
return Ok(config.output_format.formatted_string(&state));
}

pub fn get_account_stake_state(
rpc_client: &RpcClient,
stake_account_address: &Pubkey,
stake_account: solana_sdk::account::Account,
use_lamports_unit: bool,
with_rewards: Option<usize>,
use_csv: bool,
) -> Result<CliStakeState, CliError> {
if stake_account.owner != stake::program::id() {
return Err(CliError::RpcRequestError(format!(
"{stake_account_address:?} is not a stake account",
Expand Down Expand Up @@ -2572,7 +2591,7 @@ pub fn process_show_stake_account(
});
state.epoch_rewards = epoch_rewards;
}
Ok(config.output_format.formatted_string(&state))
Ok(state)
}
Err(err) => Err(CliError::RpcRequestError(format!(
"Account data could not be deserialized to stake state: {err}"
Expand Down
Loading