Skip to content

Commit

Permalink
bot: Respect the inactive balance when destaking
Browse files Browse the repository at this point in the history
  • Loading branch information
joncinque committed Jun 20, 2023
1 parent 6eb7358 commit db7a470
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
20 changes: 20 additions & 0 deletions bot/src/rpc_client_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,26 @@ pub fn send_and_confirm_transactions_with_spinner(
Err("Max retries exceeded".into())
}

pub(crate) fn get_stake_with_fallback(
rpc_client: &RpcClient,
stake_address: &Pubkey,
) -> Result<(u64, u64), Box<dyn error::Error>> {
let stake_activation = rpc_client.get_stake_activation(*stake_address, None);
if let Ok(stake_activation) = stake_activation {
Ok((stake_activation.active, stake_activation.inactive))
} else {
Ok((
0,
rpc_client.get_balance(stake_address).map_err(|err| {
format!(
"Unable to get stake account balance: {}: {}",
stake_address, err
)
})?,
))
}
}

pub struct VoteAccountInfo {
pub identity: Pubkey,
pub vote_address: Pubkey,
Expand Down
30 changes: 13 additions & 17 deletions bot/src/stake_pool_v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use {
crate::{
generic_stake_pool::*,
rpc_client_utils::{
get_all_stake_by_staker, send_and_confirm_transactions_with_spinner, MultiClient,
get_all_stake_by_staker, get_stake_with_fallback,
send_and_confirm_transactions_with_spinner, MultiClient,
},
},
log::*,
Expand Down Expand Up @@ -654,19 +655,9 @@ where
validator_stake.vote_address,
);

let balance = client.get_balance(&stake_address).map_err(|err| {
format!(
"Unable to get stake account balance: {}: {}",
stake_address, err
)
})? + client
.get_balance(&transient_stake_address)
.map_err(|err| {
format!(
"Unable to get transient stake account balance: {}: {}",
transient_stake_address, err
)
})?;
let (active_balance, inactive_balance) = get_stake_with_fallback(client, &stake_address)?;
let (transient_active_balance, transient_inactive_balance) =
get_stake_with_fallback(client, &transient_stake_address)?;

let list = if validator_stake.priority {
&mut priority_stake
Expand All @@ -678,7 +669,8 @@ where
}
};
list.push((
balance,
active_balance + transient_active_balance,
inactive_balance + transient_inactive_balance,
stake_address,
transient_stake_address,
validator_stake,
Expand All @@ -696,7 +688,8 @@ where

let mut transactions = vec![];
for (
balance,
active_balance,
inactive_balance,
stake_address,
transient_stake_address,
ValidatorStake {
Expand All @@ -711,8 +704,11 @@ where
.chain(baseline_stake)
.chain(bonus_stake)
{
let balance = active_balance + inactive_balance;
let desired_balance = match stake_state {
ValidatorStakeState::None => MIN_STAKE_ACCOUNT_BALANCE,
// there might be some additional inactive balance due to merges,
// so be sure to leave `MIN_STAKE_DELEGATION` + inactive balance
ValidatorStakeState::None => MIN_STAKE_DELEGATION + inactive_balance,
ValidatorStakeState::Baseline => baseline_stake_amount,
ValidatorStakeState::Bonus => bonus_stake_amount,
};
Expand Down

0 comments on commit db7a470

Please sign in to comment.