Skip to content

Commit

Permalink
Store limit order quote to compute risk adjusted rewards later on (#908)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinquaXD authored Dec 6, 2022
1 parent f41fe81 commit de41803
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
34 changes: 32 additions & 2 deletions crates/autopilot/src/database/orders.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use super::Postgres;
use anyhow::Result;
use chrono::{Duration, Utc};
use database::byte_array::ByteArray;
use ethcontract::U256;
use futures::{StreamExt, TryStreamExt};
use model::{
order::{Order, OrderUid},
time::now_in_epoch_seconds,
};
use number_conversions::u256_to_big_decimal;
use shared::db_order_conversions::full_order_into_model_order;
use shared::{db_order_conversions::full_order_into_model_order, fee_subsidy::FeeParameters};

/// New fee data to update a limit order with.
pub struct FeeUpdate {
Expand All @@ -21,6 +22,18 @@ pub struct FeeUpdate {
pub full_fee_amount: U256,
}

/// Data required to compute risk adjusted rewards for limit orders.
pub struct LimitOrderQuote {
/// Everything required to compute the fee amount in sell token
pub fee_parameters: FeeParameters,

/// The `sell_amount` of the quote associated with the `surplus_fee` estimation.
pub sell_amount: U256,

/// The `buy_amount` of the quote associated with the `surplus_fee` estimation.
pub buy_amount: U256,
}

impl Postgres {
pub async fn limit_orders_with_outdated_fees(&self, age: Duration) -> Result<Vec<Order>> {
let _timer = super::Metrics::get()
Expand All @@ -43,17 +56,20 @@ impl Postgres {
.await
}

/// Updates the `surplus_fee` of a limit order together with the quote used to compute that
/// fee.
pub async fn update_limit_order_fees(
&self,
order_uid: &OrderUid,
update: &FeeUpdate,
quote: &LimitOrderQuote,
) -> Result<()> {
let _timer = super::Metrics::get()
.database_queries
.with_label_values(&["update_limit_order_fees"])
.start_timer();

let mut ex = self.0.acquire().await?;
let mut ex = self.0.begin().await?;
database::orders::update_limit_order_fees(
&mut ex,
&database::byte_array::ByteArray(order_uid.0),
Expand All @@ -64,6 +80,20 @@ impl Postgres {
},
)
.await?;

database::orders::insert_quote_and_update_on_conflict(
&mut ex,
&database::orders::Quote {
order_uid: ByteArray(order_uid.0),
gas_amount: quote.fee_parameters.gas_amount,
gas_price: quote.fee_parameters.gas_price,
sell_token_price: quote.fee_parameters.sell_token_price,
sell_amount: u256_to_big_decimal(&quote.sell_amount),
buy_amount: u256_to_big_decimal(&quote.buy_amount),
},
)
.await?;
ex.commit().await?;
Ok(())
}

Expand Down
13 changes: 11 additions & 2 deletions crates/autopilot/src/limit_orders/quoter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::database::{orders::FeeUpdate, Postgres};
use crate::database::{
orders::{FeeUpdate, LimitOrderQuote},
Postgres,
};
use anyhow::Result;
use chrono::Duration;
use futures::future::join_all;
Expand Down Expand Up @@ -117,8 +120,14 @@ impl LimitOrderQuoter {
surplus_fee: quote.fee_amount,
full_fee_amount: quote.full_fee_amount,
},
&LimitOrderQuote {
fee_parameters: quote.data.fee_parameters,
sell_amount: quote.sell_amount,
buy_amount: quote.buy_amount,
},
)
.await
.await?;
Ok(())
}
}

Expand Down

0 comments on commit de41803

Please sign in to comment.