Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to

- cosmwasm-std: Implement `checked_multiply_ratio` for
`Uint64`/`Uint128`/`Uint256`
- cosmwasm-std: Implement `checked_from_ratio` for `Decimal`/`Decimal256`

### Changed

Expand Down
4 changes: 2 additions & 2 deletions packages/std/src/errors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ mod verification_error;

pub use recover_pubkey_error::RecoverPubkeyError;
pub use std_error::{
CheckedMultiplyRatioError, ConversionOverflowError, DivideByZeroError, OverflowError,
OverflowOperation, StdError, StdResult,
CheckedFromRatioError, CheckedMultiplyRatioError, ConversionOverflowError, DivideByZeroError,
OverflowError, OverflowOperation, StdError, StdResult,
};
pub use system_error::SystemError;
pub use verification_error::VerificationError;
9 changes: 9 additions & 0 deletions packages/std/src/errors/std_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,15 @@ pub enum CheckedMultiplyRatioError {
Overflow,
}

#[derive(Error, Debug, PartialEq, Eq)]
pub enum CheckedFromRatioError {
#[error("Denominator must not be zero")]
DivideByZero,

#[error("Overflow")]
Overflow,
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
5 changes: 3 additions & 2 deletions packages/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ pub use crate::binary::Binary;
pub use crate::coins::{coin, coins, has_coins, Coin};
pub use crate::deps::{Deps, DepsMut, OwnedDeps};
pub use crate::errors::{
CheckedMultiplyRatioError, ConversionOverflowError, DivideByZeroError, OverflowError,
OverflowOperation, RecoverPubkeyError, StdError, StdResult, SystemError, VerificationError,
CheckedFromRatioError, CheckedMultiplyRatioError, ConversionOverflowError, DivideByZeroError,
OverflowError, OverflowOperation, RecoverPubkeyError, StdError, StdResult, SystemError,
VerificationError,
};
#[cfg(feature = "stargate")]
pub use crate::ibc::{
Expand Down
51 changes: 43 additions & 8 deletions packages/std/src/math/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
use std::str::FromStr;
use thiserror::Error;

use crate::errors::StdError;
use crate::errors::{CheckedFromRatioError, CheckedMultiplyRatioError, StdError};
use crate::OverflowError;

use super::Fraction;
Expand Down Expand Up @@ -118,16 +118,32 @@ impl Decimal {

/// Returns the ratio (numerator / denominator) as a Decimal
pub fn from_ratio(numerator: impl Into<Uint128>, denominator: impl Into<Uint128>) -> Self {
match Decimal::checked_from_ratio(numerator, denominator) {
Ok(value) => value,
Err(CheckedFromRatioError::DivideByZero) => {
panic!("Denominator must not be zero")
}
Err(CheckedFromRatioError::Overflow) => panic!("Multiplication overflow"),
}
}

/// Returns the ratio (numerator / denominator) as a Decimal
pub fn checked_from_ratio(
numerator: impl Into<Uint128>,
denominator: impl Into<Uint128>,
) -> Result<Self, CheckedFromRatioError> {
let numerator: Uint128 = numerator.into();
let denominator: Uint128 = denominator.into();
if denominator.is_zero() {
panic!("Denominator must not be zero");
match numerator.checked_multiply_ratio(Self::DECIMAL_FRACTIONAL, denominator) {
Ok(ratio) => {
// numerator * DECIMAL_FRACTIONAL / denominator
Ok(Decimal(ratio))
}
Err(CheckedMultiplyRatioError::Overflow) => Err(CheckedFromRatioError::Overflow),
Err(CheckedMultiplyRatioError::DivideByZero) => {
Err(CheckedFromRatioError::DivideByZero)
}
}

Decimal(
// numerator * DECIMAL_FRACTIONAL / denominator
numerator.multiply_ratio(Self::DECIMAL_FRACTIONAL, denominator),
)
}

pub const fn is_zero(&self) -> bool {
Expand Down Expand Up @@ -658,6 +674,25 @@ mod tests {
Decimal::from_ratio(1u128, 0u128);
}

#[test]
#[should_panic(expected = "Multiplication overflow")]
fn decimal_from_ratio_panics_for_mul_overflow() {
Decimal::from_ratio(u128::MAX, 1u128);
}

#[test]
fn decimal_checked_from_ratio_does_not_panic() {
assert_eq!(
Decimal::checked_from_ratio(1u128, 0u128),
Err(CheckedFromRatioError::DivideByZero)
);

assert_eq!(
Decimal::checked_from_ratio(u128::MAX, 1u128),
Err(CheckedFromRatioError::Overflow)
);
}

#[test]
fn decimal_implements_fraction() {
let fraction = Decimal::from_str("1234.567").unwrap();
Expand Down
51 changes: 43 additions & 8 deletions packages/std/src/math/decimal256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
use std::str::FromStr;
use thiserror::Error;

use crate::errors::StdError;
use crate::errors::{CheckedFromRatioError, CheckedMultiplyRatioError, StdError};
use crate::{OverflowError, Uint512};

use super::Fraction;
Expand Down Expand Up @@ -130,16 +130,32 @@ impl Decimal256 {

/// Returns the ratio (numerator / denominator) as a Decimal256
pub fn from_ratio(numerator: impl Into<Uint256>, denominator: impl Into<Uint256>) -> Self {
match Decimal256::checked_from_ratio(numerator, denominator) {
Ok(value) => value,
Err(CheckedFromRatioError::DivideByZero) => {
panic!("Denominator must not be zero")
}
Err(CheckedFromRatioError::Overflow) => panic!("Multiplication overflow"),
}
}

/// Returns the ratio (numerator / denominator) as a Decimal256
pub fn checked_from_ratio(
numerator: impl Into<Uint256>,
denominator: impl Into<Uint256>,
) -> Result<Self, CheckedFromRatioError> {
let numerator: Uint256 = numerator.into();
let denominator: Uint256 = denominator.into();
if denominator.is_zero() {
panic!("Denominator must not be zero");
match numerator.checked_multiply_ratio(Self::DECIMAL_FRACTIONAL, denominator) {
Ok(ratio) => {
// numerator * DECIMAL_FRACTIONAL / denominator
Ok(Self(ratio))
}
Err(CheckedMultiplyRatioError::Overflow) => Err(CheckedFromRatioError::Overflow),
Err(CheckedMultiplyRatioError::DivideByZero) => {
Err(CheckedFromRatioError::DivideByZero)
}
}

Self(
// numerator * DECIMAL_FRACTIONAL / denominator
numerator.multiply_ratio(Self::DECIMAL_FRACTIONAL, denominator),
)
}

pub const fn is_zero(&self) -> bool {
Expand Down Expand Up @@ -690,6 +706,25 @@ mod tests {
Decimal256::from_ratio(1u128, 0u128);
}

#[test]
#[should_panic(expected = "Multiplication overflow")]
fn decimal256_from_ratio_panics_for_mul_overflow() {
Decimal256::from_ratio(Uint256::MAX, 1u128);
}

#[test]
fn decimal256_checked_from_ratio_does_not_panic() {
assert_eq!(
Decimal256::checked_from_ratio(1u128, 0u128),
Err(CheckedFromRatioError::DivideByZero)
);

assert_eq!(
Decimal256::checked_from_ratio(Uint256::MAX, 1u128),
Err(CheckedFromRatioError::Overflow)
);
}

#[test]
fn decimal256_implements_fraction() {
let fraction = Decimal256::from_str("1234.567").unwrap();
Expand Down