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

feat(cast): add flag equivalents of parseUnits, formatUnits #9165

Merged
merged 9 commits into from
Oct 24, 2024

Conversation

kien6034
Copy link
Contributor

Motivation

Closes #9030

Solution

Add two new functions to support arbitrary decimal places in unit conversions:

  • parse_units: Convert human-readable numbers to their smallest unit representation
  • format_units: Convert numbers from smallest unit back to human-readable format

Added test cases for common scenarios:

  • USDC (6 decimals)
  • Custom tokens (12 decimals)
  • Other decimal places (3 decimals)

Copy link
Collaborator

@grandizzy grandizzy left a comment

Choose a reason for hiding this comment

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

thank you! could you please add tests for these new options, you can see examples of usint casttest! macro here https://github.com/foundry-rs/foundry/blob/master/crates/cast/tests/cli/main.rs
Also mind that "fu" alias is already used for from-utf8

cast fu --help
Convert UTF8 text to hex

Usage: cast from-utf8 [TEXT]

hence tests are failing (see https://github.com/foundry-rs/foundry/blob/master/CONTRIBUTING.md#resolving-an-issue on how to run them locally)

@kien6034
Copy link
Contributor Author

thank you for reviewing @grandizzy. im not at home currently, will update tonight

@kien6034 kien6034 requested a review from grandizzy October 23, 2024 17:56
@kien6034
Copy link
Contributor Author

Hello @grandizzy , i updated the allias for formatUnits, added the tests and checked lint and testing on local

@kien6034
Copy link
Contributor Author

image tests pass locally, the failing tests are flaky i assume

Copy link
Collaborator

@grandizzy grandizzy left a comment

Choose a reason for hiding this comment

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

looks good, thanks! added comments re minor nits

crates/cast/src/lib.rs Outdated Show resolved Hide resolved
crates/cast/tests/cli/main.rs Outdated Show resolved Hide resolved
@kien6034
Copy link
Contributor Author

@grandizzy tks. Updated

@grandizzy
Copy link
Collaborator

oops, sorry, my suggestion to rename test broke it :) will revert

@kien6034
Copy link
Contributor Author

kien6034 commented Oct 24, 2024

@grandizzy no, i was my mistake, i change the function name incorrectly. Sry about that

Copy link
Collaborator

@grandizzy grandizzy left a comment

Choose a reason for hiding this comment

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

lgtm, thank you! @zerosnacks @yash-atreya mind review it too?

Copy link
Member

@zerosnacks zerosnacks left a comment

Choose a reason for hiding this comment

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

Thanks @kien6034 for your PR, proposed feature LGTM

Tested it with a wide range of numbers and it works well

Noticed that it does not specifically handle hexadecimal numbers but that is OK as it is out of the scope of this ticket

@grandizzy grandizzy merged commit bcef905 into foundry-rs:master Oct 24, 2024
21 checks passed
rplusq pushed a commit to rplusq/foundry that referenced this pull request Nov 29, 2024
…y-rs#9165)

* feat: base func for parseunints and formatuints

* test: update doc and tests

* fix: function alias

* test: cast test

* refacctor: helper fucnction

* Update crates/cast/tests/cli/main.rs

* revert: format uints function cattest func name

---------

Co-authored-by: grandizzy <[email protected]>
@grandizzy grandizzy added T-feature Type: feature C-cast Command: cast labels Dec 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-cast Command: cast T-feature Type: feature
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

feat(cast): add flag equivalents of parseUnits, formatUnits
3 participants