Skip to content

Commit

Permalink
Merge pull request #1715 from public-awesome/shanev/dec-bps
Browse files Browse the repository at this point in the history
Add bps conversion to `Decimal` and `Decimal256`
  • Loading branch information
chipshort authored Jun 9, 2023
2 parents 7cbb780 + cc67609 commit c1b4828
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ and this project adheres to
been checked before. This is useful for state-sync where we know the Wasm code
was checked when it was first uploaded. ([#1635])
- cosmwasm-std: Add `FromStr` impl for `Coin`. ([#1684])
- cosmwasm-std: Add `Decimal::bps` and `Decimal256::bps` to create a decimal
from a basis point value ([#1715]).

[#1635]: https://github.com/CosmWasm/cosmwasm/pull/1635
[#1684]: https://github.com/CosmWasm/cosmwasm/pull/1684
[#1715]: https://github.com/CosmWasm/cosmwasm/pull/1715

### Changed

Expand Down
11 changes: 11 additions & 0 deletions packages/std/src/math/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ impl Decimal {
Self(((x as u128) * 1_000_000_000_000_000).into())
}

/// Convert basis points (x/10000) into Decimal
pub fn bps(x: u64) -> Self {
Self(((x as u128) * 100_000_000_000_000).into())
}

/// Creates a decimal from a number of atomic units and the number
/// of decimal places. The inputs will be converted internally to form
/// a decimal with 18 decimal places. So the input 123 and 2 will create
Expand Down Expand Up @@ -768,6 +773,12 @@ mod tests {
assert_eq!(value.0, Decimal::DECIMAL_FRACTIONAL / Uint128::from(8u8));
}

#[test]
fn decimal_bps() {
let value = Decimal::bps(125);
assert_eq!(value.0, Decimal::DECIMAL_FRACTIONAL / Uint128::from(80u8));
}

#[test]
fn decimal_from_atomics_works() {
let one = Decimal::one();
Expand Down
14 changes: 14 additions & 0 deletions packages/std/src/math/decimal256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ impl Decimal256 {
Self(Uint256::from(x) * Uint256::from(1_000_000_000_000_000u128))
}

/// Convert basis points (x/10000) into Decimal256
pub fn bps(x: u64) -> Self {
Self(Uint256::from(x) * Uint256::from(100_000_000_000_000u128))
}

/// Creates a decimal from a number of atomic units and the number
/// of decimal places. The inputs will be converted internally to form
/// a decimal with 18 decimal places. So the input 123 and 2 will create
Expand Down Expand Up @@ -794,6 +799,15 @@ mod tests {
assert_eq!(value.0, Decimal256::DECIMAL_FRACTIONAL / Uint256::from(8u8));
}

#[test]
fn decimal256_bps() {
let value = Decimal256::bps(125);
assert_eq!(
value.0,
Decimal256::DECIMAL_FRACTIONAL / Uint256::from(80u8)
);
}

#[test]
fn decimal256_from_atomics_works() {
let one = Decimal256::one();
Expand Down

0 comments on commit c1b4828

Please sign in to comment.