pub fn safe_add(balance: &mut Uint128, amount: Uint128) -> Uint128
Expand description

Increases balance by amount and returns the actual amount added. The return value will always be equal to amount unless it overflowed.

Examples

let mut balance = Uint128::MAX - Uint128::new(2);
let actual_added = safe_add(&mut balance, Uint128::one());
 
assert_eq!(actual_added, Uint128::one());
assert_eq!(balance, Uint128::MAX - Uint128::one());
 
// Here the add operation would overflow so we saturated to the maximum value.
let actual_added = safe_add(&mut balance, Uint128::new(2));
 
assert_eq!(actual_added, Uint128::one());
assert_eq!(balance, Uint128::MAX);