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

fix: add display_currency_decimal method #3445

Merged

Conversation

therustmonk
Copy link
Contributor

@therustmonk therustmonk commented Oct 11, 2021

Description

The inner type of Tari changed from f64 to decimal_rs::Decimal.
Also, display_currency method was replaced with format_currency that adds separators to formatted strings.

Motivation and Context

display_currency method forces us to use f64 that is not accurate for finance calculations.

How Has This Been Tested?

Updated unit tests.
TODO: Smoke test: run the wallet and the node.

@therustmonk
Copy link
Contributor Author

The experiment commit contains Trai(Decimal) type.
It still uses f64 all around, but we can replace it with Decimals. It should work.

Or how about removing Tart(f64) completely and use MiniTari everywhere with some special formatting methods?


newtype_ops! { [Tari] {add sub} {:=} Self Self }
newtype_ops! { [Tari] {mul div rem} {:=} Self f64 }
#[derive(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newtype-ops looks unmaintained. How about using derive_more instead?

Copy link
Contributor

@delta1 delta1 Oct 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah if we're already using derive_more now and it's easy to remove newtype_ops I think that's fine

@therustmonk
Copy link
Contributor Author

It's also possible to remove Tari type completely and use MicroTari with a special formatted like this:

pub struct TariFormat {
    MicroTari,
    Tari,
    Auto,
}

pub struct FormattedTari {
    format: TariFormat,
    separator: Option<char>,
}

impl MicroTari {
    pub fn formatted(&self, format: TariFormat, separator: Option<char>) -> FormattedTari {
        FormattedTari {
            format,
            separator,
        }
    }
}

impl Display for FormattedTari { /* etc. */ }

What do you think?

@therustmonk therustmonk marked this pull request as ready for review October 13, 2021 13:54
Copy link
Contributor

@delta1 delta1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good, just a few small things

Comment on lines 47 to 48
#[error("Tari value convert error `{0}`")]
TariConvertError(#[from] TariConvertError),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[error("Tari value convert error `{0}`")]
TariConvertError(#[from] TariConvertError),
#[error("Tari value conversion error `{0}`")]
TariConversionError(#[from] TariConversionError),

Err(MicroTariError::ParseError("value cannot be negative".to_string()))
} else {
Ok(MicroTari::from(Tari::from(v.max(0.0))))
// TODO: Check. It can't be `NaN` anymore. Still we need `.max(0.0)` check?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this comment still current?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No idea, why we compare the unsigned value with 0.0 🤷‍♂️ I left the comment to double-check it together. How about removing that max call?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you did remove the max? Which is why I ask :)

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right, it's another one. I've removed it, because if a value is not less than 0.0 (greater or equal 0.0) it's always a win in max(0.0) comparison. I guess we had it to handle the f64::NaN case, but Decimal doesn't support NaN values at all and that comparison doesn't make sense anymore. If I'm correct about why we had that max(0.0) call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

impl From<Tari> for MicroTari {
fn from(v: Tari) -> Self {
MicroTari((v.0 * 1e6) as u64)
MicroTari((v.0 * 1_000_000u32).try_into().unwrap())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unwrap?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the past, the value could be truncated silently, I thought it's better to panic here, but probably even better to replace the trait to TryFrom.


newtype_ops! { [Tari] {add sub} {:=} Self Self }
newtype_ops! { [Tari] {mul div rem} {:=} Self f64 }
#[derive(
Copy link
Contributor

@delta1 delta1 Oct 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah if we're already using derive_more now and it's easy to remove newtype_ops I think that's fine

v.0
}
}
pub type TariConvertError = DecimalConvertError;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub type TariConvertError = DecimalConvertError;
pub type TariConversionError = DecimalConvertError;

Copy link
Collaborator

@stringhandler stringhandler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would have preferred to remove this function completely, but this is an improvement

@aviator-app aviator-app bot merged commit 1f52ffc into tari-project:development Oct 14, 2021
sdbondi added a commit to sdbondi/tari that referenced this pull request Oct 15, 2021
* development:
  fix: add display_currency_decimal method (tari-project#3445)
  fix: add sanity checks to prepare_new_block (tari-project#3448)
  test: profile wallet sqlite database operations (tari-project#3451)
  test: create cucumber test directory if necessary (tari-project#3452)
  chore: improve cucumber tags and run time speed (tari-project#3439)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants