Skip to content

Commit adad747

Browse files
authored
Merge pull request #2399 from CosmWasm/new-argument
Change {Int256,Int512,Uint256,Uint512}::new argument from bytes to i128/u128
2 parents da133ba + 77661a2 commit adad747

File tree

13 files changed

+328
-127
lines changed

13 files changed

+328
-127
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ and this project adheres to
4141
makes the use of `schemars` optional for contracts. ([#2201])
4242
- cosmwasm-std: Remove `schemars::JsonSchema` requirement from `CustomMsg`.
4343
([#2201])
44+
- cosmwasm-std: `Int256::new`/`Int512::new` now take an `i128` argument instead
45+
of bytes. Use `::from_be_bytes` if you need the old behaviour. ([#2367])
46+
- cosmwasm-std: `Uint256::new`/`Uint512::new` now take an `u128` argument
47+
instead of bytes. Use `::from_be_bytes` if you need the old behaviour.
48+
([#2367])
49+
- cosmwasm-std: Deprecate `{Decimal,Decimal256}::raw` and
50+
`{SignedDecimal,SignedDecimal256}::raw` in favour of e.g.
51+
`Decimal::new(Uint128::new(value))`. ([#2399])
52+
- cosmwasm-std: Deprecate `Uint256::from_u128` and `Int256::from_i128` in favour
53+
of `::new`. ([#2399])
4454

4555
## Fixed
4656

@@ -58,10 +68,12 @@ and this project adheres to
5868
[#2337]: https://github.com/CosmWasm/cosmwasm/issues/2337
5969
[#2340]: https://github.com/CosmWasm/cosmwasm/pull/2340
6070
[#2344]: https://github.com/CosmWasm/cosmwasm/pull/2344
71+
[#2367]: https://github.com/CosmWasm/cosmwasm/issues/2367
6172
[#2374]: https://github.com/CosmWasm/cosmwasm/issues/2374
6273
[#2378]: https://github.com/CosmWasm/cosmwasm/issues/2378
6374
[#2383]: https://github.com/CosmWasm/cosmwasm/issues/2383
6475
[#2390]: https://github.com/CosmWasm/cosmwasm/issues/2390
76+
[#2399]: https://github.com/CosmWasm/cosmwasm/pull/2399
6577

6678
## [2.2.0] - 2024-12-17
6779

packages/std/src/math/decimal.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,27 @@ impl Decimal {
4343

4444
/// Creates a Decimal(value)
4545
/// This is equivalent to `Decimal::from_atomics(value, 18)` but usable in a const context.
46+
///
47+
/// ## Examples
48+
///
49+
/// ```
50+
/// # use cosmwasm_std::{Uint128, Decimal};
51+
/// let atoms = Uint128::new(141_183_460_469_231_731_687_303_715_884_105_727_125);
52+
/// let value = Decimal::new(atoms);
53+
/// assert_eq!(value.to_string(), "141183460469231731687.303715884105727125");
54+
/// ```
55+
#[inline]
56+
#[must_use]
4657
pub const fn new(value: Uint128) -> Self {
4758
Self(value)
4859
}
4960

5061
/// Creates a Decimal(Uint128(value))
5162
/// This is equivalent to `Decimal::from_atomics(value, 18)` but usable in a const context.
63+
#[deprecated(
64+
since = "3.0.0",
65+
note = "Use Decimal::new(Uint128::new(value)) instead"
66+
)]
5267
pub const fn raw(value: u128) -> Self {
5368
Self(Uint128::new(value))
5469
}
@@ -795,6 +810,7 @@ mod tests {
795810
}
796811

797812
#[test]
813+
#[allow(deprecated)]
798814
fn decimal_raw() {
799815
let value = 300u128;
800816
assert_eq!(Decimal::raw(value).0.u128(), value);
@@ -857,7 +873,7 @@ mod tests {
857873
fn decimal_try_from_signed_works() {
858874
assert_eq!(
859875
Decimal::try_from(SignedDecimal::MAX).unwrap(),
860-
Decimal::raw(SignedDecimal::MAX.atomics().i128() as u128)
876+
Decimal::new(Uint128::new(SignedDecimal::MAX.atomics().i128() as u128))
861877
);
862878
assert_eq!(
863879
Decimal::try_from(SignedDecimal::zero()).unwrap(),

packages/std/src/math/decimal256.rs

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ pub struct Decimal256RangeExceeded;
3434

3535
impl Decimal256 {
3636
const DECIMAL_FRACTIONAL: Uint256 = // 1*10**18
37-
Uint256::from_u128(1_000_000_000_000_000_000);
37+
Uint256::new(1_000_000_000_000_000_000);
3838
const DECIMAL_FRACTIONAL_SQUARED: Uint256 = // 1*10**36
39-
Uint256::from_u128(1_000_000_000_000_000_000_000_000_000_000_000_000);
39+
Uint256::new(1_000_000_000_000_000_000_000_000_000_000_000_000);
4040

4141
/// The number of decimal places. Since decimal types are fixed-point rather than
4242
/// floating-point, this is a constant.
@@ -48,14 +48,29 @@ impl Decimal256 {
4848

4949
/// Creates a Decimal256 from Uint256
5050
/// This is equivalent to `Decimal256::from_atomics(value, 18)` but usable in a const context.
51+
///
52+
/// ## Examples
53+
///
54+
/// ```
55+
/// # use cosmwasm_std::{Uint256, Decimal256};
56+
/// let atoms = Uint256::new(141_183_460_469_231_731_687_303_715_884_105_727_125);
57+
/// let value = Decimal256::new(atoms);
58+
/// assert_eq!(value.to_string(), "141183460469231731687.303715884105727125");
59+
/// ```
60+
#[inline]
61+
#[must_use]
5162
pub const fn new(value: Uint256) -> Self {
5263
Self(value)
5364
}
5465

5566
/// Creates a Decimal256 from u128
5667
/// This is equivalent to `Decimal256::from_atomics(value, 18)` but usable in a const context.
68+
#[deprecated(
69+
since = "3.0.0",
70+
note = "Use Decimal256::new(Uint256::new(value)) instead"
71+
)]
5772
pub const fn raw(value: u128) -> Self {
58-
Self(Uint256::from_u128(value))
73+
Self(Uint256::new(value))
5974
}
6075

6176
/// Create a 1.0 Decimal256
@@ -84,7 +99,7 @@ impl Decimal256 {
8499
pub const fn percent(x: u64) -> Self {
85100
// multiplication does not overflow since `u64::MAX` * 10**16 is well in u128 range
86101
let atomics = (x as u128) * 10_000_000_000_000_000;
87-
Self(Uint256::from_u128(atomics))
102+
Self(Uint256::new(atomics))
88103
}
89104

90105
/// Convert permille (x/1000) into Decimal256
@@ -101,7 +116,7 @@ impl Decimal256 {
101116
pub const fn permille(x: u64) -> Self {
102117
// multiplication does not overflow since `u64::MAX` * 10**15 is well in u128 range
103118
let atomics = (x as u128) * 1_000_000_000_000_000;
104-
Self(Uint256::from_u128(atomics))
119+
Self(Uint256::new(atomics))
105120
}
106121

107122
/// Convert basis points (x/10000) into Decimal256
@@ -120,7 +135,7 @@ impl Decimal256 {
120135
pub const fn bps(x: u64) -> Self {
121136
// multiplication does not overflow since `u64::MAX` * 10**14 is well in u128 range
122137
let atomics = (x as u128) * 100_000_000_000_000;
123-
Self(Uint256::from_u128(atomics))
138+
Self(Uint256::new(atomics))
124139
}
125140

126141
/// Creates a decimal from a number of atomic units and the number
@@ -798,6 +813,7 @@ mod tests {
798813
}
799814

800815
#[test]
816+
#[allow(deprecated)]
801817
fn decimal256_raw() {
802818
let value = 300u128;
803819
let expected = Uint256::from(value);
@@ -2218,18 +2234,18 @@ mod tests {
22182234
#[test]
22192235
fn decimal256_to_uint_floor_works() {
22202236
let d = Decimal256::from_str("12.000000000000000001").unwrap();
2221-
assert_eq!(d.to_uint_floor(), Uint256::from_u128(12));
2237+
assert_eq!(d.to_uint_floor(), Uint256::new(12));
22222238
let d = Decimal256::from_str("12.345").unwrap();
2223-
assert_eq!(d.to_uint_floor(), Uint256::from_u128(12));
2239+
assert_eq!(d.to_uint_floor(), Uint256::new(12));
22242240
let d = Decimal256::from_str("12.999").unwrap();
2225-
assert_eq!(d.to_uint_floor(), Uint256::from_u128(12));
2241+
assert_eq!(d.to_uint_floor(), Uint256::new(12));
22262242
let d = Decimal256::from_str("0.98451384").unwrap();
2227-
assert_eq!(d.to_uint_floor(), Uint256::from_u128(0));
2243+
assert_eq!(d.to_uint_floor(), Uint256::new(0));
22282244

22292245
let d = Decimal256::from_str("75.0").unwrap();
2230-
assert_eq!(d.to_uint_floor(), Uint256::from_u128(75));
2246+
assert_eq!(d.to_uint_floor(), Uint256::new(75));
22312247
let d = Decimal256::from_str("0.0").unwrap();
2232-
assert_eq!(d.to_uint_floor(), Uint256::from_u128(0));
2248+
assert_eq!(d.to_uint_floor(), Uint256::new(0));
22332249

22342250
let d = Decimal256::MAX;
22352251
assert_eq!(
@@ -2267,16 +2283,16 @@ mod tests {
22672283
#[test]
22682284
fn decimal256_to_uint_ceil_works() {
22692285
let d = Decimal256::from_str("12.000000000000000001").unwrap();
2270-
assert_eq!(d.to_uint_ceil(), Uint256::from_u128(13));
2286+
assert_eq!(d.to_uint_ceil(), Uint256::new(13));
22712287
let d = Decimal256::from_str("12.345").unwrap();
2272-
assert_eq!(d.to_uint_ceil(), Uint256::from_u128(13));
2288+
assert_eq!(d.to_uint_ceil(), Uint256::new(13));
22732289
let d = Decimal256::from_str("12.999").unwrap();
2274-
assert_eq!(d.to_uint_ceil(), Uint256::from_u128(13));
2290+
assert_eq!(d.to_uint_ceil(), Uint256::new(13));
22752291

22762292
let d = Decimal256::from_str("75.0").unwrap();
2277-
assert_eq!(d.to_uint_ceil(), Uint256::from_u128(75));
2293+
assert_eq!(d.to_uint_ceil(), Uint256::new(75));
22782294
let d = Decimal256::from_str("0.0").unwrap();
2279-
assert_eq!(d.to_uint_ceil(), Uint256::from_u128(0));
2295+
assert_eq!(d.to_uint_ceil(), Uint256::new(0));
22802296

22812297
let d = Decimal256::MAX;
22822298
assert_eq!(

packages/std/src/math/int128.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ impl Int128 {
4747
///
4848
/// This method is less flexible than `from` but can be called in a const context.
4949
#[inline]
50+
#[must_use]
5051
pub const fn new(value: i128) -> Self {
5152
Self(value)
5253
}

0 commit comments

Comments
 (0)