Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constify Decimal/Decimal256 constructors #1824

Merged
merged 1 commit into from
Aug 22, 2023
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 @@ -29,6 +29,7 @@ and this project adheres to
- cosmwasm-check: Update clap dependency to version 4 ([#1677])
- cosmwasm-vm: Use `wasmparser` for initial validation instead of `parity-wasm`
([#1786])
- cosmwasm-std: Make constructors `Decimal{,256}::{percent,permille,bps}` const

[#1667]: https://github.com/CosmWasm/cosmwasm/pull/1667
[#1674]: https://github.com/CosmWasm/cosmwasm/pull/1674
Expand Down
50 changes: 44 additions & 6 deletions packages/std/src/math/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,56 @@ impl Decimal {
}

/// Convert x% into Decimal
pub fn percent(x: u64) -> Self {
Self(((x as u128) * 10_000_000_000_000_000).into())
///
/// ## Examples
///
/// ```
/// # use std::str::FromStr;
/// # use cosmwasm_std::Decimal;
/// const HALF: Decimal = Decimal::percent(50);
///
/// assert_eq!(HALF, Decimal::from_str("0.5").unwrap());
/// ```
pub const fn percent(x: u64) -> Self {
// multiplication does not overflow since `u64::MAX` * 10**16 is well in u128 range
let atomics = (x as u128) * 10_000_000_000_000_000;
Self(Uint128::new(atomics))
}

/// Convert permille (x/1000) into Decimal
pub fn permille(x: u64) -> Self {
Self(((x as u128) * 1_000_000_000_000_000).into())
///
/// ## Examples
///
/// ```
/// # use std::str::FromStr;
/// # use cosmwasm_std::Decimal;
/// const HALF: Decimal = Decimal::permille(500);
///
/// assert_eq!(HALF, Decimal::from_str("0.5").unwrap());
/// ```
pub const fn permille(x: u64) -> Self {
// multiplication does not overflow since `u64::MAX` * 10**15 is well in u128 range
let atomics = (x as u128) * 1_000_000_000_000_000;
Self(Uint128::new(atomics))
}

/// Convert basis points (x/10000) into Decimal
pub fn bps(x: u64) -> Self {
Self(((x as u128) * 100_000_000_000_000).into())
///
/// ## Examples
///
/// ```
/// # use std::str::FromStr;
/// # use cosmwasm_std::Decimal;
/// const TWO_BPS: Decimal = Decimal::bps(2);
/// const HALF: Decimal = Decimal::bps(5000);
///
/// assert_eq!(TWO_BPS, Decimal::from_str("0.0002").unwrap());
/// assert_eq!(HALF, Decimal::from_str("0.5").unwrap());
/// ```
pub const fn bps(x: u64) -> Self {
// multiplication does not overflow since `u64::MAX` * 10**14 is well in u128 range
let atomics = (x as u128) * 100_000_000_000_000;
Self(Uint128::new(atomics))
}

/// Creates a decimal from a number of atomic units and the number
Expand Down
50 changes: 44 additions & 6 deletions packages/std/src/math/decimal256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,56 @@ impl Decimal256 {
}

/// Convert x% into Decimal256
pub fn percent(x: u64) -> Self {
Self(Uint256::from(x) * Uint256::from(10_000_000_000_000_000u128))
///
/// ## Examples
///
/// ```
/// # use std::str::FromStr;
/// # use cosmwasm_std::Decimal256;
/// const HALF: Decimal256 = Decimal256::percent(50);
///
/// assert_eq!(HALF, Decimal256::from_str("0.5").unwrap());
/// ```
pub const fn percent(x: u64) -> Self {
// multiplication does not overflow since `u64::MAX` * 10**16 is well in u128 range
let atomics = (x as u128) * 10_000_000_000_000_000;
Self(Uint256::from_u128(atomics))
}

/// Convert permille (x/1000) into Decimal256
pub fn permille(x: u64) -> Self {
Self(Uint256::from(x) * Uint256::from(1_000_000_000_000_000u128))
///
/// ## Examples
///
/// ```
/// # use std::str::FromStr;
/// # use cosmwasm_std::Decimal256;
/// const HALF: Decimal256 = Decimal256::permille(500);
///
/// assert_eq!(HALF, Decimal256::from_str("0.5").unwrap());
/// ```
pub const fn permille(x: u64) -> Self {
// multiplication does not overflow since `u64::MAX` * 10**15 is well in u128 range
let atomics = (x as u128) * 1_000_000_000_000_000;
Self(Uint256::from_u128(atomics))
}

/// Convert basis points (x/10000) into Decimal256
pub fn bps(x: u64) -> Self {
Self(Uint256::from(x) * Uint256::from(100_000_000_000_000u128))
///
/// ## Examples
///
/// ```
/// # use std::str::FromStr;
/// # use cosmwasm_std::Decimal256;
/// const TWO_BPS: Decimal256 = Decimal256::bps(2);
/// const HALF: Decimal256 = Decimal256::bps(5000);
///
/// assert_eq!(TWO_BPS, Decimal256::from_str("0.0002").unwrap());
/// assert_eq!(HALF, Decimal256::from_str("0.5").unwrap());
/// ```
pub const fn bps(x: u64) -> Self {
// multiplication does not overflow since `u64::MAX` * 10**14 is well in u128 range
let atomics = (x as u128) * 100_000_000_000_000;
Self(Uint256::from_u128(atomics))
}

/// Creates a decimal from a number of atomic units and the number
Expand Down
Loading