From 39dc5115779862c41d8d3fc238295b90476d76d2 Mon Sep 17 00:00:00 2001 From: Christoph Otter Date: Mon, 22 May 2023 10:26:28 +0200 Subject: [PATCH] Use Coin's FromStr impl in FromStr impl of Coins --- packages/std/src/coins.rs | 32 ++------------------------------ 1 file changed, 2 insertions(+), 30 deletions(-) diff --git a/packages/std/src/coins.rs b/packages/std/src/coins.rs index 6be7af358a..27b3196009 100644 --- a/packages/std/src/coins.rs +++ b/packages/std/src/coins.rs @@ -80,37 +80,9 @@ impl FromStr for Coins { type Err = StdError; fn from_str(s: &str) -> StdResult { - // TODO: use FromStr impl for Coin once it's merged - - // Parse a string into a `Coin`. - // - // Parsing the string with regex doesn't work, because the resulting - // wasm binary would be too big from including the `regex` library. - // - // We opt for the following solution: enumerate characters in the string, - // and break before the first non-number character. Split the string at - // that index. - // - // This assumes the denom never starts with a number, which is the case: - // https://github.com/cosmos/cosmos-sdk/blob/v0.46.0/types/coin.go#L854-L856 - let parse_coin_str = |s: &str| -> StdResult { - for (i, c) in s.chars().enumerate() { - if !c.is_ascii_digit() { - let amount = Uint128::from_str(&s[..i])?; - let denom = String::from(&s[i..]); - return Ok(Coin { amount, denom }); - } - } - - Err(StdError::parse_err( - type_name::(), - format!("invalid coin string: {s}"), - )) - }; - s.split(',') - .map(parse_coin_str) - .collect::>>()? + .map(Coin::from_str) + .collect::, _>>()? .try_into() } }