Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store auction with liquidity #2663

Merged
merged 5 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/autopilot/src/infra/persistence/dto/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,6 @@ impl From<boundary::EcdsaSignature> for domain::auction::order::EcdsaSignature {
}
}

#[serde_as]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It doesn't look like this is needed.

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum FeePolicy {
Expand Down
1 change: 1 addition & 0 deletions crates/driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ path = "src/main.rs"
[dependencies]
app-data = { path = "../app-data" }
bytes-hex = { path = "../bytes-hex" }
s3 = { path = "../s3" }
async-trait = { workspace = true }
axum = { workspace = true }
bigdecimal = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/driver/src/domain/competition/auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use {
domain::{
competition::{self, auction},
eth,
liquidity,
liquidity::{self},
time,
},
infra::{self, blockchain, observe, Ethereum},
Expand Down
1 change: 1 addition & 0 deletions crates/driver/src/infra/config/file/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ pub async fn load(chain: eth::ChainId, path: &Path) -> infra::Config {
true => SolutionMerging::Allowed,
false => SolutionMerging::Forbidden,
},
s3: config.s3,
}
}))
.await,
Expand Down
7 changes: 6 additions & 1 deletion crates/driver/src/infra/config/file/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub use load::load;
use {
crate::{domain::eth, util::serialize},
crate::{domain::eth, infra::persistence::S3, util::serialize},
m-lord-renkse marked this conversation as resolved.
Show resolved Hide resolved
reqwest::Url,
serde::{Deserialize, Serialize},
serde_with::serde_as,
Expand Down Expand Up @@ -225,6 +225,11 @@ struct SolverConfig {
/// auction together.
#[serde(default)]
merge_solutions: bool,

/// S3 configuration for storing the auction in the form they are sent to
m-lord-renkse marked this conversation as resolved.
Show resolved Hide resolved
/// the solver engine
#[serde(default)]
s3: Option<S3>,
}

#[derive(Clone, Copy, Debug, Default, Deserialize, PartialEq, Serialize)]
Expand Down
1 change: 1 addition & 0 deletions crates/driver/src/infra/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod liquidity;
pub mod mempool;
pub mod notify;
pub mod observe;
pub mod persistence;
pub mod simulator;
pub mod solver;
pub mod time;
Expand Down
70 changes: 70 additions & 0 deletions crates/driver/src/infra/persistence/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use {
crate::{domain::competition::auction::Id, infra::solver::Config},
serde::Deserialize,
std::sync::Arc,
tracing::Instrument,
};

#[derive(Clone, Debug, Default, Deserialize)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
m-lord-renkse marked this conversation as resolved.
Show resolved Hide resolved
pub struct S3 {
/// The s3_instance_upload_* arguments configure how auction instances
/// should be uploaded to AWS S3.
/// They must either all be set or all not set.
m-lord-renkse marked this conversation as resolved.
Show resolved Hide resolved
pub bucket: String,

/// Prepended to the auction id to form the final instance filename on S3.
/// Something like "staging/mainnet/"
pub prefix: String,
}

impl From<S3> for s3::Config {
fn from(value: S3) -> Self {
Self {
bucket: value.bucket,
filename_prefix: value.prefix,
}
}
}

#[derive(Clone, Debug)]
pub struct Persistence {
s3: Option<Arc<s3::Uploader>>,
}

impl Persistence {
pub async fn build(config: &Config) -> Self {
if let Some(s3) = &config.s3 {
Self {
s3: Some(Arc::new(s3::Uploader::new(s3.clone().into()).await)),
}
} else {
Self { s3: None }
}
}

/// Saves the given auction with liquidity with fire and forget mentality
/// (non-blocking operation)
pub fn archive_auction(&self, auction_id: Option<Id>, body: &str) {
let Some(uploader) = self.s3.clone() else {
return;
};
let Some(id) = auction_id else {
return;
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Deciding that auctions without ids (i.e. quote requests) should not be uploaded seems like a domain decision to me. If archive_auction were to take a non-optional Id this would naturally be enforced by the type system in the domain logic.

Also there should be a comment somewhere explaining why we only store auctions which have an id.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure! I'll move it to the domain logic.
I believe (I may be wrong) that auctions without id are only the ones generated by fake_auction function 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe (I may be wrong) that auctions without id are only the ones generated by fake_auction function

Yes, that's right. Those are the quote requests which don't really make sense to store.

let body = body.to_string();
tokio::spawn(
async move {
match uploader.upload(id.to_string(), body).await {
Ok(key) => {
tracing::info!(?key, "uploaded auction to s3");
m-lord-renkse marked this conversation as resolved.
Show resolved Hide resolved
}
Err(err) => {
tracing::warn!(?err, "failed to upload auction to s3");
}
}
}
.instrument(tracing::Span::current()),
);
}
}
20 changes: 18 additions & 2 deletions crates/driver/src/infra/solver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ use {
liquidity,
time::Remaining,
},
infra::{blockchain::Ethereum, config::file::FeeHandler},
infra::{
blockchain::Ethereum,
config::file::FeeHandler,
persistence::{Persistence, S3},
},
util,
},
anyhow::Result,
Expand Down Expand Up @@ -85,6 +89,7 @@ pub struct Solver {
client: reqwest::Client,
config: Config,
eth: Ethereum,
persistence: Persistence,
}

#[derive(Debug, Clone)]
Expand All @@ -108,10 +113,13 @@ pub struct Config {
/// TODO: Remove once all solvers are moved to use limit orders for quoting
pub quote_using_limit_orders: bool,
pub merge_solutions: SolutionMerging,
/// S3 configuration for storing the auction in the form they are sent to
/// the solver engine
pub s3: Option<S3>,
}

impl Solver {
pub fn new(config: Config, eth: Ethereum) -> Result<Self> {
pub async fn new(config: Config, eth: Ethereum) -> Result<Self> {
let mut headers = reqwest::header::HeaderMap::new();
headers.insert(
reqwest::header::CONTENT_TYPE,
Expand All @@ -124,15 +132,22 @@ impl Solver {
headers.insert(header_name, val.parse()?);
}

let persistence = Persistence::build(&config).await;

Ok(Self {
client: reqwest::ClientBuilder::new()
.default_headers(headers)
.build()?,
config,
eth,
persistence,
})
}

pub fn persistence(&self) -> Persistence {
self.persistence.clone()
}

pub fn name(&self) -> &Name {
&self.config.name
}
Expand Down Expand Up @@ -187,6 +202,7 @@ impl Solver {
self.config.fee_handler,
))
.unwrap();
self.persistence.archive_auction(auction.id(), &body);
let url = shared::url::join(&self.config.endpoint, "solve");
super::observe::solver_request(&url, &body);
let mut req = self
Expand Down
18 changes: 11 additions & 7 deletions crates/driver/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use {
},
},
clap::Parser,
futures::future::join_all,
std::{net::SocketAddr, sync::Arc, time::Duration},
tokio::sync::oneshot,
};
Expand Down Expand Up @@ -51,7 +52,7 @@ async fn run_with(args: cli::Args, addr_sender: Option<oneshot::Sender<SocketAdd
let (shutdown_sender, shutdown_receiver) = tokio::sync::oneshot::channel();
let eth = ethereum(&config, ethrpc).await;
let serve = Api {
solvers: solvers(&config, &eth),
solvers: solvers(&config, &eth).await,
liquidity: liquidity(&config, &eth).await,
simulator: simulator(&config, &eth),
mempools: Mempools::new(
Expand Down Expand Up @@ -134,12 +135,15 @@ async fn ethereum(config: &infra::Config, ethrpc: blockchain::Rpc) -> Ethereum {
Ethereum::new(ethrpc, config.contracts, gas).await
}

fn solvers(config: &config::Config, eth: &Ethereum) -> Vec<Solver> {
config
.solvers
.iter()
.map(|config| Solver::new(config.clone(), eth.clone()).unwrap())
.collect()
async fn solvers(config: &config::Config, eth: &Ethereum) -> Vec<Solver> {
join_all(
config
.solvers
.iter()
.map(|config| async move { Solver::new(config.clone(), eth.clone()).await.unwrap() })
.collect::<Vec<_>>(),
)
.await
}

async fn liquidity(config: &config::Config, eth: &Ethereum) -> liquidity::Fetcher {
Expand Down
2 changes: 1 addition & 1 deletion crates/s3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct Config {
pub filename_prefix: String,
}

#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct Uploader {
bucket: String,
filename_prefix: String,
Expand Down
Loading