From 052e3b34641b82acde9c051fce437511c7337add Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=CC=81=20Duarte?= Date: Thu, 11 Dec 2025 15:07:15 +0000 Subject: [PATCH 1/4] Unify eth monetary units --- Cargo.lock | 1 + .../src/domain/competition/solution/fee.rs | 54 ++--- .../e2e/src/setup/onchain_components/mod.rs | 18 +- crates/e2e/tests/e2e/app_data.rs | 42 ++-- crates/e2e/tests/e2e/app_data_signer.rs | 33 ++- crates/e2e/tests/e2e/autopilot_leader.rs | 33 ++- crates/e2e/tests/e2e/banned_users.rs | 24 +- crates/e2e/tests/e2e/buffers.rs | 31 ++- crates/e2e/tests/e2e/cow_amm.rs | 100 ++++---- crates/e2e/tests/e2e/eth_integration.rs | 37 +-- crates/e2e/tests/e2e/eth_safe.rs | 36 ++- crates/e2e/tests/e2e/ethflow.rs | 56 +++-- crates/e2e/tests/e2e/hooks.rs | 109 +++++---- crates/e2e/tests/e2e/jit_orders.rs | 36 +-- crates/e2e/tests/e2e/limit_orders.rs | 229 ++++++++++-------- crates/e2e/tests/e2e/liquidity.rs | 9 +- .../e2e/liquidity_source_notification.rs | 13 +- crates/e2e/tests/e2e/order_cancellation.rs | 26 +- crates/e2e/tests/e2e/partial_fill.rs | 15 +- .../tests/e2e/partially_fillable_balance.rs | 43 ++-- .../e2e/tests/e2e/partially_fillable_pool.rs | 43 ++-- .../e2e/tests/e2e/place_order_with_quote.rs | 23 +- crates/e2e/tests/e2e/protocol_fee.rs | 139 +++++++---- crates/e2e/tests/e2e/quote_verification.rs | 71 +++--- crates/e2e/tests/e2e/quoting.rs | 78 +++--- crates/e2e/tests/e2e/refunder.rs | 13 +- crates/e2e/tests/e2e/replace_order.rs | 136 ++++++----- crates/e2e/tests/e2e/smart_contract_orders.rs | 43 ++-- crates/e2e/tests/e2e/solver_competition.rs | 92 ++++--- .../tests/e2e/solver_participation_guard.rs | 49 ++-- crates/e2e/tests/e2e/submission.rs | 23 +- .../tests/e2e/tracking_insufficient_funds.rs | 39 +-- crates/e2e/tests/e2e/uncovered_order.rs | 23 +- crates/e2e/tests/e2e/univ2.rs | 27 ++- crates/e2e/tests/e2e/vault_balances.rs | 27 ++- crates/e2e/tests/e2e/wrapper.rs | 17 +- crates/number/src/lib.rs | 1 + crates/number/src/units.rs | 66 +++++ crates/orderbook/src/quoter.rs | 55 ++--- crates/solvers/Cargo.toml | 1 + .../src/boundary/liquidity/limit_order.rs | 41 ++-- 41 files changed, 1141 insertions(+), 811 deletions(-) create mode 100644 crates/number/src/units.rs diff --git a/Cargo.lock b/Cargo.lock index ec6865bf74..58f7e10270 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6519,6 +6519,7 @@ dependencies = [ "mimalloc", "model", "num", + "number", "observe", "prometheus", "prometheus-metric-storage", diff --git a/crates/driver/src/domain/competition/solution/fee.rs b/crates/driver/src/domain/competition/solution/fee.rs index dd1329a6ec..9fbc112c7f 100644 --- a/crates/driver/src/domain/competition/solution/fee.rs +++ b/crates/driver/src/domain/competition/solution/fee.rs @@ -312,19 +312,19 @@ pub enum Error { // todo: should be removed once integration tests are implemented #[cfg(test)] mod tests { - use super::*; + use {super::*, number::units::EthUnit}; #[test] fn test_adjust_quote_to_out_market_sell_order_limits() { let order = Order { - sell_amount: to_wei(20), - buy_amount: to_wei(19), + sell_amount: 20u64.eth(), + buy_amount: 19u64.eth(), side: Side::Sell, }; let quote = Quote { - sell_amount: to_wei(21), - buy_amount: to_wei(18), - fee_amount: to_wei(1), + sell_amount: 21u64.eth(), + buy_amount: 18u64.eth(), + fee_amount: 1u64.eth(), }; let limit = adjust_quote_to_order_limits(order.clone(), quote).unwrap(); @@ -334,7 +334,7 @@ mod tests { ); assert_eq!( limit.buy.0, - to_wei(19), + 19u64.eth(), "Buy amount should be equal to order buy amount for out of market orders" ); } @@ -342,14 +342,14 @@ mod tests { #[test] fn test_adjust_quote_to_out_market_buy_order_limits() { let order = Order { - sell_amount: to_wei(20), - buy_amount: to_wei(19), + sell_amount: 20u64.eth(), + buy_amount: 19u64.eth(), side: Side::Buy, }; let quote = Quote { - sell_amount: to_wei(21), - buy_amount: to_wei(18), - fee_amount: to_wei(1), + sell_amount: 21u64.eth(), + buy_amount: 18u64.eth(), + fee_amount: 1u64.eth(), }; let limit = adjust_quote_to_order_limits(order.clone(), quote).unwrap(); @@ -360,7 +360,7 @@ mod tests { ); assert_eq!( limit.sell.0, - to_wei(20), + 20u64.eth(), "Sell amount should be equal to order sell amount for out of market orders." ); } @@ -368,14 +368,14 @@ mod tests { #[test] fn test_adjust_quote_to_in_market_sell_order_limits() { let order = Order { - sell_amount: to_wei(10), - buy_amount: to_wei(10), + sell_amount: 10u64.eth(), + buy_amount: 10u64.eth(), side: Side::Sell, }; let quote = Quote { - sell_amount: to_wei(10), - buy_amount: to_wei(25), - fee_amount: to_wei(2), + sell_amount: 10u64.eth(), + buy_amount: 25u64.eth(), + fee_amount: 2u64.eth(), }; let limit = adjust_quote_to_order_limits(order.clone(), quote.clone()).unwrap(); @@ -386,7 +386,7 @@ mod tests { ); assert_eq!( limit.buy.0, - to_wei(20), + 20u64.eth(), "Buy amount should be equal to quoted buy amount but reduced by fee." ); } @@ -394,21 +394,21 @@ mod tests { #[test] fn test_adjust_quote_to_in_market_buy_order_limits() { let order = Order { - sell_amount: to_wei(20), - buy_amount: to_wei(10), + sell_amount: 20u64.eth(), + buy_amount: 10u64.eth(), side: Side::Buy, }; let quote = Quote { - sell_amount: to_wei(17), - buy_amount: to_wei(10), - fee_amount: to_wei(1), + sell_amount: 17u64.eth(), + buy_amount: 10u64.eth(), + fee_amount: 1u64.eth(), }; let limit = adjust_quote_to_order_limits(order.clone(), quote.clone()).unwrap(); assert_eq!( limit.sell.0, - to_wei(18), + 18u64.eth(), "Sell amount should match quoted buy amount increased by fee" ); assert_eq!( @@ -416,8 +416,4 @@ mod tests { "Buy amount should be taken from the order for buy orders in market price." ); } - - pub fn to_wei(base: u32) -> eth::U256 { - eth::U256::from(base) * eth::U256::from(10).pow(eth::U256::from(18)) - } } diff --git a/crates/e2e/src/setup/onchain_components/mod.rs b/crates/e2e/src/setup/onchain_components/mod.rs index ed6fb4936f..2d374436cb 100644 --- a/crates/e2e/src/setup/onchain_components/mod.rs +++ b/crates/e2e/src/setup/onchain_components/mod.rs @@ -27,6 +27,7 @@ use { DomainSeparator, signature::{EcdsaSignature, EcdsaSigningScheme}, }, + number::units::EthUnit, secp256k1::SecretKey, shared::ethrpc::Web3, std::{borrow::BorrowMut, ops::Deref}, @@ -74,21 +75,6 @@ macro_rules! deploy { }}; } -pub fn to_wei_with_exp(base: u32, exp: usize) -> U256 { - U256::from(base) * U256::exp10(exp) -} - -pub fn to_wei(base: u32) -> U256 { - to_wei_with_exp(base, 18) -} - -/// Returns the provided Eth amount in wei. -/// -/// Equivalent to `amount * 10^18`. -pub fn eth(amount: u32) -> ::alloy::primitives::U256 { - ::alloy::primitives::U256::from(amount) * ::alloy::primitives::utils::Unit::ETHER.wei() -} - #[derive(Clone, Debug)] pub struct TestAccount { account: Account, @@ -371,7 +357,7 @@ impl OnchainComponents { let forked_node_api = self.web3.api::>(); forked_node_api - .set_balance(&auth_manager, to_wei(100)) + .set_balance(&auth_manager, 100u64.eth().into_legacy()) .await .expect("could not set auth_manager balance"); diff --git a/crates/e2e/tests/e2e/app_data.rs b/crates/e2e/tests/e2e/app_data.rs index cc613588d3..effb34dbae 100644 --- a/crates/e2e/tests/e2e/app_data.rs +++ b/crates/e2e/tests/e2e/app_data.rs @@ -1,12 +1,16 @@ use { app_data::{AppDataHash, hash_full_app_data}, - e2e::setup::{eth, *}, - ethrpc::alloy::{CallBuilderExt, conversions::IntoAlloy}, + e2e::setup::*, + ethrpc::alloy::{ + CallBuilderExt, + conversions::{IntoAlloy, IntoLegacy}, + }, model::{ order::{OrderCreation, OrderCreationAppData, OrderKind}, quote::{OrderQuoteRequest, OrderQuoteSide, SellAmount}, signature::EcdsaSigningScheme, }, + number::units::EthUnit, reqwest::StatusCode, secp256k1::SecretKey, shared::ethrpc::Web3, @@ -29,16 +33,19 @@ async fn local_node_app_data_full_format() { // Test that orders can be placed with the new app data format. async fn app_data(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3).await; - let [solver] = onchain.make_solvers(eth(1)).await; - let [trader] = onchain.make_accounts(eth(1)).await; + let [solver] = onchain.make_solvers(1u64.eth()).await; + let [trader] = onchain.make_accounts(1u64.eth()).await; let [token_a, token_b] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 1_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; - token_a.mint(trader.address(), eth(10)).await; + token_a.mint(trader.address(), 10u64.eth()).await; token_a - .approve(onchain.contracts().allowance.into_alloy(), eth(10)) + .approve(onchain.contracts().allowance.into_alloy(), 10u64.eth()) .from(trader.address()) .send_and_watch() .await @@ -49,9 +56,9 @@ async fn app_data(web3: Web3) { let order = OrderCreation { app_data, sell_token: *token_a.address(), - sell_amount: eth(2), + sell_amount: 2u64.eth(), buy_token: *token_b.address(), - buy_amount: eth(1), + buy_amount: 1u64.eth(), valid_to, kind: OrderKind::Sell, ..Default::default() @@ -191,16 +198,19 @@ async fn app_data(web3: Web3) { /// all supported features. async fn app_data_full_format(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3).await; - let [solver] = onchain.make_solvers(eth(1)).await; - let [trader] = onchain.make_accounts(eth(1)).await; + let [solver] = onchain.make_solvers(1u64.eth()).await; + let [trader] = onchain.make_accounts(1u64.eth()).await; let [token_a, token_b] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 1_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; - token_a.mint(trader.address(), eth(10)).await; + token_a.mint(trader.address(), 10u64.eth()).await; token_a - .approve(onchain.contracts().allowance.into_alloy(), eth(10)) + .approve(onchain.contracts().allowance.into_alloy(), 10u64.eth()) .from(trader.address()) .send_and_watch() .await @@ -211,9 +221,9 @@ async fn app_data_full_format(web3: Web3) { let order = OrderCreation { app_data, sell_token: *token_a.address(), - sell_amount: eth(2), + sell_amount: 2u64.eth(), buy_token: *token_b.address(), - buy_amount: eth(1), + buy_amount: 1u64.eth(), valid_to, kind: OrderKind::Sell, ..Default::default() diff --git a/crates/e2e/tests/e2e/app_data_signer.rs b/crates/e2e/tests/e2e/app_data_signer.rs index 4a3bdb6e46..1645ee1e02 100644 --- a/crates/e2e/tests/e2e/app_data_signer.rs +++ b/crates/e2e/tests/e2e/app_data_signer.rs @@ -1,11 +1,15 @@ use { alloy::primitives::Address, - e2e::setup::{OnchainComponents, Services, TestAccount, eth, run_test, safe::Safe, to_wei}, - ethrpc::alloy::{CallBuilderExt, conversions::IntoAlloy}, + e2e::setup::{OnchainComponents, Services, TestAccount, run_test, safe::Safe}, + ethrpc::alloy::{ + CallBuilderExt, + conversions::{IntoAlloy, IntoLegacy}, + }, model::{ order::{OrderCreation, OrderCreationAppData, OrderKind}, signature::EcdsaSigningScheme, }, + number::units::EthUnit, secp256k1::SecretKey, shared::ethrpc::Web3, web3::signing::SecretKeyRef, @@ -19,24 +23,27 @@ async fn local_node_order_creation_checks_metadata_signer() { async fn order_creation_checks_metadata_signer(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3.clone()).await; - let [solver] = onchain.make_solvers(eth(1)).await; - let [trader, adversary, safe_owner] = onchain.make_accounts(eth(1)).await; + let [solver] = onchain.make_solvers(1u64.eth()).await; + let [trader, adversary, safe_owner] = onchain.make_accounts(1u64.eth()).await; let [token_a, token_b] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 1_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; - token_a.mint(trader.address(), eth(10)).await; + token_a.mint(trader.address(), 10u64.eth()).await; token_a - .approve(onchain.contracts().allowance.into_alloy(), eth(10)) + .approve(onchain.contracts().allowance.into_alloy(), 10u64.eth()) .from(trader.address()) .send_and_watch() .await .unwrap(); - token_a.mint(adversary.address(), eth(10)).await; + token_a.mint(adversary.address(), 10u64.eth()).await; token_a - .approve(onchain.contracts().allowance.into_alloy(), eth(10)) + .approve(onchain.contracts().allowance.into_alloy(), 10u64.eth()) .from(adversary.address()) .send_and_watch() .await @@ -47,9 +54,9 @@ async fn order_creation_checks_metadata_signer(web3: Web3) { let order = OrderCreation { app_data, sell_token: *token_a.address(), - sell_amount: eth(2), + sell_amount: 2u64.eth(), buy_token: *token_b.address(), - buy_amount: eth(1), + buy_amount: 1u64.eth(), valid_to, kind: OrderKind::Sell, ..Default::default() @@ -96,10 +103,10 @@ async fn order_creation_checks_metadata_signer(web3: Web3) { // EIP-1271 let safe = Safe::deploy(safe_owner.clone(), web3.alloy.clone()).await; - token_a.mint(safe.address(), eth(10)).await; + token_a.mint(safe.address(), 10u64.eth()).await; safe.exec_alloy_call( token_a - .approve(onchain.contracts().allowance.into_alloy(), eth(10)) + .approve(onchain.contracts().allowance.into_alloy(), 10u64.eth()) .into_transaction_request(), ) .await; diff --git a/crates/e2e/tests/e2e/autopilot_leader.rs b/crates/e2e/tests/e2e/autopilot_leader.rs index fd4f7fc906..167c175c4d 100644 --- a/crates/e2e/tests/e2e/autopilot_leader.rs +++ b/crates/e2e/tests/e2e/autopilot_leader.rs @@ -1,15 +1,6 @@ use { autopilot::shutdown_controller::ShutdownController, - e2e::setup::{ - OnchainComponents, - Services, - TIMEOUT, - colocation, - eth, - run_test, - to_wei, - wait_for_condition, - }, + e2e::setup::{OnchainComponents, Services, TIMEOUT, colocation, run_test, wait_for_condition}, ethrpc::{ Web3, alloy::{ @@ -18,6 +9,7 @@ use { }, }, model::order::{OrderCreation, OrderKind}, + number::units::EthUnit, secp256k1::SecretKey, std::time::Duration, web3::signing::SecretKeyRef, @@ -33,21 +25,24 @@ async fn dual_autopilot_only_leader_produces_auctions(web3: Web3) { // TODO: Implement test that checks auction creation frequency against db // to see that only one autopilot produces auctions let mut onchain = OnchainComponents::deploy(web3).await; - let [trader] = onchain.make_accounts(eth(1)).await; - let [solver1, solver2] = onchain.make_solvers(eth(1)).await; + let [trader] = onchain.make_accounts(1u64.eth()).await; + let [solver1, solver2] = onchain.make_solvers(1u64.eth()).await; let [token_a] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 1_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; // Fund trader, settlement accounts, and pool creation - token_a.mint(solver1.address(), eth(1000)).await; - token_a.mint(solver2.address(), eth(1000)).await; + token_a.mint(solver1.address(), 1000u64.eth()).await; + token_a.mint(solver2.address(), 1000u64.eth()).await; - token_a.mint(trader.address(), eth(200)).await; + token_a.mint(trader.address(), 200u64.eth()).await; // Approve GPv2 for trading token_a - .approve(onchain.contracts().allowance.into_alloy(), eth(1000)) + .approve(onchain.contracts().allowance.into_alloy(), 1000u64.eth()) .from(trader.address()) .send_and_watch() .await @@ -113,9 +108,9 @@ async fn dual_autopilot_only_leader_produces_auctions(web3: Web3) { let order = || { OrderCreation { sell_token: *token_a.address(), - sell_amount: eth(10), + sell_amount: 10u64.eth(), buy_token: *onchain.contracts().weth.address(), - buy_amount: eth(5), + buy_amount: 5u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, ..Default::default() diff --git a/crates/e2e/tests/e2e/banned_users.rs b/crates/e2e/tests/e2e/banned_users.rs index 8230470e08..eb912035de 100644 --- a/crates/e2e/tests/e2e/banned_users.rs +++ b/crates/e2e/tests/e2e/banned_users.rs @@ -1,18 +1,13 @@ use { alloy::{ - primitives::{Address, U256, address, utils::Unit}, + primitives::{Address, address}, providers::ext::{AnvilApi, ImpersonateConfig}, }, contracts::alloy::ERC20, - e2e::setup::{ - OnchainComponents, - Services, - eth, - run_forked_test_with_block_number, - to_wei_with_exp, - }, + e2e::setup::{OnchainComponents, Services, run_forked_test_with_block_number}, ethrpc::{Web3, alloy::conversions::IntoAlloy}, model::quote::{OrderQuoteRequest, OrderQuoteSide, SellAmount}, + number::units::EthUnit, reqwest::StatusCode, }; @@ -36,7 +31,7 @@ const BANNED_USER: Address = address!("7F367cC41522cE07553e823bf3be79A889DEbe1B" async fn forked_mainnet_onchain_banned_user_test(web3: Web3) { let mut onchain = OnchainComponents::deployed(web3.clone()).await; - let [solver] = onchain.make_solvers_forked(eth(1)).await; + let [solver] = onchain.make_solvers_forked(1u64.eth()).await; let token_dai = ERC20::Instance::new( address!("6b175474e89094c44da98b954eedeac495271d0f"), @@ -51,7 +46,7 @@ async fn forked_mainnet_onchain_banned_user_test(web3: Web3) { web3.alloy .anvil_send_impersonated_transaction_with_config( token_dai - .transfer(BANNED_USER, to_wei_with_exp(1000, 18).into_alloy()) + .transfer(BANNED_USER, 1000u64.eth()) .from(DAI_WHALE_MAINNET) .into_transaction_request(), ImpersonateConfig { @@ -69,14 +64,11 @@ async fn forked_mainnet_onchain_banned_user_test(web3: Web3) { web3.alloy .anvil_send_impersonated_transaction_with_config( token_dai - .approve( - onchain.contracts().allowance.into_alloy(), - to_wei_with_exp(1000, 18).into_alloy(), - ) + .approve(onchain.contracts().allowance.into_alloy(), 1000u64.eth()) .from(BANNED_USER) .into_transaction_request(), ImpersonateConfig { - fund_amount: Some(eth(1)), + fund_amount: Some(1u64.eth()), stop_impersonate: true, }, ) @@ -96,7 +88,7 @@ async fn forked_mainnet_onchain_banned_user_test(web3: Web3) { buy_token: *token_usdt.address(), side: OrderQuoteSide::Sell { sell_amount: SellAmount::BeforeFee { - value: (U256::from(1000) * Unit::ETHER.wei()).try_into().unwrap(), + value: (1000u64.eth()).try_into().unwrap(), }, }, from: BANNED_USER, diff --git a/crates/e2e/tests/e2e/buffers.rs b/crates/e2e/tests/e2e/buffers.rs index 4e927b1e31..ce783c6131 100644 --- a/crates/e2e/tests/e2e/buffers.rs +++ b/crates/e2e/tests/e2e/buffers.rs @@ -1,11 +1,15 @@ use { ::alloy::primitives::U256, - e2e::setup::{eth, *}, - ethrpc::alloy::{CallBuilderExt, conversions::IntoAlloy}, + e2e::setup::*, + ethrpc::alloy::{ + CallBuilderExt, + conversions::{IntoAlloy, IntoLegacy}, + }, model::{ order::{OrderCreation, OrderKind}, signature::EcdsaSigningScheme, }, + number::units::EthUnit, secp256k1::SecretKey, shared::ethrpc::Web3, web3::signing::SecretKeyRef, @@ -20,24 +24,27 @@ async fn local_node_buffers() { async fn onchain_settlement_without_liquidity(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3).await; - let [solver] = onchain.make_solvers(eth(1)).await; - let [trader] = onchain.make_accounts(eth(1)).await; + let [solver] = onchain.make_solvers(1u64.eth()).await; + let [trader] = onchain.make_accounts(1u64.eth()).await; let [token_a, token_b] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 1_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; // Fund trader, settlement accounts, and pool creation - token_a.mint(trader.address(), eth(100)).await; + token_a.mint(trader.address(), 100u64.eth()).await; token_b - .mint(*onchain.contracts().gp_settlement.address(), eth(5)) + .mint(*onchain.contracts().gp_settlement.address(), 5u64.eth()) .await; - token_a.mint(solver.address(), eth(1000)).await; - token_b.mint(solver.address(), eth(1000)).await; + token_a.mint(solver.address(), 1000u64.eth()).await; + token_b.mint(solver.address(), 1000u64.eth()).await; // Approve GPv2 for trading token_a - .approve(onchain.contracts().allowance.into_alloy(), eth(100)) + .approve(onchain.contracts().allowance.into_alloy(), 100u64.eth()) .from(trader.address()) .send_and_watch() .await @@ -89,9 +96,9 @@ async fn onchain_settlement_without_liquidity(web3: Web3) { // Place Order let order = OrderCreation { sell_token: *token_a.address(), - sell_amount: eth(9), + sell_amount: 9u64.eth(), buy_token: *token_b.address(), - buy_amount: eth(5), + buy_amount: 5u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Buy, ..Default::default() diff --git a/crates/e2e/tests/e2e/cow_amm.rs b/crates/e2e/tests/e2e/cow_amm.rs index 4d9aaf0669..a8d4d889ee 100644 --- a/crates/e2e/tests/e2e/cow_amm.rs +++ b/crates/e2e/tests/e2e/cow_amm.rs @@ -1,6 +1,6 @@ use { alloy::{ - primitives::{Address, Bytes, FixedBytes, U256, address, utils::Unit}, + primitives::{Address, Bytes, FixedBytes, U256, address}, providers::ext::{AnvilApi, ImpersonateConfig}, }, contracts::alloy::{ @@ -14,12 +14,9 @@ use { Services, TIMEOUT, colocation::{self, SolverEngine}, - eth, mock::Mock, run_forked_test_with_block_number, run_test, - to_wei, - to_wei_with_exp, wait_for_condition, }, ethcontract::{BlockId, BlockNumber}, @@ -32,6 +29,7 @@ use { quote::{OrderQuoteRequest, OrderQuoteSide, SellAmount}, signature::EcdsaSigningScheme, }, + number::units::EthUnit, secp256k1::SecretKey, shared::ethrpc::Web3, solvers_dto::solution::{ @@ -57,18 +55,24 @@ async fn local_node_cow_amm_jit() { async fn cow_amm_jit(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3.clone()).await; - let [solver] = onchain.make_solvers(eth(100)).await; - let [bob, cow_amm_owner] = onchain.make_accounts(eth(1000)).await; + let [solver] = onchain.make_solvers(100u64.eth()).await; + let [bob, cow_amm_owner] = onchain.make_accounts(1000u64.eth()).await; let [dai] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(300_000), to_wei(100)) + .deploy_tokens_with_weth_uni_v2_pools( + 300_000u64.eth().into_legacy(), + 100u64.eth().into_legacy(), + ) .await; // Fund the buffers with a lot of buy tokens so we can pay out the required // tokens for 2 orders in the same direction without having to worry about // getting the liquidity on-chain. - dai.mint(*onchain.contracts().gp_settlement.address(), eth(100_000)) - .await; + dai.mint( + *onchain.contracts().gp_settlement.address(), + 100_000u64.eth(), + ) + .await; // set up cow_amm let oracle = @@ -85,9 +89,9 @@ async fn cow_amm_jit(web3: Web3) { .unwrap(); // Fund cow amm owner with 2_000 dai and allow factory take them - dai.mint(cow_amm_owner.address(), eth(2_000)).await; + dai.mint(cow_amm_owner.address(), 2_000u64.eth()).await; - dai.approve(*cow_amm_factory.address(), eth(2_000)) + dai.approve(*cow_amm_factory.address(), 2_000u64.eth()) .from(cow_amm_owner.address()) .send_and_watch() .await @@ -97,7 +101,7 @@ async fn cow_amm_jit(web3: Web3) { .contracts() .weth .deposit() - .value(eth(1)) + .value(1u64.eth()) .from(cow_amm_owner.address()) .send_and_watch() .await @@ -105,7 +109,7 @@ async fn cow_amm_jit(web3: Web3) { onchain .contracts() .weth - .approve(*cow_amm_factory.address(), eth(1)) + .approve(*cow_amm_factory.address(), 1u64.eth()) .from(cow_amm_owner.address()) .send_and_watch() .await @@ -136,9 +140,9 @@ async fn cow_amm_jit(web3: Web3) { cow_amm_factory .create( *dai.address(), - eth(2_000), + 2_000u64.eth(), *onchain.contracts().weth.address(), - eth(1), + 1u64.eth(), U256::ZERO, // min traded token *oracle.address(), Bytes::copy_from_slice(&oracle_data), @@ -220,7 +224,7 @@ async fn cow_amm_jit(web3: Web3) { buyToken: *dai.address(), receiver: Default::default(), sellAmount: U256::from(10).pow(U256::from(17)), - buyAmount: eth(230), + buyAmount: 230u64.eth(), validTo: valid_to, appData: FixedBytes(APP_DATA), feeAmount: U256::ZERO, @@ -291,7 +295,7 @@ async fn cow_amm_jit(web3: Web3) { sell_token: *onchain.contracts().weth.address(), sell_amount: alloy::primitives::U256::from(10).pow(alloy::primitives::U256::from(17)), // 0.1 WETH buy_token: *dai.address(), - buy_amount: eth(230), // 230 DAI + buy_amount: 230u64.eth(), // 230 DAI valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, ..Default::default() @@ -312,8 +316,8 @@ async fn cow_amm_jit(web3: Web3) { id: 1, // assume price of the univ2 pool prices: HashMap::from([ - (*dai.address(), eth(100)), - (*onchain.contracts().weth.address(), eth(300_000)), + (*dai.address(), 100u64.eth()), + (*onchain.contracts().weth.address(), 300_000u64.eth()), ]), trades: vec![ solvers_dto::solution::Trade::Jit(solvers_dto::solution::JitTrade { @@ -399,8 +403,8 @@ async fn cow_amm_driver_support(web3: Web3) { }; let mut onchain = OnchainComponents::deployed_with(web3.clone(), deployed_contracts).await; - let [solver] = onchain.make_solvers_forked(eth(11)).await; - let [trader] = onchain.make_accounts(eth(1)).await; + let [solver] = onchain.make_solvers_forked(11u64.eth()).await; + let [trader] = onchain.make_accounts(1u64.eth()).await; // find some USDC available onchain const USDC_WHALE_MAINNET: Address = address!("28c6c06298d514db089934071355e5743bf21d60"); @@ -460,7 +464,7 @@ async fn cow_amm_driver_support(web3: Web3) { // Give trader some USDC web3.alloy .anvil_send_impersonated_transaction_with_config( - usdc.transfer(trader.address(), to_wei_with_exp(1000, 6).into_alloy()) + usdc.transfer(trader.address(), 1000u64.mwei()) .from(USDC_WHALE_MAINNET) .into_transaction_request(), ImpersonateConfig { @@ -475,14 +479,11 @@ async fn cow_amm_driver_support(web3: Web3) { .unwrap(); // Approve GPv2 for trading - usdc.approve( - onchain.contracts().allowance.into_alloy(), - to_wei_with_exp(1000, 6).into_alloy(), - ) - .from(trader.address()) - .send_and_watch() - .await - .unwrap(); + usdc.approve(onchain.contracts().allowance.into_alloy(), 1000u64.mwei()) + .from(trader.address()) + .send_and_watch() + .await + .unwrap(); // Empty liquidity of one of the AMMs to test EmptyPoolRemoval maintenance job. const ZERO_BALANCE_AMM: Address = address!("b3bf81714f704720dcb0351ff0d42eca61b069fc"); @@ -581,9 +582,9 @@ factory = "0xf76c421bAb7df8548604E60deCCcE50477C10462" // Place Orders let order = OrderCreation { sell_token: *usdc.address(), - sell_amount: alloy::primitives::U256::from(to_wei_with_exp(1000, 6).low_u128()), + sell_amount: 1000u64.mwei(), buy_token: *usdt.address(), - buy_amount: alloy::primitives::U256::from(to_wei_with_exp(2000, 6).low_u128()), + buy_amount: 2000u64.mwei(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, ..Default::default() @@ -602,7 +603,7 @@ factory = "0xf76c421bAb7df8548604E60deCCcE50477C10462" buy_token: *usdt.address(), side: OrderQuoteSide::Sell { sell_amount: SellAmount::BeforeFee { - value: (U256::from(1000) * Unit::MWEI.wei()).try_into().unwrap(), + value: (1000u64.mwei()).try_into().unwrap(), }, }, ..Default::default() @@ -687,11 +688,14 @@ async fn local_node_cow_amm_opposite_direction() { async fn cow_amm_opposite_direction(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3.clone()).await; - let [solver] = onchain.make_solvers(eth(100)).await; - let [bob, cow_amm_owner] = onchain.make_accounts(eth(1000)).await; + let [solver] = onchain.make_solvers(100u64.eth()).await; + let [bob, cow_amm_owner] = onchain.make_accounts(1000u64.eth()).await; let [dai] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(300_000), to_wei(100)) + .deploy_tokens_with_weth_uni_v2_pools( + 300_000u64.eth().into_legacy(), + 100u64.eth().into_legacy(), + ) .await; // No need to fund the buffers since we're testing the CoW AMM directly filling @@ -713,9 +717,9 @@ async fn cow_amm_opposite_direction(web3: Web3) { // Fund the CoW AMM owner with DAI and WETH and approve the factory to transfer // them - dai.mint(cow_amm_owner.address(), eth(2_000)).await; + dai.mint(cow_amm_owner.address(), 2_000u64.eth()).await; - dai.approve(*cow_amm_factory.address(), eth(2_000)) + dai.approve(*cow_amm_factory.address(), 2_000u64.eth()) .from(cow_amm_owner.address()) .send_and_watch() .await @@ -726,14 +730,14 @@ async fn cow_amm_opposite_direction(web3: Web3) { .weth .deposit() .from(cow_amm_owner.address()) - .value(eth(1)) + .value(1u64.eth()) .send_and_watch() .await .unwrap(); onchain .contracts() .weth - .approve(*cow_amm_factory.address(), eth(1)) + .approve(*cow_amm_factory.address(), 1u64.eth()) .from(cow_amm_owner.address()) .send_and_watch() .await @@ -744,7 +748,7 @@ async fn cow_amm_opposite_direction(web3: Web3) { .weth .deposit() .from(solver.address()) - .value(eth(1)) + .value(1u64.eth()) .send_and_watch() .await .unwrap(); @@ -775,9 +779,9 @@ async fn cow_amm_opposite_direction(web3: Web3) { cow_amm_factory .create( *dai.address(), - eth(2_000), + 2_000u64.eth(), *onchain.contracts().weth.address(), - eth(1), + 1u64.eth(), U256::ZERO, // min traded token *oracle.address(), Bytes::copy_from_slice(&oracle_data), @@ -844,7 +848,7 @@ async fn cow_amm_opposite_direction(web3: Web3) { .unwrap() .unwrap(); let valid_to = block.timestamp.as_u32() + 300; - let executed_amount = eth(230); + let executed_amount = 230u64.eth(); // CoW AMM order remains the same (selling WETH for DAI) let cow_amm_order = contracts::alloy::cow_amm::CowAmm::GPv2Order::Data { @@ -896,7 +900,7 @@ async fn cow_amm_opposite_direction(web3: Web3) { }; // Fund trader "bob" with DAI and approve allowance - dai.mint(bob.address(), eth(250)).await; + dai.mint(bob.address(), 250u64.eth()).await; dai.approve( onchain.contracts().allowance.into_alloy(), @@ -929,14 +933,14 @@ async fn cow_amm_opposite_direction(web3: Web3) { // Set the fees appropriately let fee_cow_amm = alloy::primitives::U256::from(10).pow(alloy::primitives::U256::from(16)); // 0.01 WETH - let fee_user = eth(1); // 1 DAI + let fee_user = 1u64.eth(); // 1 DAI let mocked_solutions = |order_uid: OrderUid| { Solution { id: 1, prices: HashMap::from([ - (*dai.address(), eth(1)), // 1 DAI = $1 - (*onchain.contracts().weth.address(), eth(2300)), // 1 WETH = $2300 + (*dai.address(), 1u64.eth()), // 1 DAI = $1 + (*onchain.contracts().weth.address(), 2300u64.eth()), // 1 WETH = $2300 ]), trades: vec![ solvers_dto::solution::Trade::Jit(solvers_dto::solution::JitTrade { diff --git a/crates/e2e/tests/e2e/eth_integration.rs b/crates/e2e/tests/e2e/eth_integration.rs index fd9a891fb9..9f94e7c8a9 100644 --- a/crates/e2e/tests/e2e/eth_integration.rs +++ b/crates/e2e/tests/e2e/eth_integration.rs @@ -1,6 +1,5 @@ use { - ::alloy::primitives::{U256, utils::Unit}, - e2e::setup::{eth, *}, + e2e::setup::*, ethcontract::prelude::Address, ethrpc::alloy::{ CallBuilderExt, @@ -11,7 +10,7 @@ use { quote::{OrderQuoteRequest, OrderQuoteSide, SellAmount}, signature::EcdsaSigningScheme, }, - number::nonzero::NonZeroU256, + number::{nonzero::NonZeroU256, units::EthUnit}, secp256k1::SecretKey, shared::ethrpc::Web3, web3::signing::SecretKeyRef, @@ -26,27 +25,30 @@ async fn local_node_eth_integration() { async fn eth_integration(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3.clone()).await; - let [solver] = onchain.make_solvers(eth(1)).await; - let [trader_a, trader_b] = onchain.make_accounts(eth(1)).await; + let [solver] = onchain.make_solvers(1u64.eth()).await; + let [trader_a, trader_b] = onchain.make_accounts(1u64.eth()).await; // Create & mint tokens to trade, pools for fee connections let [token] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(100_000), to_wei(100_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 100_000u64.eth().into_legacy(), + 100_000u64.eth().into_legacy(), + ) .await; - token.mint(trader_a.address(), eth(51)).await; - token.mint(trader_b.address(), eth(51)).await; + token.mint(trader_a.address(), 51u64.eth()).await; + token.mint(trader_b.address(), 51u64.eth()).await; // Approve GPv2 for trading token - .approve(onchain.contracts().allowance.into_alloy(), eth(51)) + .approve(onchain.contracts().allowance.into_alloy(), 51u64.eth()) .from(trader_a.address()) .send_and_watch() .await .unwrap(); token - .approve(onchain.contracts().allowance.into_alloy(), eth(51)) + .approve(onchain.contracts().allowance.into_alloy(), 51u64.eth()) .from(trader_b.address()) .send_and_watch() .await @@ -70,7 +72,7 @@ async fn eth_integration(web3: Web3) { from: Address::default().into_alloy(), side: OrderQuoteSide::Sell { sell_amount: SellAmount::AfterFee { - value: NonZeroU256::try_from(U256::from(43) * Unit::ETHER.wei()).unwrap(), + value: NonZeroU256::try_from(43u64.eth()).unwrap(), }, }, ..Default::default() @@ -88,9 +90,9 @@ async fn eth_integration(web3: Web3) { let order_buy_eth_a = OrderCreation { kind: OrderKind::Buy, sell_token: *token.address(), - sell_amount: eth(50), + sell_amount: 50u64.eth(), buy_token: BUY_ETH_ADDRESS, - buy_amount: eth(49), + buy_amount: 49u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, ..Default::default() } @@ -103,9 +105,9 @@ async fn eth_integration(web3: Web3) { let order_buy_eth_b = OrderCreation { kind: OrderKind::Sell, sell_token: *token.address(), - sell_amount: eth(50), + sell_amount: 50u64.eth(), buy_token: BUY_ETH_ADDRESS, - buy_amount: eth(49), + buy_amount: 49u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, ..Default::default() } @@ -130,8 +132,9 @@ async fn eth_integration(web3: Web3) { .await .unwrap(); - let trader_a_eth_decreased = (balance_a - trader_a_eth_balance_before) == to_wei(49); - let trader_b_eth_increased = balance_b >= to_wei(49); + let trader_a_eth_decreased = + (balance_a - trader_a_eth_balance_before) == 49u64.eth().into_legacy(); + let trader_b_eth_increased = balance_b >= 49u64.eth().into_legacy(); trader_a_eth_decreased && trader_b_eth_increased }; wait_for_condition(TIMEOUT, trade_happened).await.unwrap(); diff --git a/crates/e2e/tests/e2e/eth_safe.rs b/crates/e2e/tests/e2e/eth_safe.rs index 912a5bacb8..0996422097 100644 --- a/crates/e2e/tests/e2e/eth_safe.rs +++ b/crates/e2e/tests/e2e/eth_safe.rs @@ -1,20 +1,15 @@ use { ::alloy::{primitives::U256, providers::Provider}, - e2e::setup::{ - OnchainComponents, - Services, - TIMEOUT, - eth, - run_test, - safe::Safe, - to_wei, - wait_for_condition, + e2e::setup::{OnchainComponents, Services, TIMEOUT, run_test, safe::Safe, wait_for_condition}, + ethrpc::alloy::{ + CallBuilderExt, + conversions::{IntoAlloy, IntoLegacy}, }, - ethrpc::alloy::{CallBuilderExt, conversions::IntoAlloy}, model::{ order::{BUY_ETH_ADDRESS, OrderCreation, OrderKind}, signature::{Signature, hashed_eip712_message}, }, + number::units::EthUnit, shared::ethrpc::Web3, }; @@ -28,24 +23,27 @@ async fn test(web3: Web3) { tracing::info!("Setting up chain state."); let mut onchain = OnchainComponents::deploy(web3.clone()).await; - let [solver] = onchain.make_solvers(eth(10)).await; - let [trader] = onchain.make_accounts(eth(10)).await; + let [solver] = onchain.make_solvers(10u64.eth()).await; + let [trader] = onchain.make_accounts(10u64.eth()).await; let safe = Safe::deploy(trader.clone(), web3.alloy.clone()).await; let [token] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(1000), to_wei(1000)) + .deploy_tokens_with_weth_uni_v2_pools( + 1000u64.eth().into_legacy(), + 1000u64.eth().into_legacy(), + ) .await; - token.mint(trader.address(), eth(4)).await; + token.mint(trader.address(), 4u64.eth()).await; safe.exec_alloy_call( token - .approve(onchain.contracts().allowance.into_alloy(), eth(4)) + .approve(onchain.contracts().allowance.into_alloy(), 4u64.eth()) .into_transaction_request(), ) .await; - token.mint(safe.address(), eth(4)).await; + token.mint(safe.address(), 4u64.eth()).await; token - .approve(onchain.contracts().allowance.into_alloy(), eth(4)) + .approve(onchain.contracts().allowance.into_alloy(), 4u64.eth()) .from(trader.address()) .send_and_watch() .await @@ -67,9 +65,9 @@ async fn test(web3: Web3) { let mut order = OrderCreation { from: Some(safe.address()), sell_token: *token.address(), - sell_amount: eth(4), + sell_amount: 4u64.eth(), buy_token: BUY_ETH_ADDRESS, - buy_amount: eth(3), + buy_amount: 3u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, partially_fillable: true, kind: OrderKind::Sell, diff --git a/crates/e2e/tests/e2e/ethflow.rs b/crates/e2e/tests/e2e/ethflow.rs index 35a12b078c..60e8893e2f 100644 --- a/crates/e2e/tests/e2e/ethflow.rs +++ b/crates/e2e/tests/e2e/ethflow.rs @@ -16,9 +16,7 @@ use { TIMEOUT, TRADES_ENDPOINT, TestAccount, - eth, run_test, - to_wei, wait_for_condition, }, ethcontract::{Account, H160, H256, U256}, @@ -55,13 +53,13 @@ use { signature::{Signature, hashed_eip712_message}, trade::Trade, }, - number::nonzero::NonZeroU256, + number::{nonzero::NonZeroU256, units::EthUnit}, refunder::refund_service::{INVALIDATED_OWNER, NO_OWNER}, reqwest::Client, shared::signature_validator::check_erc1271_result, }; -const DAI_PER_ETH: u32 = 1_000; +const DAI_PER_ETH: u64 = 1_000; #[tokio::test] #[ignore] @@ -92,18 +90,21 @@ async fn local_node_eth_flow_zero_buy_amount() { async fn eth_flow_tx(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3.clone()).await; - let [solver] = onchain.make_solvers(eth(2)).await; - let [trader] = onchain.make_accounts(eth(2)).await; + let [solver] = onchain.make_solvers(2u64.eth()).await; + let [trader] = onchain.make_accounts(2u64.eth()).await; // Create token with Uniswap pool for price estimation let [dai] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(DAI_PER_ETH * 1_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + (DAI_PER_ETH * 1_000).eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; // Get a quote from the services let buy_token = *dai.address(); let receiver = Address::repeat_byte(0x42); - let sell_amount = eth(1); + let sell_amount = 1u64.eth(); let intent = EthFlowTradeIntent { sell_amount, buy_token, @@ -114,7 +115,7 @@ async fn eth_flow_tx(web3: Web3) { services.start_protocol(solver).await; let approve_call_data = { - let call_builder = dai.approve(trader.address(), eth(10)); + let call_builder = dai.approve(trader.address(), 10u64.eth()); let calldata = call_builder.calldata(); const_hex::encode_prefixed(calldata) }; @@ -227,7 +228,7 @@ async fn eth_flow_tx(web3: Web3) { .call() .await .unwrap(); - assert_eq!(allowance, eth(10)); + assert_eq!(allowance, 10u64.eth()); let allowance = onchain .contracts() @@ -236,7 +237,7 @@ async fn eth_flow_tx(web3: Web3) { .call() .await .unwrap(); - assert_eq!(allowance, eth(10)); + assert_eq!(allowance, 10u64.eth()); // Just to be super sure we assert that we indeed were not // able to set an allowance on behalf of the settlement contract. @@ -261,12 +262,15 @@ async fn eth_flow_tx(web3: Web3) { async fn eth_flow_without_quote(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3.clone()).await; - let [solver] = onchain.make_solvers(eth(2)).await; - let [trader] = onchain.make_accounts(eth(2)).await; + let [solver] = onchain.make_solvers(2u64.eth()).await; + let [trader] = onchain.make_accounts(2u64.eth()).await; // Create token with Uniswap pool for price estimation let [dai] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(DAI_PER_ETH * 1_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + (DAI_PER_ETH * 1_000).eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; let services = Services::new(&onchain).await; @@ -279,7 +283,7 @@ async fn eth_flow_without_quote(web3: Web3) { + 3600; let ethflow_order = ExtendedEthFlowOrder(CoWSwapEthFlow::EthFlowOrder::Data { buyToken: *dai.address(), - sellAmount: eth(1), + sellAmount: 1u64.eth(), buyAmount: alloy::primitives::U256::ONE, validTo: valid_to, partiallyFillable: false, @@ -314,10 +318,13 @@ async fn eth_flow_without_quote(web3: Web3) { async fn eth_flow_indexing_after_refund(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3.clone()).await; - let [solver] = onchain.make_solvers(eth(2)).await; - let [trader, dummy_trader] = onchain.make_accounts(eth(2)).await; + let [solver] = onchain.make_solvers(2u64.eth()).await; + let [trader, dummy_trader] = onchain.make_accounts(2u64.eth()).await; let [dai] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(DAI_PER_ETH * 1000), to_wei(1000)) + .deploy_tokens_with_weth_uni_v2_pools( + (DAI_PER_ETH * 1000).eth().into_legacy(), + 1000u64.eth().into_legacy(), + ) .await; let services = Services::new(&onchain).await; @@ -362,7 +369,7 @@ async fn eth_flow_indexing_after_refund(web3: Web3) { // Create the actual order that should be picked up by the services and matched. let buy_token = *dai.address(); let receiver = Address::repeat_byte(0x42); - let sell_amount = eth(1); + let sell_amount = 1u64.eth(); let valid_to = chrono::offset::Utc::now().timestamp() as u32 + timestamp_of_current_block_in_seconds(&web3.alloy) .await @@ -873,12 +880,15 @@ impl EthFlowTradeIntent { async fn eth_flow_zero_buy_amount(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3.clone()).await; - let [solver] = onchain.make_solvers(eth(2)).await; - let [trader_a, trader_b] = onchain.make_accounts(eth(2)).await; + let [solver] = onchain.make_solvers(2u64.eth()).await; + let [trader_a, trader_b] = onchain.make_accounts(2u64.eth()).await; // Create token with Uniswap pool for price estimation let [dai] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(DAI_PER_ETH * 1_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + (DAI_PER_ETH * 1_000).eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; let services = Services::new(&onchain).await; @@ -892,7 +902,7 @@ async fn eth_flow_zero_buy_amount(web3: Web3) { + 3600; let ethflow_order = ExtendedEthFlowOrder(CoWSwapEthFlow::EthFlowOrder::Data { buyToken: *dai.address(), - sellAmount: eth(1), + sellAmount: 1u64.eth(), buyAmount: alloy::primitives::U256::from(buy_amount), validTo: valid_to, partiallyFillable: false, diff --git a/crates/e2e/tests/e2e/hooks.rs b/crates/e2e/tests/e2e/hooks.rs index fc04a8c4d5..289c3866ac 100644 --- a/crates/e2e/tests/e2e/hooks.rs +++ b/crates/e2e/tests/e2e/hooks.rs @@ -1,18 +1,13 @@ use { - alloy::{ - primitives::{U256, utils::Unit}, - providers::Provider, - }, + alloy::providers::Provider, app_data::Hook, e2e::setup::{ OnchainComponents, Services, TIMEOUT, - eth, onchain_components, run_test, safe::Safe, - to_wei, wait_for_condition, }, ethrpc::alloy::{ @@ -24,7 +19,7 @@ use { quote::{OrderQuoteRequest, OrderQuoteSide, SellAmount}, signature::{EcdsaSigningScheme, Signature, hashed_eip712_message}, }, - number::nonzero::NonZeroU256, + number::{nonzero::NonZeroU256, units::EthUnit}, reqwest::StatusCode, secp256k1::SecretKey, serde_json::json, @@ -65,15 +60,19 @@ async fn local_node_quote_verification() { async fn gas_limit(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3).await; - let [solver] = onchain.make_solvers(eth(1)).await; - let [trader] = onchain.make_accounts(eth(1)).await; + let [solver] = onchain.make_solvers(1u64.eth()).await; + let [trader] = onchain.make_accounts(1u64.eth()).await; let cow = onchain - .deploy_cow_weth_pool(to_wei(1_000_000), to_wei(1_000), to_wei(1_000)) + .deploy_cow_weth_pool( + 1_000_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; // Fund trader accounts and approve relayer - cow.fund(trader.address(), eth(5)).await; - cow.approve(onchain.contracts().allowance.into_alloy(), eth(5)) + cow.fund(trader.address(), 5u64.eth()).await; + cow.approve(onchain.contracts().allowance.into_alloy(), 5u64.eth()) .from(trader.address()) .send_and_watch() .await @@ -84,9 +83,9 @@ async fn gas_limit(web3: Web3) { let order = OrderCreation { sell_token: *cow.address(), - sell_amount: eth(4), + sell_amount: 4u64.eth(), buy_token: *onchain.contracts().weth.address(), - buy_amount: eth(3), + buy_amount: 3u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, app_data: OrderCreationAppData::Full { @@ -119,18 +118,26 @@ async fn gas_limit(web3: Web3) { async fn allowance(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3).await; - let [solver] = onchain.make_solvers(eth(1)).await; - let [trader] = onchain.make_accounts(eth(1)).await; + let [solver] = onchain.make_solvers(1u64.eth()).await; + let [trader] = onchain.make_accounts(1u64.eth()).await; let cow = onchain - .deploy_cow_weth_pool(to_wei(1_000_000), to_wei(1_000), to_wei(1_000)) + .deploy_cow_weth_pool( + 1_000_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; // Fund trader accounts - cow.fund(trader.address(), eth(5)).await; + cow.fund(trader.address(), 5u64.eth()).await; // Sign a permit pre-interaction for trading. let permit = cow - .permit(&trader, onchain.contracts().allowance, to_wei(5)) + .permit( + &trader, + onchain.contracts().allowance, + 5u64.eth().into_legacy(), + ) .await; // Setup a malicious interaction for setting approvals to steal funds from // the settlement contract. @@ -161,9 +168,9 @@ async fn allowance(web3: Web3) { let order = OrderCreation { sell_token: *cow.address(), - sell_amount: eth(5), + sell_amount: 5u64.eth(), buy_token: *onchain.contracts().weth.address(), - buy_amount: eth(3), + buy_amount: 3u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, app_data: OrderCreationAppData::Full { @@ -188,7 +195,7 @@ async fn allowance(web3: Web3) { onchain.mint_block().await; let balance = cow.balanceOf(trader.address()).call().await.unwrap(); - assert_eq!(balance, eth(5)); + assert_eq!(balance, 5u64.eth()); tracing::info!("Waiting for trade."); let trade_happened = || async { @@ -260,8 +267,8 @@ async fn signature(web3: Web3) { let chain_id = alloy::primitives::U256::from(web3.alloy.get_chain_id().await.unwrap()); - let [solver] = onchain.make_solvers(eth(1)).await; - let [trader] = onchain.make_accounts(eth(1)).await; + let [solver] = onchain.make_solvers(1u64.eth()).await; + let [trader] = onchain.make_accounts(1u64.eth()).await; let safe_infra = onchain_components::safe::Infrastructure::new(web3.alloy.clone()).await; @@ -299,14 +306,17 @@ async fn signature(web3: Web3) { ); let [token] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(100_000), to_wei(100_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 100_000u64.eth().into_legacy(), + 100_000u64.eth().into_legacy(), + ) .await; - token.mint(safe.address(), eth(5)).await; + token.mint(safe.address(), 5u64.eth()).await; // Sign an approval transaction for trading. This will be at nonce 0 because // it is the first transaction evah! let approval_call_data = token - .approve(onchain.contracts().allowance.into_alloy(), eth(5)) + .approve(onchain.contracts().allowance.into_alloy(), 5u64.eth()) .calldata() .to_vec(); let approval_builder = safe.sign_transaction( @@ -341,11 +351,11 @@ async fn signature(web3: Web3) { // To not throw an error because we can't get a verifiable quote // we make the order partially fillable and sell slightly more than // `from` currently has. - sell_amount: eth(6), + sell_amount: 6u64.eth(), partially_fillable: true, sell_token: *token.address(), buy_token: *onchain.contracts().weth.address(), - buy_amount: eth(3), + buy_amount: 3u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, app_data: OrderCreationAppData::Full { @@ -374,7 +384,7 @@ async fn signature(web3: Web3) { .await .unwrap() .into_legacy(); - assert_eq!(balance, to_wei(5)); + assert_eq!(balance, 5u64.eth().into_legacy()); // Check that the Safe really hasn't been deployed yet. let code = web3.alloy.get_code_at(safe.address()).await.unwrap(); @@ -414,20 +424,23 @@ async fn signature(web3: Web3) { async fn partial_fills(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3.clone()).await; - let [solver] = onchain.make_solvers(eth(1)).await; - let [trader] = onchain.make_accounts(eth(3)).await; + let [solver] = onchain.make_solvers(1u64.eth()).await; + let [trader] = onchain.make_accounts(3u64.eth()).await; let counter = contracts::alloy::test::Counter::Instance::deploy(web3.alloy.clone()) .await .unwrap(); let [token] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 1_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; let sell_token = onchain.contracts().weth.clone(); sell_token - .approve(onchain.contracts().allowance.into_alloy(), eth(2)) + .approve(onchain.contracts().allowance.into_alloy(), 2u64.eth()) .from(trader.address()) .send_and_watch() .await @@ -435,7 +448,7 @@ async fn partial_fills(web3: Web3) { sell_token .deposit() .from(trader.address()) - .value(eth(1)) + .value(1u64.eth()) .send_and_watch() .await .unwrap(); @@ -465,9 +478,9 @@ async fn partial_fills(web3: Web3) { tracing::info!("Placing order"); let order = OrderCreation { sell_token: *sell_token.address(), - sell_amount: eth(2), + sell_amount: 2u64.eth(), buy_token: *token.address(), - buy_amount: eth(1), + buy_amount: 1u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, partially_fillable: true, @@ -517,7 +530,7 @@ async fn partial_fills(web3: Web3) { sell_token .deposit() .from(trader.address()) - .value(eth(1)) + .value(1u64.eth()) .send_and_watch() .await .unwrap(); @@ -541,8 +554,8 @@ async fn quote_verification(web3: Web3) { let chain_id = alloy::primitives::U256::from(web3.alloy.get_chain_id().await.unwrap()); - let [trader] = onchain.make_accounts(eth(1)).await; - let [solver] = onchain.make_solvers(eth(1)).await; + let [trader] = onchain.make_accounts(1u64.eth()).await; + let [solver] = onchain.make_solvers(1u64.eth()).await; let safe_infra = onchain_components::safe::Infrastructure::new(web3.alloy.clone()).await; @@ -575,12 +588,15 @@ async fn quote_verification(web3: Web3) { ); let [token] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(100_000), to_wei(100_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 100_000u64.eth().into_legacy(), + 100_000u64.eth().into_legacy(), + ) .await; - token.mint(safe.address(), eth(5)).await; + token.mint(safe.address(), 5u64.eth()).await; token - .approve(onchain.contracts().allowance.into_alloy(), eth(5)) + .approve(onchain.contracts().allowance.into_alloy(), 5u64.eth()) .from(trader.address()) .send_and_watch() .await @@ -590,7 +606,10 @@ async fn quote_verification(web3: Web3) { // to fund the trade in a pre-hook. let transfer_builder = safe.sign_transaction( *token.address(), - token.transfer(trader.address(), eth(5)).calldata().to_vec(), + token + .transfer(trader.address(), 5u64.eth()) + .calldata() + .to_vec(), alloy::primitives::U256::ZERO, ); let call_data = transfer_builder.calldata().to_vec(); @@ -617,7 +636,7 @@ async fn quote_verification(web3: Web3) { buy_token: *onchain.contracts().weth.address(), side: OrderQuoteSide::Sell { sell_amount: SellAmount::BeforeFee { - value: NonZeroU256::try_from(U256::from(5) * Unit::ETHER.wei()).unwrap(), + value: NonZeroU256::try_from(5u64.eth()).unwrap(), }, }, app_data: OrderCreationAppData::Full { diff --git a/crates/e2e/tests/e2e/jit_orders.rs b/crates/e2e/tests/e2e/jit_orders.rs index 6984c53a44..4b0c1420ca 100644 --- a/crates/e2e/tests/e2e/jit_orders.rs +++ b/crates/e2e/tests/e2e/jit_orders.rs @@ -1,6 +1,6 @@ use { ::alloy::primitives::U256, - e2e::setup::{colocation::SolverEngine, eth, mock::Mock, solution::JitOrder, *}, + e2e::setup::{colocation::SolverEngine, mock::Mock, solution::JitOrder, *}, ethrpc::alloy::{ CallBuilderExt, conversions::{IntoAlloy, IntoLegacy}, @@ -9,6 +9,7 @@ use { order::{OrderClass, OrderCreation, OrderKind}, signature::EcdsaSigningScheme, }, + number::units::EthUnit, secp256k1::SecretKey, shared::ethrpc::Web3, solvers_dto::solution::{Asset, Solution}, @@ -25,20 +26,23 @@ async fn local_node_single_limit_order() { async fn single_limit_order_test(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3.clone()).await; - let [solver] = onchain.make_solvers(eth(100)).await; - let [trader] = onchain.make_accounts(eth(100)).await; + let [solver] = onchain.make_solvers(100u64.eth()).await; + let [trader] = onchain.make_accounts(100u64.eth()).await; let [token] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(300_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 300_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; - token.mint(solver.address(), eth(100)).await; + token.mint(solver.address(), 100u64.eth()).await; onchain .contracts() .weth .deposit() .from(trader.address()) - .value(eth(20)) + .value(20u64.eth()) .send_and_watch() .await .unwrap(); @@ -112,9 +116,9 @@ async fn single_limit_order_test(web3: Web3) { // Place order let order = OrderCreation { sell_token: *onchain.contracts().weth.address(), - sell_amount: eth(10), + sell_amount: 10u64.eth(), buy_token: *token.address(), - buy_amount: eth(5), + buy_amount: 5u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, ..Default::default() @@ -135,11 +139,11 @@ async fn single_limit_order_test(web3: Web3) { let (jit_order, jit_order_uid) = JitOrder { owner: trader.address().into_legacy(), sell: Asset { - amount: eth(10), + amount: 10u64.eth(), token: *token.address(), }, buy: Asset { - amount: eth(1), + amount: 1u64.eth(), token: *onchain.contracts().weth.address(), }, kind: OrderKind::Sell, @@ -157,16 +161,16 @@ async fn single_limit_order_test(web3: Web3) { mock_solver.configure_solution(Some(Solution { id: 0, prices: HashMap::from([ - (*token.address(), eth(1)), - (*onchain.contracts().weth.address(), eth(1)), + (*token.address(), 1u64.eth()), + (*onchain.contracts().weth.address(), 1u64.eth()), ]), trades: vec![ solvers_dto::solution::Trade::Jit(solvers_dto::solution::JitTrade { order: jit_order, // Making it 9 + 1 so we cover the edge case of fill-or-kill solution mismatches // when observing settlements https://github.com/cowprotocol/services/pull/3440 - executed_amount: eth(9), - fee: Some(eth(1)), + executed_amount: 9u64.eth(), + fee: Some(1u64.eth()), }), solvers_dto::solution::Trade::Fulfillment(solvers_dto::solution::Fulfillment { executed_amount: order.sell_amount, @@ -190,11 +194,11 @@ async fn single_limit_order_test(web3: Web3) { let solver_balance_after = token.balanceOf(solver.address()).call().await.unwrap(); let trader_balance_increased = - trader_balance_after.saturating_sub(trader_balance_before) >= eth(5); + trader_balance_after.saturating_sub(trader_balance_before) >= 5u64.eth(); // Since the fee is 0 in the custom solution, the balance difference has to be // exactly 10 wei let solver_balance_decreased = - solver_balance_before.saturating_sub(solver_balance_after) == eth(10); + solver_balance_before.saturating_sub(solver_balance_after) == 10u64.eth(); trader_balance_increased && solver_balance_decreased }) .await diff --git a/crates/e2e/tests/e2e/limit_orders.rs b/crates/e2e/tests/e2e/limit_orders.rs index f8f15119d8..b02a03a169 100644 --- a/crates/e2e/tests/e2e/limit_orders.rs +++ b/crates/e2e/tests/e2e/limit_orders.rs @@ -1,14 +1,14 @@ use { crate::database::AuctionTransaction, ::alloy::{ - primitives::{Address, U256, address, utils::Unit}, + primitives::{Address, U256, address}, providers::ext::{AnvilApi, ImpersonateConfig}, }, bigdecimal::BigDecimal, contracts::alloy::ERC20, database::byte_array::ByteArray, driver::domain::eth::NonZeroU256, - e2e::setup::{eth, *}, + e2e::setup::*, ethrpc::alloy::{ CallBuilderExt, conversions::{IntoAlloy, IntoLegacy}, @@ -19,7 +19,7 @@ use { quote::{OrderQuoteRequest, OrderQuoteSide, SellAmount}, signature::EcdsaSigningScheme, }, - number::conversions::big_decimal_to_big_uint, + number::{conversions::big_decimal_to_big_uint, units::EthUnit}, secp256k1::SecretKey, shared::ethrpc::Web3, std::{collections::HashMap, ops::DerefMut}, @@ -97,18 +97,21 @@ async fn forked_node_gnosis_single_limit_order() { async fn single_limit_order_test(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3.clone()).await; - let [solver] = onchain.make_solvers(eth(1)).await; - let [trader_a] = onchain.make_accounts(eth(1)).await; + let [solver] = onchain.make_solvers(1u64.eth()).await; + let [trader_a] = onchain.make_accounts(1u64.eth()).await; let [token_a, token_b] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 1_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; // Fund trader accounts - token_a.mint(trader_a.address(), eth(10)).await; + token_a.mint(trader_a.address(), 10u64.eth()).await; // Create and fund Uniswap pool - token_a.mint(solver.address(), eth(1000)).await; - token_b.mint(solver.address(), eth(1000)).await; + token_a.mint(solver.address(), 1000u64.eth()).await; + token_b.mint(solver.address(), 1000u64.eth()).await; onchain .contracts() .uniswap_v2_factory @@ -119,14 +122,20 @@ async fn single_limit_order_test(web3: Web3) { .unwrap(); token_a - .approve(*onchain.contracts().uniswap_v2_router.address(), eth(1000)) + .approve( + *onchain.contracts().uniswap_v2_router.address(), + 1000u64.eth(), + ) .from(solver.address()) .send_and_watch() .await .unwrap(); token_b - .approve(*onchain.contracts().uniswap_v2_router.address(), eth(1000)) + .approve( + *onchain.contracts().uniswap_v2_router.address(), + 1000u64.eth(), + ) .from(solver.address()) .send_and_watch() .await @@ -137,8 +146,8 @@ async fn single_limit_order_test(web3: Web3) { .addLiquidity( *token_a.address(), *token_b.address(), - eth(1000), - eth(1000), + 1000u64.eth(), + 1000u64.eth(), U256::ZERO, U256::ZERO, solver.address(), @@ -152,7 +161,7 @@ async fn single_limit_order_test(web3: Web3) { // Approve GPv2 for trading token_a - .approve(onchain.contracts().allowance.into_alloy(), eth(10)) + .approve(onchain.contracts().allowance.into_alloy(), 10u64.eth()) .from(trader_a.address()) .send_and_watch() .await @@ -164,9 +173,9 @@ async fn single_limit_order_test(web3: Web3) { let order = OrderCreation { sell_token: *token_a.address(), - sell_amount: eth(10), + sell_amount: 10u64.eth(), buy_token: *token_b.address(), - buy_amount: eth(5), + buy_amount: 5u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, ..Default::default() @@ -194,7 +203,7 @@ async fn single_limit_order_test(web3: Web3) { tracing::info!("Waiting for trade."); wait_for_condition(TIMEOUT, || async { let balance_after = token_b.balanceOf(trader_a.address()).call().await.unwrap(); - balance_after.checked_sub(balance_before).unwrap() >= eth(5) + balance_after.checked_sub(balance_before).unwrap() >= 5u64.eth() }) .await .unwrap(); @@ -212,17 +221,20 @@ async fn single_limit_order_test(web3: Web3) { async fn two_limit_orders_test(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3.clone()).await; - let [solver] = onchain.make_solvers(eth(1)).await; - let [trader_a, trader_b] = onchain.make_accounts(eth(1)).await; + let [solver] = onchain.make_solvers(1u64.eth()).await; + let [trader_a, trader_b] = onchain.make_accounts(1u64.eth()).await; let [token_a, token_b] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 1_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; // Fund trader accounts and prepare funding Uniswap pool - token_a.mint(trader_a.address(), eth(10)).await; - token_b.mint(trader_b.address(), eth(10)).await; - token_a.mint(solver.address(), eth(1_000)).await; - token_b.mint(solver.address(), eth(1_000)).await; + token_a.mint(trader_a.address(), 10u64.eth()).await; + token_b.mint(trader_b.address(), 10u64.eth()).await; + token_a.mint(solver.address(), 1_000u64.eth()).await; + token_b.mint(solver.address(), 1_000u64.eth()).await; // Create and fund Uniswap pool onchain @@ -235,14 +247,20 @@ async fn two_limit_orders_test(web3: Web3) { .unwrap(); token_a - .approve(*onchain.contracts().uniswap_v2_router.address(), eth(1000)) + .approve( + *onchain.contracts().uniswap_v2_router.address(), + 1000u64.eth(), + ) .from(solver.address()) .send_and_watch() .await .unwrap(); token_b - .approve(*onchain.contracts().uniswap_v2_router.address(), eth(1000)) + .approve( + *onchain.contracts().uniswap_v2_router.address(), + 1000u64.eth(), + ) .from(solver.address()) .send_and_watch() .await @@ -253,8 +271,8 @@ async fn two_limit_orders_test(web3: Web3) { .addLiquidity( *token_a.address(), *token_b.address(), - eth(1000), - eth(1000), + 1000u64.eth(), + 1000u64.eth(), U256::ZERO, U256::ZERO, solver.address(), @@ -268,14 +286,14 @@ async fn two_limit_orders_test(web3: Web3) { // Approve GPv2 for trading token_a - .approve(onchain.contracts().allowance.into_alloy(), eth(10)) + .approve(onchain.contracts().allowance.into_alloy(), 10u64.eth()) .from(trader_a.address()) .send_and_watch() .await .unwrap(); token_b - .approve(onchain.contracts().allowance.into_alloy(), eth(10)) + .approve(onchain.contracts().allowance.into_alloy(), 10u64.eth()) .from(trader_b.address()) .send_and_watch() .await @@ -287,9 +305,9 @@ async fn two_limit_orders_test(web3: Web3) { let order_a = OrderCreation { sell_token: *token_a.address(), - sell_amount: eth(10), + sell_amount: 10u64.eth(), buy_token: *token_b.address(), - buy_amount: eth(5), + buy_amount: 5u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, ..Default::default() @@ -311,9 +329,9 @@ async fn two_limit_orders_test(web3: Web3) { let order_b = OrderCreation { sell_token: *token_b.address(), - sell_amount: eth(5), + sell_amount: 5u64.eth(), buy_token: *token_a.address(), - buy_amount: eth(2), + buy_amount: 2u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, ..Default::default() @@ -333,8 +351,8 @@ async fn two_limit_orders_test(web3: Web3) { wait_for_condition(TIMEOUT, || async { let balance_after_a = token_b.balanceOf(trader_a.address()).call().await.unwrap(); let balance_after_b = token_a.balanceOf(trader_b.address()).call().await.unwrap(); - let order_a_settled = balance_after_a.saturating_sub(balance_before_a) >= eth(5); - let order_b_settled = balance_after_b.saturating_sub(balance_before_b) >= eth(2); + let order_a_settled = balance_after_a.saturating_sub(balance_before_a) >= 5u64.eth(); + let order_b_settled = balance_after_b.saturating_sub(balance_before_b) >= 2u64.eth(); order_a_settled && order_b_settled }) .await @@ -344,40 +362,46 @@ async fn two_limit_orders_test(web3: Web3) { async fn two_limit_orders_multiple_winners_test(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3).await; - let [solver_a, solver_b] = onchain.make_solvers(eth(1)).await; - let [trader_a, trader_b] = onchain.make_accounts(eth(1)).await; + let [solver_a, solver_b] = onchain.make_solvers(1u64.eth()).await; + let [trader_a, trader_b] = onchain.make_accounts(1u64.eth()).await; let [token_a, token_b, token_c, token_d] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 1_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; // Fund traders - token_a.mint(trader_a.address(), eth(10)).await; - token_b.mint(trader_b.address(), eth(10)).await; + token_a.mint(trader_a.address(), 10u64.eth()).await; + token_b.mint(trader_b.address(), 10u64.eth()).await; // Create more liquid routes between token_a (token_b) and weth via base_a // (base_b). base_a has more liquidity than base_b, leading to the solver that // knows about base_a to offer different solution. let [base_a, base_b] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(10_000), to_wei(10_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 10_000u64.eth().into_legacy(), + 10_000u64.eth().into_legacy(), + ) .await; onchain - .seed_uni_v2_pool((&token_a, eth(100_000)), (&base_a, eth(100_000))) + .seed_uni_v2_pool((&token_a, 100_000u64.eth()), (&base_a, 100_000u64.eth())) .await; onchain - .seed_uni_v2_pool((&token_b, eth(10_000)), (&base_b, eth(10_000))) + .seed_uni_v2_pool((&token_b, 10_000u64.eth()), (&base_b, 10_000u64.eth())) .await; // Approve GPv2 for trading token_a - .approve(onchain.contracts().allowance.into_alloy(), eth(100)) + .approve(onchain.contracts().allowance.into_alloy(), 100u64.eth()) .from(trader_a.address()) .send_and_watch() .await .unwrap(); token_b - .approve(onchain.contracts().allowance.into_alloy(), eth(100)) + .approve(onchain.contracts().allowance.into_alloy(), 100u64.eth()) .from(trader_b.address()) .send_and_watch() .await @@ -421,9 +445,9 @@ async fn two_limit_orders_multiple_winners_test(web3: Web3) { // Place Orders let order_a = OrderCreation { sell_token: *token_a.address(), - sell_amount: eth(10), + sell_amount: 10u64.eth(), buy_token: *token_c.address(), - buy_amount: eth(5), + buy_amount: 5u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, ..Default::default() @@ -437,9 +461,9 @@ async fn two_limit_orders_multiple_winners_test(web3: Web3) { let order_b = OrderCreation { sell_token: *token_b.address(), - sell_amount: eth(10), + sell_amount: 10u64.eth(), buy_token: *token_d.address(), - buy_amount: eth(5), + buy_amount: 5u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, ..Default::default() @@ -586,17 +610,20 @@ async fn two_limit_orders_multiple_winners_test(web3: Web3) { async fn too_many_limit_orders_test(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3.clone()).await; - let [solver] = onchain.make_solvers(eth(1)).await; - let [trader] = onchain.make_accounts(eth(1)).await; + let [solver] = onchain.make_solvers(1u64.eth()).await; + let [trader] = onchain.make_accounts(1u64.eth()).await; let [token_a] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 1_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; - token_a.mint(trader.address(), eth(1)).await; + token_a.mint(trader.address(), 1u64.eth()).await; // Approve GPv2 for trading token_a - .approve(onchain.contracts().allowance.into_alloy(), eth(101)) + .approve(onchain.contracts().allowance.into_alloy(), 101u64.eth()) .from(trader.address()) .send_and_watch() .await @@ -629,9 +656,9 @@ async fn too_many_limit_orders_test(web3: Web3) { let order = OrderCreation { sell_token: *token_a.address(), - sell_amount: eth(1), + sell_amount: 1u64.eth(), buy_token: *onchain.contracts().weth.address(), - buy_amount: eth(1), + buy_amount: 1u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, ..Default::default() @@ -647,9 +674,9 @@ async fn too_many_limit_orders_test(web3: Web3) { // one limit order per user. let order = OrderCreation { sell_token: *token_a.address(), - sell_amount: eth(1), + sell_amount: 1u64.eth(), buy_token: *onchain.contracts().weth.address(), - buy_amount: eth(2), + buy_amount: 2u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, ..Default::default() @@ -668,17 +695,20 @@ async fn too_many_limit_orders_test(web3: Web3) { async fn limit_does_not_apply_to_in_market_orders_test(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3.clone()).await; - let [solver] = onchain.make_solvers(eth(1)).await; - let [trader] = onchain.make_accounts(eth(1)).await; + let [solver] = onchain.make_solvers(1u64.eth()).await; + let [trader] = onchain.make_accounts(1u64.eth()).await; let [token] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 1_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; - token.mint(trader.address(), eth(100)).await; + token.mint(trader.address(), 100u64.eth()).await; // Approve GPv2 for trading token - .approve(onchain.contracts().allowance.into_alloy(), eth(101)) + .approve(onchain.contracts().allowance.into_alloy(), 101u64.eth()) .from(trader.address()) .send_and_watch() .await @@ -715,7 +745,7 @@ async fn limit_does_not_apply_to_in_market_orders_test(web3: Web3) { buy_token: *onchain.contracts().weth.address(), side: OrderQuoteSide::Sell { sell_amount: SellAmount::BeforeFee { - value: NonZeroU256::try_from(U256::from(5) * Unit::ETHER.wei()).unwrap(), + value: NonZeroU256::try_from(5u64.eth()).unwrap(), }, }, ..Default::default() @@ -727,7 +757,7 @@ async fn limit_does_not_apply_to_in_market_orders_test(web3: Web3) { sell_token: *token.address(), sell_amount: quote.quote.sell_amount, buy_token: *onchain.contracts().weth.address(), - buy_amount: quote.quote.buy_amount.saturating_sub(eth(4)), + buy_amount: quote.quote.buy_amount.saturating_sub(4u64.eth()), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, ..Default::default() @@ -742,9 +772,9 @@ async fn limit_does_not_apply_to_in_market_orders_test(web3: Web3) { // Place a "limit" order let order = OrderCreation { sell_token: *token.address(), - sell_amount: eth(1), + sell_amount: 1u64.eth(), buy_token: *onchain.contracts().weth.address(), - buy_amount: eth(3), + buy_amount: 3u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, ..Default::default() @@ -763,7 +793,7 @@ async fn limit_does_not_apply_to_in_market_orders_test(web3: Web3) { sell_token: *token.address(), sell_amount: quote.quote.sell_amount, buy_token: *onchain.contracts().weth.address(), - buy_amount: quote.quote.buy_amount.saturating_sub(eth(2)), + buy_amount: quote.quote.buy_amount.saturating_sub(2u64.eth()), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, ..Default::default() @@ -778,9 +808,9 @@ async fn limit_does_not_apply_to_in_market_orders_test(web3: Web3) { // Place a "limit" order in order to see if fails let order = OrderCreation { sell_token: *token.address(), - sell_amount: eth(1), + sell_amount: 1u64.eth(), buy_token: *onchain.contracts().weth.address(), - buy_amount: eth(2), + buy_amount: 2u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, ..Default::default() @@ -799,9 +829,9 @@ async fn limit_does_not_apply_to_in_market_orders_test(web3: Web3) { async fn forked_mainnet_single_limit_order_test(web3: Web3) { let mut onchain = OnchainComponents::deployed(web3.clone()).await; - let [solver] = onchain.make_solvers_forked(eth(1)).await; + let [solver] = onchain.make_solvers_forked(1u64.eth()).await; - let [trader] = onchain.make_accounts(eth(1)).await; + let [trader] = onchain.make_accounts(1u64.eth()).await; let token_usdc = ERC20::Instance::new( address!("a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"), @@ -817,7 +847,7 @@ async fn forked_mainnet_single_limit_order_test(web3: Web3) { web3.alloy .anvil_send_impersonated_transaction_with_config( token_usdc - .transfer(trader.address(), to_wei_with_exp(1000, 6).into_alloy()) + .transfer(trader.address(), 1000u64.mwei()) .from(USDC_WHALE_MAINNET) .into_transaction_request(), ImpersonateConfig { @@ -833,10 +863,7 @@ async fn forked_mainnet_single_limit_order_test(web3: Web3) { // Approve GPv2 for trading token_usdc - .approve( - onchain.contracts().allowance.into_alloy(), - to_wei_with_exp(1000, 6).into_alloy(), - ) + .approve(onchain.contracts().allowance.into_alloy(), 1000u64.mwei()) .from(trader.address()) .send_and_watch() .await @@ -850,9 +877,9 @@ async fn forked_mainnet_single_limit_order_test(web3: Web3) { let order = OrderCreation { sell_token: *token_usdc.address(), - sell_amount: to_wei_with_exp(1000, 6).into_alloy(), + sell_amount: 1000u64.mwei(), buy_token: *token_usdt.address(), - buy_amount: to_wei_with_exp(500, 6).into_alloy(), + buy_amount: 500u64.mwei(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, ..Default::default() @@ -871,7 +898,7 @@ async fn forked_mainnet_single_limit_order_test(web3: Web3) { buy_token: *token_usdt.address(), side: OrderQuoteSide::Sell { sell_amount: SellAmount::BeforeFee { - value: (U256::from(1000) * Unit::MWEI.wei()).try_into().unwrap(), + value: (1000u64.mwei()).try_into().unwrap(), }, }, ..Default::default() @@ -893,8 +920,7 @@ async fn forked_mainnet_single_limit_order_test(web3: Web3) { let buy_token_balance_after = token_usdt.balanceOf(trader.address()).call().await.unwrap(); (sell_token_balance_before > sell_token_balance_after) - && (buy_token_balance_after - >= buy_token_balance_before + to_wei_with_exp(500, 6).into_alloy()) + && (buy_token_balance_after >= buy_token_balance_before + 500u64.mwei()) }) .await .unwrap(); @@ -903,9 +929,9 @@ async fn forked_mainnet_single_limit_order_test(web3: Web3) { async fn forked_gnosis_single_limit_order_test(web3: Web3) { let mut onchain = OnchainComponents::deployed(web3.clone()).await; - let [solver] = onchain.make_solvers_forked(eth(1)).await; + let [solver] = onchain.make_solvers_forked(1u64.eth()).await; - let [trader] = onchain.make_accounts(eth(1)).await; + let [trader] = onchain.make_accounts(1u64.eth()).await; let token_usdc = ERC20::Instance::new( address!("ddafbb505ad214d7b80b1f830fccc89b60fb7a83"), @@ -921,7 +947,7 @@ async fn forked_gnosis_single_limit_order_test(web3: Web3) { web3.alloy .anvil_send_impersonated_transaction_with_config( token_usdc - .transfer(trader.address(), to_wei_with_exp(1000, 6).into_alloy()) + .transfer(trader.address(), 1000u64.mwei()) .from(USDC_WHALE_GNOSIS) .into_transaction_request(), ImpersonateConfig { @@ -937,10 +963,7 @@ async fn forked_gnosis_single_limit_order_test(web3: Web3) { // Approve GPv2 for trading token_usdc - .approve( - onchain.contracts().allowance.into_alloy(), - to_wei_with_exp(1000, 6).into_alloy(), - ) + .approve(onchain.contracts().allowance.into_alloy(), 1000u64.mwei()) .from(trader.address()) .send_and_watch() .await @@ -952,9 +975,9 @@ async fn forked_gnosis_single_limit_order_test(web3: Web3) { let order = OrderCreation { sell_token: *token_usdc.address(), - sell_amount: to_wei_with_exp(1000, 6).into_alloy(), + sell_amount: 1000u64.mwei(), buy_token: *token_wxdai.address(), - buy_amount: eth(500), + buy_amount: 500u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, ..Default::default() @@ -986,7 +1009,7 @@ async fn forked_gnosis_single_limit_order_test(web3: Web3) { .unwrap(); (sell_token_balance_before > sell_token_balance_after) - && (buy_token_balance_after >= buy_token_balance_before + eth(500)) + && (buy_token_balance_after >= buy_token_balance_before + 500u64.eth()) }) .await .unwrap(); @@ -995,17 +1018,17 @@ async fn forked_gnosis_single_limit_order_test(web3: Web3) { async fn no_liquidity_limit_order(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3.clone()).await; - let [solver] = onchain.make_solvers(eth(10_000)).await; - let [trader_a] = onchain.make_accounts(eth(1)).await; + let [solver] = onchain.make_solvers(10_000u64.eth()).await; + let [trader_a] = onchain.make_accounts(1u64.eth()).await; let [token_a, unsupported] = onchain.deploy_tokens(solver.account()).await; // Fund trader accounts - token_a.mint(trader_a.address(), eth(10)).await; + token_a.mint(trader_a.address(), 10u64.eth()).await; // Approve GPv2 for trading token_a - .approve(onchain.contracts().allowance.into_alloy(), eth(10)) + .approve(onchain.contracts().allowance.into_alloy(), 10u64.eth()) .from(trader_a.address()) .send_and_watch() .await @@ -1051,9 +1074,9 @@ async fn no_liquidity_limit_order(web3: Web3) { // Place order let mut order = OrderCreation { sell_token: *token_a.address(), - sell_amount: eth(10), + sell_amount: 10u64.eth(), buy_token: *onchain.contracts().weth.address(), - buy_amount: eth(1), + buy_amount: 1u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, ..Default::default() @@ -1089,7 +1112,11 @@ async fn no_liquidity_limit_order(web3: Web3) { // Create liquidity onchain - .seed_weth_uni_v2_pools([&token_a].iter().copied(), to_wei(1000), to_wei(1000)) + .seed_weth_uni_v2_pools( + [&token_a].iter().copied(), + 1000u64.eth().into_legacy(), + 1000u64.eth().into_legacy(), + ) .await; // Drive solution @@ -1130,5 +1157,5 @@ async fn no_liquidity_limit_order(web3: Web3) { .call() .await .unwrap(); - assert!(balance_after.checked_sub(balance_before).unwrap() >= eth(5)); + assert!(balance_after.checked_sub(balance_before).unwrap() >= 5u64.eth()); } diff --git a/crates/e2e/tests/e2e/liquidity.rs b/crates/e2e/tests/e2e/liquidity.rs index 0edf750f0d..2343099795 100644 --- a/crates/e2e/tests/e2e/liquidity.rs +++ b/crates/e2e/tests/e2e/liquidity.rs @@ -14,9 +14,7 @@ use { TIMEOUT, TestAccount, colocation, - eth, run_forked_test_with_block_number, - to_wei_with_exp, wait_for_condition, }, }, @@ -33,6 +31,7 @@ use { order::{OrderCreation, OrderKind}, signature::EcdsaSigningScheme, }, + number::units::EthUnit, secp256k1::SecretKey, web3::signing::SecretKeyRef, }; @@ -57,8 +56,8 @@ async fn forked_node_zero_ex_liquidity_mainnet() { async fn zero_ex_liquidity(web3: Web3) { let mut onchain = OnchainComponents::deployed(web3.clone()).await; - let [solver] = onchain.make_solvers_forked(eth(1)).await; - let [trader, zeroex_maker] = onchain.make_accounts(eth(1)).await; + let [solver] = onchain.make_solvers_forked(1u64.eth()).await; + let [trader, zeroex_maker] = onchain.make_accounts(1u64.eth()).await; let token_usdc = ERC20::Instance::new( address!("a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"), @@ -76,7 +75,7 @@ async fn zero_ex_liquidity(web3: Web3) { }; let zeroex = IZeroex::Instance::deployed(&zeroex_provider).await.unwrap(); - let amount = to_wei_with_exp(5, 8).into_alloy(); + let amount = 500u64.mwei(); // Give trader some USDC web3.alloy diff --git a/crates/e2e/tests/e2e/liquidity_source_notification.rs b/crates/e2e/tests/e2e/liquidity_source_notification.rs index 5395f1924c..40c4309f23 100644 --- a/crates/e2e/tests/e2e/liquidity_source_notification.rs +++ b/crates/e2e/tests/e2e/liquidity_source_notification.rs @@ -14,10 +14,8 @@ use { Services, TIMEOUT, colocation::{self, SolverEngine}, - eth, mock::Mock, run_forked_test_with_block_number, - to_wei_with_exp, wait_for_condition, }, }, @@ -32,6 +30,7 @@ use { order::{OrderCreation, OrderKind}, signature::EcdsaSigningScheme, }, + number::units::EthUnit, secp256k1::SecretKey, solvers_dto::solution::Solution, std::collections::HashMap, @@ -60,16 +59,16 @@ async fn liquidity_source_notification(web3: Web3) { let mut onchain = OnchainComponents::deployed(web3.clone()).await; // Define trade params - let trade_amount = to_wei_with_exp(5, 8).into_alloy(); + let trade_amount = 500u64.mwei(); // Create parties accounts // solver - represents both baseline solver engine for quoting and liquorice // solver engine for solving - let [solver] = onchain.make_solvers_forked(eth(1)).await; + let [solver] = onchain.make_solvers_forked(1u64.eth()).await; // trader - the account that will place CoW order // liquorice_maker - the account that will place Liquorice order to fill CoW // order with - let [trader, liquorice_maker] = onchain.make_accounts(eth(1)).await; + let [trader, liquorice_maker] = onchain.make_accounts(1u64.eth()).await; // Access trade tokens contracts let token_usdc = ERC20::Instance::new( @@ -306,8 +305,8 @@ http-timeout = "10s" liquorice_solver_api_mock.configure_solution(Some(Solution { id: 1, prices: HashMap::from([ - (*token_usdc.address(), eth(11)), - (*token_usdt.address(), eth(10)), + (*token_usdc.address(), 11u64.eth()), + (*token_usdt.address(), 10u64.eth()), ]), trades: vec![solvers_dto::solution::Trade::Fulfillment( solvers_dto::solution::Fulfillment { diff --git a/crates/e2e/tests/e2e/order_cancellation.rs b/crates/e2e/tests/e2e/order_cancellation.rs index 3fc1cbfc32..b21409b48d 100644 --- a/crates/e2e/tests/e2e/order_cancellation.rs +++ b/crates/e2e/tests/e2e/order_cancellation.rs @@ -1,8 +1,11 @@ use { - ::alloy::primitives::{U256, utils::Unit}, + ::alloy::primitives::U256, database::order_events::OrderEventLabel, - e2e::setup::{eth, *}, - ethrpc::alloy::{CallBuilderExt, conversions::IntoAlloy}, + e2e::setup::*, + ethrpc::alloy::{ + CallBuilderExt, + conversions::{IntoAlloy, IntoLegacy}, + }, model::{ order::{ CancellationPayload, @@ -17,7 +20,7 @@ use { quote::{OrderQuoteRequest, OrderQuoteSide, SellAmount}, signature::{EcdsaSignature, EcdsaSigningScheme}, }, - number::nonzero::NonZeroU256, + number::{nonzero::NonZeroU256, units::EthUnit}, secp256k1::SecretKey, serde_json::json, shared::ethrpc::Web3, @@ -33,18 +36,21 @@ async fn local_node_order_cancellation() { async fn order_cancellation(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3).await; - let [solver] = onchain.make_solvers(eth(1)).await; - let [trader] = onchain.make_accounts(eth(1)).await; + let [solver] = onchain.make_solvers(1u64.eth()).await; + let [trader] = onchain.make_accounts(1u64.eth()).await; let [token] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 1_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; - token.mint(trader.address(), eth(10)).await; + token.mint(trader.address(), 10u64.eth()).await; // Approve GPv2 for trading token - .approve(onchain.contracts().allowance.into_alloy(), eth(10)) + .approve(onchain.contracts().allowance.into_alloy(), 10u64.eth()) .from(trader.address()) .send_and_watch() .await @@ -95,7 +101,7 @@ async fn order_cancellation(web3: Web3) { buy_token: *onchain.contracts().weth.address(), side: OrderQuoteSide::Sell { sell_amount: SellAmount::AfterFee { - value: NonZeroU256::try_from(U256::ONE * Unit::ETHER.wei()).unwrap(), + value: NonZeroU256::try_from(1u64.eth()).unwrap(), }, }, app_data: OrderCreationAppData::Full { diff --git a/crates/e2e/tests/e2e/partial_fill.rs b/crates/e2e/tests/e2e/partial_fill.rs index 7a2476913b..d5570b706c 100644 --- a/crates/e2e/tests/e2e/partial_fill.rs +++ b/crates/e2e/tests/e2e/partial_fill.rs @@ -9,6 +9,7 @@ use { order::{OrderCreation, OrderKind}, signature::{EcdsaSigningScheme, Signature, SigningScheme}, }, + number::units::EthUnit, orderbook::dto::order::Status, secp256k1::SecretKey, shared::ethrpc::Web3, @@ -25,17 +26,17 @@ async fn test(web3: Web3) { tracing::info!("Setting up chain state."); let mut onchain = OnchainComponents::deploy(web3).await; - let [solver] = onchain.make_solvers(eth(10)).await; - let [trader] = onchain.make_accounts(eth(10)).await; + let [solver] = onchain.make_solvers(10u64.eth()).await; + let [trader] = onchain.make_accounts(10u64.eth()).await; // Use a shallow pool to make partial fills easier to setup. let [token] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(10), to_wei(10)) + .deploy_tokens_with_weth_uni_v2_pools(10u64.eth().into_legacy(), 10u64.eth().into_legacy()) .await; onchain .contracts() .weth - .approve(onchain.contracts().allowance.into_alloy(), eth(4)) + .approve(onchain.contracts().allowance.into_alloy(), 4u64.eth()) .from(trader.address()) .send_and_watch() .await @@ -45,7 +46,7 @@ async fn test(web3: Web3) { .weth .deposit() .from(trader.address()) - .value(eth(4)) + .value(4u64.eth()) .send_and_watch() .await .unwrap(); @@ -59,9 +60,9 @@ async fn test(web3: Web3) { assert_eq!(balance, U256::ZERO); let order = OrderCreation { sell_token: *onchain.contracts().weth.address(), - sell_amount: eth(4), + sell_amount: 4u64.eth(), buy_token: *token.address(), - buy_amount: eth(3), + buy_amount: 3u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, partially_fillable: true, kind: OrderKind::Sell, diff --git a/crates/e2e/tests/e2e/partially_fillable_balance.rs b/crates/e2e/tests/e2e/partially_fillable_balance.rs index 36b5b26210..3066f73931 100644 --- a/crates/e2e/tests/e2e/partially_fillable_balance.rs +++ b/crates/e2e/tests/e2e/partially_fillable_balance.rs @@ -1,11 +1,15 @@ use { ::alloy::primitives::U256, - e2e::setup::{eth, *}, - ethrpc::alloy::{CallBuilderExt, conversions::IntoAlloy}, + e2e::setup::*, + ethrpc::alloy::{ + CallBuilderExt, + conversions::{IntoAlloy, IntoLegacy}, + }, model::{ order::{OrderCreation, OrderKind}, signature::EcdsaSigningScheme, }, + number::units::EthUnit, secp256k1::SecretKey, shared::ethrpc::Web3, web3::signing::SecretKeyRef, @@ -20,15 +24,18 @@ async fn local_node_partially_fillable_balance() { async fn test(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3).await; - let [solver] = onchain.make_solvers(eth(1)).await; - let [trader_a] = onchain.make_accounts(eth(1)).await; + let [solver] = onchain.make_solvers(1u64.eth()).await; + let [trader_a] = onchain.make_accounts(1u64.eth()).await; let [token_a, token_b] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(10_000), to_wei(10_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 10_000u64.eth().into_legacy(), + 10_000u64.eth().into_legacy(), + ) .await; - token_a.mint(trader_a.address(), eth(50)).await; - token_a.mint(solver.address(), eth(1000)).await; - token_b.mint(solver.address(), eth(1000)).await; + token_a.mint(trader_a.address(), 50u64.eth()).await; + token_a.mint(solver.address(), 1000u64.eth()).await; + token_b.mint(solver.address(), 1000u64.eth()).await; onchain .contracts() @@ -40,14 +47,20 @@ async fn test(web3: Web3) { .unwrap(); token_a - .approve(*onchain.contracts().uniswap_v2_router.address(), eth(1000)) + .approve( + *onchain.contracts().uniswap_v2_router.address(), + 1000u64.eth(), + ) .from(solver.address()) .send_and_watch() .await .unwrap(); token_b - .approve(*onchain.contracts().uniswap_v2_router.address(), eth(1000)) + .approve( + *onchain.contracts().uniswap_v2_router.address(), + 1000u64.eth(), + ) .from(solver.address()) .send_and_watch() .await @@ -58,8 +71,8 @@ async fn test(web3: Web3) { .addLiquidity( *token_a.address(), *token_b.address(), - eth(1000), - eth(1000), + 1000u64.eth(), + 1000u64.eth(), U256::ZERO, U256::ZERO, solver.address(), @@ -71,7 +84,7 @@ async fn test(web3: Web3) { .unwrap(); token_a - .approve(onchain.contracts().allowance.into_alloy(), eth(500)) + .approve(onchain.contracts().allowance.into_alloy(), 500u64.eth()) .from(trader_a.address()) .send_and_watch() .await @@ -82,9 +95,9 @@ async fn test(web3: Web3) { let order_a = OrderCreation { sell_token: *token_a.address(), - sell_amount: eth(100), + sell_amount: 100u64.eth(), buy_token: *token_b.address(), - buy_amount: eth(50), + buy_amount: 50u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, partially_fillable: true, diff --git a/crates/e2e/tests/e2e/partially_fillable_pool.rs b/crates/e2e/tests/e2e/partially_fillable_pool.rs index fc02f5a8d6..bb19af4b57 100644 --- a/crates/e2e/tests/e2e/partially_fillable_pool.rs +++ b/crates/e2e/tests/e2e/partially_fillable_pool.rs @@ -1,11 +1,15 @@ use { ::alloy::primitives::U256, - e2e::setup::{eth, *}, - ethrpc::alloy::{CallBuilderExt, conversions::IntoAlloy}, + e2e::setup::*, + ethrpc::alloy::{ + CallBuilderExt, + conversions::{IntoAlloy, IntoLegacy}, + }, model::{ order::{OrderCreation, OrderKind}, signature::EcdsaSigningScheme, }, + number::units::EthUnit, secp256k1::SecretKey, shared::ethrpc::Web3, web3::signing::SecretKeyRef, @@ -19,15 +23,18 @@ async fn local_node_partially_fillable_pool() { async fn test(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3).await; - let [solver] = onchain.make_solvers(eth(1)).await; - let [trader_a] = onchain.make_accounts(eth(1)).await; + let [solver] = onchain.make_solvers(1u64.eth()).await; + let [trader_a] = onchain.make_accounts(1u64.eth()).await; let [token_a, token_b] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 1_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; - token_a.mint(trader_a.address(), eth(500)).await; - token_a.mint(solver.address(), eth(1000)).await; - token_b.mint(solver.address(), eth(1000)).await; + token_a.mint(trader_a.address(), 500u64.eth()).await; + token_a.mint(solver.address(), 1000u64.eth()).await; + token_b.mint(solver.address(), 1000u64.eth()).await; onchain .contracts() @@ -39,14 +46,20 @@ async fn test(web3: Web3) { .unwrap(); token_a - .approve(*onchain.contracts().uniswap_v2_router.address(), eth(1000)) + .approve( + *onchain.contracts().uniswap_v2_router.address(), + 1000u64.eth(), + ) .from(solver.address()) .send_and_watch() .await .unwrap(); token_b - .approve(*onchain.contracts().uniswap_v2_router.address(), eth(1000)) + .approve( + *onchain.contracts().uniswap_v2_router.address(), + 1000u64.eth(), + ) .from(solver.address()) .send_and_watch() .await @@ -57,8 +70,8 @@ async fn test(web3: Web3) { .addLiquidity( *token_a.address(), *token_b.address(), - eth(1000), - eth(1000), + 1000u64.eth(), + 1000u64.eth(), U256::ZERO, U256::ZERO, solver.address(), @@ -70,7 +83,7 @@ async fn test(web3: Web3) { .unwrap(); token_a - .approve(onchain.contracts().allowance.into_alloy(), eth(500)) + .approve(onchain.contracts().allowance.into_alloy(), 500u64.eth()) .from(trader_a.address()) .send_and_watch() .await @@ -82,9 +95,9 @@ async fn test(web3: Web3) { let order_a = OrderCreation { sell_token: *token_a.address(), - sell_amount: eth(500), + sell_amount: 500u64.eth(), buy_token: *token_b.address(), - buy_amount: eth(390), + buy_amount: 390u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, partially_fillable: true, diff --git a/crates/e2e/tests/e2e/place_order_with_quote.rs b/crates/e2e/tests/e2e/place_order_with_quote.rs index 1fc1b48c87..7d4c4eacc9 100644 --- a/crates/e2e/tests/e2e/place_order_with_quote.rs +++ b/crates/e2e/tests/e2e/place_order_with_quote.rs @@ -1,13 +1,17 @@ use { - ::alloy::primitives::{U256, utils::Unit}, + ::alloy::primitives::U256, driver::domain::eth::NonZeroU256, e2e::{nodes::local_node::TestNodeApi, setup::*}, - ethrpc::alloy::{CallBuilderExt, conversions::IntoAlloy}, + ethrpc::alloy::{ + CallBuilderExt, + conversions::{IntoAlloy, IntoLegacy}, + }, model::{ order::{OrderCreation, OrderKind}, quote::{OrderQuoteRequest, OrderQuoteSide, SellAmount}, signature::EcdsaSigningScheme, }, + number::units::EthUnit, secp256k1::SecretKey, shared::ethrpc::Web3, std::ops::DerefMut, @@ -23,16 +27,19 @@ async fn local_node_test() { async fn place_order_with_quote(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3.clone()).await; - let [solver] = onchain.make_solvers(eth(10)).await; - let [trader] = onchain.make_accounts(eth(10)).await; + let [solver] = onchain.make_solvers(10u64.eth()).await; + let [trader] = onchain.make_accounts(10u64.eth()).await; let [token] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 1_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; onchain .contracts() .weth - .approve(onchain.contracts().allowance.into_alloy(), eth(3)) + .approve(onchain.contracts().allowance.into_alloy(), 3u64.eth()) .from(trader.address()) .send_and_watch() .await @@ -42,7 +49,7 @@ async fn place_order_with_quote(web3: Web3) { .weth .deposit() .from(trader.address()) - .value(eth(3)) + .value(3u64.eth()) .send_and_watch() .await .unwrap(); @@ -58,7 +65,7 @@ async fn place_order_with_quote(web3: Web3) { .expect("Must be able to disable automine"); tracing::info!("Quoting"); - let quote_sell_amount = U256::ONE * Unit::ETHER.wei(); + let quote_sell_amount = 1u64.eth(); let quote_request = OrderQuoteRequest { from: trader.address(), sell_token: *onchain.contracts().weth.address(), diff --git a/crates/e2e/tests/e2e/protocol_fee.rs b/crates/e2e/tests/e2e/protocol_fee.rs index 05548ea2c5..c4b2a1b6e9 100644 --- a/crates/e2e/tests/e2e/protocol_fee.rs +++ b/crates/e2e/tests/e2e/protocol_fee.rs @@ -3,7 +3,7 @@ use { driver::domain::eth::NonZeroU256, e2e::{ assert_approximately_eq, - setup::{eth, fee::*, *}, + setup::{fee::*, *}, }, ethcontract::{Address, prelude::U256}, ethrpc::alloy::{ @@ -22,6 +22,7 @@ use { }, signature::EcdsaSigningScheme, }, + number::units::EthUnit, reqwest::StatusCode, secp256k1::SecretKey, serde_json::json, @@ -83,14 +84,14 @@ async fn combined_protocol_fees(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3.clone()).await; - let [solver] = onchain.make_solvers(eth(200)).await; - let [trader] = onchain.make_accounts(eth(200)).await; + let [solver] = onchain.make_solvers(200u64.eth()).await; + let [trader] = onchain.make_accounts(200u64.eth()).await; let [ limit_order_token, market_order_token, partner_fee_order_token, ] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(20), to_wei(20)) + .deploy_tokens_with_weth_uni_v2_pools(20u64.eth().into_legacy(), 20u64.eth().into_legacy()) .await; for token in &[ @@ -98,17 +99,23 @@ async fn combined_protocol_fees(web3: Web3) { &market_order_token, &partner_fee_order_token, ] { - token.mint(solver.address(), eth(1000)).await; + token.mint(solver.address(), 1000u64.eth()).await; token - .approve(*onchain.contracts().uniswap_v2_router.address(), eth(1000)) + .approve( + *onchain.contracts().uniswap_v2_router.address(), + 1000u64.eth(), + ) .from(solver.address()) .send_and_watch() .await .unwrap(); token - .approve(*onchain.contracts().uniswap_v2_router.address(), eth(100)) + .approve( + *onchain.contracts().uniswap_v2_router.address(), + 100u64.eth(), + ) .from(trader.address()) .send_and_watch() .await @@ -118,7 +125,7 @@ async fn combined_protocol_fees(web3: Web3) { onchain .contracts() .weth - .approve(onchain.contracts().allowance.into_alloy(), eth(100)) + .approve(onchain.contracts().allowance.into_alloy(), 100u64.eth()) .from(trader.address()) .send_and_watch() .await @@ -128,14 +135,17 @@ async fn combined_protocol_fees(web3: Web3) { .weth .deposit() .from(trader.address()) - .value(eth(100)) + .value(100u64.eth()) .send_and_watch() .await .unwrap(); onchain .contracts() .weth - .approve(*onchain.contracts().uniswap_v2_router.address(), eth(200)) + .approve( + *onchain.contracts().uniswap_v2_router.address(), + 200u64.eth(), + ) .from(solver.address()) .send_and_watch() .await @@ -163,7 +173,7 @@ async fn combined_protocol_fees(web3: Web3) { tracing::info!("Acquiring quotes."); let quote_valid_to = model::time::now_in_epoch_seconds() + 300; - let sell_amount = to_wei(10); + let sell_amount = 10u64.eth().into_legacy(); let [limit_quote_before, market_quote_before, partner_fee_quote] = futures::future::try_join_all( [ @@ -224,13 +234,13 @@ async fn combined_protocol_fees(web3: Web3) { tracing::info!("Rebalancing AMM pools for market & limit order."); onchain - .mint_token_to_weth_uni_v2_pool(&market_order_token, eth(1000)) + .mint_token_to_weth_uni_v2_pool(&market_order_token, 1000u64.eth()) .await; onchain - .mint_token_to_weth_uni_v2_pool(&limit_order_token, eth(1000)) + .mint_token_to_weth_uni_v2_pool(&limit_order_token, 1000u64.eth()) .await; onchain - .mint_token_to_weth_uni_v2_pool(&partner_fee_order_token, eth(1000)) + .mint_token_to_weth_uni_v2_pool(&partner_fee_order_token, 1000u64.eth()) .await; tracing::info!("Waiting for liquidity state to update"); @@ -450,23 +460,29 @@ async fn surplus_partner_fee(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3.clone()).await; - let [solver] = onchain.make_solvers(eth(200)).await; - let [trader] = onchain.make_accounts(eth(200)).await; + let [solver] = onchain.make_solvers(200u64.eth()).await; + let [trader] = onchain.make_accounts(200u64.eth()).await; let [token] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(20), to_wei(20)) + .deploy_tokens_with_weth_uni_v2_pools(20u64.eth().into_legacy(), 20u64.eth().into_legacy()) .await; - token.mint(solver.address(), eth(1000)).await; + token.mint(solver.address(), 1000u64.eth()).await; token - .approve(*onchain.contracts().uniswap_v2_router.address(), eth(1000)) + .approve( + *onchain.contracts().uniswap_v2_router.address(), + 1000u64.eth(), + ) .from(solver.address()) .send_and_watch() .await .unwrap(); token - .approve(*onchain.contracts().uniswap_v2_router.address(), eth(100)) + .approve( + *onchain.contracts().uniswap_v2_router.address(), + 100u64.eth(), + ) .from(trader.address()) .send_and_watch() .await @@ -474,7 +490,7 @@ async fn surplus_partner_fee(web3: Web3) { onchain .contracts() .weth - .approve(onchain.contracts().allowance.into_alloy(), eth(100)) + .approve(onchain.contracts().allowance.into_alloy(), 100u64.eth()) .from(trader.address()) .send_and_watch() .await @@ -484,14 +500,17 @@ async fn surplus_partner_fee(web3: Web3) { .weth .deposit() .from(trader.address()) - .value(eth(100)) + .value(100u64.eth()) .send_and_watch() .await .unwrap(); onchain .contracts() .weth - .approve(*onchain.contracts().uniswap_v2_router.address(), eth(200)) + .approve( + *onchain.contracts().uniswap_v2_router.address(), + 200u64.eth(), + ) .from(solver.address()) .send_and_watch() .await @@ -511,10 +530,10 @@ async fn surplus_partner_fee(web3: Web3) { .await; let order = OrderCreation { - sell_amount: eth(10), + sell_amount: 10u64.eth(), sell_token: *onchain.contracts().weth.address(), // just set any low amount since it doesn't matter for this test - buy_amount: eth(1), + buy_amount: 1u64.eth(), buy_token: *token.address(), app_data: partner_fee_app_data.clone(), valid_to: model::time::now_in_epoch_seconds() + 300, @@ -672,18 +691,21 @@ async fn volume_fee_buy_order_test(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3.clone()).await; - let [solver] = onchain.make_solvers(eth(1)).await; - let [trader] = onchain.make_accounts(eth(1)).await; + let [solver] = onchain.make_solvers(1u64.eth()).await; + let [trader] = onchain.make_accounts(1u64.eth()).await; let [token_gno, token_dai] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1000)) + .deploy_tokens_with_weth_uni_v2_pools( + 1_000u64.eth().into_legacy(), + 1000u64.eth().into_legacy(), + ) .await; // Fund trader accounts - token_gno.mint(trader.address(), eth(100)).await; + token_gno.mint(trader.address(), 100u64.eth()).await; // Create and fund Uniswap pool - token_gno.mint(solver.address(), eth(1000)).await; - token_dai.mint(solver.address(), eth(1000)).await; + token_gno.mint(solver.address(), 1000u64.eth()).await; + token_dai.mint(solver.address(), 1000u64.eth()).await; onchain .contracts() .uniswap_v2_factory @@ -694,14 +716,20 @@ async fn volume_fee_buy_order_test(web3: Web3) { .unwrap(); token_gno - .approve(*onchain.contracts().uniswap_v2_router.address(), eth(1000)) + .approve( + *onchain.contracts().uniswap_v2_router.address(), + 1000u64.eth(), + ) .from(solver.address()) .send_and_watch() .await .unwrap(); token_dai - .approve(*onchain.contracts().uniswap_v2_router.address(), eth(1000)) + .approve( + *onchain.contracts().uniswap_v2_router.address(), + 1000u64.eth(), + ) .from(solver.address()) .send_and_watch() .await @@ -712,8 +740,8 @@ async fn volume_fee_buy_order_test(web3: Web3) { .addLiquidity( *token_gno.address(), *token_dai.address(), - eth(1000), - eth(1000), + 1000u64.eth(), + 1000u64.eth(), ::alloy::primitives::U256::ZERO, ::alloy::primitives::U256::ZERO, solver.address(), @@ -727,7 +755,7 @@ async fn volume_fee_buy_order_test(web3: Web3) { // Approve GPv2 for trading token_gno - .approve(onchain.contracts().allowance.into_alloy(), eth(100)) + .approve(onchain.contracts().allowance.into_alloy(), 100u64.eth()) .from(trader.address()) .send_and_watch() .await @@ -750,7 +778,7 @@ async fn volume_fee_buy_order_test(web3: Web3) { token_gno.address().into_legacy(), token_dai.address().into_legacy(), OrderKind::Buy, - to_wei(5), + 5u64.eth().into_legacy(), model::time::now_in_epoch_seconds() + 300, ) .await @@ -761,7 +789,7 @@ async fn volume_fee_buy_order_test(web3: Web3) { sell_token: *token_gno.address(), sell_amount: (quote.sell_amount * AlloyU256::from(3) / AlloyU256::from(2)), buy_token: *token_dai.address(), - buy_amount: eth(5), + buy_amount: 5u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Buy, ..Default::default() @@ -827,18 +855,21 @@ async fn volume_fee_buy_order_upcoming_future_test(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3.clone()).await; - let [solver] = onchain.make_solvers(eth(1)).await; - let [trader] = onchain.make_accounts(eth(1)).await; + let [solver] = onchain.make_solvers(1u64.eth()).await; + let [trader] = onchain.make_accounts(1u64.eth()).await; let [token_gno, token_dai] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1000)) + .deploy_tokens_with_weth_uni_v2_pools( + 1_000u64.eth().into_legacy(), + 1000u64.eth().into_legacy(), + ) .await; // Fund trader accounts - token_gno.mint(trader.address(), eth(100)).await; + token_gno.mint(trader.address(), 100u64.eth()).await; // Create and fund Uniswap pool - token_gno.mint(solver.address(), eth(1000)).await; - token_dai.mint(solver.address(), eth(1000)).await; + token_gno.mint(solver.address(), 1000u64.eth()).await; + token_dai.mint(solver.address(), 1000u64.eth()).await; onchain .contracts() .uniswap_v2_factory @@ -849,14 +880,20 @@ async fn volume_fee_buy_order_upcoming_future_test(web3: Web3) { .unwrap(); token_gno - .approve(*onchain.contracts().uniswap_v2_router.address(), eth(1000)) + .approve( + *onchain.contracts().uniswap_v2_router.address(), + 1000u64.eth(), + ) .from(solver.address()) .send_and_watch() .await .unwrap(); token_dai - .approve(*onchain.contracts().uniswap_v2_router.address(), eth(1000)) + .approve( + *onchain.contracts().uniswap_v2_router.address(), + 1000u64.eth(), + ) .from(solver.address()) .send_and_watch() .await @@ -867,8 +904,8 @@ async fn volume_fee_buy_order_upcoming_future_test(web3: Web3) { .addLiquidity( *token_gno.address(), *token_dai.address(), - eth(1000), - eth(1000), + 1000u64.eth(), + 1000u64.eth(), ::alloy::primitives::U256::ZERO, ::alloy::primitives::U256::ZERO, solver.address(), @@ -882,7 +919,7 @@ async fn volume_fee_buy_order_upcoming_future_test(web3: Web3) { // Approve GPv2 for trading token_gno - .approve(onchain.contracts().allowance.into_alloy(), eth(100)) + .approve(onchain.contracts().allowance.into_alloy(), 100u64.eth()) .from(trader.address()) .send_and_watch() .await @@ -905,7 +942,7 @@ async fn volume_fee_buy_order_upcoming_future_test(web3: Web3) { token_gno.address().into_legacy(), token_dai.address().into_legacy(), OrderKind::Buy, - to_wei(5), + 5u64.eth().into_legacy(), model::time::now_in_epoch_seconds() + 300, ) .await @@ -916,7 +953,7 @@ async fn volume_fee_buy_order_upcoming_future_test(web3: Web3) { sell_token: *token_gno.address(), sell_amount: (quote.sell_amount * AlloyU256::from(3) / AlloyU256::from(2)), buy_token: *token_dai.address(), - buy_amount: eth(5), + buy_amount: 5u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Buy, ..Default::default() diff --git a/crates/e2e/tests/e2e/quote_verification.rs b/crates/e2e/tests/e2e/quote_verification.rs index a1db55bf09..d162a1d0f2 100644 --- a/crates/e2e/tests/e2e/quote_verification.rs +++ b/crates/e2e/tests/e2e/quote_verification.rs @@ -1,21 +1,24 @@ use { ::alloy::{ - primitives::{Address, U256, address, utils::Unit}, + primitives::{Address, U256, address}, providers::Provider, }, bigdecimal::{BigDecimal, Zero}, - e2e::setup::{eth, *}, + e2e::setup::*, ethcontract::H160, ethrpc::{ Web3, - alloy::{CallBuilderExt, conversions::IntoAlloy}, + alloy::{ + CallBuilderExt, + conversions::{IntoAlloy, IntoLegacy}, + }, }, model::{ interaction::InteractionData, order::{BuyTokenDestination, OrderKind, SellTokenSource}, quote::{OrderQuoteRequest, OrderQuoteSide, SellAmount}, }, - number::nonzero::NonZeroU256, + number::{nonzero::NonZeroU256, units::EthUnit}, serde_json::json, shared::{ price_estimation::{ @@ -86,16 +89,19 @@ async fn standard_verified_quote(web3: Web3) { tracing::info!("Setting up chain state."); let mut onchain = OnchainComponents::deploy(web3).await; - let [solver] = onchain.make_solvers(eth(10)).await; - let [trader] = onchain.make_accounts(eth(1)).await; + let [solver] = onchain.make_solvers(10u64.eth()).await; + let [trader] = onchain.make_accounts(1u64.eth()).await; let [token] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 1_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; - token.mint(trader.address(), eth(1)).await; + token.mint(trader.address(), 1u64.eth()).await; token - .approve(onchain.contracts().allowance.into_alloy(), eth(1)) + .approve(onchain.contracts().allowance.into_alloy(), 1u64.eth()) .from(trader.address()) .send_and_watch() .await @@ -113,7 +119,7 @@ async fn standard_verified_quote(web3: Web3) { buy_token: *onchain.contracts().weth.address(), side: OrderQuoteSide::Sell { sell_amount: SellAmount::BeforeFee { - value: (U256::ONE * Unit::ETHER.wei()).try_into().unwrap(), + value: (1u64.eth()).try_into().unwrap(), }, }, ..Default::default() @@ -234,10 +240,13 @@ async fn verified_quote_eth_balance(web3: Web3) { tracing::info!("Setting up chain state."); let mut onchain = OnchainComponents::deploy(web3).await; - let [solver] = onchain.make_solvers(eth(10)).await; - let [trader] = onchain.make_accounts(eth(1)).await; + let [solver] = onchain.make_solvers(10u64.eth()).await; + let [trader] = onchain.make_accounts(1u64.eth()).await; let [token] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 1_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; let weth = &onchain.contracts().weth; @@ -268,7 +277,7 @@ async fn verified_quote_eth_balance(web3: Web3) { buy_token: *token.address(), side: OrderQuoteSide::Sell { sell_amount: SellAmount::BeforeFee { - value: (U256::ONE * Unit::ETHER.wei()).try_into().unwrap(), + value: (1u64.eth()).try_into().unwrap(), }, }, ..Default::default() @@ -284,16 +293,19 @@ async fn verified_quote_for_settlement_contract(web3: Web3) { tracing::info!("Setting up chain state."); let mut onchain = OnchainComponents::deploy(web3).await; - let [solver] = onchain.make_solvers(eth(10)).await; - let [trader] = onchain.make_accounts(eth(3)).await; + let [solver] = onchain.make_solvers(10u64.eth()).await; + let [trader] = onchain.make_accounts(3u64.eth()).await; let [token] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 1_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; // Send 3 ETH to the settlement contract so we can get verified quotes for // selling WETH. onchain - .send_wei(*onchain.contracts().gp_settlement.address(), eth(3)) + .send_wei(*onchain.contracts().gp_settlement.address(), 3u64.eth()) .await; tracing::info!("Starting services."); @@ -305,7 +317,7 @@ async fn verified_quote_for_settlement_contract(web3: Web3) { buy_token: *token.address(), side: OrderQuoteSide::Sell { sell_amount: SellAmount::BeforeFee { - value: (U256::from(3) * Unit::ETHER.wei()).try_into().unwrap(), + value: (3u64.eth()).try_into().unwrap(), }, }, ..Default::default() @@ -362,10 +374,13 @@ async fn verified_quote_with_simulated_balance(web3: Web3) { tracing::info!("Setting up chain state."); let mut onchain = OnchainComponents::deploy(web3).await; - let [solver] = onchain.make_solvers(eth(10)).await; - let [trader] = onchain.make_accounts(eth(0)).await; + let [solver] = onchain.make_solvers(10u64.eth()).await; + let [trader] = onchain.make_accounts(0u64.eth()).await; let [token] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 1_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; let weth = &onchain.contracts().weth; @@ -410,7 +425,7 @@ async fn verified_quote_with_simulated_balance(web3: Web3) { buy_token: *weth.address(), side: OrderQuoteSide::Sell { sell_amount: SellAmount::BeforeFee { - value: (U256::ONE * Unit::ETHER.wei()).try_into().unwrap(), + value: (1u64.eth()).try_into().unwrap(), }, }, ..Default::default() @@ -450,7 +465,7 @@ async fn verified_quote_with_simulated_balance(web3: Web3) { buy_token: *token.address(), side: OrderQuoteSide::Sell { sell_amount: SellAmount::BeforeFee { - value: (U256::ONE * Unit::ETHER.wei()).try_into().unwrap(), + value: (1u64.eth()).try_into().unwrap(), }, }, ..Default::default() @@ -468,7 +483,7 @@ async fn verified_quote_with_simulated_balance(web3: Web3) { buy_token: *token.address(), side: OrderQuoteSide::Sell { sell_amount: SellAmount::BeforeFee { - value: (U256::ONE * Unit::ETHER.wei()).try_into().unwrap(), + value: (1u64.eth()).try_into().unwrap(), }, }, ..Default::default() @@ -486,7 +501,7 @@ async fn verified_quote_with_simulated_balance(web3: Web3) { buy_token: *token.address(), side: OrderQuoteSide::Sell { sell_amount: SellAmount::BeforeFee { - value: (U256::ONE * Unit::ETHER.wei()).try_into().unwrap(), + value: (1u64.eth()).try_into().unwrap(), }, }, app_data: model::order::OrderCreationAppData::Full { @@ -517,7 +532,7 @@ async fn verified_quote_with_simulated_balance(web3: Web3) { async fn usdt_quote_verification(web3: Web3) { let mut onchain = OnchainComponents::deployed(web3.clone()).await; - let [solver] = onchain.make_solvers_forked(eth(1)).await; + let [solver] = onchain.make_solvers_forked(1u64.eth()).await; let usdc = address!("a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"); let usdt = address!("dac17f958d2ee523a2206206994597c13d831ec7"); @@ -540,7 +555,7 @@ async fn usdt_quote_verification(web3: Web3) { buy_token: usdc, side: OrderQuoteSide::Sell { sell_amount: SellAmount::BeforeFee { - value: (U256::from(1000) * Unit::ETHER.wei()).try_into().unwrap(), + value: (1000u64.eth()).try_into().unwrap(), }, }, ..Default::default() diff --git a/crates/e2e/tests/e2e/quoting.rs b/crates/e2e/tests/e2e/quoting.rs index bcd00fb887..7074d2e73a 100644 --- a/crates/e2e/tests/e2e/quoting.rs +++ b/crates/e2e/tests/e2e/quoting.rs @@ -1,14 +1,16 @@ use { - ::alloy::primitives::{U256, utils::Unit}, - e2e::setup::{colocation::SolverEngine, eth, mock::Mock, *}, - ethrpc::alloy::{CallBuilderExt, conversions::IntoAlloy}, + e2e::setup::{colocation::SolverEngine, mock::Mock, *}, + ethrpc::alloy::{ + CallBuilderExt, + conversions::{IntoAlloy, IntoLegacy}, + }, futures::FutureExt, model::{ order::{OrderCreation, OrderCreationAppData, OrderKind}, quote::{OrderQuoteRequest, OrderQuoteSide, QuoteSigningScheme, SellAmount}, signature::EcdsaSigningScheme, }, - number::nonzero::NonZeroU256, + number::{nonzero::NonZeroU256, units::EthUnit}, secp256k1::SecretKey, serde_json::json, shared::ethrpc::Web3, @@ -50,16 +52,19 @@ async fn test(web3: Web3) { tracing::info!("Setting up chain state."); let mut onchain = OnchainComponents::deploy(web3).await; - let [solver] = onchain.make_solvers(eth(10)).await; - let [trader] = onchain.make_accounts(eth(10)).await; + let [solver] = onchain.make_solvers(10u64.eth()).await; + let [trader] = onchain.make_accounts(10u64.eth()).await; let [token] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 1_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; onchain .contracts() .weth - .approve(onchain.contracts().allowance.into_alloy(), eth(3)) + .approve(onchain.contracts().allowance.into_alloy(), 3u64.eth()) .from(trader.address()) .send_and_watch() .await @@ -69,7 +74,7 @@ async fn test(web3: Web3) { .weth .deposit() .from(trader.address()) - .value(eth(3)) + .value(3u64.eth()) .send_and_watch() .await .unwrap(); @@ -94,7 +99,7 @@ async fn test(web3: Web3) { buy_token: *token.address(), side: OrderQuoteSide::Sell { sell_amount: SellAmount::BeforeFee { - value: NonZeroU256::try_from(U256::ONE * Unit::ETHER.wei()).unwrap(), + value: NonZeroU256::try_from(1u64.eth()).unwrap(), }, }, ..Default::default() @@ -198,16 +203,19 @@ async fn uses_stale_liquidity(web3: Web3) { tracing::info!("Setting up chain state."); let mut onchain = OnchainComponents::deploy(web3.clone()).await; - let [solver] = onchain.make_solvers(eth(10)).await; - let [trader] = onchain.make_accounts(eth(2)).await; + let [solver] = onchain.make_solvers(10u64.eth()).await; + let [trader] = onchain.make_accounts(2u64.eth()).await; let [token] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 1_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; onchain .contracts() .weth - .approve(onchain.contracts().allowance.into_alloy(), eth(1)) + .approve(onchain.contracts().allowance.into_alloy(), 1u64.eth()) .from(trader.address()) .send_and_watch() .await @@ -217,7 +225,7 @@ async fn uses_stale_liquidity(web3: Web3) { .weth .deposit() .from(trader.address()) - .value(eth(1)) + .value(1u64.eth()) .send_and_watch() .await .unwrap(); @@ -232,7 +240,7 @@ async fn uses_stale_liquidity(web3: Web3) { buy_token: *token.address(), side: OrderQuoteSide::Sell { sell_amount: SellAmount::AfterFee { - value: NonZeroU256::new(U256::ONE * Unit::ETHER.wei()).unwrap(), + value: NonZeroU256::new(1u64.eth()).unwrap(), }, }, ..Default::default() @@ -244,7 +252,7 @@ async fn uses_stale_liquidity(web3: Web3) { // Now, we want to manually unbalance the pools and assert that the quote // doesn't change (as the price estimation will use stale pricing data). onchain - .mint_token_to_weth_uni_v2_pool(&token, eth(1_000)) + .mint_token_to_weth_uni_v2_pool(&token, 1_000u64.eth()) .await; tracing::info!("performining second quote, which should match first"); @@ -270,10 +278,13 @@ async fn quote_timeout(web3: Web3) { tracing::info!("Setting up chain state."); let mut onchain = OnchainComponents::deploy(web3.clone()).await; - let [solver] = onchain.make_solvers(eth(10)).await; - let [trader] = onchain.make_accounts(eth(2)).await; + let [solver] = onchain.make_solvers(10u64.eth()).await; + let [trader] = onchain.make_accounts(2u64.eth()).await; let [sell_token] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 1_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; tracing::info!("Starting services."); @@ -333,7 +344,7 @@ async fn quote_timeout(web3: Web3) { buy_token: *sell_token.address(), side: OrderQuoteSide::Sell { sell_amount: SellAmount::BeforeFee { - value: NonZeroU256::try_from(U256::ONE * Unit::ETHER.wei()).unwrap(), + value: NonZeroU256::try_from(1u64.eth()).unwrap(), }, }, timeout, @@ -381,10 +392,10 @@ async fn quote_timeout(web3: Web3) { assert_within_variance(start, MAX_QUOTE_TIME_MS); // set up trader to pass balance checks during order creation - sell_token.mint(trader.address(), eth(1)).await; + sell_token.mint(trader.address(), 1u64.eth()).await; sell_token - .approve(onchain.contracts().allowance.into_alloy(), eth(1)) + .approve(onchain.contracts().allowance.into_alloy(), 1u64.eth()) .from(trader.address()) .send_and_watch() .await @@ -392,9 +403,9 @@ async fn quote_timeout(web3: Web3) { let order = OrderCreation { sell_token: *sell_token.address(), - sell_amount: eth(1), + sell_amount: 1u64.eth(), buy_token: Default::default(), - buy_amount: eth(1), + buy_amount: 1u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, partially_fillable: true, @@ -419,16 +430,19 @@ async fn quote_timeout(web3: Web3) { async fn volume_fee(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3).await; - let [solver] = onchain.make_solvers(eth(10)).await; - let [trader] = onchain.make_accounts(eth(10)).await; + let [solver] = onchain.make_solvers(10u64.eth()).await; + let [trader] = onchain.make_accounts(10u64.eth()).await; let [token] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 1_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; onchain .contracts() .weth - .approve(onchain.contracts().allowance.into_alloy(), eth(3)) + .approve(onchain.contracts().allowance.into_alloy(), 3u64.eth()) .from(trader.address()) .send_and_watch() .await @@ -438,7 +452,7 @@ async fn volume_fee(web3: Web3) { .weth .deposit() .from(trader.address()) - .value(eth(3)) + .value(3u64.eth()) .send_and_watch() .await .unwrap(); @@ -463,7 +477,7 @@ async fn volume_fee(web3: Web3) { buy_token: *token.address(), side: OrderQuoteSide::Sell { sell_amount: SellAmount::BeforeFee { - value: NonZeroU256::try_from(U256::ONE * Unit::ETHER.wei()).unwrap(), + value: NonZeroU256::try_from(1u64.eth()).unwrap(), }, }, ..Default::default() @@ -481,7 +495,7 @@ async fn volume_fee(web3: Web3) { sell_token: *onchain.contracts().weth.address(), buy_token: *token.address(), side: OrderQuoteSide::Buy { - buy_amount_after_fee: NonZeroU256::try_from(U256::ONE * Unit::ETHER.wei()).unwrap(), + buy_amount_after_fee: NonZeroU256::try_from(1u64.eth()).unwrap(), }, ..Default::default() }; diff --git a/crates/e2e/tests/e2e/refunder.rs b/crates/e2e/tests/e2e/refunder.rs index 7881eef9a6..125b3d8fc2 100644 --- a/crates/e2e/tests/e2e/refunder.rs +++ b/crates/e2e/tests/e2e/refunder.rs @@ -5,11 +5,11 @@ use { e2e::{nodes::local_node::TestNodeApi, setup::*}, ethrpc::{ Web3, - alloy::conversions::TryIntoAlloyAsync, + alloy::conversions::{IntoLegacy, TryIntoAlloyAsync}, block_stream::timestamp_of_current_block_in_seconds, }, model::quote::{OrderQuoteRequest, OrderQuoteSide, QuoteSigningScheme, Validity}, - number::nonzero::NonZeroU256, + number::{nonzero::NonZeroU256, units::EthUnit}, refunder::refund_service::RefundService, sqlx::PgPool, }; @@ -23,10 +23,13 @@ async fn local_node_refunder_tx() { async fn refunder_tx(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3.clone()).await; - let [solver] = onchain.make_solvers(eth(10)).await; - let [user, refunder] = onchain.make_accounts(eth(10)).await; + let [solver] = onchain.make_solvers(10u64.eth()).await; + let [user, refunder] = onchain.make_accounts(10u64.eth()).await; let [token] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 1_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; let services = Services::new(&onchain).await; diff --git a/crates/e2e/tests/e2e/replace_order.rs b/crates/e2e/tests/e2e/replace_order.rs index acfb65a3f5..313a577702 100644 --- a/crates/e2e/tests/e2e/replace_order.rs +++ b/crates/e2e/tests/e2e/replace_order.rs @@ -1,14 +1,15 @@ use { ::alloy::primitives::U256, - e2e::{ - nodes::local_node::TestNodeApi, - setup::{eth, *}, + e2e::{nodes::local_node::TestNodeApi, setup::*}, + ethrpc::alloy::{ + CallBuilderExt, + conversions::{IntoAlloy, IntoLegacy}, }, - ethrpc::alloy::{CallBuilderExt, conversions::IntoAlloy}, model::{ order::{OrderCreation, OrderCreationAppData, OrderKind, OrderStatus}, signature::EcdsaSigningScheme, }, + number::units::EthUnit, orderbook::{ api::IntoWarpReply, orderbook::{OrderCancellationError, OrderReplacementError}, @@ -41,18 +42,21 @@ async fn local_node_try_replace_executed_order() { async fn try_replace_unreplaceable_order_test(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3.clone()).await; - let [solver] = onchain.make_solvers(eth(1)).await; - let [trader] = onchain.make_accounts(eth(1)).await; + let [solver] = onchain.make_solvers(1u64.eth()).await; + let [trader] = onchain.make_accounts(1u64.eth()).await; let [token_a, token_b] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 1_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; // Fund trader accounts - token_a.mint(trader.address(), eth(30)).await; + token_a.mint(trader.address(), 30u64.eth()).await; // Create and fund Uniswap pool - token_a.mint(solver.address(), eth(1000)).await; - token_b.mint(solver.address(), eth(1000)).await; + token_a.mint(solver.address(), 1000u64.eth()).await; + token_b.mint(solver.address(), 1000u64.eth()).await; onchain .contracts() .uniswap_v2_factory @@ -63,14 +67,20 @@ async fn try_replace_unreplaceable_order_test(web3: Web3) { .unwrap(); token_a - .approve(*onchain.contracts().uniswap_v2_router.address(), eth(1000)) + .approve( + *onchain.contracts().uniswap_v2_router.address(), + 1000u64.eth(), + ) .from(solver.address()) .send_and_watch() .await .unwrap(); token_b - .approve(*onchain.contracts().uniswap_v2_router.address(), eth(1000)) + .approve( + *onchain.contracts().uniswap_v2_router.address(), + 1000u64.eth(), + ) .from(solver.address()) .send_and_watch() .await @@ -81,8 +91,8 @@ async fn try_replace_unreplaceable_order_test(web3: Web3) { .addLiquidity( *token_a.address(), *token_b.address(), - eth(1000), - eth(1000), + 1000u64.eth(), + 1000u64.eth(), U256::ZERO, U256::ZERO, solver.address(), @@ -96,7 +106,7 @@ async fn try_replace_unreplaceable_order_test(web3: Web3) { // Approve GPv2 for trading token_a - .approve(onchain.contracts().allowance.into_alloy(), eth(15)) + .approve(onchain.contracts().allowance.into_alloy(), 15u64.eth()) .from(trader.address()) .send_and_watch() .await @@ -114,9 +124,9 @@ async fn try_replace_unreplaceable_order_test(web3: Web3) { let order = OrderCreation { sell_token: *token_a.address(), - sell_amount: eth(10), + sell_amount: 10u64.eth(), buy_token: *token_b.address(), - buy_amount: eth(5), + buy_amount: 5u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, ..Default::default() @@ -145,9 +155,9 @@ async fn try_replace_unreplaceable_order_test(web3: Web3) { // Replace order let new_order = OrderCreation { sell_token: *token_a.address(), - sell_amount: eth(3), + sell_amount: 3u64.eth(), buy_token: *token_b.address(), - buy_amount: eth(1), + buy_amount: 1u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, partially_fillable: false, @@ -187,7 +197,7 @@ async fn try_replace_unreplaceable_order_test(web3: Web3) { tracing::info!("Waiting for the old order to be executed"); wait_for_condition(TIMEOUT, || async { let balance_after = token_a.balanceOf(trader.address()).call().await.unwrap(); - balance_before.saturating_sub(balance_after) == eth(10) + balance_before.saturating_sub(balance_after) == 10u64.eth() && !services.get_trades(&order_id).await.unwrap().is_empty() }) .await @@ -213,19 +223,22 @@ async fn try_replace_unreplaceable_order_test(web3: Web3) { async fn try_replace_someone_else_order_test(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3.clone()).await; - let [solver] = onchain.make_solvers(eth(1)).await; - let [trader_a, trader_b] = onchain.make_accounts(eth(1)).await; + let [solver] = onchain.make_solvers(1u64.eth()).await; + let [trader_a, trader_b] = onchain.make_accounts(1u64.eth()).await; let [token_a, token_b] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 1_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; // Fund trader accounts - token_a.mint(trader_a.address(), eth(30)).await; - token_a.mint(trader_b.address(), eth(30)).await; + token_a.mint(trader_a.address(), 30u64.eth()).await; + token_a.mint(trader_b.address(), 30u64.eth()).await; // Create and fund Uniswap pool - token_a.mint(solver.address(), eth(1000)).await; - token_b.mint(solver.address(), eth(1000)).await; + token_a.mint(solver.address(), 1000u64.eth()).await; + token_b.mint(solver.address(), 1000u64.eth()).await; onchain .contracts() .uniswap_v2_factory @@ -236,14 +249,20 @@ async fn try_replace_someone_else_order_test(web3: Web3) { .unwrap(); token_a - .approve(*onchain.contracts().uniswap_v2_router.address(), eth(1000)) + .approve( + *onchain.contracts().uniswap_v2_router.address(), + 1000u64.eth(), + ) .from(solver.address()) .send_and_watch() .await .unwrap(); token_b - .approve(*onchain.contracts().uniswap_v2_router.address(), eth(1000)) + .approve( + *onchain.contracts().uniswap_v2_router.address(), + 1000u64.eth(), + ) .from(solver.address()) .send_and_watch() .await @@ -254,8 +273,8 @@ async fn try_replace_someone_else_order_test(web3: Web3) { .addLiquidity( *token_a.address(), *token_b.address(), - eth(1000), - eth(1000), + 1000u64.eth(), + 1000u64.eth(), U256::ZERO, U256::ZERO, solver.address(), @@ -269,14 +288,14 @@ async fn try_replace_someone_else_order_test(web3: Web3) { // Approve GPv2 for trading token_a - .approve(onchain.contracts().allowance.into_alloy(), eth(15)) + .approve(onchain.contracts().allowance.into_alloy(), 15u64.eth()) .from(trader_a.address()) .send_and_watch() .await .unwrap(); token_a - .approve(onchain.contracts().allowance.into_alloy(), eth(15)) + .approve(onchain.contracts().allowance.into_alloy(), 15u64.eth()) .from(trader_b.address()) .send_and_watch() .await @@ -290,9 +309,9 @@ async fn try_replace_someone_else_order_test(web3: Web3) { let order = OrderCreation { sell_token: *token_a.address(), - sell_amount: eth(10), + sell_amount: 10u64.eth(), buy_token: *token_b.address(), - buy_amount: eth(5), + buy_amount: 5u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, partially_fillable: false, kind: OrderKind::Sell, @@ -308,9 +327,9 @@ async fn try_replace_someone_else_order_test(web3: Web3) { // Replace order let new_order = OrderCreation { sell_token: *token_a.address(), - sell_amount: eth(3), + sell_amount: 3u64.eth(), buy_token: *token_b.address(), - buy_amount: eth(1), + buy_amount: 1u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, partially_fillable: false, @@ -336,7 +355,7 @@ async fn try_replace_someone_else_order_test(web3: Web3) { wait_for_condition(TIMEOUT, || async { onchain.mint_block().await; let balance_after = token_a.balanceOf(trader_a.address()).call().await.unwrap(); - balance_before.saturating_sub(balance_after) == eth(10) + balance_before.saturating_sub(balance_after) == 10u64.eth() }) .await .unwrap(); @@ -345,18 +364,21 @@ async fn try_replace_someone_else_order_test(web3: Web3) { async fn single_replace_order_test(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3.clone()).await; - let [solver] = onchain.make_solvers(eth(1)).await; - let [trader] = onchain.make_accounts(eth(1)).await; + let [solver] = onchain.make_solvers(1u64.eth()).await; + let [trader] = onchain.make_accounts(1u64.eth()).await; let [token_a, token_b] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 1_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; // Fund trader accounts - token_a.mint(trader.address(), eth(30)).await; + token_a.mint(trader.address(), 30u64.eth()).await; // Create and fund Uniswap pool - token_a.mint(solver.address(), eth(1000)).await; - token_b.mint(solver.address(), eth(1000)).await; + token_a.mint(solver.address(), 1000u64.eth()).await; + token_b.mint(solver.address(), 1000u64.eth()).await; onchain .contracts() .uniswap_v2_factory @@ -367,14 +389,20 @@ async fn single_replace_order_test(web3: Web3) { .unwrap(); token_a - .approve(*onchain.contracts().uniswap_v2_router.address(), eth(1000)) + .approve( + *onchain.contracts().uniswap_v2_router.address(), + 1000u64.eth(), + ) .from(solver.address()) .send_and_watch() .await .unwrap(); token_b - .approve(*onchain.contracts().uniswap_v2_router.address(), eth(1000)) + .approve( + *onchain.contracts().uniswap_v2_router.address(), + 1000u64.eth(), + ) .from(solver.address()) .send_and_watch() .await @@ -385,8 +413,8 @@ async fn single_replace_order_test(web3: Web3) { .addLiquidity( *token_a.address(), *token_b.address(), - eth(1000), - eth(1000), + 1000u64.eth(), + 1000u64.eth(), U256::ZERO, U256::ZERO, solver.address(), @@ -400,7 +428,7 @@ async fn single_replace_order_test(web3: Web3) { // Approve GPv2 for trading token_a - .approve(onchain.contracts().allowance.into_alloy(), eth(15)) + .approve(onchain.contracts().allowance.into_alloy(), 15u64.eth()) .from(trader.address()) .send_and_watch() .await @@ -427,9 +455,9 @@ async fn single_replace_order_test(web3: Web3) { let balance_before = token_a.balanceOf(trader.address()).call().await.unwrap(); let order = OrderCreation { sell_token: *token_a.address(), - sell_amount: eth(10), + sell_amount: 10u64.eth(), buy_token: *token_b.address(), - buy_amount: eth(5), + buy_amount: 5u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, ..Default::default() @@ -456,9 +484,9 @@ async fn single_replace_order_test(web3: Web3) { // Replace order let new_order = OrderCreation { sell_token: *token_a.address(), - sell_amount: eth(3), + sell_amount: 3u64.eth(), buy_token: *token_b.address(), - buy_amount: eth(1), + buy_amount: 1u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, partially_fillable: false, @@ -505,7 +533,7 @@ async fn single_replace_order_test(web3: Web3) { wait_for_condition(TIMEOUT, || async { let balance_after = token_a.balanceOf(trader.address()).call().await.unwrap(); onchain.mint_block().await; - balance_before.saturating_sub(balance_after) == eth(3) + balance_before.saturating_sub(balance_after) == 3u64.eth() }) .await .unwrap(); diff --git a/crates/e2e/tests/e2e/smart_contract_orders.rs b/crates/e2e/tests/e2e/smart_contract_orders.rs index f8bbc598fc..8104eea948 100644 --- a/crates/e2e/tests/e2e/smart_contract_orders.rs +++ b/crates/e2e/tests/e2e/smart_contract_orders.rs @@ -1,12 +1,16 @@ use { ::alloy::primitives::Address, - e2e::setup::{eth, safe::Safe, *}, + e2e::setup::{safe::Safe, *}, ethcontract::U256, - ethrpc::alloy::{CallBuilderExt, conversions::IntoAlloy}, + ethrpc::alloy::{ + CallBuilderExt, + conversions::{IntoAlloy, IntoLegacy}, + }, model::{ order::{OrderCreation, OrderCreationAppData, OrderKind, OrderStatus, OrderUid}, signature::Signature, }, + number::units::EthUnit, reqwest::StatusCode, shared::ethrpc::Web3, }; @@ -26,20 +30,23 @@ async fn local_node_max_gas_limit() { async fn smart_contract_orders(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3.clone()).await; - let [solver] = onchain.make_solvers(eth(1)).await; - let [trader] = onchain.make_accounts(eth(1)).await; + let [solver] = onchain.make_solvers(1u64.eth()).await; + let [trader] = onchain.make_accounts(1u64.eth()).await; let safe = Safe::deploy(trader, web3.alloy.clone()).await; let [token] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(100_000), to_wei(100_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 100_000u64.eth().into_legacy(), + 100_000u64.eth().into_legacy(), + ) .await; - token.mint(safe.address(), eth(10)).await; + token.mint(safe.address(), 10u64.eth()).await; // Approve GPv2 for trading safe.exec_alloy_call( token - .approve(onchain.contracts().allowance.into_alloy(), eth(10)) + .approve(onchain.contracts().allowance.into_alloy(), 10u64.eth()) .into_transaction_request(), ) .await; @@ -50,9 +57,9 @@ async fn smart_contract_orders(web3: Web3) { let order_template = OrderCreation { kind: OrderKind::Sell, sell_token: *token.address(), - sell_amount: eth(5), + sell_amount: 5u64.eth(), buy_token: *onchain.contracts().weth.address(), - buy_amount: eth(3), + buy_amount: 3u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, ..Default::default() }; @@ -145,7 +152,7 @@ async fn smart_contract_orders(web3: Web3) { .await .expect("Couldn't fetch native token balance"); - token_balance.is_zero() && weth_balance > eth(6) + token_balance.is_zero() && weth_balance > 6u64.eth() }) .await .unwrap(); @@ -154,22 +161,26 @@ async fn smart_contract_orders(web3: Web3) { async fn erc1271_gas_limit(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3.clone()).await; - let [solver] = onchain.make_solvers(eth(1)).await; + let [solver] = onchain.make_solvers(1u64.eth()).await; let trader = contracts::alloy::test::GasHog::Instance::deploy(web3.alloy.clone()) .await .unwrap(); let cow = onchain - .deploy_cow_weth_pool(to_wei(1_000_000), to_wei(1_000), to_wei(1_000)) + .deploy_cow_weth_pool( + 1_000_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; // Fund trader accounts and approve relayer - cow.fund(*trader.address(), eth(5)).await; + cow.fund(*trader.address(), 5u64.eth()).await; trader .approve( *cow.address(), onchain.contracts().allowance.into_alloy(), - eth(10), + 10u64.eth(), ) .from(solver.address()) .send_and_watch() @@ -193,9 +204,9 @@ async fn erc1271_gas_limit(web3: Web3) { let order = OrderCreation { sell_token: *cow.address(), - sell_amount: eth(4), + sell_amount: 4u64.eth(), buy_token: *onchain.contracts().weth.address(), - buy_amount: eth(3), + buy_amount: 3u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, signature: Signature::Eip1271(signature.to_vec()), diff --git a/crates/e2e/tests/e2e/solver_competition.rs b/crates/e2e/tests/e2e/solver_competition.rs index 88b1910044..f468181e5c 100644 --- a/crates/e2e/tests/e2e/solver_competition.rs +++ b/crates/e2e/tests/e2e/solver_competition.rs @@ -1,6 +1,6 @@ use { ::alloy::primitives::U256, - e2e::setup::{colocation::SolverEngine, eth, mock::Mock, *}, + e2e::setup::{colocation::SolverEngine, mock::Mock, *}, ethrpc::alloy::{ CallBuilderExt, conversions::{IntoAlloy, IntoLegacy}, @@ -9,6 +9,7 @@ use { order::{OrderCreation, OrderKind}, signature::EcdsaSigningScheme, }, + number::units::EthUnit, secp256k1::SecretKey, shared::ethrpc::Web3, solvers_dto::solution::Solution, @@ -37,20 +38,23 @@ async fn local_node_store_filtered_solutions() { async fn solver_competition(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3).await; - let [solver] = onchain.make_solvers(eth(1)).await; - let [trader] = onchain.make_accounts(eth(1)).await; + let [solver] = onchain.make_solvers(1u64.eth()).await; + let [trader] = onchain.make_accounts(1u64.eth()).await; let [token_a] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 1_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; // Fund trader, settlement accounts, and pool creation - token_a.mint(trader.address(), eth(10)).await; - token_a.mint(solver.address(), eth(1000)).await; + token_a.mint(trader.address(), 10u64.eth()).await; + token_a.mint(solver.address(), 1000u64.eth()).await; // Approve GPv2 for trading token_a - .approve(onchain.contracts().allowance.into_alloy(), eth(100)) + .approve(onchain.contracts().allowance.into_alloy(), 100u64.eth()) .from(trader.address()) .send_and_watch() .await @@ -99,9 +103,9 @@ async fn solver_competition(web3: Web3) { // Place Order let order = OrderCreation { sell_token: *token_a.address(), - sell_amount: eth(10), + sell_amount: 10u64.eth(), buy_token: *onchain.contracts().weth.address(), - buy_amount: eth(5), + buy_amount: 5u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, ..Default::default() @@ -156,40 +160,46 @@ async fn solver_competition(web3: Web3) { async fn wrong_solution_submission_address(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3).await; - let [solver] = onchain.make_solvers(eth(1)).await; - let [trader_a, trader_b] = onchain.make_accounts(eth(1)).await; + let [solver] = onchain.make_solvers(1u64.eth()).await; + let [trader_a, trader_b] = onchain.make_accounts(1u64.eth()).await; let [token_a, token_b] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 1_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; // Fund traders - token_a.mint(trader_a.address(), eth(10)).await; - token_b.mint(trader_b.address(), eth(10)).await; + token_a.mint(trader_a.address(), 10u64.eth()).await; + token_b.mint(trader_b.address(), 10u64.eth()).await; // Create more liquid routes between token_a (token_b) and weth via base_a // (base_b). base_a has more liquidity then base_b, leading to the solver that // knows about base_a to win let [base_a, base_b] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(10_000), to_wei(10_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 10_000u64.eth().into_legacy(), + 10_000u64.eth().into_legacy(), + ) .await; onchain - .seed_uni_v2_pool((&token_a, eth(100_000)), (&base_a, eth(100_000))) + .seed_uni_v2_pool((&token_a, 100_000u64.eth()), (&base_a, 100_000u64.eth())) .await; onchain - .seed_uni_v2_pool((&token_b, eth(10_000)), (&base_b, eth(10_000))) + .seed_uni_v2_pool((&token_b, 10_000u64.eth()), (&base_b, 10_000u64.eth())) .await; // Approve GPv2 for trading token_a - .approve(onchain.contracts().allowance.into_alloy(), eth(100)) + .approve(onchain.contracts().allowance.into_alloy(), 100u64.eth()) .from(trader_a.address()) .send_and_watch() .await .unwrap(); token_b - .approve(onchain.contracts().allowance.into_alloy(), eth(100)) + .approve(onchain.contracts().allowance.into_alloy(), 100u64.eth()) .from(trader_b.address()) .send_and_watch() .await @@ -241,9 +251,9 @@ async fn wrong_solution_submission_address(web3: Web3) { // Place Orders let order_a = OrderCreation { sell_token: *token_a.address(), - sell_amount: eth(10), + sell_amount: 10u64.eth(), buy_token: *onchain.contracts().weth.address(), - buy_amount: eth(5), + buy_amount: 5u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, ..Default::default() @@ -259,9 +269,9 @@ async fn wrong_solution_submission_address(web3: Web3) { let order_b = OrderCreation { sell_token: *token_b.address(), - sell_amount: eth(10), + sell_amount: 10u64.eth(), buy_token: *onchain.contracts().weth.address(), - buy_amount: eth(5), + buy_amount: 5u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, ..Default::default() @@ -303,26 +313,29 @@ async fn wrong_solution_submission_address(web3: Web3) { async fn store_filtered_solutions(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3.clone()).await; - let [good_solver_account, bad_solver_account] = onchain.make_solvers(eth(100)).await; - let [trader] = onchain.make_accounts(eth(100)).await; + let [good_solver_account, bad_solver_account] = onchain.make_solvers(100u64.eth()).await; + let [trader] = onchain.make_accounts(100u64.eth()).await; let [token_a, token_b, token_c] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(300_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 300_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; // give the settlement contract a ton of the traded tokens so that the mocked // solver solutions can simply give money away to make the trade execute token_b - .mint(*onchain.contracts().gp_settlement.address(), eth(50)) + .mint(*onchain.contracts().gp_settlement.address(), 50u64.eth()) .await; token_c - .mint(*onchain.contracts().gp_settlement.address(), eth(50)) + .mint(*onchain.contracts().gp_settlement.address(), 50u64.eth()) .await; // set up trader for their order - token_a.mint(trader.address(), eth(2)).await; + token_a.mint(trader.address(), 2u64.eth()).await; token_a - .approve(onchain.contracts().allowance.into_alloy(), eth(2)) + .approve(onchain.contracts().allowance.into_alloy(), 2u64.eth()) .from(trader.address()) .send_and_watch() .await @@ -392,9 +405,9 @@ async fn store_filtered_solutions(web3: Web3) { // Place order let order_ab = OrderCreation { sell_token: *token_a.address(), - sell_amount: eth(1), + sell_amount: 1u64.eth(), buy_token: *token_b.address(), - buy_amount: eth(1), + buy_amount: 1u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, ..Default::default() @@ -407,9 +420,9 @@ async fn store_filtered_solutions(web3: Web3) { let order_ac = OrderCreation { sell_token: *token_a.address(), - sell_amount: eth(1), + sell_amount: 1u64.eth(), buy_token: *token_c.address(), - buy_amount: eth(1), + buy_amount: 1u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, ..Default::default() @@ -435,7 +448,10 @@ async fn store_filtered_solutions(web3: Web3) { // good solver settles order_ab at a price 3:1 good_solver.configure_solution(Some(Solution { id: 0, - prices: HashMap::from([(*token_a.address(), eth(3)), (*token_b.address(), eth(1))]), + prices: HashMap::from([ + (*token_a.address(), 3u64.eth()), + (*token_b.address(), 1u64.eth()), + ]), trades: vec![solvers_dto::solution::Trade::Fulfillment( solvers_dto::solution::Fulfillment { executed_amount: order_ab.sell_amount, @@ -457,9 +473,9 @@ async fn store_filtered_solutions(web3: Web3) { bad_solver.configure_solution(Some(Solution { id: 0, prices: HashMap::from([ - (*token_a.address(), eth(2)), - (*token_b.address(), eth(1)), - (*token_c.address(), eth(1)), + (*token_a.address(), 2u64.eth()), + (*token_b.address(), 1u64.eth()), + (*token_c.address(), 1u64.eth()), ]), trades: vec![ solvers_dto::solution::Trade::Fulfillment(solvers_dto::solution::Fulfillment { diff --git a/crates/e2e/tests/e2e/solver_participation_guard.rs b/crates/e2e/tests/e2e/solver_participation_guard.rs index 5b302ec30e..8b3ccc3412 100644 --- a/crates/e2e/tests/e2e/solver_participation_guard.rs +++ b/crates/e2e/tests/e2e/solver_participation_guard.rs @@ -8,19 +8,21 @@ use { Services, TIMEOUT, TestAccount, - eth, run_test, - to_wei, wait_for_condition, }, ethrpc::{ Web3, - alloy::{CallBuilderExt, conversions::IntoAlloy}, + alloy::{ + CallBuilderExt, + conversions::{IntoAlloy, IntoLegacy}, + }, }, model::{ order::{OrderClass, OrderCreation, OrderKind}, signature::EcdsaSigningScheme, }, + number::units::EthUnit, secp256k1::SecretKey, sqlx::Row, std::time::Instant, @@ -48,7 +50,7 @@ async fn local_node_not_allowed_solver() { async fn non_settling_solver(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3.clone()).await; - let [solver, solver_b] = onchain.make_solvers(eth(1)).await; + let [solver, solver_b] = onchain.make_solvers(1u64.eth()).await; let (trader_a, token_a, token_b) = setup(&mut onchain, &solver).await; let services = Services::new(&onchain).await; @@ -119,7 +121,7 @@ async fn non_settling_solver(web3: Web3) { async fn low_settling_solver(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3.clone()).await; - let [solver, solver_b] = onchain.make_solvers(eth(1)).await; + let [solver, solver_b] = onchain.make_solvers(1u64.eth()).await; let (trader_a, token_a, token_b) = setup(&mut onchain, &solver).await; let services = Services::new(&onchain).await; @@ -187,7 +189,7 @@ async fn low_settling_solver(web3: Web3) { async fn not_allowed_solver(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3.clone()).await; - let [solver] = onchain.make_solvers(eth(1)).await; + let [solver] = onchain.make_solvers(1u64.eth()).await; let (trader_a, token_a, token_b) = setup(&mut onchain, &solver).await; let solver_address = solver.address(); @@ -231,17 +233,20 @@ async fn setup( onchain: &mut OnchainComponents, solver: &TestAccount, ) -> (TestAccount, MintableToken, MintableToken) { - let [trader_a] = onchain.make_accounts(eth(1)).await; + let [trader_a] = onchain.make_accounts(1u64.eth()).await; let [token_a, token_b] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 1_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; // Fund trader accounts - token_a.mint(trader_a.address(), eth(1000)).await; + token_a.mint(trader_a.address(), 1000u64.eth()).await; // Create and fund Uniswap pool - token_a.mint(solver.address(), eth(1000)).await; - token_b.mint(solver.address(), eth(1000)).await; + token_a.mint(solver.address(), 1000u64.eth()).await; + token_b.mint(solver.address(), 1000u64.eth()).await; onchain .contracts() .uniswap_v2_factory @@ -252,14 +257,20 @@ async fn setup( .unwrap(); token_a - .approve(*onchain.contracts().uniswap_v2_router.address(), eth(1000)) + .approve( + *onchain.contracts().uniswap_v2_router.address(), + 1000u64.eth(), + ) .from(solver.address()) .send_and_watch() .await .unwrap(); token_b - .approve(*onchain.contracts().uniswap_v2_router.address(), eth(1000)) + .approve( + *onchain.contracts().uniswap_v2_router.address(), + 1000u64.eth(), + ) .from(solver.address()) .send_and_watch() .await @@ -270,8 +281,8 @@ async fn setup( .addLiquidity( *token_a.address(), *token_b.address(), - eth(1000), - eth(1000), + 1000u64.eth(), + 1000u64.eth(), U256::ZERO, U256::ZERO, solver.address(), @@ -285,7 +296,7 @@ async fn setup( // Approve GPv2 for trading token_a - .approve(onchain.contracts().allowance.into_alloy(), eth(1000)) + .approve(onchain.contracts().allowance.into_alloy(), 1000u64.eth()) .from(trader_a.address()) .send_and_watch() .await @@ -327,9 +338,9 @@ async fn execute_order( ) -> anyhow::Result<()> { let order = OrderCreation { sell_token: *token_a.address(), - sell_amount: eth(10), + sell_amount: 10u64.eth(), buy_token: *token_b.address(), - buy_amount: eth(5), + buy_amount: 5u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, ..Default::default() @@ -350,7 +361,7 @@ async fn execute_order( tracing::info!("Waiting for trade."); wait_for_condition(TIMEOUT, || async { let balance_after = token_b.balanceOf(trader_a.address()).call().await.unwrap(); - let balance_changes = balance_after.checked_sub(balance_before).unwrap() >= eth(5); + let balance_changes = balance_after.checked_sub(balance_before).unwrap() >= 5u64.eth(); let auction_ids_after = fetch_last_settled_auction_ids(services.db()).await.len() > auction_ids_before; balance_changes && auction_ids_after diff --git a/crates/e2e/tests/e2e/submission.rs b/crates/e2e/tests/e2e/submission.rs index 41d6f18acf..c3948b9e5b 100644 --- a/crates/e2e/tests/e2e/submission.rs +++ b/crates/e2e/tests/e2e/submission.rs @@ -2,12 +2,16 @@ use { ::alloy::primitives::U256, e2e::{nodes::local_node::TestNodeApi, setup::*}, ethcontract::{BlockId, H160, H256}, - ethrpc::alloy::{CallBuilderExt, conversions::IntoAlloy}, + ethrpc::alloy::{ + CallBuilderExt, + conversions::{IntoAlloy, IntoLegacy}, + }, futures::{Stream, StreamExt}, model::{ order::{OrderCreation, OrderKind}, signature::EcdsaSigningScheme, }, + number::units::EthUnit, secp256k1::SecretKey, shared::ethrpc::Web3, std::time::Duration, @@ -23,17 +27,20 @@ async fn local_node_test() { async fn test_cancel_on_expiry(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3.clone()).await; - let [solver] = onchain.make_solvers(eth(10)).await; + let [solver] = onchain.make_solvers(10u64.eth()).await; let nonce = solver.nonce(&web3).await; - let [trader] = onchain.make_accounts(eth(10)).await; + let [trader] = onchain.make_accounts(10u64.eth()).await; let [token] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 1_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; onchain .contracts() .weth - .approve(onchain.contracts().allowance.into_alloy(), eth(3)) + .approve(onchain.contracts().allowance.into_alloy(), 3u64.eth()) .from(trader.address()) .send_and_watch() .await @@ -43,7 +50,7 @@ async fn test_cancel_on_expiry(web3: Web3) { .weth .deposit() .from(trader.address()) - .value(eth(3)) + .value(3u64.eth()) .send_and_watch() .await .unwrap(); @@ -63,9 +70,9 @@ async fn test_cancel_on_expiry(web3: Web3) { assert_eq!(balance, U256::ZERO); let order = OrderCreation { sell_token: *onchain.contracts().weth.address(), - sell_amount: eth(2), + sell_amount: 2u64.eth(), buy_token: *token.address(), - buy_amount: eth(1), + buy_amount: 1u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Buy, ..Default::default() diff --git a/crates/e2e/tests/e2e/tracking_insufficient_funds.rs b/crates/e2e/tests/e2e/tracking_insufficient_funds.rs index bcb71a4082..963853aa3b 100644 --- a/crates/e2e/tests/e2e/tracking_insufficient_funds.rs +++ b/crates/e2e/tests/e2e/tracking_insufficient_funds.rs @@ -1,11 +1,15 @@ use { database::order_events::{OrderEvent, OrderEventLabel}, e2e::setup::*, - ethrpc::alloy::{CallBuilderExt, conversions::IntoAlloy}, + ethrpc::alloy::{ + CallBuilderExt, + conversions::{IntoAlloy, IntoLegacy}, + }, model::{ order::{OrderCreation, OrderKind}, signature::EcdsaSigningScheme, }, + number::units::EthUnit, secp256k1::SecretKey, shared::ethrpc::Web3, web3::signing::SecretKeyRef, @@ -21,16 +25,19 @@ async fn test(web3: Web3) { tracing::info!("Setting up chain state."); let mut onchain = OnchainComponents::deploy(web3).await; - let [solver] = onchain.make_solvers(eth(10)).await; - let [trader_a, trader_b] = onchain.make_accounts(eth(10)).await; + let [solver] = onchain.make_solvers(10u64.eth()).await; + let [trader_a, trader_b] = onchain.make_accounts(10u64.eth()).await; let [token] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 1_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; onchain .contracts() .weth - .approve(onchain.contracts().allowance.into_alloy(), eth(3)) + .approve(onchain.contracts().allowance.into_alloy(), 3u64.eth()) .from(trader_a.address()) .send_and_watch() .await @@ -40,14 +47,14 @@ async fn test(web3: Web3) { .weth .deposit() .from(trader_a.address()) - .value(eth(3)) + .value(3u64.eth()) .send_and_watch() .await .unwrap(); onchain .contracts() .weth - .approve(onchain.contracts().allowance.into_alloy(), eth(3)) + .approve(onchain.contracts().allowance.into_alloy(), 3u64.eth()) .from(trader_b.address()) .send_and_watch() .await @@ -57,7 +64,7 @@ async fn test(web3: Web3) { .weth .deposit() .from(trader_b.address()) - .value(eth(3)) + .value(3u64.eth()) .send_and_watch() .await .unwrap(); @@ -69,9 +76,9 @@ async fn test(web3: Web3) { tracing::info!("Placing order"); let order_a = OrderCreation { sell_token: *onchain.contracts().weth.address(), - sell_amount: eth(2), + sell_amount: 2u64.eth(), buy_token: *token.address(), - buy_amount: eth(1), + buy_amount: 1u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Buy, ..Default::default() @@ -83,9 +90,9 @@ async fn test(web3: Web3) { ); let order_b = OrderCreation { sell_token: *onchain.contracts().weth.address(), - sell_amount: eth(2), + sell_amount: 2u64.eth(), buy_token: *token.address(), - buy_amount: eth(1), + buy_amount: 1u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Buy, ..Default::default() @@ -102,7 +109,7 @@ async fn test(web3: Web3) { onchain .contracts() .weth - .withdraw(eth(3)) + .withdraw(3u64.eth()) .from(trader_a.address()) .send_and_watch() .await @@ -110,7 +117,7 @@ async fn test(web3: Web3) { onchain .contracts() .weth - .withdraw(eth(3)) + .withdraw(3u64.eth()) .from(trader_b.address()) .send_and_watch() .await @@ -137,7 +144,7 @@ async fn test(web3: Web3) { .weth .deposit() .from(trader_a.address()) - .value(eth(3)) + .value(3u64.eth()) .send_and_watch() .await .unwrap(); @@ -159,7 +166,7 @@ async fn test(web3: Web3) { .weth .deposit() .from(trader_b.address()) - .value(eth(3)) + .value(3u64.eth()) .send_and_watch() .await .unwrap(); diff --git a/crates/e2e/tests/e2e/uncovered_order.rs b/crates/e2e/tests/e2e/uncovered_order.rs index 903121a73b..84b56057e4 100644 --- a/crates/e2e/tests/e2e/uncovered_order.rs +++ b/crates/e2e/tests/e2e/uncovered_order.rs @@ -1,10 +1,14 @@ use { e2e::setup::*, - ethrpc::alloy::{CallBuilderExt, conversions::IntoAlloy}, + ethrpc::alloy::{ + CallBuilderExt, + conversions::{IntoAlloy, IntoLegacy}, + }, model::{ order::{OrderCreation, OrderKind}, signature::EcdsaSigningScheme, }, + number::units::EthUnit, secp256k1::SecretKey, shared::ethrpc::Web3, web3::signing::SecretKeyRef, @@ -23,14 +27,17 @@ async fn test(web3: Web3) { tracing::info!("Setting up chain state."); let mut onchain = OnchainComponents::deploy(web3).await; - let [solver] = onchain.make_solvers(eth(10)).await; - let [trader] = onchain.make_accounts(eth(10)).await; + let [solver] = onchain.make_solvers(10u64.eth()).await; + let [trader] = onchain.make_accounts(10u64.eth()).await; let [token] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 1_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; let weth = &onchain.contracts().weth; - weth.approve(onchain.contracts().allowance.into_alloy(), eth(3)) + weth.approve(onchain.contracts().allowance.into_alloy(), 3u64.eth()) .from(trader.address()) .send_and_watch() .await @@ -43,10 +50,10 @@ async fn test(web3: Web3) { tracing::info!("Placing order with 0 sell tokens"); let order = OrderCreation { sell_token: *weth.address(), - sell_amount: eth(2), + sell_amount: 2u64.eth(), fee_amount: ::alloy::primitives::U256::ZERO, buy_token: *token.address(), - buy_amount: eth(1), + buy_amount: 1u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, partially_fillable: false, @@ -75,7 +82,7 @@ async fn test(web3: Web3) { tracing::info!("Deposit ETH to make order executable"); weth.deposit() .from(trader.address()) - .value(eth(2)) + .value(2u64.eth()) .send_and_watch() .await .unwrap(); diff --git a/crates/e2e/tests/e2e/univ2.rs b/crates/e2e/tests/e2e/univ2.rs index ac09dfee97..86609eeb00 100644 --- a/crates/e2e/tests/e2e/univ2.rs +++ b/crates/e2e/tests/e2e/univ2.rs @@ -2,12 +2,16 @@ use { ::alloy::primitives::U256, contracts::alloy::GPv2Settlement, database::order_events::{OrderEvent, OrderEventLabel}, - e2e::setup::{eth, *}, - ethrpc::alloy::{CallBuilderExt, conversions::IntoAlloy}, + e2e::setup::*, + ethrpc::alloy::{ + CallBuilderExt, + conversions::{IntoAlloy, IntoLegacy}, + }, model::{ order::{OrderCreation, OrderKind}, signature::EcdsaSigningScheme, }, + number::units::EthUnit, secp256k1::SecretKey, shared::ethrpc::Web3, web3::signing::SecretKeyRef, @@ -23,16 +27,19 @@ async fn test(web3: Web3) { tracing::info!("Setting up chain state."); let mut onchain = OnchainComponents::deploy(web3).await; - let [solver] = onchain.make_solvers(eth(10)).await; - let [trader] = onchain.make_accounts(eth(10)).await; + let [solver] = onchain.make_solvers(10u64.eth()).await; + let [trader] = onchain.make_accounts(10u64.eth()).await; let [token] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 1_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; onchain .contracts() .weth - .approve(onchain.contracts().allowance.into_alloy(), eth(3)) + .approve(onchain.contracts().allowance.into_alloy(), 3u64.eth()) .from(trader.address()) .send_and_watch() .await @@ -42,7 +49,7 @@ async fn test(web3: Web3) { .weth .deposit() .from(trader.address()) - .value(eth(3)) + .value(3u64.eth()) .send_and_watch() .await .unwrap(); @@ -56,9 +63,9 @@ async fn test(web3: Web3) { assert_eq!(balance, U256::ZERO); let order = OrderCreation { sell_token: *onchain.contracts().weth.address(), - sell_amount: eth(2), + sell_amount: 2u64.eth(), buy_token: *token.address(), - buy_amount: eth(1), + buy_amount: 1u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Buy, ..Default::default() @@ -106,7 +113,7 @@ async fn test(web3: Web3) { wait_for_condition(TIMEOUT, trade_happened).await.unwrap(); let balance = token.balanceOf(trader.address()).call().await.unwrap(); - assert_eq!(balance, eth(1)); + assert_eq!(balance, 1u64.eth()); let all_events_registered = || async { let events = crate::database::events_of_order(services.db(), &uid).await; diff --git a/crates/e2e/tests/e2e/vault_balances.rs b/crates/e2e/tests/e2e/vault_balances.rs index b56794a168..b52b38667d 100644 --- a/crates/e2e/tests/e2e/vault_balances.rs +++ b/crates/e2e/tests/e2e/vault_balances.rs @@ -1,10 +1,14 @@ use { - e2e::setup::{eth, *}, - ethrpc::alloy::{CallBuilderExt, conversions::IntoAlloy}, + e2e::setup::*, + ethrpc::alloy::{ + CallBuilderExt, + conversions::{IntoAlloy, IntoLegacy}, + }, model::{ order::{OrderCreation, OrderKind, SellTokenSource}, signature::EcdsaSigningScheme, }, + number::units::EthUnit, secp256k1::SecretKey, shared::ethrpc::Web3, web3::signing::SecretKeyRef, @@ -19,18 +23,21 @@ async fn local_node_vault_balances() { async fn vault_balances(web3: Web3) { let mut onchain = OnchainComponents::deploy(web3).await; - let [solver] = onchain.make_solvers(eth(1)).await; - let [trader] = onchain.make_accounts(eth(1)).await; + let [solver] = onchain.make_solvers(1u64.eth()).await; + let [trader] = onchain.make_accounts(1u64.eth()).await; let [token] = onchain - .deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000)) + .deploy_tokens_with_weth_uni_v2_pools( + 1_000u64.eth().into_legacy(), + 1_000u64.eth().into_legacy(), + ) .await; - token.mint(trader.address(), eth(10)).await; + token.mint(trader.address(), 10u64.eth()).await; // Approve GPv2 for trading token - .approve(*onchain.contracts().balancer_vault.address(), eth(10)) + .approve(*onchain.contracts().balancer_vault.address(), 10u64.eth()) .from(trader.address()) .send_and_watch() .await @@ -55,10 +62,10 @@ async fn vault_balances(web3: Web3) { let order = OrderCreation { kind: OrderKind::Sell, sell_token: *token.address(), - sell_amount: eth(10), + sell_amount: 10u64.eth(), sell_token_balance: SellTokenSource::External, buy_token: *onchain.contracts().weth.address(), - buy_amount: eth(8), + buy_amount: 8u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, ..Default::default() } @@ -94,7 +101,7 @@ async fn vault_balances(web3: Web3) { .await .unwrap(); - token_balance.is_zero() && weth_balance_after.saturating_sub(balance_before) >= eth(8) + token_balance.is_zero() && weth_balance_after.saturating_sub(balance_before) >= 8u64.eth() }) .await .unwrap(); diff --git a/crates/e2e/tests/e2e/wrapper.rs b/crates/e2e/tests/e2e/wrapper.rs index aabae6e68a..5eee690181 100644 --- a/crates/e2e/tests/e2e/wrapper.rs +++ b/crates/e2e/tests/e2e/wrapper.rs @@ -1,6 +1,6 @@ use { ::alloy::{ - primitives::{Address, U256, address, utils::Unit}, + primitives::{Address, address}, providers::{ Provider, ext::{AnvilApi, DebugApi, ImpersonateConfig}, @@ -19,6 +19,7 @@ use { quote::{OrderQuoteRequest, OrderQuoteSide, SellAmount}, signature::EcdsaSigningScheme, }, + number::units::EthUnit, secp256k1::SecretKey, serde_json::json, shared::ethrpc::Web3, @@ -48,8 +49,8 @@ async fn forked_node_mainnet_wrapper() { async fn forked_mainnet_wrapper_test(web3: Web3) { let mut onchain = OnchainComponents::deployed(web3.clone()).await; - let [solver] = onchain.make_solvers_forked(eth(1)).await; - let [trader] = onchain.make_accounts(eth(2)).await; + let [solver] = onchain.make_solvers_forked(1u64.eth()).await; + let [trader] = onchain.make_accounts(2u64.eth()).await; let token_weth = onchain.contracts().weth.clone(); let token_usdc = ERC20::Instance::new( @@ -75,7 +76,7 @@ async fn forked_mainnet_wrapper_test(web3: Web3) { ) .into_transaction_request(), ImpersonateConfig { - fund_amount: Some(eth(1)), + fund_amount: Some(1u64.eth()), stop_impersonate: true, }, ) @@ -88,7 +89,7 @@ async fn forked_mainnet_wrapper_test(web3: Web3) { // Trader deposits ETH to get WETH token_weth .deposit() - .value(eth(1)) + .value(1u64.eth()) .from(trader.address()) .send_and_watch() .await @@ -96,7 +97,7 @@ async fn forked_mainnet_wrapper_test(web3: Web3) { // Approve GPv2 for trading token_weth - .approve(onchain.contracts().allowance.into_alloy(), eth(1)) + .approve(onchain.contracts().allowance.into_alloy(), 1u64.eth()) .from(trader.address()) .send_and_watch() .await @@ -137,7 +138,7 @@ async fn forked_mainnet_wrapper_test(web3: Web3) { buy_token: *token_usdc.address(), side: OrderQuoteSide::Sell { sell_amount: SellAmount::BeforeFee { - value: (U256::ONE * Unit::ETHER.wei()).try_into().unwrap(), + value: (1u64.eth()).try_into().unwrap(), }, }, app_data: OrderCreationAppData::Both { @@ -155,7 +156,7 @@ async fn forked_mainnet_wrapper_test(web3: Web3) { expected: app_data_hash, }, sell_token: *token_weth.address(), - sell_amount: eth(1), + sell_amount: 1u64.eth(), buy_token: *token_usdc.address(), buy_amount: ::alloy::primitives::U256::ONE, valid_to: model::time::now_in_epoch_seconds() + 300, diff --git a/crates/number/src/lib.rs b/crates/number/src/lib.rs index 5d277018a1..5293f6d0ff 100644 --- a/crates/number/src/lib.rs +++ b/crates/number/src/lib.rs @@ -1,3 +1,4 @@ pub mod conversions; pub mod nonzero; pub mod serialization; +pub mod units; diff --git a/crates/number/src/units.rs b/crates/number/src/units.rs new file mode 100644 index 0000000000..ed84d560ce --- /dev/null +++ b/crates/number/src/units.rs @@ -0,0 +1,66 @@ +use alloy::primitives::{ + U256, + utils::{ParseUnits, Unit, parse_units}, +}; + +pub trait EthUnit: std::marker::Sized { + /// Returns the current wei amount. + fn wei(self) -> U256; + + /// Returns the current Mwei amount as wei (i.e. 1e6 wei). + fn mwei(self) -> U256 { + self.wei() * Unit::MWEI.wei() + } + + /// Returns the current Gwei amount as wei (i.e. 1e9 wei). + fn gwei(self) -> U256 { + self.wei() * Unit::GWEI.wei() + } + + /// Returns the current Eth amount as wei (i.e. 1e18 wei). + fn eth(self) -> U256 { + self.wei() * Unit::ETHER.wei() + } +} + +impl EthUnit for u64 { + fn wei(self) -> U256 { + U256::from(self) + } +} + +impl EthUnit for u128 { + fn wei(self) -> U256 { + U256::from(self) + } +} + +impl EthUnit 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}"), + } + } +} diff --git a/crates/orderbook/src/quoter.rs b/crates/orderbook/src/quoter.rs index 7487ccc100..ee1f7ff4e9 100644 --- a/crates/orderbook/src/quoter.rs +++ b/crates/orderbook/src/quoter.rs @@ -254,15 +254,12 @@ mod tests { use { super::*, crate::arguments::FeeFactor, - alloy::primitives::{U256, utils::Unit}, + alloy::primitives::U256, model::quote::OrderQuoteSide, + number::units::EthUnit, shared::order_quoting::{Quote, QuoteData}, }; - fn eth(base: u32) -> U256 { - U256::from(base) * Unit::ETHER.wei() - } - fn create_test_quote(sell_amount: U256, buy_amount: U256) -> Quote { Quote { id: None, @@ -294,10 +291,10 @@ mod tests { }; // Selling 100 tokens, expecting to buy 100 tokens - let quote = create_test_quote(eth(100), eth(100)); + let quote = create_test_quote(100u64.eth(), 100u64.eth()); let side = OrderQuoteSide::Sell { sell_amount: model::quote::SellAmount::BeforeFee { - value: number::nonzero::NonZeroU256::try_from(eth(100)).unwrap(), + value: number::nonzero::NonZeroU256::try_from(100u64.eth()).unwrap(), }, }; @@ -307,12 +304,12 @@ mod tests { // - sell_amount stays the same // - buy_amount is reduced by 0.02% of original buy_amount // - protocol_fee_bps = "2" - assert_eq!(result.sell_amount, eth(100)); + assert_eq!(result.sell_amount, 100u64.eth()); assert_eq!(result.protocol_fee_bps, Some("2".to_string())); // buy_amount should be reduced by 0.02% // Expected: 100 - (100 * 0.0002) = 100 - 0.02 = 99.98 - let expected_buy = eth(100) - (eth(100) / U256::from(5000)); // 0.02% = 1/5000 + let expected_buy = 100u64.eth() - (100u64.eth() / U256::from(5000)); // 0.02% = 1/5000 assert_eq!(result.buy_amount, expected_buy); } @@ -327,9 +324,9 @@ mod tests { }; // Buying 100 tokens, expecting to sell 100 tokens, with no network fee - let quote = create_test_quote(eth(100), eth(100)); + let quote = create_test_quote(100u64.eth(), 100u64.eth()); let side = OrderQuoteSide::Buy { - buy_amount_after_fee: number::nonzero::NonZeroU256::try_from(eth(100)).unwrap(), + buy_amount_after_fee: number::nonzero::NonZeroU256::try_from(100u64.eth()).unwrap(), }; let result = get_adjusted_quote_data("e, Some(&volume_fee_config), &side).unwrap(); @@ -338,12 +335,12 @@ mod tests { // - buy_amount stays the same // - sell_amount is increased by 0.02% of original sell_amount // - protocol_fee_bps = "2" - assert_eq!(result.buy_amount, eth(100)); + assert_eq!(result.buy_amount, 100u64.eth()); assert_eq!(result.protocol_fee_bps, Some("2".to_string())); // sell_amount should be increased by 0.02% of sell_amount (no network fee) // Expected: 100 + (100 * 0.0002) = 100 + 0.02 = 100.02 - let expected_sell = eth(100) + (eth(100) / U256::from(5000)); // 0.02% = 1/5000 + let expected_sell = 100u64.eth() + (100u64.eth() / U256::from(5000)); // 0.02% = 1/5000 assert_eq!(result.sell_amount, expected_sell); } @@ -356,10 +353,10 @@ mod tests { }; // Buying 100 tokens, expecting to sell 100 tokens, with 5 token network fee - let mut quote = create_test_quote(eth(100), eth(100)); - quote.fee_amount = eth(5); // Network fee in sell token + let mut quote = create_test_quote(100u64.eth(), 100u64.eth()); + quote.fee_amount = 5u64.eth(); // Network fee in sell token let side = OrderQuoteSide::Buy { - buy_amount_after_fee: number::nonzero::NonZeroU256::try_from(eth(100)).unwrap(), + buy_amount_after_fee: number::nonzero::NonZeroU256::try_from(100u64.eth()).unwrap(), }; let result = get_adjusted_quote_data("e, Some(&volume_fee_config), &side).unwrap(); @@ -368,16 +365,16 @@ mod tests { // - buy_amount stays the same // - protocol fee is calculated on (sell_amount + network_fee) // - sell_amount is increased by protocol fee - assert_eq!(result.buy_amount, eth(100)); + assert_eq!(result.buy_amount, 100u64.eth()); assert_eq!(result.protocol_fee_bps, Some("2".to_string())); // Total volume = sell_amount + network_fee = 100 + 5 = 105 // Protocol fee = 105 * 0.0002 = 0.021 // sell_amount should be increased by protocol fee // Expected: 100 + 0.021 = 100.021 - let total_volume = eth(100) + eth(5); // 105 + let total_volume = 100u64.eth() + 5u64.eth(); // 105 let expected_protocol_fee = total_volume / U256::from(5000); // 0.021 - let expected_sell = eth(100) + expected_protocol_fee; // 100.021 + let expected_sell = 100u64.eth() + expected_protocol_fee; // 100.021 assert_eq!(result.sell_amount, expected_sell); } @@ -390,20 +387,20 @@ mod tests { }; // Selling 100 tokens, expecting to buy 200 tokens (2:1 price ratio) - let quote = create_test_quote(eth(100), eth(200)); + let quote = create_test_quote(100u64.eth(), 200u64.eth()); let side = OrderQuoteSide::Sell { sell_amount: model::quote::SellAmount::BeforeFee { - value: number::nonzero::NonZeroU256::try_from(eth(100)).unwrap(), + value: number::nonzero::NonZeroU256::try_from(100u64.eth()).unwrap(), }, }; let result = get_adjusted_quote_data("e, Some(&volume_fee_config), &side).unwrap(); assert_eq!(result.protocol_fee_bps, Some("10".to_string())); - assert_eq!(result.sell_amount, eth(100)); + assert_eq!(result.sell_amount, 100u64.eth()); // buy_amount reduced by 0.1% of 200 = 0.2 tokens - let expected_buy = eth(200) - (eth(200) / U256::from(1000)); + let expected_buy = 200u64.eth() - (200u64.eth() / U256::from(1000)); assert_eq!(result.buy_amount, expected_buy); } @@ -424,10 +421,10 @@ mod tests { effective_from_timestamp: None, }; - let quote = create_test_quote(eth(100), eth(100)); + let quote = create_test_quote(100u64.eth(), 100u64.eth()); let side = OrderQuoteSide::Sell { sell_amount: model::quote::SellAmount::BeforeFee { - value: number::nonzero::NonZeroU256::try_from(eth(100)).unwrap(), + value: number::nonzero::NonZeroU256::try_from(100u64.eth()).unwrap(), }, }; @@ -447,18 +444,18 @@ mod tests { }; // Selling 100 tokens, expecting to buy 100 tokens - let quote = create_test_quote(eth(100), eth(100)); + let quote = create_test_quote(100u64.eth(), 100u64.eth()); let side = OrderQuoteSide::Sell { sell_amount: model::quote::SellAmount::BeforeFee { - value: number::nonzero::NonZeroU256::try_from(eth(100)).unwrap(), + value: number::nonzero::NonZeroU256::try_from(100u64.eth()).unwrap(), }, }; let result = get_adjusted_quote_data("e, Some(&volume_fee_config), &side).unwrap(); // Since the effective date is in the future, no volume fee should be applied - assert_eq!(result.sell_amount, eth(100)); - assert_eq!(result.buy_amount, eth(100)); + assert_eq!(result.sell_amount, 100u64.eth()); + assert_eq!(result.buy_amount, 100u64.eth()); assert_eq!(result.protocol_fee_bps, None); } } diff --git a/crates/solvers/Cargo.toml b/crates/solvers/Cargo.toml index b6d96d6220..6d9e50d8d7 100644 --- a/crates/solvers/Cargo.toml +++ b/crates/solvers/Cargo.toml @@ -28,6 +28,7 @@ hyper = { workspace = true } itertools = { workspace = true } mimalloc = { workspace = true, optional = true } num = { workspace = true } +number = { workspace = true } prometheus = { workspace = true } prometheus-metric-storage = { workspace = true } reqwest = { workspace = true } diff --git a/crates/solvers/src/boundary/liquidity/limit_order.rs b/crates/solvers/src/boundary/liquidity/limit_order.rs index 671c9526dc..6ccf01bd2f 100644 --- a/crates/solvers/src/boundary/liquidity/limit_order.rs +++ b/crates/solvers/src/boundary/liquidity/limit_order.rs @@ -51,6 +51,7 @@ mod tests { super::*, crate::domain::{eth, liquidity::limit_order::TakerAmount}, alloy::primitives::address, + number::units::EthUnit, }; fn create_limit_order(maker_amount: U256, taker_amount: U256, fee_amount: U256) -> LimitOrder { @@ -69,10 +70,10 @@ mod tests { #[tokio::test] async fn amount_out_in_round_trip() { - let maker_amount = to_wei(300); - let taker_amount = to_wei(100); - let fee_amount = to_wei(10); - let desired_in_amount = to_wei(50); + let maker_amount = 300u64.eth(); + let taker_amount = 100u64.eth(); + let fee_amount = 10u64.eth(); + let desired_in_amount = 50u64.eth(); let order = create_limit_order(maker_amount, taker_amount, fee_amount); let out_token = order.maker.token.0; @@ -92,10 +93,10 @@ mod tests { #[tokio::test] async fn amount_in_out_round_trip() { - let maker_amount = to_wei(100); - let taker_amount = to_wei(300); - let fee_amount = to_wei(10); - let desired_out_amount = to_wei(50); + let maker_amount = 100u64.eth(); + let taker_amount = 300u64.eth(); + let fee_amount = 10u64.eth(); + let desired_out_amount = 50u64.eth(); let order = create_limit_order(maker_amount, taker_amount, fee_amount); let out_token = order.maker.token.0; @@ -115,9 +116,9 @@ mod tests { #[tokio::test] async fn too_high_in_amount() { - let maker_amount = to_wei(300); - let taker_amount = to_wei(100); - let fee_amount = to_wei(10); + let maker_amount = 300u64.eth(); + let taker_amount = 100u64.eth(); + let fee_amount = 10u64.eth(); let order = create_limit_order(maker_amount, taker_amount, fee_amount); let out_token = order.maker.token.0; @@ -130,9 +131,9 @@ mod tests { #[tokio::test] async fn too_high_out_amount() { - let maker_amount = to_wei(100); - let taker_amount = to_wei(300); - let fee_amount = to_wei(10); + let maker_amount = 100u64.eth(); + let taker_amount = 300u64.eth(); + let fee_amount = 10u64.eth(); let order = create_limit_order(maker_amount, taker_amount, fee_amount); let out_token = order.maker.token.0; @@ -145,22 +146,18 @@ mod tests { #[tokio::test] async fn wrong_tokens() { - let maker_amount = to_wei(100); - let taker_amount = to_wei(100); - let fee_amount = to_wei(10); + let maker_amount = 100u64.eth(); + let taker_amount = 100u64.eth(); + let fee_amount = 10u64.eth(); let order = create_limit_order(maker_amount, taker_amount, fee_amount); let out_token = order.maker.token.0; let in_token = order.taker.token.0; - let amount = to_wei(1); + let amount = 1u64.eth(); let amount_in = order.get_amount_in(out_token, (amount, in_token)).await; let amount_out = order.get_amount_out(in_token, (amount, out_token)).await; assert!(amount_in.is_none()); assert!(amount_out.is_none()); } - - fn to_wei(base: u32) -> U256 { - U256::from(base) * U256::from(10).pow(U256::from(18)) - } } From 8a84b5f07cceef15a6316d1e055de0b61de0ccb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=CC=81=20Duarte?= Date: Thu, 11 Dec 2025 15:40:53 +0000 Subject: [PATCH 2/4] fix lints --- crates/autopilot/src/maintenance.rs | 1 - crates/driver/src/domain/mempools.rs | 35 +++++++++++++++----------- crates/shared/src/order_quoting.rs | 2 +- crates/shared/src/trade_finding/mod.rs | 1 - 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/crates/autopilot/src/maintenance.rs b/crates/autopilot/src/maintenance.rs index 44a9c5ac16..1344c6ba67 100644 --- a/crates/autopilot/src/maintenance.rs +++ b/crates/autopilot/src/maintenance.rs @@ -152,7 +152,6 @@ impl Maintenance { self_ .cow_amm_indexer .iter() - .cloned() .map(|indexer| async move { indexer.run_maintenance().await }), ), ) diff --git a/crates/driver/src/domain/mempools.rs b/crates/driver/src/domain/mempools.rs index c70f5ccd46..14e2179009 100644 --- a/crates/driver/src/domain/mempools.rs +++ b/crates/driver/src/domain/mempools.rs @@ -48,19 +48,18 @@ impl Mempools { settlement: &Settlement, submission_deadline: BlockNo, ) -> Result { - let (submission, _remaining_futures) = - select_ok(self.mempools.iter().cloned().map(|mempool| { - async move { - let result = self - .submit(&mempool, solver, settlement, submission_deadline) - .instrument(tracing::info_span!("mempool", kind = mempool.to_string())) - .await; - observe::mempool_executed(&mempool, settlement, &result); - result - } - .boxed() - })) - .await?; + let (submission, _remaining_futures) = select_ok(self.mempools.iter().map(|mempool| { + async move { + let result = self + .submit(mempool, solver, settlement, submission_deadline) + .instrument(tracing::info_span!("mempool", kind = mempool.to_string())) + .await; + observe::mempool_executed(mempool, settlement, &result); + result + } + .boxed() + })) + .await?; Ok(submission.tx_hash) } @@ -373,13 +372,19 @@ pub enum RevertProtection { #[derive(Debug, thiserror::Error)] pub enum Error { - #[error("Mined reverted transaction: {tx_id:?}, block number: {reverted_at_block}")] + #[error( + "Mined reverted transaction: {tx_id:?}, submitted at: {submitted_at_block}, reverted at: \ + {reverted_at_block}" + )] Revert { tx_id: eth::TxId, submitted_at_block: BlockNo, reverted_at_block: BlockNo, }, - #[error("Simulation started reverting during submission, block number: {reverted_at_block}")] + #[error( + "Simulation started reverting during submission, submitted at: {submitted_at_block}, \ + reverted at: {reverted_at_block}" + )] SimulationRevert { submitted_at_block: BlockNo, reverted_at_block: BlockNo, diff --git a/crates/shared/src/order_quoting.rs b/crates/shared/src/order_quoting.rs index e133985f2d..ab6234fa59 100644 --- a/crates/shared/src/order_quoting.rs +++ b/crates/shared/src/order_quoting.rs @@ -263,7 +263,7 @@ pub trait OrderQuoting: Send + Sync { #[derive(Error, Debug)] pub enum CalculateQuoteError { - #[error("sell amount does not cover fee")] + #[error("sell amount does not cover fee: {fee_amount}")] SellAmountDoesNotCoverFee { fee_amount: U256 }, #[error("{estimator_kind:?} estimator failed: {source}")] diff --git a/crates/shared/src/trade_finding/mod.rs b/crates/shared/src/trade_finding/mod.rs index d25483f7bf..182e905a9c 100644 --- a/crates/shared/src/trade_finding/mod.rs +++ b/crates/shared/src/trade_finding/mod.rs @@ -285,7 +285,6 @@ pub fn map_interactions(interactions: &[InteractionData]) -> Vec { pub fn map_interactions_data(interactions: &[Interaction]) -> Vec { interactions .iter() - .cloned() .map(|i| i.to_interaction_data()) .collect() } From 3529ededa2ff69d675843807ab4849f85e6f0334 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=CC=81=20Duarte?= Date: Thu, 11 Dec 2025 16:10:05 +0000 Subject: [PATCH 3/4] docs --- crates/number/src/units.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/number/src/units.rs b/crates/number/src/units.rs index ed84d560ce..429e53f34c 100644 --- a/crates/number/src/units.rs +++ b/crates/number/src/units.rs @@ -4,20 +4,20 @@ use alloy::primitives::{ }; pub trait EthUnit: std::marker::Sized { - /// Returns the current wei amount. + /// Converts this value to wei. fn wei(self) -> U256; - /// Returns the current Mwei amount as wei (i.e. 1e6 wei). + /// Converts this value from Mwei to wei (multiplies by 1e6). fn mwei(self) -> U256 { self.wei() * Unit::MWEI.wei() } - /// Returns the current Gwei amount as wei (i.e. 1e9 wei). + /// Converts this value from Gwei to wei (multiplies by 1e9). fn gwei(self) -> U256 { self.wei() * Unit::GWEI.wei() } - /// Returns the current Eth amount as wei (i.e. 1e18 wei). + /// Converts this value from Eth to wei (multiplies by 1e18). fn eth(self) -> U256 { self.wei() * Unit::ETHER.wei() } From cf6d5ead3d85a4460395b5d16c50652cc8c12966 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=CC=81=20Duarte?= Date: Fri, 12 Dec 2025 09:50:58 +0000 Subject: [PATCH 4/4] Rename units --- crates/e2e/tests/e2e/cow_amm.rs | 10 ++-- crates/e2e/tests/e2e/limit_orders.rs | 18 +++---- crates/e2e/tests/e2e/liquidity.rs | 2 +- .../e2e/liquidity_source_notification.rs | 2 +- crates/number/src/units.rs | 51 ++++--------------- 5 files changed, 25 insertions(+), 58 deletions(-) diff --git a/crates/e2e/tests/e2e/cow_amm.rs b/crates/e2e/tests/e2e/cow_amm.rs index a8d4d889ee..1fc3bbaf52 100644 --- a/crates/e2e/tests/e2e/cow_amm.rs +++ b/crates/e2e/tests/e2e/cow_amm.rs @@ -464,7 +464,7 @@ async fn cow_amm_driver_support(web3: Web3) { // Give trader some USDC web3.alloy .anvil_send_impersonated_transaction_with_config( - usdc.transfer(trader.address(), 1000u64.mwei()) + usdc.transfer(trader.address(), 1000u64.matom()) .from(USDC_WHALE_MAINNET) .into_transaction_request(), ImpersonateConfig { @@ -479,7 +479,7 @@ async fn cow_amm_driver_support(web3: Web3) { .unwrap(); // Approve GPv2 for trading - usdc.approve(onchain.contracts().allowance.into_alloy(), 1000u64.mwei()) + usdc.approve(onchain.contracts().allowance.into_alloy(), 1000u64.matom()) .from(trader.address()) .send_and_watch() .await @@ -582,9 +582,9 @@ factory = "0xf76c421bAb7df8548604E60deCCcE50477C10462" // Place Orders let order = OrderCreation { sell_token: *usdc.address(), - sell_amount: 1000u64.mwei(), + sell_amount: 1000u64.matom(), buy_token: *usdt.address(), - buy_amount: 2000u64.mwei(), + buy_amount: 2000u64.matom(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, ..Default::default() @@ -603,7 +603,7 @@ factory = "0xf76c421bAb7df8548604E60deCCcE50477C10462" buy_token: *usdt.address(), side: OrderQuoteSide::Sell { sell_amount: SellAmount::BeforeFee { - value: (1000u64.mwei()).try_into().unwrap(), + value: (1000u64.matom()).try_into().unwrap(), }, }, ..Default::default() diff --git a/crates/e2e/tests/e2e/limit_orders.rs b/crates/e2e/tests/e2e/limit_orders.rs index b02a03a169..69b835c135 100644 --- a/crates/e2e/tests/e2e/limit_orders.rs +++ b/crates/e2e/tests/e2e/limit_orders.rs @@ -847,7 +847,7 @@ async fn forked_mainnet_single_limit_order_test(web3: Web3) { web3.alloy .anvil_send_impersonated_transaction_with_config( token_usdc - .transfer(trader.address(), 1000u64.mwei()) + .transfer(trader.address(), 1000u64.matom()) .from(USDC_WHALE_MAINNET) .into_transaction_request(), ImpersonateConfig { @@ -863,7 +863,7 @@ async fn forked_mainnet_single_limit_order_test(web3: Web3) { // Approve GPv2 for trading token_usdc - .approve(onchain.contracts().allowance.into_alloy(), 1000u64.mwei()) + .approve(onchain.contracts().allowance.into_alloy(), 1000u64.matom()) .from(trader.address()) .send_and_watch() .await @@ -877,9 +877,9 @@ async fn forked_mainnet_single_limit_order_test(web3: Web3) { let order = OrderCreation { sell_token: *token_usdc.address(), - sell_amount: 1000u64.mwei(), + sell_amount: 1000u64.matom(), buy_token: *token_usdt.address(), - buy_amount: 500u64.mwei(), + buy_amount: 500u64.matom(), valid_to: model::time::now_in_epoch_seconds() + 300, kind: OrderKind::Sell, ..Default::default() @@ -898,7 +898,7 @@ async fn forked_mainnet_single_limit_order_test(web3: Web3) { buy_token: *token_usdt.address(), side: OrderQuoteSide::Sell { sell_amount: SellAmount::BeforeFee { - value: (1000u64.mwei()).try_into().unwrap(), + value: (1000u64.matom()).try_into().unwrap(), }, }, ..Default::default() @@ -920,7 +920,7 @@ async fn forked_mainnet_single_limit_order_test(web3: Web3) { let buy_token_balance_after = token_usdt.balanceOf(trader.address()).call().await.unwrap(); (sell_token_balance_before > sell_token_balance_after) - && (buy_token_balance_after >= buy_token_balance_before + 500u64.mwei()) + && (buy_token_balance_after >= buy_token_balance_before + 500u64.matom()) }) .await .unwrap(); @@ -947,7 +947,7 @@ async fn forked_gnosis_single_limit_order_test(web3: Web3) { web3.alloy .anvil_send_impersonated_transaction_with_config( token_usdc - .transfer(trader.address(), 1000u64.mwei()) + .transfer(trader.address(), 1000u64.matom()) .from(USDC_WHALE_GNOSIS) .into_transaction_request(), ImpersonateConfig { @@ -963,7 +963,7 @@ async fn forked_gnosis_single_limit_order_test(web3: Web3) { // Approve GPv2 for trading token_usdc - .approve(onchain.contracts().allowance.into_alloy(), 1000u64.mwei()) + .approve(onchain.contracts().allowance.into_alloy(), 1000u64.matom()) .from(trader.address()) .send_and_watch() .await @@ -975,7 +975,7 @@ async fn forked_gnosis_single_limit_order_test(web3: Web3) { let order = OrderCreation { sell_token: *token_usdc.address(), - sell_amount: 1000u64.mwei(), + sell_amount: 1000u64.matom(), buy_token: *token_wxdai.address(), buy_amount: 500u64.eth(), valid_to: model::time::now_in_epoch_seconds() + 300, diff --git a/crates/e2e/tests/e2e/liquidity.rs b/crates/e2e/tests/e2e/liquidity.rs index 2343099795..10ef9f9e64 100644 --- a/crates/e2e/tests/e2e/liquidity.rs +++ b/crates/e2e/tests/e2e/liquidity.rs @@ -75,7 +75,7 @@ async fn zero_ex_liquidity(web3: Web3) { }; let zeroex = IZeroex::Instance::deployed(&zeroex_provider).await.unwrap(); - let amount = 500u64.mwei(); + let amount = 500u64.matom(); // Give trader some USDC web3.alloy diff --git a/crates/e2e/tests/e2e/liquidity_source_notification.rs b/crates/e2e/tests/e2e/liquidity_source_notification.rs index 40c4309f23..1c31e0e2e5 100644 --- a/crates/e2e/tests/e2e/liquidity_source_notification.rs +++ b/crates/e2e/tests/e2e/liquidity_source_notification.rs @@ -59,7 +59,7 @@ async fn liquidity_source_notification(web3: Web3) { let mut onchain = OnchainComponents::deployed(web3.clone()).await; // Define trade params - let trade_amount = 500u64.mwei(); + let trade_amount = 500u64.matom(); // Create parties accounts // solver - represents both baseline solver engine for quoting and liquorice diff --git a/crates/number/src/units.rs b/crates/number/src/units.rs index 429e53f34c..6bef7ade61 100644 --- a/crates/number/src/units.rs +++ b/crates/number/src/units.rs @@ -1,66 +1,33 @@ -use alloy::primitives::{ - U256, - utils::{ParseUnits, Unit, parse_units}, -}; +use alloy::primitives::{U256, utils::Unit}; pub trait EthUnit: std::marker::Sized { /// Converts this value to wei. - fn wei(self) -> U256; + fn atom(self) -> U256; /// Converts this value from Mwei to wei (multiplies by 1e6). - fn mwei(self) -> U256 { - self.wei() * Unit::MWEI.wei() + fn matom(self) -> U256 { + self.atom() * U256::from(10).pow(U256::from(6)) } /// Converts this value from Gwei to wei (multiplies by 1e9). - fn gwei(self) -> U256 { - self.wei() * Unit::GWEI.wei() + fn gatom(self) -> U256 { + self.atom() * U256::from(10).pow(U256::from(9)) } /// Converts this value from Eth to wei (multiplies by 1e18). fn eth(self) -> U256 { - self.wei() * Unit::ETHER.wei() + self.atom() * Unit::ETHER.wei() } } impl EthUnit for u64 { - fn wei(self) -> U256 { + fn atom(self) -> U256 { U256::from(self) } } impl EthUnit for u128 { - fn wei(self) -> U256 { + fn atom(self) -> U256 { U256::from(self) } } - -impl EthUnit 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}"), - } - } -}