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 stake #441

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
30 changes: 23 additions & 7 deletions cli/src/spend_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,13 @@ where
let mut 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 from_rent_exempt_minimum =
if amount == SpendAmount::RentExempt || amount == SpendAmount::Available {
let data = rpc_client.get_account_data(from_pubkey)?;
rpc_client.get_minimum_balance_for_rent_exemption(data.len())?
} else {
0
};
if amount == SpendAmount::Available {
if let Some(account) = rpc_client
.get_account_with_commitment(from_pubkey, commitment)?
Expand Down Expand Up @@ -201,7 +202,7 @@ where
fee,
},
)),
SpendAmount::All | SpendAmount::Available => {
SpendAmount::All => {
let lamports = if from_pubkey == fee_pubkey {
from_balance.saturating_sub(fee)
} else {
Expand All @@ -215,6 +216,21 @@ where
},
))
}
SpendAmount::Available => {
let mut lamports = if from_pubkey == fee_pubkey {
from_balance.saturating_sub(fee)
} else {
from_balance
};
lamports = lamports.saturating_sub(from_rent_exempt_minimum);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, this isn't quite right. If there's no active stake, the rent-exempt reserve is available to be withdrawn. So the rent-exempt minimum only needs to be subtracted when active_stake is Some.
I think we need to do this on L133 above.

Copy link
Author

@najeal najeal Apr 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can either:

  • 1: subtract at line 133 and delete the sub at line 225. (The case SpendAmount::Available will finally do the same logic as SpendAmount::All in resolve_spend_message function).
  • 2: Or just reset from_rent_exempt_minimum to 0 when active_stake is None (L133) and keep the rest of the code as it is.

What do you prefer?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer option 1

Ok((
build_message(lamports),
SpendAndFee {
spend: lamports,
fee,
},
))
}
SpendAmount::RentExempt => {
let mut lamports = if from_pubkey == fee_pubkey {
from_balance.saturating_sub(fee)
Expand Down