Skip to content

Commit

Permalink
Orderbook mock
Browse files Browse the repository at this point in the history
  • Loading branch information
squadgazzz committed Jan 17, 2025
1 parent 4743607 commit 56e21d4
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 9 deletions.
2 changes: 1 addition & 1 deletion crates/driver/src/domain/competition/order/app_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl AppDataRetriever {
let url = self_
.0
.base_url
.join(&format!("v1/app_data/{:?}", app_data))?;
.join(&format!("v1/app_data/{:?}", app_data.0))?;
let response = self_.0.client.get(url).send().await?;

let validated_app_data = match response.status() {
Expand Down
4 changes: 4 additions & 0 deletions crates/driver/src/infra/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ pub struct Args {
#[clap(long, env)]
pub ethrpc: Url,

/// The orderbook API base URL.
#[clap(long, env)]
pub orderbook_url: Url,

/// Path to the driver configuration file. This file should be in TOML
/// format. For an example see
/// https://github.com/cowprotocol/services/blob/main/crates/driver/example.toml.
Expand Down
1 change: 0 additions & 1 deletion crates/driver/src/infra/config/file/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,5 @@ pub async fn load(chain: Chain, path: &Path) -> infra::Config {
order_priority_strategies: config.order_priority_strategies,
archive_node_url: config.archive_node_url,
simulation_bad_token_max_age: config.simulation_bad_token_max_age,
orderbook_url: config.orderbook_url,
}
}
3 changes: 0 additions & 3 deletions crates/driver/src/infra/config/file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ struct Config {
default = "default_simulation_bad_token_max_age"
)]
simulation_bad_token_max_age: Duration,

/// URL of the orderbook API to fetch order's app-data from.
orderbook_url: Url,
}

#[serde_as]
Expand Down
1 change: 0 additions & 1 deletion crates/driver/src/infra/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,4 @@ pub struct Config {
pub order_priority_strategies: Vec<OrderPriorityStrategy>,
pub archive_node_url: Option<Url>,
pub simulation_bad_token_max_age: Duration,
pub orderbook_url: Url,
}
2 changes: 1 addition & 1 deletion crates/driver/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ async fn run_with(args: cli::Args, addr_sender: Option<oneshot::Sender<SocketAdd
let _ = shutdown_receiver.await;
},
config.order_priority_strategies,
config.orderbook_url,
args.orderbook_url,
);

futures::pin_mut!(serve);
Expand Down
8 changes: 7 additions & 1 deletion crates/driver/src/tests/setup/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use {
crate::{
domain::competition::order,
infra::config::file::OrderPriorityStrategy,
tests::{hex_address, setup::blockchain::Trade},
tests::{
hex_address,
setup::{blockchain::Trade, orderbook::Orderbook},
},
},
rand::seq::SliceRandom,
serde_json::json,
Expand Down Expand Up @@ -31,6 +34,7 @@ impl Driver {
config: &Config,
solvers: &Vec<(Solver, SocketAddr)>,
blockchain: &Blockchain,
orderbook: Orderbook,
) -> Self {
let (config_file, config_temp_path) = match config.config_file.as_ref() {
Some(config_file) => (config_file.to_owned(), None),
Expand All @@ -46,6 +50,8 @@ impl Driver {
"0.0.0.0:0".to_owned(),
"--ethrpc".to_owned(),
blockchain.web3_url.clone(),
"--orderbook-url".to_owned(),
format!("http://{:?}", orderbook.addr).to_owned(),
"--config".to_owned(),
config_file.to_str().unwrap().to_owned(),
];
Expand Down
8 changes: 7 additions & 1 deletion crates/driver/src/tests/setup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ use {
ETH_ORDER_AMOUNT,
},
hex_address,
setup::blockchain::{Blockchain, Interaction, Trade},
setup::{
blockchain::{Blockchain, Interaction, Trade},
orderbook::Orderbook,
},
},
},
app_data::AppDataHash,
Expand All @@ -51,6 +54,7 @@ use {
pub mod blockchain;
mod driver;
pub mod fee;
mod orderbook;
mod solver;

#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -957,6 +961,7 @@ impl Setup {
(solver.clone(), instance.addr)
}))
.await;
let orderbook = Orderbook::start();
let driver = Driver::new(
&driver::Config {
config_file,
Expand All @@ -966,6 +971,7 @@ impl Setup {
},
&solvers_with_address,
&blockchain,
orderbook,
)
.await;

Expand Down
28 changes: 28 additions & 0 deletions crates/driver/src/tests/setup/orderbook.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use {
axum::{extract::Path, http::StatusCode, routing::get, Router},
std::net::SocketAddr,
};

pub struct Orderbook {
pub addr: SocketAddr,
}

impl Orderbook {
pub fn start() -> Self {
let app = Router::new().route("/v1/app_data/:app_data", get(Self::mock_handler));
let server =
axum::Server::bind(&"0.0.0.0:0".parse().unwrap()).serve(app.into_make_service());
let addr = server.local_addr();
println!("Orderbook mock server listening on {}", addr);

tokio::spawn(server);

Orderbook { addr }
}

/// Default mock handler that always returns 404 Not Found.
async fn mock_handler(Path(app_data): Path<String>) -> StatusCode {
println!("Received app_data request: {}", app_data);
StatusCode::NOT_FOUND
}
}

0 comments on commit 56e21d4

Please sign in to comment.