Skip to content

Conversation

@jmg-duarte
Copy link
Contributor

Description

Migrate the native price estimation endpoint and trait to alloy

Changes

  • Migrates v1/token/{addr}/native_price endpoint to alloy
  • Migrates the NativePriceEstimating trait to alloy
  • Refactors the tests and rest of codebase

How to test

Existing tests

@jmg-duarte jmg-duarte requested a review from a team as a code owner November 7, 2025 12:15
Copy link
Contributor

@squadgazzz squadgazzz left a comment

Choose a reason for hiding this comment

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

LGTM assuming the new conversion logic is tested.

Comment on lines +285 to 287
fn token(u: u64) -> Address {
Address::left_padding_from(&u.to_be_bytes())
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I am always afraid of this kind of changes. Did you test it somehow?

Copy link
Contributor

Choose a reason for hiding this comment

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

left_padding_from docs

Create a new byte array from the given slice src, left-padding it with zeroes if necessary.

Note
The given bytes are interpreted in big endian order.

Panics
Panics if src.len() > N.

H160::from_low_u64_be behaviour, where B is BigEndian

fn from_low_u64_with_byteorder<B>(val: u64) -> Self
where
	B: $crate::byteorder::ByteOrder,
{
	let mut buf = [0x0; 8];
	B::write_u64(&mut buf, val);
	let capped = $crate::core_::cmp::min(Self::len_bytes(), 8);
	let mut bytes = [0x0; $crate::core_::mem::size_of::<Self>()];
	bytes[(Self::len_bytes() - capped)..].copy_from_slice(&buf[..capped]);
	Self::from_slice(&bytes)
}

The byte order is preserved and H160's function 0 pads the result to the desired size.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This runs just fine

use alloy::primitives::Address;
use ethcontract::H160;

fn main() {
    assert_eq!(
        Address::left_padding_from(&1024u64.to_be_bytes()).0.0,
        H160::from_low_u64_be(1024u64).0
    );
}

As an extra note, in certain places I use "with_last_byte" instead because the number fits in a byte, thus it doesn't need the fancy u64::to_be_bytes(), etc

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.

LGTM

Comment on lines +285 to 287
fn token(u: u64) -> Address {
Address::left_padding_from(&u.to_be_bytes())
}
Copy link
Contributor

Choose a reason for hiding this comment

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

left_padding_from docs

Create a new byte array from the given slice src, left-padding it with zeroes if necessary.

Note
The given bytes are interpreted in big endian order.

Panics
Panics if src.len() > N.

H160::from_low_u64_be behaviour, where B is BigEndian

fn from_low_u64_with_byteorder<B>(val: u64) -> Self
where
	B: $crate::byteorder::ByteOrder,
{
	let mut buf = [0x0; 8];
	B::write_u64(&mut buf, val);
	let capped = $crate::core_::cmp::min(Self::len_bytes(), 8);
	let mut bytes = [0x0; $crate::core_::mem::size_of::<Self>()];
	bytes[(Self::len_bytes() - capped)..].copy_from_slice(&buf[..capped]);
	Self::from_slice(&bytes)
}

The byte order is preserved and H160's function 0 pads the result to the desired size.

Base automatically changed from jmgd/alloy/quote-dto to main November 10, 2025 10:36
@jmg-duarte jmg-duarte enabled auto-merge November 10, 2025 11:25
@jmg-duarte jmg-duarte added this pull request to the merge queue Nov 10, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Nov 10, 2025
@jmg-duarte jmg-duarte added this pull request to the merge queue Nov 10, 2025
Merged via the queue into main with commit 29137fe Nov 10, 2025
18 checks passed
@jmg-duarte jmg-duarte deleted the jmgd/alloy/native-price branch November 10, 2025 12:01
@github-actions github-actions bot locked and limited conversation to collaborators Nov 10, 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