This repository has been archived by the owner on Oct 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ceil ratio
- Loading branch information
Showing
8 changed files
with
114 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -2,5 +2,6 @@ pub mod adapters; | |
pub mod coins; | ||
pub mod error; | ||
pub mod extensions; | ||
pub mod math; | ||
pub mod msg; | ||
pub mod traits; |
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use cosmwasm_std::{CheckedMultiplyRatioError, Uint128, Uint256}; | ||
|
||
pub trait CeilRatio { | ||
fn multiply_ratio_ceil( | ||
&self, | ||
numerator: Uint128, | ||
denominator: Uint128, | ||
) -> Result<Uint128, CheckedMultiplyRatioError>; | ||
} | ||
|
||
impl CeilRatio for Uint128 { | ||
/// Using `checked_multiply_ratio()` results in a rounding down due to the nature of integer math. | ||
/// This function performs the same math, but rounds up. The is particularly useful in ensuring | ||
/// safety in certain situations (e.g. calculating what an account owes) | ||
fn multiply_ratio_ceil( | ||
&self, | ||
numerator: Uint128, | ||
denominator: Uint128, | ||
) -> Result<Uint128, CheckedMultiplyRatioError> { | ||
// Perform the normal multiply ratio. | ||
// Converts to Uint256 to reduce likeliness of overflow errors | ||
let new_numerator = self.full_mul(numerator); | ||
let denom_256 = Uint256::from(denominator); | ||
let mut result = new_numerator | ||
.checked_div(denom_256) | ||
.map_err(|_| CheckedMultiplyRatioError::DivideByZero)?; | ||
|
||
// Check if there's a remainder with that same division. | ||
// If so, round up (by adding one). | ||
if !new_numerator | ||
.checked_rem(denom_256) | ||
.map_err(|_| CheckedMultiplyRatioError::DivideByZero)? | ||
.is_zero() | ||
{ | ||
result += Uint256::one(); | ||
} | ||
|
||
match result.try_into() { | ||
Ok(ratio) => Ok(ratio), | ||
Err(_) => Err(CheckedMultiplyRatioError::Overflow), | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod ceil_ratio; | ||
|
||
pub use ceil_ratio::*; |
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use cosmwasm_std::{CheckedMultiplyRatioError, Uint128}; | ||
use mars_rover::math::CeilRatio; | ||
|
||
const MAX_UINT128_SIZE: u128 = 340_282_366_920_938_463_463_374_607_431_768_211_455; | ||
|
||
#[test] | ||
fn test_divide_by_zero() { | ||
let err = Uint128::new(123) | ||
.multiply_ratio_ceil(Uint128::new(12), Uint128::zero()) | ||
.unwrap_err(); | ||
assert_eq!(err, CheckedMultiplyRatioError::DivideByZero) | ||
} | ||
|
||
#[test] | ||
fn test_result_exceeds_128_bit_capacity() { | ||
let err = Uint128::new(MAX_UINT128_SIZE) | ||
.multiply_ratio_ceil(Uint128::new(2), Uint128::new(1)) | ||
.unwrap_err(); | ||
assert_eq!(err, CheckedMultiplyRatioError::Overflow) | ||
} | ||
|
||
#[test] | ||
fn test_works_with_zero() { | ||
let res = Uint128::zero() | ||
.multiply_ratio_ceil(Uint128::new(1), Uint128::new(10)) | ||
.unwrap(); | ||
assert_eq!(res, Uint128::zero()) | ||
} | ||
|
||
#[test] | ||
fn test_works_with_one() { | ||
let res = Uint128::one() | ||
.multiply_ratio_ceil(Uint128::new(1), Uint128::new(10)) | ||
.unwrap(); | ||
assert_eq!(res, Uint128::one()) | ||
} | ||
|
||
#[test] | ||
fn test_not_increment_if_divides_cleanly() { | ||
// 56088 / 123 = 456 | ||
let res = Uint128::new(56088) | ||
.multiply_ratio_ceil(Uint128::new(1), Uint128::new(123)) | ||
.unwrap(); | ||
assert_eq!(res, Uint128::new(456)) | ||
} | ||
|
||
#[test] | ||
fn test_rounds_up() { | ||
// 56000 / 123 = 455.28455284 | ||
let res = Uint128::new(56000) | ||
.multiply_ratio_ceil(Uint128::new(1), Uint128::new(123)) | ||
.unwrap(); | ||
assert_eq!(res, Uint128::new(456)) | ||
} |