diff --git a/CHANGELOG.md b/CHANGELOG.md index c1b90334d..d59e998e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ### Added ### Changed +- [\#293](https://github.com/Manta-Network/manta-rs/pull/293) Add decimals argument to AssetMetadata display ### Deprecated diff --git a/manta-accounting/src/asset.rs b/manta-accounting/src/asset.rs index 3e4b54274..10111a30b 100644 --- a/manta-accounting/src/asset.rs +++ b/manta-accounting/src/asset.rs @@ -1073,29 +1073,27 @@ pub struct AssetMetadata { } impl AssetMetadata { - /// Returns a string formatting of only the `value` interpreted using `self` as the metadata. + /// Returns a string formatting of only the `value` with `digits` fractional digits, + /// interpreted using `self` as the metadata. #[inline] - pub fn display_value(&self, value: V) -> String + pub fn display_value(&self, value: V, digits: u32) -> String where for<'v> &'v V: Div, { - // TODO: What if we want more than three `FRACTIONAL_DIGITS`? How do we make this method - // more general? - const FRACTIONAL_DIGITS: u32 = 3; let value_base_units = &value / (10u128.pow(self.decimals)); - let fractional_digits = &value / (10u128.pow(self.decimals - FRACTIONAL_DIGITS)) - % (10u128.pow(FRACTIONAL_DIGITS)); - format!("{value_base_units}.{fractional_digits:0>3}") + let fractional_digits = + &value / (10u128.pow(self.decimals - digits)) % (10u128.pow(digits)); + format!("{value_base_units}.{fractional_digits}") } - /// Returns a string formatting of `value` interpreted using `self` as the metadata including - /// the symbol. + /// Returns a string formatting of `value` with `digits` fractional digits, interpreted using + /// `self` as the metadata including the symbol. #[inline] - pub fn display(&self, value: V) -> String + pub fn display(&self, value: V, digits: u32) -> String where for<'v> &'v V: Div, { - format!("{} {}", self.display_value(value), self.symbol) + format!("{} {}", self.display_value(value, digits), self.symbol) } }