Skip to content

Commit

Permalink
Merge branch 'main' into feat/legacy-solver-e2e-setup
Browse files Browse the repository at this point in the history
  • Loading branch information
devanoneth authored Jan 16, 2024
2 parents bd6953c + 69a9683 commit 7c4c47c
Show file tree
Hide file tree
Showing 72 changed files with 2,339 additions and 1,599 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/pull-request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ jobs:
- uses: Swatinem/rust-cache@v2
# Start the build process in the background. The following cargo test command will automatically
# wait for the build process to be done before proceeding.
- run: cargo build -p orderbook -p database --tests &
- run: cargo build -p orderbook -p database -p autopilot --tests &
- uses: taiki-e/install-action@nextest
- uses: yu-ichiro/spin-up-docker-compose-action@v1
with:
file: docker-compose.yaml
up-opts: -d db migrations
- run: cargo nextest run postgres -p orderbook -p database --test-threads 1 --run-ignored ignored-only
- run: cargo nextest run postgres -p orderbook -p database -p autopilot --test-threads 1 --run-ignored ignored-only

test-local-node:
timeout-minutes: 60
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ jobs:
uses: actions/checkout@v2
with:
fetch-depth: 0 # Fetch all history for all branches and tags
# Requires "Read and Write access to code" permission
token: ${{ secrets.RELEASE_ACTION_ACCESS_TOKEN }}

