Skip to content

Commit

Permalink
Revert back to FullOrder
Browse files Browse the repository at this point in the history
  • Loading branch information
squadgazzz committed Aug 23, 2024
1 parent 2d0813b commit 6d012c5
Show file tree
Hide file tree
Showing 5 changed files with 288 additions and 546 deletions.
4 changes: 2 additions & 2 deletions crates/autopilot/src/infra/persistence/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,14 +394,14 @@ impl Persistence {
&self,
after_timestamp: DateTime<Utc>,
min_valid_to: i64,
) -> anyhow::Result<Vec<database::orders::OrderWithoutTrades>> {
) -> anyhow::Result<Vec<database::orders::FullOrder>> {
let _timer = Metrics::get()
.database_queries
.with_label_values(&["orders_after"])
.start_timer();
let mut ex = self.postgres.pool.acquire().await.context("begin")?;
Ok(
database::orders::orders_without_trades_after(&mut ex, after_timestamp, min_valid_to)
database::orders::full_orders_after(&mut ex, after_timestamp, min_valid_to)
.try_collect()
.await?,
)
Expand Down
29 changes: 9 additions & 20 deletions crates/autopilot/src/solvable_orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use {
signature::Signature,
time::now_in_epoch_seconds,
},
number::conversions::{big_decimal_to_u256, big_uint_to_big_decimal, u256_to_big_decimal},
number::conversions::{big_decimal_to_u256, u256_to_big_decimal},
primitive_types::{H160, H256, U256},
prometheus::{
Histogram,
Expand Down Expand Up @@ -169,7 +169,7 @@ impl SolvableOrdersCache {

fn build_solvable_orders(
current_orders: &boundary::SolvableOrders,
new_orders: Vec<database::orders::OrderWithoutTrades>,
new_orders: Vec<database::orders::FullOrder>,
mut new_trades: HashMap<domain::OrderUid, database::trades::TradedAmounts>,
new_quotes: HashMap<domain::OrderUid, domain::Quote>,
) -> Result<boundary::SolvableOrders> {
Expand Down Expand Up @@ -200,29 +200,18 @@ impl SolvableOrdersCache {
let Some(trade_amounts) = new_trades.remove(&uid) else {
continue;
};
let (executed_sell_amount, executed_buy_amount, executed_fee_amount) = orders
.get(&domain::OrderUid(new_order.uid.0))
.map(|o| {
(
big_uint_to_big_decimal(&o.metadata.executed_sell_amount),
big_uint_to_big_decimal(&o.metadata.executed_buy_amount),
u256_to_big_decimal(&o.metadata.executed_fee_amount),
)
})
.unwrap_or_default();
let sum_sell = trade_amounts.sell_amount + executed_sell_amount;
let sum_buy = trade_amounts.buy_amount + executed_buy_amount;

let fulfilled = match new_order.kind {
database::orders::OrderKind::Sell => sum_sell >= new_order.sell_amount,
database::orders::OrderKind::Buy => sum_buy >= new_order.buy_amount,
database::orders::OrderKind::Sell => {
trade_amounts.sell_amount >= new_order.sell_amount
}
database::orders::OrderKind::Buy => {
trade_amounts.buy_amount >= new_order.buy_amount
}
};

if !fulfilled {
let sum_fee = trade_amounts.fee_amount + executed_fee_amount;
let full_order =
database::orders::FullOrder::new(new_order, sum_sell, sum_buy, sum_fee);
let order = full_order_into_model_order(full_order).map_err(anyhow::Error::from)?;
let order = full_order_into_model_order(new_order).map_err(anyhow::Error::from)?;
orders.insert(uid, order);

if let Some(new_quote) = new_quotes.get(&uid) {
Expand Down
Loading

0 comments on commit 6d012c5

Please sign in to comment.