Skip to content

Commit 914055c

Browse files
jmg-duarteclaude
andauthored
[TRIVIAL] Migrate order, quote, and validation modules to alloy (#3926)
Large but overall trivial # Description This PR continues the migration from the deprecated `ethcontract` library to the modern `alloy` library by converting order, quote, and validation-related modules to use alloy primitive types (`Address`, `U256`). This is part of a broader effort to modernize the codebase and adopt alloy as the standard Ethereum library, replacing ethcontract's `H160` and `U256` types with their alloy equivalents. # Changes - **Model layer** - [x] Migrate `QuoteAmounts` struct to use `alloy::primitives::U256` in `crates/model/src/order.rs` - [x] Simplify `within_market` method by removing manual byte conversions - [x] Migrate `OrderQuoteRequest`, `OrderQuote`, `OrderQuoteResponse` to use `alloy::primitives::Address` in `crates/model/src/quote.rs` - [x] Update solver competition models to use alloy types - **Shared layer** - [x] Migrate `QuoteParameters`, `Quote`, and `QuoteData` structs to alloy types in `crates/shared/src/order_quoting.rs` - [x] Update quote scaling logic to use `widening_mul` instead of `full_mul` - [x] Migrate `Amounts` struct and validation logic in `crates/shared/src/order_validation.rs` - [x] Update `is_order_outside_market_price` to use typed `widening_mul` calls - [x] Migrate fee calculations, event storage, and price estimation modules - **Orderbook layer** - [x] Update database operations to handle alloy types with proper conversions - [x] Migrate DTOs (auction, order) to use alloy primitives - [x] Update quote API endpoints and quoter logic - [x] Migrate solver competition storage - **Autopilot** - [x] Update onchain order event handling - [x] Migrate quote domain logic - [x] Update run loop to work with alloy types - **E2E tests** - [x] Update all test files to use alloy `Address` creation methods (`with_last_byte` instead of `from_low_u64_be`) - [x] Update test setup infrastructure and onchain components - **Math utilities** - [x] Remove deprecated math functions from solvers util module - [x] Update driver math utilities ## How to test Existing tests --------- Co-authored-by: Claude <[email protected]>
1 parent bcb5e34 commit 914055c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+806
-1092
lines changed

crates/autopilot/src/database/onchain_order_events/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -600,9 +600,9 @@ fn convert_onchain_order_placement(
600600
// executed fast (we don't want to reserve the user's ETH for too long)
601601
if quote.as_ref().is_ok_and(|quote| {
602602
!order_data.within_market(QuoteAmounts {
603-
sell: quote.sell_amount.into_legacy(),
604-
buy: quote.buy_amount.into_legacy(),
605-
fee: quote.fee_amount.into_legacy(),
603+
sell: quote.sell_amount,
604+
buy: quote.buy_amount,
605+
fee: quote.fee_amount,
606606
})
607607
}) {
608608
tracing::debug!(%order_uid, ?owner, "order is outside market price");

crates/autopilot/src/run_loop.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,9 @@ impl RunLoop {
472472
.map(|(index, participant)| SolverSettlement {
473473
solver: participant.driver().name.clone(),
474474
solver_address: participant.solution().solver().0.into_alloy(),
475-
score: Some(Score::Solver(participant.solution().score().get().0)),
475+
score: Some(Score::Solver(
476+
participant.solution().score().get().0.into_alloy(),
477+
)),
476478
ranking: index + 1,
477479
orders: participant
478480
.solution()

crates/driver/src/util/math.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ pub fn mul_ratio_ceil(x: U256, q: U256, d: U256) -> Option<U256> {
2828

2929
// fast path when math in U256 doesn't overflow
3030
if let Some(p) = x.checked_mul(q) {
31-
let (div, rem) = (p / d, p % d);
31+
let (div, rem) = p.div_rem(d);
3232
return div.checked_add(U256::from(!rem.is_zero()));
3333
}
3434

3535
let p = x.widening_mul(q);
3636
let d = U512::from(d);
3737
// SAFETY: at this point !d.is_zero() upholds
38-
let (div, rem) = (p / d, p % d);
38+
let (div, rem) = p.div_rem(d);
3939

4040
let result = U256::uint_try_from(div).ok()?;
4141
result.checked_add(U256::from(!rem.is_zero()))

crates/e2e/src/api/zeroex.rs

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
use {
22
crate::setup::TestAccount,
3-
autopilot::domain::eth::U256,
3+
alloy::primitives::{Address, B256, U256},
44
chrono::{DateTime, NaiveDateTime, Utc},
5-
driver::domain::eth::H256,
65
ethcontract::common::abi::{Token, encode},
6+
ethrpc::alloy::conversions::IntoLegacy,
77
hex_literal::hex,
88
model::DomainSeparator,
9-
shared::{
10-
zeroex_api,
11-
zeroex_api::{Order, OrderMetadata, OrderRecord, ZeroExSignature},
12-
},
9+
shared::zeroex_api::{self, Order, OrderMetadata, OrderRecord, ZeroExSignature},
1310
std::{net::SocketAddr, sync::LazyLock},
1411
warp::{Filter, Reply},
1512
web3::{signing, types::H160},
@@ -55,17 +52,17 @@ impl ZeroExApi {
5552
}
5653

5754
pub struct Eip712TypedZeroExOrder {
58-
pub maker_token: H160,
59-
pub taker_token: H160,
55+
pub maker_token: Address,
56+
pub taker_token: Address,
6057
pub maker_amount: u128,
6158
pub taker_amount: u128,
6259
pub remaining_fillable_taker_amount: u128,
6360
pub taker_token_fee_amount: u128,
64-
pub maker: H160,
65-
pub taker: H160,
66-
pub sender: H160,
67-
pub fee_recipient: H160,
68-
pub pool: H256,
61+
pub maker: Address,
62+
pub taker: Address,
63+
pub sender: Address,
64+
pub fee_recipient: Address,
65+
pub pool: B256,
6966
pub expiry: u64,
7067
pub salt: U256,
7168
}
@@ -85,15 +82,15 @@ impl Eip712TypedZeroExOrder {
8582
Order {
8683
chain_id,
8784
expiry: NaiveDateTime::MAX.and_utc().timestamp() as u64,
88-
fee_recipient: self.fee_recipient,
89-
maker: self.maker,
90-
maker_token: self.maker_token,
85+
fee_recipient: self.fee_recipient.into_legacy(),
86+
maker: self.maker.into_legacy(),
87+
maker_token: self.maker_token.into_legacy(),
9188
maker_amount: self.maker_amount,
92-
pool: self.pool,
93-
salt: self.salt,
94-
sender: self.sender,
95-
taker: self.taker,
96-
taker_token: self.taker_token,
89+
pool: self.pool.into_legacy(),
90+
salt: self.salt.into_legacy(),
91+
sender: self.sender.into_legacy(),
92+
taker: self.taker.into_legacy(),
93+
taker_token: self.taker_token.into_legacy(),
9794
taker_amount: self.taker_amount,
9895
taker_token_fee_amount: self.taker_token_fee_amount,
9996
verifying_contract,
@@ -131,18 +128,18 @@ impl Eip712TypedZeroExOrder {
131128
fn hash_struct(&self) -> [u8; 32] {
132129
let mut hash_data = [0u8; 416];
133130
hash_data[0..32].copy_from_slice(&Self::ZEROEX_LIMIT_ORDER_TYPEHASH);
134-
hash_data[44..64].copy_from_slice(self.maker_token.as_fixed_bytes());
135-
hash_data[76..96].copy_from_slice(self.taker_token.as_fixed_bytes());
131+
hash_data[44..64].copy_from_slice(self.maker_token.as_slice());
132+
hash_data[76..96].copy_from_slice(self.taker_token.as_slice());
136133
hash_data[112..128].copy_from_slice(&self.maker_amount.to_be_bytes());
137134
hash_data[144..160].copy_from_slice(&self.taker_amount.to_be_bytes());
138135
hash_data[176..192].copy_from_slice(&self.taker_token_fee_amount.to_be_bytes());
139-
hash_data[204..224].copy_from_slice(self.maker.as_fixed_bytes());
140-
hash_data[236..256].copy_from_slice(self.taker.as_fixed_bytes());
141-
hash_data[268..288].copy_from_slice(self.sender.as_fixed_bytes());
142-
hash_data[300..320].copy_from_slice(self.fee_recipient.as_fixed_bytes());
143-
hash_data[320..352].copy_from_slice(self.pool.as_fixed_bytes());
136+
hash_data[204..224].copy_from_slice(self.maker.as_slice());
137+
hash_data[236..256].copy_from_slice(self.taker.as_slice());
138+
hash_data[268..288].copy_from_slice(self.sender.as_slice());
139+
hash_data[300..320].copy_from_slice(self.fee_recipient.as_slice());
140+
hash_data[320..352].copy_from_slice(self.pool.as_slice());
144141
hash_data[376..384].copy_from_slice(&self.expiry.to_be_bytes());
145-
self.salt.to_big_endian(&mut hash_data[384..416]);
142+
hash_data[384..416].copy_from_slice(&self.salt.to_be_bytes::<32>());
146143
signing::keccak256(&hash_data)
147144
}
148145
}

0 commit comments

Comments
 (0)