- name: Create Tag
id: tag_version
Expand All @@ -29,8 +31,6 @@ jobs:
NEW_MINOR=$((MINOR+1))
NEW_TAG="${MAJOR}.${NEW_MINOR}.0"
echo ::set-output name=tag::$NEW_TAG
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git tag $NEW_TAG
git push origin --tags
Expand Down
37 changes: 16 additions & 21 deletions crates/autopilot/src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub struct Arguments {

/// The number of order events to insert in a single batch.
#[clap(long, env, default_value = "500")]
pub order_events_insert_batch_size: NonZeroUsize,
pub insert_batch_size: NonZeroUsize,

/// Skip syncing past events (useful for local deployments)
#[clap(long, env, action = clap::ArgAction::Set, default_value = "false")]
Expand Down Expand Up @@ -254,7 +254,7 @@ impl std::fmt::Display for Arguments {
order_events_cleanup_interval,
order_events_cleanup_threshold,
db_url,
order_events_insert_batch_size,
insert_batch_size,
native_price_estimation_results_required,
auction_update_interval,
max_settlement_transaction_wait,
Expand Down Expand Up @@ -322,11 +322,7 @@ impl std::fmt::Display for Arguments {
"order_events_cleanup_threshold: {:?}",
order_events_cleanup_threshold
)?;
writeln!(
f,
"order_events_insert_batch_size: {}",
order_events_insert_batch_size
)?;
writeln!(f, "insert_batch_size: {}", insert_batch_size)?;
writeln!(
f,
"native_price_estimation_results_required: {}",
Expand All @@ -347,15 +343,15 @@ impl std::fmt::Display for Arguments {
pub struct FeePolicy {
/// Type of fee policy to use. Examples:
///
/// - Price improvement without cap
/// price_improvement:0.5:1.0
/// - Surplus without cap
/// surplus:0.5:1.0
///
/// - Price improvement with cap:
/// price_improvement:0.5:0.06
/// - Surplus with cap:
/// surplus:0.5:0.06
///
/// - Volume based:
/// volume:0.1
#[clap(long, env, default_value = "priceImprovement:0.0:1.0")]
#[clap(long, env, default_value = "surplus:0.0:1.0")]
pub fee_policy_kind: FeePolicyKind,

/// Should protocol fees be collected or skipped for orders whose
Expand All @@ -368,10 +364,10 @@ pub struct FeePolicy {
impl FeePolicy {
pub fn to_domain(self) -> domain::fee::Policy {
match self.fee_policy_kind {
FeePolicyKind::PriceImprovement {
FeePolicyKind::Surplus {
factor,
max_volume_factor,
} => domain::fee::Policy::PriceImprovement {
} => domain::fee::Policy::Surplus {
factor,
max_volume_factor,
},
Expand All @@ -382,9 +378,8 @@ impl FeePolicy {

#[derive(clap::Parser, Debug, Clone)]
pub enum FeePolicyKind {
/// How much of the order's price improvement over max(limit price,
/// best_bid) should be taken as a protocol fee.
PriceImprovement { factor: f64, max_volume_factor: f64 },
/// How much of the order's surplus should be taken as a protocol fee.
Surplus { factor: f64, max_volume_factor: f64 },
/// How much of the order's volume should be taken as a protocol fee.
Volume { factor: f64 },
}
Expand All @@ -396,18 +391,18 @@ impl FromStr for FeePolicyKind {
let mut parts = s.split(':');
let kind = parts.next().ok_or("missing fee policy kind")?;
match kind {
"priceImprovement" => {
"surplus" => {
let factor = parts
.next()
.ok_or("missing price improvement factor")?
.ok_or("missing surplus factor")?
.parse::<f64>()
.map_err(|e| format!("invalid price improvement factor: {}", e))?;
.map_err(|e| format!("invalid surplus factor: {}", e))?;
let max_volume_factor = parts
.next()
.ok_or("missing max volume factor")?
.parse::<f64>()
.map_err(|e| format!("invalid max volume factor: {}", e))?;
Ok(Self::PriceImprovement {
Ok(Self::Surplus {
factor,
max_volume_factor,
})
Expand Down
11 changes: 7 additions & 4 deletions crates/autopilot/src/boundary/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
pub use {
crate::database::{
competition::Competition,
order_events::{store_order_events, OrderEventLabel},
crate::{
database::{
competition::Competition,
order_events::{store_order_events, OrderEventLabel},
},
driver_model::{reveal, settle, solve},
},
database::orders::Quote as DatabaseQuote,
database,
model::{
app_data::AppDataHash,
interaction::InteractionData,
Expand Down
4 changes: 2 additions & 2 deletions crates/autopilot/src/boundary/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use {crate::domain, shared::remaining_amounts};

pub fn to_domain(
order: model::order::Order,
fee_policies: Vec<domain::fee::Policy>,
protocol_fees: Vec<domain::fee::Policy>,
) -> domain::Order {
let remaining_order = remaining_amounts::Order::from(order.clone());
let order_is_untouched = remaining_order.executed_amount.is_zero();
Expand All @@ -15,6 +15,7 @@ pub fn to_domain(
buy_amount: order.data.buy_amount,
solver_fee: order.metadata.full_fee_amount,
user_fee: order.data.fee_amount,
protocol_fees,
valid_to: order.data.valid_to,
kind: order.data.kind.into(),
receiver: order.data.receiver,
Expand All @@ -35,6 +36,5 @@ pub fn to_domain(
class: order.metadata.class.into(),
app_data: order.data.app_data.into(),
signature: order.signature.into(),
fee_policies,
}
}
24 changes: 10 additions & 14 deletions crates/autopilot/src/database.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
use {
sqlx::{Executor, PgConnection, PgPool},
std::{num::NonZeroUsize, time::Duration},
tracing::Instrument,
};

mod auction;
pub mod auction_prices;
pub mod auction_transaction;
pub mod competition;
pub mod ethflow_events;
mod events;
pub mod fee_policies;
pub mod on_settlement_event_updater;
pub mod onchain_order_events;
pub mod order_events;
pub mod orders;
mod quotes;
pub mod recent_settlements;

use {
sqlx::{Executor, PgConnection, PgPool},
std::{num::NonZeroUsize, time::Duration},
tracing::Instrument,
};

#[derive(Debug, Clone)]
pub struct Config {
pub order_events_insert_batch_size: NonZeroUsize,
pub insert_batch_size: NonZeroUsize,
}

#[derive(Debug, Clone)]
Expand All @@ -29,15 +30,10 @@ pub struct Postgres {
}

impl Postgres {
pub async fn new(
url: &str,
order_events_insert_batch_size: NonZeroUsize,
) -> sqlx::Result<Self> {
pub async fn new(url: &str, insert_batch_size: NonZeroUsize) -> sqlx::Result<Self> {
Ok(Self {
pool: PgPool::connect(url).await?,
config: Config {
order_events_insert_batch_size,
},
config: Config { insert_batch_size },
})
}

Expand Down
15 changes: 0 additions & 15 deletions crates/autopilot/src/database/auction_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,4 @@ impl super::Postgres {
)
.await
}

pub async fn get_auction_id(
&self,
tx_from: H160,
tx_nonce: i64,
) -> Result<Option<i64>, sqlx::Error> {
let _timer = super::Metrics::get()
.database_queries
.with_label_values(&["get_auction_id"])
.start_timer();

let mut ex = self.pool.acquire().await?;
database::auction_transaction::get_auction_id(&mut ex, &ByteArray(tx_from.0), tx_nonce)
.await
}
}
102 changes: 102 additions & 0 deletions crates/autopilot/src/database/fee_policies.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
use {
crate::infra::persistence::dto,
sqlx::{PgConnection, QueryBuilder},
};

pub async fn insert_batch(
ex: &mut PgConnection,
fee_policies: impl IntoIterator<Item = dto::FeePolicy>,
) -> Result<(), sqlx::Error> {
let mut query_builder = QueryBuilder::new(
"INSERT INTO fee_policies (auction_id, order_uid, kind, surplus_factor, \
max_volume_factor, volume_factor) ",
);

query_builder.push_values(fee_policies, |mut b, fee_policy| {
b.push_bind(fee_policy.auction_id)
.push_bind(fee_policy.order_uid)
.push_bind(fee_policy.kind)
.push_bind(fee_policy.surplus_factor)
.push_bind(fee_policy.max_volume_factor)
.push_bind(fee_policy.volume_factor);
});

query_builder.build().execute(ex).await.map(|_| ())
}

pub async fn fetch(
ex: &mut PgConnection,
auction_id: dto::AuctionId,
order_uid: database::OrderUid,
) -> Result<Vec<dto::FeePolicy>, sqlx::Error> {
const QUERY: &str = r#"
SELECT * FROM fee_policies
WHERE auction_id = $1 AND order_uid = $2
ORDER BY application_order
"#;
let rows = sqlx::query_as::<_, dto::FeePolicy>(QUERY)
.bind(auction_id)
.bind(order_uid)
.fetch_all(ex)
.await?
.into_iter()
.collect();
Ok(rows)
}

#[cfg(test)]
mod tests {
use {super::*, database::byte_array::ByteArray, sqlx::Connection};

#[tokio::test]
#[ignore]
async fn postgres_roundtrip() {
let mut db = PgConnection::connect("postgresql://").await.unwrap();
let mut db = db.begin().await.unwrap();
database::clear_DANGER_(&mut db).await.unwrap();

// same primary key for all fee policies
let (auction_id, order_uid) = (1, ByteArray([1; 56]));

// surplus fee policy without caps
let fee_policy_1 = dto::FeePolicy {
auction_id,
order_uid,
kind: dto::fee_policy::FeePolicyKind::Surplus,
surplus_factor: Some(0.1),
max_volume_factor: Some(1.0),
volume_factor: None,
};
// surplus fee policy with caps
let fee_policy_2 = dto::FeePolicy {
auction_id,
order_uid,
kind: dto::fee_policy::FeePolicyKind::Surplus,
surplus_factor: Some(0.2),
max_volume_factor: Some(0.05),
volume_factor: None,
};
// volume based fee policy
let fee_policy_3 = dto::FeePolicy {
auction_id,
order_uid,
kind: dto::fee_policy::FeePolicyKind::Volume,
surplus_factor: None,
max_volume_factor: None,
volume_factor: Some(0.06),
};
insert_batch(
&mut db,
vec![
fee_policy_1.clone(),
fee_policy_2.clone(),
fee_policy_3.clone(),
],
)
.await
.unwrap();

let output = fetch(&mut db, 1, order_uid).await.unwrap();
assert_eq!(output, vec![fee_policy_1, fee_policy_2, fee_policy_3]);
}
}
Loading

0 comments on commit 7c4c47c

Please sign in to comment.