Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/iroha_cli/CommandLineHelp.md
Original file line number Diff line number Diff line change
Expand Up @@ -656,13 +656,13 @@ Retrieve details of a specific asset definition

Register an asset definition

**Usage:** `iroha asset definition register [OPTIONS] --id <ID> --spec <SPEC>`
**Usage:** `iroha asset definition register [OPTIONS] --id <ID>`

###### **Options:**

* `-i`, `--id <ID>` — Asset definition in the format "asset#domain"
* `-m`, `--mint-once` — Disables minting after the first instance
* `-s`, `--spec <SPEC>` — Numeric spec of the asset
* `-s`, `--scale <SCALE>` — Numeric scale of the asset. No value means unconstrained



Expand Down
6 changes: 3 additions & 3 deletions crates/iroha_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ mod asset {
context.print_data(&entry)
}
Register(args) => {
let mut entry = AssetDefinition::new(args.id, args.spec);
let mut entry = AssetDefinition::new(args.id, args.scale.into());
if args.mint_once {
entry = entry.mintable_once();
}
Expand Down Expand Up @@ -912,9 +912,9 @@ mod asset {
/// Disables minting after the first instance
#[arg(short, long)]
pub mint_once: bool,
/// Numeric spec of the asset
/// Numeric scale of the asset. No value means unconstrained.
#[arg(short, long)]
pub spec: NumericSpec,
pub scale: Option<u32>,
}

#[derive(clap::Args, Debug)]
Expand Down
77 changes: 4 additions & 73 deletions crates/iroha_numeric/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ extern crate alloc;
use alloc::{format, string::String, string::ToString, vec, vec::Vec};
use core::str::FromStr;

use derive_more::Display;
use derive_more::{Display, From};
use parity_scale_codec::{Decode, Encode};
use rust_decimal::{prelude::ToPrimitive, Decimal};
use serde::{Deserialize, Serialize};
use serde_with::{DeserializeFromStr, SerializeDisplay};

/// Decimal number with arbitrary precision and scale.
///
Expand Down Expand Up @@ -45,8 +44,9 @@ pub struct Numeric {
Ord,
Default,
Hash,
SerializeDisplay,
DeserializeFromStr,
From,
Deserialize,
Serialize,
Encode,
Decode,
iroha_schema::IntoSchema,
Expand Down Expand Up @@ -348,35 +348,6 @@ impl core::str::FromStr for Numeric {
}
}

impl core::str::FromStr for NumericSpec {
type Err = NumericSpecParseError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
// Valid formats:
// Numeric
// Numeric(scale)

// Trim Numeric prefix return error if not present
let Some(s) = s.strip_prefix("Numeric") else {
return Err(NumericSpecParseError::StartWithNumeric);
};

let scale = if s.is_empty() {
None
} else {
// Trim braces
let Some(s) = s.strip_prefix('(').and_then(|s| s.strip_suffix(')')) else {
return Err(NumericSpecParseError::WrappedInBraces);
};

// Parse scale
Some(s.parse().map_err(NumericSpecParseError::InvalidScale)?)
};

Ok(NumericSpec { scale })
}
}

impl core::fmt::Display for NumericSpec {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, "Numeric")?;
Expand Down Expand Up @@ -563,44 +534,4 @@ mod tests {

assert_eq!(num1, num2);
}

#[test]
fn check_numeric_scale_from_str() {
// Valid representations
assert_eq!(NumericSpec { scale: None }, "Numeric".parse().unwrap());
assert_eq!(
NumericSpec { scale: Some(0) },
"Numeric(0)".parse().unwrap()
);
assert_eq!(
NumericSpec { scale: Some(42) },
"Numeric(42)".parse().unwrap()
);

// Invalid representations
assert!(matches!(
"RandomString".parse::<NumericSpec>().unwrap_err(),
NumericSpecParseError::StartWithNumeric
));
assert!(matches!(
"Numeric%123%".parse::<NumericSpec>().unwrap_err(),
NumericSpecParseError::WrappedInBraces
));
assert!(matches!(
"Numeric(123".parse::<NumericSpec>().unwrap_err(),
NumericSpecParseError::WrappedInBraces
));
assert!(matches!(
"Numeric123)".parse::<NumericSpec>().unwrap_err(),
NumericSpecParseError::WrappedInBraces
));
assert!(matches!(
"Numeric(NaN)".parse::<NumericSpec>().unwrap_err(),
NumericSpecParseError::InvalidScale(_)
));
assert!(matches!(
"Numeric(-1)".parse::<NumericSpec>().unwrap_err(),
NumericSpecParseError::InvalidScale(_)
));
}
}
8 changes: 6 additions & 2 deletions defaults/genesis.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@
"Register": {
"AssetDefinition": {
"id": "rose#wonderland",
"spec": "Numeric",
"spec": {
"scale": null
},
"mintable": "Infinitely",
"logo": null,
"metadata": {}
Expand Down Expand Up @@ -87,7 +89,9 @@
"Register": {
"AssetDefinition": {
"id": "cabbage#garden_of_live_flowers",
"spec": "Numeric",
"spec": {
"scale": null
},
"mintable": "Infinitely",
"logo": null,
"metadata": {}
Expand Down
Loading