Skip to content

Commit

Permalink
Merge pull request #1902 from CosmWasm/1501-coin-constructor
Browse files Browse the repository at this point in the history
[2.0] Use `impl Into<_>` for `Coin` constructor
  • Loading branch information
chipshort authored Oct 18, 2023
2 parents 6e7b1e6 + 434ef6d commit febc0af
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ and this project adheres to
([#1874])
- cosmwasm-vm: Make `CacheOptions` non-exhaustive and add a constructor.
([#1898])
- cosmwasm-std: `Coin::new` now takes `Into<Uint128>` instead of `u128` as the
first argument and `DecCoin::new` takes `Into<Decimal256>` instead of
`Decimal256`. ([#1902])

[#1874]: https://github.com/CosmWasm/cosmwasm/pull/1874
[#1879]: https://github.com/CosmWasm/cosmwasm/pull/1879
[#1890]: https://github.com/CosmWasm/cosmwasm/pull/1890
[#1898]: https://github.com/CosmWasm/cosmwasm/pull/1898
[#1902]: https://github.com/CosmWasm/cosmwasm/pull/1902

## [1.5.0-rc.0]

Expand Down
7 changes: 7 additions & 0 deletions MIGRATING.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ major releases of `cosmwasm`. Note that you can also view the
+Uint128::new(123456).mul_floor(Decimal::percent(1));
```

- When calling `Coin::new`, you now have to explicitly specify the integer type:

```diff
-Coin::new(1234, "uatom")
+Coin::new(1234u128, "uatom")
```

## 1.4.x -> 1.5.0

- Update `cosmwasm-*` dependencies in Cargo.toml (skip the ones you don't use):
Expand Down
12 changes: 6 additions & 6 deletions packages/std/src/coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ pub struct Coin {
}

impl Coin {
pub fn new(amount: u128, denom: impl Into<String>) -> Self {
pub fn new(amount: impl Into<Uint128>, denom: impl Into<String>) -> Self {
Coin {
amount: Uint128::new(amount),
amount: amount.into(),
denom: denom.into(),
}
}
Expand Down Expand Up @@ -195,16 +195,16 @@ mod tests {

#[test]
fn parse_coin() {
let expected = Coin::new(123, "ucosm");
let expected = Coin::new(123u128, "ucosm");
assert_eq!("123ucosm".parse::<Coin>().unwrap(), expected);
// leading zeroes should be ignored
assert_eq!("00123ucosm".parse::<Coin>().unwrap(), expected);
// 0 amount parses correctly
assert_eq!("0ucosm".parse::<Coin>().unwrap(), Coin::new(0, "ucosm"));
assert_eq!("0ucosm".parse::<Coin>().unwrap(), Coin::new(0u128, "ucosm"));
// ibc denom should work
let ibc_str = "11111ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2";
let ibc_coin = Coin::new(
11111,
11111u128,
"ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2",
);
assert_eq!(ibc_str.parse::<Coin>().unwrap(), ibc_coin);
Expand Down Expand Up @@ -244,7 +244,7 @@ mod tests {

#[test]
fn debug_coin() {
let coin = Coin::new(123, "ucosm");
let coin = Coin::new(123u128, "ucosm");
assert_eq!(format!("{coin:?}"), r#"Coin { 123 "ucosm" }"#);
}
}
2 changes: 1 addition & 1 deletion packages/std/src/ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ mod tests {
let msg = IbcMsg::Transfer {
channel_id: "channel-123".to_string(),
to_address: "my-special-addr".into(),
amount: Coin::new(12345678, "uatom"),
amount: Coin::new(12345678u128, "uatom"),
timeout: IbcTimeout::with_timestamp(Timestamp::from_nanos(1234567890)),
};
let encoded = to_string(&msg).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions packages/std/src/query/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ mod tests {

#[test]
fn private_constructor_works() {
let response = AllBalanceResponse::new(vec![Coin::new(1234, "uatom")]);
let response = AllBalanceResponse::new(vec![Coin::new(1234u128, "uatom")]);
assert_eq!(
response,
AllBalanceResponse {
amount: vec![Coin::new(1234, "uatom")]
amount: vec![Coin::new(1234u128, "uatom")]
}
);
}
Expand Down
8 changes: 4 additions & 4 deletions packages/std/src/query/distribution.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::Addr;
use crate::{Addr, Decimal256};

use super::query_response::QueryResponseType;

Expand Down Expand Up @@ -63,14 +63,14 @@ pub struct DecCoin {
///
/// Some chains have choosen atto (10^-18) for their token's base denomination. If we used `Decimal` here, we could only store
/// 340282366920938463463.374607431768211455atoken which is 340.28 TOKEN.
pub amount: crate::Decimal256,
pub amount: Decimal256,
}

impl DecCoin {
pub fn new(amount: crate::Decimal256, denom: impl Into<String>) -> Self {
pub fn new(amount: impl Into<Decimal256>, denom: impl Into<String>) -> Self {
Self {
denom: denom.into(),
amount,
amount: amount.into(),
}
}
}
Expand Down

0 comments on commit febc0af

Please sign in to comment.