Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions crates/autopilot/src/run.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use ethrpc::alloy::ProviderLabelingExt;
use {
crate::{
arguments::{Account, Arguments},
Expand All @@ -8,8 +9,7 @@ use {
onchain_order_events::{
OnchainOrderParser,
ethflow_events::{
EthFlowOnchainOrderParser,
determine_ethflow_indexing_start,
EthFlowOnchainOrderParser, determine_ethflow_indexing_start,
determine_ethflow_refund_indexing_start,
},
event_retriever::CoWSwapOnchainOrdersContract,
Expand Down Expand Up @@ -472,6 +472,34 @@ pub async fn run(args: Arguments, shutdown_controller: ShutdownController) {
.await;
}

let order_execution_simulator = {
let web3 = web3.labeled("order_simulation");
let tenderly = args
.shared
.tenderly
.get_api_instance(&http_factory, "order_simulation".to_owned())
.unwrap_or_else(|err| {
tracing::warn!(?err, "failed to initialize tenderly api");
None
})
.map(|t| {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I get that t here is tenderly but its easier to read than thinking twice what the t is

Suggested change
.map(|t| {
.map(|tenderly| {

Arc::new(shared::tenderly_api::TenderlyCodeSimulator::new(
t,
chain.id(),
))
});
let balance_overrides = args.price_estimation.balance_overrides.init(web3.clone());
let settlement = GPv2Settlement::Instance::new(
*eth.contracts().settlement().address(),
web3.provider.clone(),
);
Arc::new(shared::order_simulation::OrderExecutionSimulator::new(
settlement,
balance_overrides,
tenderly,
))
};

let quoter = Arc::new(OrderQuoter::new(
price_estimator,
native_price_estimator.clone(),
Expand All @@ -493,6 +521,7 @@ pub async fn run(args: Arguments, shutdown_controller: ShutdownController) {
},
balance_fetcher.clone(),
args.price_estimation.quote_verification,
order_execution_simulator,
args.price_estimation.quote_timeout,
));

Expand Down
48 changes: 32 additions & 16 deletions crates/orderbook/src/run.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
use ethrpc::alloy::ProviderLabelingExt;
use {
crate::{
api,
arguments::Arguments,
database::Postgres,
ipfs::Ipfs,
ipfs_app_data::IpfsAppData,
orderbook::Orderbook,
quoter::QuoteHandler,
api, arguments::Arguments, database::Postgres, ipfs::Ipfs, ipfs_app_data::IpfsAppData,
orderbook::Orderbook, quoter::QuoteHandler,
},
alloy::providers::Provider,
anyhow::{Context, Result, anyhow},
app_data::Validator,
chain::Chain,
clap::Parser,
contracts::alloy::{
BalancerV2Vault,
ChainalysisOracle,
GPv2Settlement,
HooksTrampoline,
IUniswapV3Factory,
WETH9,
support::Balances,
BalancerV2Vault, ChainalysisOracle, GPv2Settlement, HooksTrampoline, IUniswapV3Factory,
WETH9, support::Balances,
},
futures::{FutureExt, StreamExt},
model::{DomainSeparator, order::BUY_ETH_ADDRESS},
Expand All @@ -44,8 +35,7 @@ use {
order_quoting::{self, OrderQuoter},
order_validation::{OrderValidPeriodConfiguration, OrderValidator},
price_estimation::{
PriceEstimating,
QuoteVerificationMode,
PriceEstimating, QuoteVerificationMode,
factory::{self, PriceEstimatorFactory},
native::NativePriceEstimating,
},
Expand Down Expand Up @@ -358,6 +348,31 @@ pub async fn run(args: Arguments) {
max_limit: args.max_limit_order_validity_period,
};

let order_execution_simulator = {
let web3 = web3.labeled("order_simulation");
let tenderly = args
.shared
.tenderly
.get_api_instance(&http_factory, "order_simulation".to_owned())
.unwrap_or_else(|err| {
tracing::warn!(?err, "failed to initialize tenderly api");
None
})
.map(|t| {
Arc::new(shared::tenderly_api::TenderlyCodeSimulator::new(
t, chain_id,
))
});
let balance_overrides = args.price_estimation.balance_overrides.init(web3.clone());
let settlement =
GPv2Settlement::Instance::new(*settlement_contract.address(), web3.provider.clone());
Arc::new(shared::order_simulation::OrderExecutionSimulator::new(
settlement,
balance_overrides,
tenderly,
))
};

let create_quoter = |price_estimator: Arc<dyn PriceEstimating>,
verification: QuoteVerificationMode| {
Arc::new(OrderQuoter::new(
Expand All @@ -381,6 +396,7 @@ pub async fn run(args: Arguments) {
},
balance_fetcher.clone(),
verification,
order_execution_simulator.clone(),
args.price_estimation.quote_timeout,
))
};
Expand Down
1 change: 1 addition & 0 deletions crates/shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub mod http_solver;
pub mod interaction;
pub mod maintenance;
pub mod order_quoting;
pub mod order_simulation;
pub mod order_validation;
pub mod price_estimation;
pub mod recent_block_cache;
Expand Down
43 changes: 34 additions & 9 deletions crates/shared/src/order_quoting.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use {
super::price_estimation::{
self,
PriceEstimating,
PriceEstimationError,
native::NativePriceEstimating,
self, PriceEstimating, PriceEstimationError, native::NativePriceEstimating,
},
crate::{
account_balances::{BalanceFetching, Query},
db_order_conversions::order_kind_from,
fee::FeeParameters,
gas_price_estimation::GasPriceEstimating,
order_simulation::OrderExecutionSimulating,
order_validation::PreOrderData,
price_estimation::{Estimate, QuoteVerificationMode, Verification},
trade_finding::external::dto,
Expand Down Expand Up @@ -391,13 +389,13 @@ impl Now for DateTime<Utc> {
}
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Validity {
pub eip1271_onchain_quote: Duration,
pub presign_onchain_quote: Duration,
pub standard_quote: Duration,
}

#[cfg(test)]
impl Default for Validity {
fn default() -> Self {
Self {
Expand All @@ -418,6 +416,8 @@ pub struct OrderQuoter {
validity: Validity,
balance_fetcher: Arc<dyn BalanceFetching>,
quote_verification: QuoteVerificationMode,
#[allow(dead_code)]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why is this not used?

order_execution_simulator: Arc<dyn OrderExecutionSimulating>,
default_quote_timeout: std::time::Duration,
}

Expand All @@ -431,6 +431,7 @@ impl OrderQuoter {
validity: Validity,
balance_fetcher: Arc<dyn BalanceFetching>,
quote_verification: QuoteVerificationMode,
order_execution_simulator: Arc<dyn OrderExecutionSimulating>,
default_quote_timeout: std::time::Duration,
) -> Self {
Self {
Expand All @@ -442,6 +443,7 @@ impl OrderQuoter {
validity,
balance_fetcher,
quote_verification,
order_execution_simulator,
default_quote_timeout,
}
}
Expand Down Expand Up @@ -782,19 +784,19 @@ pub struct QuoteMetadataV1 {

#[cfg(test)]
mod tests {
use crate::order_simulation::SimulationOptions;

use {
super::*,
crate::{
account_balances::MockBalanceFetching,
gas_price_estimation::FakeGasPriceEstimator,
price_estimation::{
HEALTHY_PRICE_ESTIMATION_TIME,
MockPriceEstimating,
HEALTHY_PRICE_ESTIMATION_TIME, MockPriceEstimating,
native::MockNativePriceEstimating,
},
},
Address,
U256 as AlloyU256,
Address, U256 as AlloyU256,
alloy::eips::eip1559::Eip1559Estimation,
chrono::Utc,
futures::FutureExt,
Expand All @@ -803,6 +805,19 @@ mod tests {
number::nonzero::NonZeroU256,
};

struct FakeOrderExecutionSimulator;
#[async_trait::async_trait]
impl OrderExecutionSimulating for FakeOrderExecutionSimulator {
async fn simulate_order_execution(
&self,
_: &model::order::Order,
_: &model::DomainSeparator,
_: SimulationOptions,
) -> Result<()> {
Ok(())
}
}

fn mock_balance_fetcher() -> Arc<dyn BalanceFetching> {
let mut mock = MockBalanceFetching::new();
mock.expect_get_balances()
Expand Down Expand Up @@ -938,6 +953,7 @@ mod tests {
validity: super::Validity::default(),
quote_verification: QuoteVerificationMode::Unverified,
balance_fetcher: mock_balance_fetcher(),
order_execution_simulator: Arc::new(FakeOrderExecutionSimulator),
default_quote_timeout: HEALTHY_PRICE_ESTIMATION_TIME,
};

Expand Down Expand Up @@ -1078,6 +1094,7 @@ mod tests {
validity: Validity::default(),
quote_verification: QuoteVerificationMode::Unverified,
balance_fetcher: mock_balance_fetcher(),
order_execution_simulator: Arc::new(FakeOrderExecutionSimulator),
default_quote_timeout: HEALTHY_PRICE_ESTIMATION_TIME,
};

Expand Down Expand Up @@ -1213,6 +1230,7 @@ mod tests {
validity: Validity::default(),
quote_verification: QuoteVerificationMode::Unverified,
balance_fetcher: mock_balance_fetcher(),
order_execution_simulator: Arc::new(FakeOrderExecutionSimulator),
default_quote_timeout: HEALTHY_PRICE_ESTIMATION_TIME,
};

Expand Down Expand Up @@ -1311,6 +1329,7 @@ mod tests {
validity: Validity::default(),
quote_verification: QuoteVerificationMode::Unverified,
balance_fetcher: mock_balance_fetcher(),
order_execution_simulator: Arc::new(FakeOrderExecutionSimulator),
default_quote_timeout: HEALTHY_PRICE_ESTIMATION_TIME,
};

Expand Down Expand Up @@ -1384,6 +1403,7 @@ mod tests {
validity: Validity::default(),
quote_verification: QuoteVerificationMode::Unverified,
balance_fetcher: mock_balance_fetcher(),
order_execution_simulator: Arc::new(FakeOrderExecutionSimulator),
default_quote_timeout: HEALTHY_PRICE_ESTIMATION_TIME,
};

Expand Down Expand Up @@ -1445,6 +1465,7 @@ mod tests {
validity: Validity::default(),
quote_verification: QuoteVerificationMode::Unverified,
balance_fetcher: mock_balance_fetcher(),
order_execution_simulator: Arc::new(FakeOrderExecutionSimulator),
default_quote_timeout: HEALTHY_PRICE_ESTIMATION_TIME,
};

Expand Down Expand Up @@ -1528,6 +1549,7 @@ mod tests {
validity: Validity::default(),
quote_verification: QuoteVerificationMode::Unverified,
balance_fetcher: mock_balance_fetcher(),
order_execution_simulator: Arc::new(FakeOrderExecutionSimulator),
default_quote_timeout: HEALTHY_PRICE_ESTIMATION_TIME,
};

Expand Down Expand Up @@ -1613,6 +1635,7 @@ mod tests {
validity: Validity::default(),
quote_verification: QuoteVerificationMode::Unverified,
balance_fetcher: mock_balance_fetcher(),
order_execution_simulator: Arc::new(FakeOrderExecutionSimulator),
default_quote_timeout: HEALTHY_PRICE_ESTIMATION_TIME,
};

Expand Down Expand Up @@ -1686,6 +1709,7 @@ mod tests {
validity: Validity::default(),
quote_verification: QuoteVerificationMode::Unverified,
balance_fetcher: mock_balance_fetcher(),
order_execution_simulator: Arc::new(FakeOrderExecutionSimulator),
default_quote_timeout: HEALTHY_PRICE_ESTIMATION_TIME,
};

Expand Down Expand Up @@ -1717,6 +1741,7 @@ mod tests {
validity: Validity::default(),
quote_verification: QuoteVerificationMode::Unverified,
balance_fetcher: mock_balance_fetcher(),
order_execution_simulator: Arc::new(FakeOrderExecutionSimulator),
default_quote_timeout: HEALTHY_PRICE_ESTIMATION_TIME,
};

Expand Down
Loading