From 5bf3619248e0ced41eedea34fe2c4b4ef743c8b7 Mon Sep 17 00:00:00 2001 From: Shane Vitarana Date: Wed, 7 Jun 2023 12:58:04 -0400 Subject: [PATCH 1/5] Add bps conversion to Decimal and Decimal256 --- packages/std/src/math/decimal.rs | 11 +++++++++++ packages/std/src/math/decimal256.rs | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/packages/std/src/math/decimal.rs b/packages/std/src/math/decimal.rs index 3c3842128c..113be3258a 100644 --- a/packages/std/src/math/decimal.rs +++ b/packages/std/src/math/decimal.rs @@ -76,6 +76,11 @@ impl Decimal { Self(((x as u128) * 1_000_000_000_000_000).into()) } + /// Convert bps (basis points) 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 @@ -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(); diff --git a/packages/std/src/math/decimal256.rs b/packages/std/src/math/decimal256.rs index bd6109718a..37371a6c00 100644 --- a/packages/std/src/math/decimal256.rs +++ b/packages/std/src/math/decimal256.rs @@ -85,6 +85,11 @@ impl Decimal256 { Self(Uint256::from(x) * Uint256::from(1_000_000_000_000_000u128)) } + /// Convert bps (basis points) 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 @@ -794,6 +799,12 @@ 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(); From a5addb8d901ed06eb51dc256c9367d778457ddc9 Mon Sep 17 00:00:00 2001 From: Shane Vitarana Date: Wed, 7 Jun 2023 13:08:10 -0400 Subject: [PATCH 2/5] Fix formatting --- packages/std/src/math/decimal256.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/std/src/math/decimal256.rs b/packages/std/src/math/decimal256.rs index 37371a6c00..89338ec2dd 100644 --- a/packages/std/src/math/decimal256.rs +++ b/packages/std/src/math/decimal256.rs @@ -802,7 +802,10 @@ mod tests { #[test] fn decimal256_bps() { let value = Decimal256::bps(125); - assert_eq!(value.0, Decimal256::DECIMAL_FRACTIONAL / Uint256::from(80u8)); + assert_eq!( + value.0, + Decimal256::DECIMAL_FRACTIONAL / Uint256::from(80u8) + ); } #[test] From 97a1048755c179f7c467a32197f56f3dbaca97f6 Mon Sep 17 00:00:00 2001 From: "shane.stars" Date: Wed, 7 Jun 2023 16:53:41 -0400 Subject: [PATCH 3/5] Update packages/std/src/math/decimal.rs Co-authored-by: Simon Warta <2603011+webmaster128@users.noreply.github.com> --- packages/std/src/math/decimal.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/std/src/math/decimal.rs b/packages/std/src/math/decimal.rs index 113be3258a..9e4e3424aa 100644 --- a/packages/std/src/math/decimal.rs +++ b/packages/std/src/math/decimal.rs @@ -76,7 +76,7 @@ impl Decimal { Self(((x as u128) * 1_000_000_000_000_000).into()) } - /// Convert bps (basis points) into Decimal + /// Convert basis points (x/10000) into Decimal pub fn bps(x: u64) -> Self { Self(((x as u128) * 100_000_000_000_000).into()) } From bd5245c984b04dd52fe9571acd8e1d45a03c55c0 Mon Sep 17 00:00:00 2001 From: "shane.stars" Date: Wed, 7 Jun 2023 16:53:48 -0400 Subject: [PATCH 4/5] Update packages/std/src/math/decimal256.rs Co-authored-by: Simon Warta <2603011+webmaster128@users.noreply.github.com> --- packages/std/src/math/decimal256.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/std/src/math/decimal256.rs b/packages/std/src/math/decimal256.rs index 89338ec2dd..157e7397b7 100644 --- a/packages/std/src/math/decimal256.rs +++ b/packages/std/src/math/decimal256.rs @@ -85,7 +85,7 @@ impl Decimal256 { Self(Uint256::from(x) * Uint256::from(1_000_000_000_000_000u128)) } - /// Convert bps (basis points) into Decimal256 + /// Convert basis points (x/10000) into Decimal256 pub fn bps(x: u64) -> Self { Self(Uint256::from(x) * Uint256::from(100_000_000_000_000u128)) } From cc67609090f92c2fa3858ebaaf50138103e0ba37 Mon Sep 17 00:00:00 2001 From: Shane Vitarana Date: Wed, 7 Jun 2023 17:31:23 -0400 Subject: [PATCH 5/5] Update CHANGELOG --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a93f88179d..b1682da85b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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