Repository: CosmWasm/cosmwasm (rust)

Issue:
Make Uint128 inner value private

Pull request:
Make `Uint128` inner field private
Closes #905 

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3506df879..367cfdd7d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,14 +8,17 @@ and this project adheres to
 
 ### Removed
 
+- cosmwasm-std: Make `Uint128` inner field private ([#905])
 - cosmwasm-std: Remove `Context` - deprecated in previous release
 - cosmwasm-std: Remove `HandleResponse`, `InitResponse`, and `MigrateResponse` -
   deprecated in previous release
 
+[#905]: https://github.com/CosmWasm/cosmwasm/issues/905
+
 ### Changed
 
-- cosmwasm-std: Rename the `send` function parameter to `funds` in `WasmMsg`
-  for consistency with the wasmd message types.
+- cosmwasm-std: Rename the `send` function parameter to `funds` in `WasmMsg` for
+  consistency with the wasmd message types.
 
 ## [0.14.1] - 2021-06-14
 
diff --git a/contracts/crypto-verify/src/contract.rs b/contracts/crypto-verify/src/contract.rs
index 3a37d0281..ef5f86bcc 100644
--- a/contracts/crypto-verify/src/contract.rs
+++ b/contracts/crypto-verify/src/contract.rs
@@ -425,9 +425,9 @@ mod tests {
         let chain_id = 4; // Rinkeby, see https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md#list-of-chain-ids
         let from = "0x0a65766695a712af41b5cfecaad217b1a11cb22a";
         let to = "0xe137f5264b6b528244e1643a2d570b37660b7f14";
-        let gas_limit = Uint128(0x226c8);
-        let gas_price = Uint128(0x3b9aca00);
-        let value = Uint128(0x53177c);
+        let gas_limit = Uint128::new(0x226c8);
+        let gas_price = Uint128::new(0x3b9aca00);
+        let value = Uint128::new(0x53177c);
         let data = hex!("536561726368207478207465737420302e36353930383639313733393634333335");
         let r = hex!("b9299dab50b3cddcaecd64b29bfbd5cd30fac1a1adea1b359a13c4e5171492a6");
         let s = hex!("573059c66d894684488f92e7ce1f91b158ca57b0235485625b576a3b98c480ac");
diff --git a/contracts/staking/src/contract.rs b/contracts/staking/src/contract.rs
index c52bb447d..933e55d13 100644
--- a/contracts/staking/src/contract.rs
+++ b/contracts/staking/src/contract.rs
@@ -112,10 +112,10 @@ pub fn transfer(
 fn get_bonded<U: Into<String>>(querier: &QuerierWrapper, contract_addr: U) -> StdResult<Uint128> {
     let bonds = querier.query_all_delegations(contract_addr)?;
     if bonds.is_empty() {
-        return Ok(Uint128(0));
+        return Ok(Uint128::new(0));
     }
     let denom = bonds[0].amount.denom.as_str();
-    bonds.iter().fold(Ok(Uint128(0)), |racc, d| {
+    bonds.iter().fold(Ok(Uint128::new(0)), |racc, d| {
         let acc = racc?;
         if d.amount.denom.as_str() != denom {
             Err(StdError::generic_err(format!(
@@ -213,7 +213,7 @@ pub fn unbond(deps: DepsMut, env: Env, info: MessageInfo, amount: Uint128) -> St
     accounts.update(&sender_raw, |balance| -> StdResult<_> {
         Ok(balance.unwrap_or_default().checked_sub(amount)?)
     })?;
-    if tax > Uint128(0) {
+    if tax > Uint128::new(0) {
         // add tax to the owner
         accounts.update(&owner_raw, |balance: Option<Uint128>| -> StdResult<_> {
             Ok(balance.unwrap_or_default() + tax)
@@ -490,7 +490,7 @@ mod tests {
             decimals: 9,
             validator: String::from(DEFAULT_VALIDATOR),
             exit_tax: Decimal::percent(tax_percent),
-            min_withdrawal: Uint128(min_withdrawal),
+            min_withdrawal: Uint128::new(min_withdrawal),
         }
     }
 
@@ -515,7 +515,7 @@ mod tests {
             decimals: 9,
             validator: String::from("my-validator"),
             exit_tax: Decimal::percent(2),
-            min_withdrawal: Uint128(50),
+            min_withdrawal: Uint128::new(50),
         };
         let info = mock_info(&creator, &[]);
 
@@ -549,7 +549,7 @@ mod tests {
             decimals: 0,
             validator: String::from("my-validator"),
             exit_tax: Decimal::percent(2),
-            min_withdrawal: Uint128(50),
+            min_withdrawal: Uint128::new(50),
         };
         let info = mock_info(&creator, &[]);
 
@@ -564,9 +564,9 @@ mod tests {
         assert_eq!(token.decimals, msg.decimals);
 
         // no balance
-        assert_eq!(get_balance(deps.as_ref(), &creator), Uint128(0));
+        assert_eq!(get_balance(deps.as_ref(), &creator), Uint128::new(0));
         // no claims
-        assert_eq!(get_claims(deps.as_ref(), &creator), Uint128(0));
+        assert_eq!(get_claims(deps.as_ref(), &creator), Uint128::new(0));
 
         // investment info correct
         let invest = query_investment(deps.as_ref()).unwrap();
@@ -575,7 +575,7 @@ mod tests {
         assert_eq!(invest.exit_tax, msg.exit_tax);
         assert_eq!(invest.min_withdrawal, msg.min_withdrawal);
 
-        assert_eq!(invest.token_supply, Uint128(0));
+        assert_eq!(invest.token_supply, Uint128::new(0));
         assert_eq!(invest.staked_tokens, coin(0, "ustake"));
         assert_eq!(invest.nominal_value, Decimal::one());
     }
@@ -611,11 +611,11 @@ mod tests {
         }
 
         // bob got 1000 DRV for 1000 stake at a 1.0 ratio
-        assert_eq!(get_balance(deps.as_ref(), &bob), Uint128(1000));
+        assert_eq!(get_balance(deps.as_ref(), &bob), Uint128::new(1000));
 
         // investment info correct (updated supply)
         let invest = query_investment(deps.as_ref()).unwrap();
-        assert_eq!(invest.token_supply, Uint128(1000));
+        assert_eq!(invest.token_supply, Uint128::new(1000));
         assert_eq!(invest.staked_tokens, coin(1000, "ustake"));
         assert_eq!(invest.nominal_value, Decimal::one());
     }
@@ -655,7 +655,7 @@ mod tests {
 
         // we should now see 1000 issues and 1500 bonded (and a price of 1.5)
         let invest = query_investment(deps.as_ref()).unwrap();
-        assert_eq!(invest.token_supply, Uint128(1000));
+        assert_eq!(invest.token_supply, Uint128::new(1000));
         assert_eq!(invest.staked_tokens, coin(1500, "ustake"));
         let ratio = Decimal::from_str("1.5").unwrap();
         assert_eq!(invest.nominal_value, ratio);
@@ -671,10 +671,10 @@ mod tests {
         set_delegation(&mut deps.querier, 3000, "ustake");
 
         // alice should have gotten 2000 DRV for the 3000 stake, keeping the ratio at 1.5
-        assert_eq!(get_balance(deps.as_ref(), &alice), Uint128(2000));
+        assert_eq!(get_balance(deps.as_ref(), &alice), Uint128::new(2000));
 
         let invest = query_investment(deps.as_ref()).unwrap();
-        assert_eq!(invest.token_supply, Uint128(3000));
+        assert_eq!(invest.token_supply, Uint128::new(3000));
         assert_eq!(invest.staked_tokens, coin(4500, "ustake"));
         assert_eq!(invest.nominal_value, ratio);
     }
@@ -744,7 +744,7 @@ mod tests {
 
         // creator now tries to unbond these tokens - this must fail
         let unbond_msg = ExecuteMsg::Unbond {
-            amount: Uint128(600),
+            amount: Uint128::new(600),
         };
         let info = mock_info(&creator, &[]);
         let res = execute(deps.as_mut(), mock_env(), info, unbond_msg);
@@ -759,11 +759,11 @@ mod tests {
         // 60 are taken and send to the owner
         // 540 are unbonded in exchange for 540 * 1.5 = 810 native tokens
         let unbond_msg = ExecuteMsg::Unbond {
-            amount: Uint128(600),
+            amount: Uint128::new(600),
         };
-        let owner_cut = Uint128(60);
-        let bobs_claim = Uint128(810);
-        let bobs_balance = Uint128(400);
+        let owner_cut = Uint128::new(60);
+        let bobs_claim = Uint128::new(810);
+        let bobs_balance = Uint128::new(400);
         let info = mock_info(&bob, &[]);
         let res = execute(deps.as_mut(), mock_env(), info, unbond_msg).unwrap();
         assert_eq!(1, res.messages.len());
diff --git a/packages/std/src/coins.rs b/packages/std/src/coins.rs
index b13f3fd08..735705d07 100644
--- a/packages/std/src/coins.rs
+++ b/packages/std/src/coins.rs
@@ -13,7 +13,7 @@ pub struct Coin {
 impl Coin {
     pub fn new<S: Into<String>>(amount: u128, denom: S) -> Self {
         Coin {
-            amount: Uint128(amount),
+            amount: Uint128::new(amount),
             denom: denom.into(),
         }
     }
@@ -90,7 +90,7 @@ mod tests {
     #[test]
     fn coin_implements_display() {
         let a = Coin {
-            amount: Uint128(123),
+            amount: Uint128::new(123),
             denom: "ucosm".to_string(),
         };
 
@@ -105,7 +105,7 @@ mod tests {
         assert_eq!(
             a,
             Coin {
-                amount: Uint128(123),
+                amount: Uint128::new(123),
                 denom: "ucosm".to_string()
             }
         );
@@ -114,7 +114,7 @@ mod tests {
         assert_eq!(
             zero,
             Coin {
-                amount: Uint128(0),
+                amount: Uint128::new(0),
                 denom: "ucosm".to_string()
             }
         );
@@ -123,7 +123,7 @@ mod tests {
         assert_eq!(
             string_denom,
             Coin {
-                amount: Uint128(42),
+                amount: Uint128::new(42),
                 denom: "ucosm".to_string()
             }
         );
@@ -135,7 +135,7 @@ mod tests {
         assert_eq!(
             a,
             vec![Coin {
-                amount: Uint128(123),
+                amount: Uint128::new(123),
                 denom: "ucosm".to_string()
             }]
         );
@@ -144,7 +144,7 @@ mod tests {
         assert_eq!(
             zero,
             vec![Coin {
-                amount: Uint128(0),
+                amount: Uint128::new(0),
                 denom: "ucosm".to_string()
             }]
         );
@@ -153,7 +153,7 @@ mod tests {
         assert_eq!(
             string_denom,
             vec![Coin {
-                amount: Uint128(42),
+                amount: Uint128::new(42),
                 denom: "ucosm".to_string()
             }]
         );
diff --git a/packages/std/src/math/decimal.rs b/packages/std/src/math/decimal.rs
index f642b747f..b4fe188cc 100644
--- a/packages/std/src/math/decimal.rs
+++ b/packages/std/src/math/decimal.rs
@@ -560,19 +560,19 @@ mod tests {
     // in this test the Decimal is on the right
     fn uint128_decimal_multiply() {
         // a*b
-        let left = Uint128(300);
+        let left = Uint128::new(300);
         let right = Decimal::one() + Decimal::percent(50); // 1.5
-        assert_eq!(left * right, Uint128(450));
+        assert_eq!(left * right, Uint128::new(450));
 
         // a*0
-        let left = Uint128(300);
+        let left = Uint128::new(300);
         let right = Decimal::zero();
-        assert_eq!(left * right, Uint128(0));
+        assert_eq!(left * right, Uint128::new(0));
 
         // 0*a
-        let left = Uint128(0);
+        let left = Uint128::new(0);
         let right = Decimal::one() + Decimal::percent(50); // 1.5
-        assert_eq!(left * right, Uint128(0));
+        assert_eq!(left * right, Uint128::new(0));
     }
 
     #[test]
@@ -580,30 +580,30 @@ mod tests {
     fn decimal_uint128_multiply() {
         // a*b
         let left = Decimal::one() + Decimal::percent(50); // 1.5
-        let right = Uint128(300);
-        assert_eq!(left * right, Uint128(450));
+        let right = Uint128::new(300);
+        assert_eq!(left * right, Uint128::new(450));
 
         // 0*a
         let left = Decimal::zero();
-        let right = Uint128(300);
-        assert_eq!(left * right, Uint128(0));
+        let right = Uint128::new(300);
+        assert_eq!(left * right, Uint128::new(0));
 
         // a*0
         let left = Decimal::one() + Decimal::percent(50); // 1.5
-        let right = Uint128(0);
-        assert_eq!(left * right, Uint128(0));
+        let right = Uint128::new(0);
+        assert_eq!(left * right, Uint128::new(0));
     }
 
     #[test]
     fn decimal_uint128_division() {
         // a/b
         let left = Decimal::percent(150); // 1.5
-        let right = Uint128(3);
+        let right = Uint128::new(3);
         assert_eq!(left / right, Decimal::percent(50));
 
         // 0/a
         let left = Decimal::zero();
-        let right = Uint128(300);
+        let right = Uint128::new(300);
         assert_eq!(left / right, Decimal::zero());
     }
 
@@ -611,7 +611,7 @@ mod tests {
     #[should_panic(expected = "attempt to divide by zero")]
     fn decimal_uint128_divide_by_zero() {
         let left = Decimal::percent(150); // 1.5
-        let right = Uint128(0);
+        let right = Uint128::new(0);
         let _result = left / right;
     }
 
@@ -619,12 +619,12 @@ mod tests {
     fn decimal_uint128_div_assign() {
         // a/b
         let mut dec = Decimal::percent(150); // 1.5
-        dec /= Uint128(3);
+        dec /= Uint128::new(3);
         assert_eq!(dec, Decimal::percent(50));
 
         // 0/a
         let mut dec = Decimal::zero();
-        dec /= Uint128(300);
+        dec /= Uint128::new(300);
         assert_eq!(dec, Decimal::zero());
     }
 
@@ -633,7 +633,7 @@ mod tests {
     fn decimal_uint128_div_assign_by_zero() {
         // a/0
         let mut dec = Decimal::percent(50);
-        dec /= Uint128(0);
+        dec /= Uint128::new(0);
     }
 
     #[test]
diff --git a/packages/std/src/math/uint128.rs b/packages/std/src/math/uint128.rs
index 1c67b3a27..84cd1b726 100644
--- a/packages/std/src/math/uint128.rs
+++ b/packages/std/src/math/uint128.rs
@@ -27,9 +27,7 @@ use crate::errors::{DivideByZeroError, OverflowError, OverflowOperation, StdErro
 /// assert_eq!(c.u128(), 70);
 /// ```
 #[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, JsonSchema)]
-pub struct Uint128(
-    #[schemars(with = "String")] pub u128, // Simon thinks this should be private, but does not want to worry about breaking code right now
-);
+pub struct Uint128(#[schemars(with = "String")] u128);
 
 impl Uint128 {
     /// Creates a Uint128(value).
diff --git a/packages/std/src/results/attribute.rs b/packages/std/src/results/attribute.rs
index 619e12bba..d0f343e5a 100644
--- a/packages/std/src/results/attribute.rs
+++ b/packages/std/src/results/attribute.rs
@@ -31,7 +31,7 @@ mod tests {
         assert_eq!(attr("foo", "42"), expeceted);
         assert_eq!(attr("foo".to_string(), "42"), expeceted);
         assert_eq!(attr("foo", "42".to_string()), expeceted);
-        assert_eq!(attr("foo", Uint128(42)), expeceted);
+        assert_eq!(attr("foo", Uint128::new(42)), expeceted);
         assert_eq!(attr("foo", 42), expeceted);
     }
 }
diff --git a/packages/std/src/traits.rs b/packages/std/src/traits.rs
index d2b752f97..f6b842a98 100644
--- a/packages/std/src/traits.rs
+++ b/packages/std/src/traits.rs
@@ -354,6 +354,6 @@ mod tests {
             .unwrap()
             .unwrap();
         let balance: BalanceResponse = from_slice(&raw).unwrap();
-        assert_eq!(balance.amount.amount, Uint128(5));
+        assert_eq!(balance.amount.amount, Uint128::new(5));
     }
 }


Test patch:
diff --git a/contracts/crypto-verify/tests/integration.rs b/contracts/crypto-verify/tests/integration.rs
index 41b6c984d..494b5e03d 100644
--- a/contracts/crypto-verify/tests/integration.rs
+++ b/contracts/crypto-verify/tests/integration.rs
@@ -225,9 +225,9 @@ fn verify_ethereum_transaction_works() {
     let chain_id = 4; // Rinkeby, see https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md#list-of-chain-ids
     let from = "0x0a65766695a712af41b5cfecaad217b1a11cb22a";
     let to = "0xe137f5264b6b528244e1643a2d570b37660b7f14";
-    let gas_limit = Uint128(0x226c8);
-    let gas_price = Uint128(0x3b9aca00);
-    let value = Uint128(0x53177c);
+    let gas_limit = Uint128::new(0x226c8);
+    let gas_price = Uint128::new(0x3b9aca00);
+    let value = Uint128::new(0x53177c);
     let data = hex!("536561726368207478207465737420302e36353930383639313733393634333335");
     let r = hex!("b9299dab50b3cddcaecd64b29bfbd5cd30fac1a1adea1b359a13c4e5171492a6");
     let s = hex!("573059c66d894684488f92e7ce1f91b158ca57b0235485625b576a3b98c480ac");
diff --git a/contracts/staking/tests/integration.rs b/contracts/staking/tests/integration.rs
index f908d62e1..102eff94f 100644
--- a/contracts/staking/tests/integration.rs
+++ b/contracts/staking/tests/integration.rs
@@ -58,7 +58,7 @@ fn initialization_with_missing_validator() {
         decimals: 9,
         validator: String::from("my-validator"),
         exit_tax: Decimal::percent(2),
-        min_withdrawal: Uint128(50),
+        min_withdrawal: Uint128::new(50),
     };
     let info = mock_info(&creator, &[]);
 
@@ -96,7 +96,7 @@ fn proper_initialization() {
         decimals: 9,
         validator: String::from("my-validator"),
         exit_tax: Decimal::percent(2),
-        min_withdrawal: Uint128(50),
+        min_withdrawal: Uint128::new(50),
     };
     let info = mock_info(&creator, &[]);
 
@@ -121,7 +121,7 @@ fn proper_initialization() {
     )
     .unwrap();
     let bal: BalanceResponse = from_binary(&res).unwrap();
-    assert_eq!(bal.balance, Uint128(0));
+    assert_eq!(bal.balance, Uint128::new(0));
 
     // no claims
     let res = query(
@@ -133,7 +133,7 @@ fn proper_initialization() {
     )
     .unwrap();
     let claim: ClaimsResponse = from_binary(&res).unwrap();
-    assert_eq!(claim.claims, Uint128(0));
+    assert_eq!(claim.claims, Uint128::new(0));
 
     // investment info correct
     let res = query(&mut deps, mock_env(), QueryMsg::Investment {}).unwrap();
@@ -143,7 +143,7 @@ fn proper_initialization() {
     assert_eq!(invest.exit_tax, msg.exit_tax);
     assert_eq!(invest.min_withdrawal, msg.min_withdrawal);
 
-    assert_eq!(invest.token_supply, Uint128(0));
+    assert_eq!(invest.token_supply, Uint128::new(0));
     assert_eq!(invest.staked_tokens, coin(0, "ustake"));
     assert_eq!(invest.nominal_value, Decimal::one());
 }