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

v1.18: Cli stake-split: adjust transfer amount if recipient has lamports (backport of #266) #369

Merged
merged 1 commit into from
Mar 22, 2024
Merged
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
2 changes: 1 addition & 1 deletion cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2249,7 +2249,7 @@ mod tests {
memo: None,
split_stake_account: 1,
seed: None,
lamports: 30,
lamports: 200_000_000,
fee_payer: 0,
compute_unit_price: None,
rent_exempt_reserve: None,
Expand Down
79 changes: 46 additions & 33 deletions cli/src/stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ use {
},
solana_rpc_client_nonce_utils::blockhash_query::BlockhashQuery,
solana_sdk::{
account::from_account,
account::{from_account, Account},
account_utils::StateMut,
clock::{Clock, UnixTimestamp, SECONDS_PER_DAY},
commitment_config::CommitmentConfig,
epoch_schedule::EpochSchedule,
feature_set,
message::Message,
native_token::Sol,
pubkey::Pubkey,
stake::{
self,
Expand Down Expand Up @@ -1980,40 +1981,49 @@ pub fn process_split_stake(
};

let rent_exempt_reserve = if !sign_only {
if let Ok(stake_account) = rpc_client.get_account(&split_stake_account_address) {
if stake_account.owner == stake::program::id() {
return Err(CliError::BadParameter(format!(
let stake_minimum_delegation = rpc_client.get_stake_minimum_delegation()?;
if lamports < stake_minimum_delegation {
let lamports = Sol(lamports);
let stake_minimum_delegation = Sol(stake_minimum_delegation);
return Err(CliError::BadParameter(format!(
"need at least {stake_minimum_delegation} for minimum stake delegation, \
provided: {lamports}"
))
.into());
}

let check_stake_account = |account: Account| -> Result<u64, CliError> {
match account.owner {
owner if owner == stake::program::id() => Err(CliError::BadParameter(format!(
"Stake account {split_stake_account_address} already exists"
))
.into());
} else if stake_account.owner == system_program::id() {
if !stake_account.data.is_empty() {
return Err(CliError::BadParameter(format!(
"Account {split_stake_account_address} has data and cannot be used to split stake"
))
.into());
))),
owner if owner == system_program::id() => {
if !account.data.is_empty() {
Err(CliError::BadParameter(format!(
"Account {split_stake_account_address} has data and cannot be used to split stake"
)))
} else {
// if `stake_account`'s owner is the system_program and its data is
// empty, `stake_account` is allowed to receive the stake split
Ok(account.lamports)
}
}
// if `stake_account`'s owner is the system_program and its data is
// empty, `stake_account` is allowed to receive the stake split
} else {
return Err(CliError::BadParameter(format!(
_ => Err(CliError::BadParameter(format!(
"Account {split_stake_account_address} already exists and cannot be used to split stake"
))
.into());
)))
}
}
};
let current_balance =
if let Ok(stake_account) = rpc_client.get_account(&split_stake_account_address) {
check_stake_account(stake_account)?
} else {
0
};

let minimum_balance =
let rent_exempt_reserve =
rpc_client.get_minimum_balance_for_rent_exemption(StakeStateV2::size_of())?;

if lamports < minimum_balance {
return Err(CliError::BadParameter(format!(
"need at least {minimum_balance} lamports for stake account to be rent exempt, \
provided lamports: {lamports}"
))
.into());
}
minimum_balance
rent_exempt_reserve.saturating_sub(current_balance)
} else {
rent_exempt_reserve
.cloned()
Expand All @@ -2022,11 +2032,14 @@ pub fn process_split_stake(

let recent_blockhash = blockhash_query.get_blockhash(rpc_client, config.commitment)?;

let mut ixs = vec![system_instruction::transfer(
&fee_payer.pubkey(),
&split_stake_account_address,
rent_exempt_reserve,
)];
let mut ixs = vec![];
if rent_exempt_reserve > 0 {
ixs.push(system_instruction::transfer(
&fee_payer.pubkey(),
&split_stake_account_address,
rent_exempt_reserve,
));
}
if let Some(seed) = split_stake_account_seed {
ixs.append(
&mut stake_instruction::split_with_seed(
Expand Down
Loading