Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asset display fix #296

Merged
merged 10 commits into from
Dec 22, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion manta-accounting/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
15 changes: 12 additions & 3 deletions manta-accounting/src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
use alloc::{
collections::btree_map::{BTreeMap, Entry as BTreeMapEntry},
format,
string::String,
string::{String, ToString},
vec,
vec::Vec,
};
Expand All @@ -40,6 +40,7 @@ use core::{
};
use derive_more::{Display, From};
use manta_crypto::{
arkworks::std,
constraint::{HasInput, Input},
eclair::{
self,
Expand All @@ -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,
};
Expand Down Expand Up @@ -1079,11 +1080,17 @@ impl AssetMetadata {
pub fn display_value<V>(&self, value: V, digits: u32) -> String
where
for<'v> &'v V: Div<u128, Output = u128>,
V: manta_crypto::arkworks::std::fmt::Display,
V: std::ops::Sub<u128, Output = u128>,
{
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
Expand All @@ -1092,6 +1099,8 @@ impl AssetMetadata {
pub fn display<V>(&self, value: V, digits: u32) -> String
where
for<'v> &'v V: Div<u128, Output = u128>,
V: manta_crypto::arkworks::std::fmt::Display,
V: std::ops::Sub<u128, Output = u128>,
{
format!("{} {}", self.display_value(value, digits), self.symbol)
}
Expand Down