-
Notifications
You must be signed in to change notification settings - Fork 154
Migrate NonZeroU256 to alloy #3967
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
Conversation
bde4296 to
49def68
Compare
m-sz
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure about the inconsistencies with using to_wei -> U256::from(10).pow(U256::from(18)) instead of using eth
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a huge fan of the introduction of alloy::utils::Unit. If there is already the effort being made to change all those values I think something like this would make the most sense.
That way we can just do 12.mwei() and 1234.44.eth().
use alloy::primitives::{
U256,
ruint::uint,
utils::{ParseUnits, parse_units},
};
trait Unit: std::marker::Sized {
fn wei(self) -> U256;
fn mwei(self) -> U256 {
const BASE: U256 = uint!(1_000_000_U256);
self.wei() * BASE
}
fn gwei(self) -> U256 {
const BASE: U256 = uint!(1_000_000_000_U256);
self.wei() * BASE
}
fn eth(self) -> U256 {
const BASE: U256 = uint!(1_000_000_000_000_000_000_U256);
self.wei() * BASE
}
}
impl Unit for u64 {
fn wei(self) -> U256 {
U256::from(self)
}
}
impl Unit for u128 {
fn wei(self) -> U256 {
U256::from(self)
}
}
impl Unit for f64 {
fn wei(self) -> U256 {
match parse_units(&self.to_string(), "wei").unwrap() {
ParseUnits::U256(val) => val,
_ => panic!("could not parse number as u256: {self}"),
}
}
fn mwei(self) -> U256 {
match parse_units(&self.to_string(), "mwei").unwrap() {
ParseUnits::U256(val) => val,
_ => panic!("could not parse number as u256: {self}"),
}
}
fn gwei(self) -> U256 {
match parse_units(&self.to_string(), "gwei").unwrap() {
ParseUnits::U256(val) => val,
_ => panic!("could not parse number as u256: {self}"),
}
}
fn eth(self) -> U256 {
match parse_units(&self.to_string(), "ether").unwrap() {
ParseUnits::U256(val) => val,
_ => panic!("could not parse number as u256: {self}"),
}
}
}
MartinquaXD
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine with unifying the number values in a follow up PR.
Description
Migrates the NonZero type for U256 to alloy
Changes
How to test
Existing tests