-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Maths] Use new maths functions in utilities (#305)
- Loading branch information
1 parent
e63c4e3
commit a6c9e5c
Showing
17 changed files
with
146 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,21 @@ | ||
use crate::error::UxdError; | ||
use crate::utils::checked_add; | ||
use crate::utils::checked_as_u128; | ||
use crate::utils::checked_sub; | ||
use anchor_lang::prelude::*; | ||
|
||
pub fn checked_add_u128_and_i128(value_before: u128, change_delta: i128) -> Result<u128> { | ||
// In case of a simple positive change (increase), add the two positive values | ||
if change_delta >= 0 { | ||
let increase: u128 = u128::try_from(change_delta) | ||
.ok() | ||
.ok_or(UxdError::MathOverflow)?; | ||
return Ok(value_before | ||
.checked_add(increase) | ||
.ok_or(UxdError::MathOverflow)?); | ||
let increase: u128 = checked_as_u128(change_delta)?; | ||
return checked_add(value_before, increase); | ||
} | ||
// In case of a negative change, substract the absolute value of the delta (decrease) | ||
let decrease: u128 = if change_delta == i128::MIN { | ||
// special case: i128::MIN does not have an i128 absolute value | ||
u128::try_from(i128::MAX) | ||
.ok() | ||
.ok_or(UxdError::MathOverflow)? | ||
.checked_add(1) | ||
.ok_or(UxdError::MathOverflow)? | ||
checked_add(checked_as_u128(i128::MAX)?, 1)? | ||
} else { | ||
u128::try_from(change_delta.checked_abs().ok_or(UxdError::MathOverflow)?) | ||
.ok() | ||
.ok_or(UxdError::MathOverflow)? | ||
checked_as_u128(change_delta.checked_abs().ok_or(UxdError::MathOverflow)?)? | ||
}; | ||
Ok(value_before | ||
.checked_sub(decrease) | ||
.ok_or(UxdError::MathOverflow)?) | ||
checked_sub(value_before, decrease) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.