pub fn unbond(&mut self, validator: &PublicKey, maybe_amount: Option<U512>) -> Result<U512> {
        let min = self
            .max_without(validator)
            .unwrap_or_else(U512::zero)
            .saturating_sub(MAX_SPREAD);
        let max_decrease = MAX_DECREASE.min(self.sum() * MAX_REL_DECREASE / 1_000_000);

        if let Some(amount) = maybe_amount {
            // The minimum stake value to not violate the maximum spread.
            let stake = self.0.get_mut(validator).ok_or(Error::NotBonded)?;
            if *stake > amount {
                if *stake - amount < min {
                    return Err(Error::SpreadTooHigh);
                }
                if amount > max_decrease {
                    return Err(Error::UnbondTooLarge);
                }
                *stake -= amount;
                return Ok(amount);
            }
        }
        if self.0.len() == 1 {
            return Err(Error::CannotUnbondLastValidator);
        }

        // If the the amount is greater or equal to the stake, remove the validator.
        let stake = self.0.remove(validator).ok_or(Error::NotBonded)?;

        if let Some(amount) = maybe_amount {
            if amount > stake {
                return Err(Error::UnbondTooLarge);
            }
        }

        if stake > min.saturating_add(max_decrease) && stake > max_decrease {
            return Err(Error::UnbondTooLarge);
        }
        Ok(stake)
    }
/// If `maybe_amount` is `None`, removes all the validator's stakes,
/// otherwise subtracts the given amount. If the stakes are lower than
/// the specified amount, it also subtracts all the stakes.
///
/// Returns the amount that was actually subtracted from the stakes, or an
/// error if
/// * unbonding the specified amount is not allowed,
/// * tries to unbond last validator,
/// * validator was not bonded.
If `maybe_amount` is `None`, removes all the validator's stakes,
otherwise subtracts the given amount. If the stakes are lower than
the specified amount, it also subtracts all the stakes.

Returns the amount that was actually subtracted from the stakes, or an
error if
unbonding the specified amount is not allowed,
tries to unbond last validator,
validator was not bonded.
If ` maybe_amount ` is ` None ` removes all the validator ' s stakes otherwise subtracts the given amount . If the stakes are lower than the specified amount it also subtracts all the stakes . Returns the amount that was actually subtracted from the stakes or an error if unbonding the specified amount is not allowed tries to unbond last validator validator was not bonded .
pub fn unbond(&mut self, validator: &PublicKey, maybe_amount: Option<U512>) -> Result<U512> {
        let min = self
            .max_without(validator)
            .unwrap_or_else(U512::zero)
            .saturating_sub(MAX_SPREAD);
        let max_decrease = MAX_DECREASE.min(self.sum() * MAX_REL_DECREASE / 1_000_000);
        if let Some(amount) = maybe_amount {
            // The minimum stake value to not violate the maximum spread.
            let stake = self.0.get_mut(validator).ok_or(Error::NotBonded)?;
            if *stake > amount {
                if *stake - amount < min {
                    return Err(Error::SpreadTooHigh);
                }
                if amount > max_decrease {
                    return Err(Error::UnbondTooLarge);
                }
                *stake -= amount;
                return Ok(amount);
            }
        }
        if self.0.len() == 1 {
            return Err(Error::CannotUnbondLastValidator);
        }
        // If the the amount is greater or equal to the stake, remove the validator.
        let stake = self.0.remove(validator).ok_or(Error::NotBonded)?;
        if let Some(amount) = maybe_amount {
            if amount > stake {
                return Err(Error::UnbondTooLarge);
            }
        }
        if stake > min.saturating_add(max_decrease) && stake > max_decrease {
            return Err(Error::UnbondTooLarge);
        }
        Ok(stake)
    }
pub fn unbond ( & mut self , validator : & PublicKey , maybe_amount : Option < U512 > ) -> Result < U512 > { let min = self . max_without ( validator ) . unwrap_or_else ( U512 :: zero ) . saturating_sub ( MAX_SPREAD ) ; let max_decrease = MAX_DECREASE . min ( self . sum ( ) * MAX_REL_DECREASE / 1_000_000 ) ; if let Some ( amount ) = maybe_amount { // The minimum stake value to not violate the maximum spread. let stake = self . 0 . get_mut ( validator ) . ok_or ( Error :: NotBonded ) ? ; if * stake > amount { if * stake - amount < min { return Err ( Error :: SpreadTooHigh ) ; } if amount > max_decrease { return Err ( Error :: UnbondTooLarge ) ; } * stake -= amount ; return Ok ( amount ) ; } } if self . 0 . len ( ) == 1 { return Err ( Error :: CannotUnbondLastValidator ) ; } // If the the amount is greater or equal to the stake, remove the validator. let stake = self . 0 . remove ( validator ) . ok_or ( Error :: NotBonded ) ? ; if let Some ( amount ) = maybe_amount { if amount > stake { return Err ( Error :: UnbondTooLarge ) ; } } if stake > min . saturating_add ( max_decrease ) && stake > max_decrease { return Err ( Error :: UnbondTooLarge ) ; } Ok ( stake ) }
If `maybe_amount` is `None`, removes all the validator's stakes,
otherwise subtracts the given amount.
If ` maybe_amount ` is ` None ` removes all the validator ' s stakes otherwise subtracts the given amount .
&mut self
validator
&PublicKey
maybe_amount
Option<U512>
&mut self
validator
&PublicKey
maybe_amount
Option<U512>