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: correct formatting of decimals in ds-test logs [WIP] #361

Merged
merged 4 commits into from
Jan 5, 2022

Conversation

nanexcool
Copy link
Contributor

log_named_decimal_uint incorrectly displays logs:

    function testDecimals() public {
        emit log_named_decimal_uint("balance", address(this).balance, 18);
        emit log_named_decimal_uint("balance", 12345678, 18);
    }

Before:

Success: testDecimals()

  balance: 79228162514264340000000000000000000000000000000
  balance: 12345678000000000000000000

After:

Success: testDecimals()

  balance: "79228162514.264337593543950335"
  balance: "0.000000000012345678"

ethers-rs parse_units restricts units to u32 making numbers with large decimals like Maker's rad with 45 decimals crash.

`log_named_decimal_uint` incorrectly displays logs:

```
    function testDecimals() public {
        emit log_named_decimal_uint("balance", address(this).balance, 18);
        emit log_named_decimal_uint("balance", 12345678, 18);
    }
```
Before:
```
Success: testDecimals()

  balance: 79228162514264340000000000000000000000000000000
  balance: 12345678000000000000000000
```
After:
```
Success: testDecimals()

  balance: "79228162514.264337593543950335"
  balance: "0.000000000012345678"
```

ethers-rs `parse_units` restricts units to `u32` making numbers with large decimals like Maker's `rad` with 45 decimals crash.
Still need to convert from string to U256 or something... :|
@@ -93,7 +93,7 @@ pub(crate) fn convert_log(log: Log) -> Option<String> {
format!(
"{}: {:?}",
inner.key,
ethers::utils::parse_units(inner.val, inner.decimals.as_u32()).unwrap()
ethers::utils::format_units(inner.val, inner.decimals.as_u32()).unwrap()
Copy link
Collaborator

Choose a reason for hiding this comment

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

We also need this same change a few lines up for LogNamedDecimalIntFilter right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, although that may have to be an ethers-rs fix, getting this error when changing parse_units to format_units

ethers::utils::format_units(inner.val, inner.decimals.as_u32()).unwrap()
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `From<ethers::prelude::I256>` is not implemented for `ethers::prelude::U256`

Copy link
Member

@gakonst gakonst Jan 3, 2022

Choose a reason for hiding this comment

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

Yeah we do not support I256 for format units:

You could do this here by getting the absolute value of the I256, formatting it as string, and then prepending a minus if its sign is a negative number.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Adding support for I256 to ethers-rs is probably the best way to do this right? If so we can merge this PR, and I (or @nanexcool if you'd rather do it) can take a shot at adding I256 support to ethers-rs format_units, then follow up with a PR here to change LogNamedDecimalIntFilter to use format_units

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mds1 I think I'm gonna do a couple of Rust tutorials first instead of heading straight for another PR 😂 Go for it!

Copy link
Collaborator

Choose a reason for hiding this comment

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

sounds good! I've been working my way through rustlings which has been pretty good, but please share any good rust tutorials you find! 😛

@gakonst
Copy link
Member

gakonst commented Jan 3, 2022

@nanexcool sorry to pull you back for a small change but figure you'll benefit from reading that part of the code, you need to make a small change in the test

@nanexcool
Copy link
Contributor Author

@gakonst all good! This is a learning experience for me!

Seems I'm returning a String enclosed in " when it should be a number... will investigate further!

@@ -93,7 +93,7 @@ pub(crate) fn convert_log(log: Log) -> Option<String> {
format!(
"{}: {:?}",
inner.key,
ethers::utils::parse_units(inner.val, inner.decimals.as_u32()).unwrap()
ethers::utils::format_units(inner.val, inner.decimals.as_u32()).unwrap().parse::<f64>().unwrap()
Copy link
Member

Choose a reason for hiding this comment

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

This conversion does not exactly make sense, what do you want to achieve?

format_units takes a number and units and it formats them as a string. The double quotation marks you're seeing are due to the {:?} in the format!, which indicates that it uses the Debug impl of a String. You can change it to format!("{}: {}") and they should be gone.

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, I see! Yes, I was basically trying to remove the double quotation marks. Will change the format and resubmit.

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.

3 participants