diff --git a/CHANGELOG.md b/CHANGELOG.md index d59e998e7..d81285ec8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ### Removed ### Fixed +- [\#296](https://github.com/Manta-Network/manta-rs/pull/296) Fix AssetMetadata display for values less than 1 ### Security diff --git a/manta-accounting/Cargo.toml b/manta-accounting/Cargo.toml index c6adeaf2e..f6e8290df 100644 --- a/manta-accounting/Cargo.toml +++ b/manta-accounting/Cargo.toml @@ -61,7 +61,7 @@ derivative = { version = "2.2.0", default-features = false, features = ["use_cor derive_more = { version = "0.99.16", default-features = false, features = ["add", "add_assign", "display", "from", "sum"] } futures = { version = "0.3.21", optional = true, default-features = false, features = ["alloc"] } indexmap = { version = "1.8.0", optional = true, default-features = false } -manta-crypto = { path = "../manta-crypto", default-features = false } +manta-crypto = { path = "../manta-crypto", default-features = false, features = ["arkworks"] } manta-util = { path = "../manta-util", default-features = false, features = ["alloc"] } parking_lot = { version = "0.12.0", optional = true, default-features = false } rand_chacha = { version = "0.3.1", optional = true, default-features = false } diff --git a/manta-accounting/src/asset.rs b/manta-accounting/src/asset.rs index 10111a30b..91fc146a0 100644 --- a/manta-accounting/src/asset.rs +++ b/manta-accounting/src/asset.rs @@ -26,7 +26,7 @@ use alloc::{ collections::btree_map::{BTreeMap, Entry as BTreeMapEntry}, format, - string::String, + string::{String, ToString}, vec, vec::Vec, }; @@ -40,6 +40,7 @@ use core::{ }; use derive_more::{Display, From}; use manta_crypto::{ + arkworks::std, constraint::{HasInput, Input}, eclair::{ self, @@ -64,7 +65,7 @@ use manta_util::{ use manta_util::serde::{Deserialize, Serialize}; #[cfg(feature = "std")] -use std::{ +use self::std::{ collections::hash_map::{Entry as HashMapEntry, HashMap, RandomState}, hash::BuildHasher, }; @@ -1079,11 +1080,17 @@ impl AssetMetadata { pub fn display_value(&self, value: V, digits: u32) -> String where for<'v> &'v V: Div, + V: manta_crypto::arkworks::std::fmt::Display, + V: std::ops::Sub, { let value_base_units = &value / (10u128.pow(self.decimals)); let fractional_digits = &value / (10u128.pow(self.decimals - digits)) % (10u128.pow(digits)); - format!("{value_base_units}.{fractional_digits}") + + let decimals: u128 = value - (value_base_units * 10u128.pow(digits)); + let decimals_length: u32 = decimals.to_string().len().try_into().unwrap(); + let leading_zeros = "0".repeat((digits - decimals_length).try_into().unwrap()); + format!("{value_base_units}.{leading_zeros}{fractional_digits}") } /// Returns a string formatting of `value` with `digits` fractional digits, interpreted using @@ -1092,6 +1099,8 @@ impl AssetMetadata { pub fn display(&self, value: V, digits: u32) -> String where for<'v> &'v V: Div, + V: manta_crypto::arkworks::std::fmt::Display, + V: std::ops::Sub, { format!("{} {}", self.display_value(value, digits), self.symbol) }