Skip to content

Conversation

@jmg-duarte
Copy link
Contributor

Description

Migrates the NonZero type for U256 to alloy

Changes

  • Renames U256 -> NonZeroU256 to avoid aliases and confusion
  • Migrates a bunch of to_wei calls to a more verbose but explicit calculation
  • Refactors call sites to alloy

How to test

Existing tests

@jmg-duarte jmg-duarte requested a review from a team as a code owner December 11, 2025 10:37
Base automatically changed from jmgd/alloy/primitive-types-autopilot to main December 11, 2025 10:41
Copy link
Contributor

@m-sz m-sz left a 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

Copy link
Contributor

@MartinquaXD MartinquaXD left a 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}"),
        }
    }
}

Copy link
Contributor

@MartinquaXD MartinquaXD left a 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.

@jmg-duarte jmg-duarte added this pull request to the merge queue Dec 11, 2025
Merged via the queue into main with commit 961583d Dec 11, 2025
18 checks passed
@jmg-duarte jmg-duarte deleted the jmgd/alloy/nonzero branch December 11, 2025 14:30
@github-actions github-actions bot locked and limited conversation to collaborators Dec 11, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants