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

[Feature] Macros to parse Ether and other units from string to U256 #787

Open
cakevm opened this issue Oct 25, 2024 · 3 comments
Open

[Feature] Macros to parse Ether and other units from string to U256 #787

cakevm opened this issue Oct 25, 2024 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@cakevm
Copy link

cakevm commented Oct 25, 2024

Component

primitives

Describe the feature you would like

Often you have fixed values that could already be parsed during compile time. Currently I am initializing those values using lazy_static. My proposal would be to have macros to parse ether, wei, etc. from a string to U256.

Currently you will write:

let val: U256 = parse_units("0.01", "ether").unwrap().get_absolute();

Proposal:

let val: U256 = ether!("0.01");

If the parsing fails it will throw an error during compile time.

Additional context

No response

@cakevm cakevm added the enhancement New feature or request label Oct 25, 2024
@makcandrov
Copy link

if you still need it I open sourced my implementation of these macros: alloy-unit-macros

@DaniPopes
Copy link
Member

In the future we will be able to make more and more of the primitives const fn as more standard library methods and traits do, which will include the utils functions.

For now, you can use the uint! macro or the crate linked above

@jenpaff jenpaff added this to Alloy Mar 4, 2025
@github-project-automation github-project-automation bot moved this to Todo in Alloy Mar 4, 2025
@jenpaff jenpaff added this to the v1.0 milestone Mar 4, 2025
@DaniPopes
Copy link
Member

Reviewed this again, currently still not possible as it would require a lot more const fns in ruint, which are blocked by const traits.

@DaniPopes DaniPopes removed this from the v1.0 milestone Mar 5, 2025
@DaniPopes DaniPopes moved this from Todo to Blocked in Alloy Mar 10, 2025
@DaniPopes DaniPopes moved this from Blocked to In Progress in Alloy Mar 10, 2025
@DaniPopes DaniPopes moved this from In Progress to Blocked in Alloy Mar 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Blocked
Development

No branches or pull requests

5 participants
@jenpaff @DaniPopes @makcandrov @cakevm and others