From 33a1e577801ca2b8778bc19fa26f41bbbad7295f Mon Sep 17 00:00:00 2001 From: sunce86 Date: Fri, 10 May 2024 15:04:16 +0200 Subject: [PATCH 01/11] initial commit --- crates/autopilot/src/domain/settlement/mod.rs | 62 +++++++++++++++++++ crates/autopilot/src/run_loop.rs | 6 +- 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/crates/autopilot/src/domain/settlement/mod.rs b/crates/autopilot/src/domain/settlement/mod.rs index e698cc8255..2eae3d3906 100644 --- a/crates/autopilot/src/domain/settlement/mod.rs +++ b/crates/autopilot/src/domain/settlement/mod.rs @@ -8,8 +8,10 @@ use { auction::{self, order}, eth, }, + run_loop, }, ethcontract::{common::FunctionExt, tokens::Tokenize, U256}, + std::collections::HashMap, trade::Trade, }; @@ -115,6 +117,66 @@ impl Settlement { Ok(Self { trades, auction_id }) } + + /// Build a settlement from a solved auction. + pub fn from_solution( + solution: &run_loop::Solution, + auction: &crate::domain::auction::Auction, + auction_id: auction::Id, + ) -> Self { + let auction_orders = auction + .orders + .iter() + .map(|o| (o.uid, o)) + .collect::>(); + + let trades = solution + .orders() + .iter() + .map(|(order_id, amounts)| { + let order = auction_orders.get(order_id).unwrap(); + let sell_token = &order.sell_token; + let buy_token = &order.buy_token; + trade::Trade::new( + order.uid, + eth::Asset { + token: sell_token.clone().into(), + amount: order.sell_amount.into(), + }, + eth::Asset { + token: buy_token.clone().into(), + amount: order.buy_amount.into(), + }, + order.side, + match order.side { + order::Side::Sell => amounts.sell_amount.into(), + order::Side::Buy => amounts.buy_amount.into(), + }, + trade::Prices { + uniform: trade::ClearingPrices { + sell: solution.clearing_prices()[sell_token], + buy: solution.clearing_prices()[buy_token], + }, + custom: trade::ClearingPrices { + sell: match order.side { + order::Side::Sell => amounts.buy_amount.into(), + order::Side::Buy => amounts.sell_amount.into(), + }, + buy: match order.side { + order::Side::Sell => amounts.sell_amount.into(), + order::Side::Buy => amounts.buy_amount.into(), + } + }, + }, + ) + }) + .collect(); + + Self { + trades, + auction_id, + } + } } /// Trade flags are encoded in a 256-bit integer field. For more information on diff --git a/crates/autopilot/src/run_loop.rs b/crates/autopilot/src/run_loop.rs index 60d0b4600b..f39075aa54 100644 --- a/crates/autopilot/src/run_loop.rs +++ b/crates/autopilot/src/run_loop.rs @@ -530,7 +530,7 @@ struct Participant<'a> { solution: Solution, } -struct Solution { +pub struct Solution { id: u64, account: H160, score: NonZeroU256, @@ -546,6 +546,10 @@ impl Solution { pub fn orders(&self) -> &HashMap { &self.orders } + + pub fn clearing_prices(&self) -> &HashMap { + &self.clearing_prices + } } #[derive(Debug, thiserror::Error)] From c43192d06f5c59e0156ff6a8f18005109f59532c Mon Sep 17 00:00:00 2001 From: sunce86 Date: Fri, 10 May 2024 15:35:57 +0200 Subject: [PATCH 02/11] error handling --- crates/autopilot/src/domain/settlement/mod.rs | 130 ++++++++++-------- 1 file changed, 72 insertions(+), 58 deletions(-) diff --git a/crates/autopilot/src/domain/settlement/mod.rs b/crates/autopilot/src/domain/settlement/mod.rs index 2eae3d3906..26fc8231d4 100644 --- a/crates/autopilot/src/domain/settlement/mod.rs +++ b/crates/autopilot/src/domain/settlement/mod.rs @@ -70,9 +70,9 @@ impl Settlement { let tokenized = function .decode_input(calldata) - .map_err(|err| EncodingError::Decoding(err).with(auction_id))?; + .map_err(|err| error::Encoding::Decoding(err).with(auction_id))?; let tokenized = ::from_token(web3::ethabi::Token::Tuple(tokenized)) - .map_err(|err| EncodingError::Tokenizing(err).with(auction_id))?; + .map_err(|err| error::Encoding::Tokenizing(err).with(auction_id))?; let (tokens, clearing_prices, decoded_trades, _interactions) = tokenized; @@ -91,7 +91,7 @@ impl Settlement { tokens.iter().position(|token| token == &buy_token).unwrap(); trades.push(trade::Trade::new( tokenized::order_uid(&trade, &tokens, domain_separator) - .map_err(|err| EncodingError::OrderUidRecover(err).with(auction_id))?, + .map_err(|err| error::Encoding::OrderUidRecover(err).with(auction_id))?, eth::Asset { token: sell_token.into(), amount: trade.3.into(), @@ -121,61 +121,63 @@ impl Settlement { /// Build a settlement from a solved auction. pub fn from_solution( solution: &run_loop::Solution, - auction: &crate::domain::auction::Auction, + auction: &auction::Auction, auction_id: auction::Id, - ) -> Self { + ) -> Result { let auction_orders = auction .orders .iter() .map(|o| (o.uid, o)) .collect::>(); - let trades = solution - .orders() - .iter() - .map(|(order_id, amounts)| { - let order = auction_orders.get(order_id).unwrap(); - let sell_token = &order.sell_token; - let buy_token = &order.buy_token; - trade::Trade::new( - order.uid, - eth::Asset { - token: sell_token.clone().into(), - amount: order.sell_amount.into(), - }, - eth::Asset { - token: buy_token.clone().into(), - amount: order.buy_amount.into(), - }, - order.side, - match order.side { - order::Side::Sell => amounts.sell_amount.into(), - order::Side::Buy => amounts.buy_amount.into(), + let mut trades = Vec::with_capacity(solution.orders().len()); + for (order_uid, traded) in solution.orders().iter() { + let order = auction_orders + .get(order_uid) + .ok_or(error::Solution::MissingOrder)?; + let sell_token = &order.sell_token; + let buy_token = &order.buy_token; + trades.push(trade::Trade::new( + order.uid, + eth::Asset { + token: (*sell_token).into(), + amount: order.sell_amount.into(), + }, + eth::Asset { + token: (*buy_token).into(), + amount: order.buy_amount.into(), + }, + order.side, + match order.side { + order::Side::Sell => traded.sell_amount.into(), + order::Side::Buy => traded.buy_amount.into(), + }, + trade::Prices { + uniform: trade::ClearingPrices { + sell: *solution + .clearing_prices() + .get(sell_token) + .ok_or(error::Solution::MissingClearingPrice)?, + buy: *solution + .clearing_prices() + .get(buy_token) + .ok_or(error::Solution::MissingClearingPrice)?, }, - trade::Prices { - uniform: trade::ClearingPrices { - sell: solution.clearing_prices()[sell_token], - buy: solution.clearing_prices()[buy_token], + custom: trade::ClearingPrices { + sell: match order.side { + order::Side::Sell => traded.buy_amount, + order::Side::Buy => traded.sell_amount, }, - custom: trade::ClearingPrices { - sell: match order.side { - order::Side::Sell => amounts.buy_amount.into(), - order::Side::Buy => amounts.sell_amount.into(), - }, - buy: match order.side { - order::Side::Sell => amounts.sell_amount.into(), - order::Side::Buy => amounts.buy_amount.into(), - } + buy: match order.side { + order::Side::Sell => traded.sell_amount, + order::Side::Buy => traded.buy_amount, }, }, - ) - }) - .collect(); - - Self { - trades, - auction_id, + }, + )); } + + Ok(Self { trades, auction_id }) } } @@ -237,19 +239,31 @@ impl From for TradeFlags { } } -#[derive(Debug, thiserror::Error)] -pub enum EncodingError { - #[error("unable to decode settlement calldata: {0}")] - Decoding(#[from] web3::ethabi::Error), - #[error("unable to tokenize calldata into expected format: {0}")] - Tokenizing(#[from] ethcontract::tokens::Error), - #[error("unable to recover order uid: {0}")] - OrderUidRecover(#[from] tokenized::Error), -} +pub mod error { + use super::*; + + #[derive(Debug, thiserror::Error)] + pub enum Encoding { + #[error("unable to decode settlement calldata: {0}")] + Decoding(#[from] web3::ethabi::Error), + #[error("unable to tokenize calldata into expected format: {0}")] + Tokenizing(#[from] ethcontract::tokens::Error), + #[error("unable to recover order uid: {0}")] + OrderUidRecover(#[from] tokenized::Error), + } + + impl Encoding { + pub fn with(self, auction: auction::Id) -> Error { + Error::Encoding(auction, self) + } + } -impl EncodingError { - pub fn with(self, auction: auction::Id) -> Error { - Error::Encoding(auction, self) + #[derive(Debug, thiserror::Error)] + pub enum Solution { + #[error("order not found in the auction")] + MissingOrder, + #[error("referenced clearing price missing")] + MissingClearingPrice, } } From c7bf825019bf9e40b15de71d18fed2d250b16a38 Mon Sep 17 00:00:00 2001 From: sunce86 Date: Mon, 13 May 2024 09:54:08 +0200 Subject: [PATCH 03/11] add logging --- crates/autopilot/src/run_loop.rs | 37 +++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/crates/autopilot/src/run_loop.rs b/crates/autopilot/src/run_loop.rs index f39075aa54..d0af09d353 100644 --- a/crates/autopilot/src/run_loop.rs +++ b/crates/autopilot/src/run_loop.rs @@ -1,7 +1,11 @@ use { crate::{ database::competition::Competition, - domain::{self, auction::order::Class, OrderUid}, + domain::{ + self, + auction::{self, order::Class}, + OrderUid, + }, infra::{ self, solvers::dto::{reveal, settle, solve}, @@ -141,6 +145,37 @@ impl RunLoop { } }; + { + // Also calculate the surplus from the solution itself + // For now, don't actually use it for ranking, instead log differences to solver + // provided score for debugging purposes. + let auction_prices = auction + .prices + .iter() + .map(|(k, v)| { + ( + (*k).into(), + auction::Price::new(domain::eth::Ether(*v)).unwrap(), + ) + }) + .collect(); + let settlement = + domain::settlement::Settlement::from_solution(solution, &auction, auction_id) + .map(|s| s.native_surplus(&auction_prices)); + + if let Ok(Ok(surplus)) = settlement { + if surplus.0 != solution.score.get() { + tracing::warn!( + "surplus mismatch: solver provided score: {}, calculated surplus: {}", + solution.score.get(), + surplus.0 + ); + } + } else { + tracing::warn!(?settlement, "failed to calculate surplus"); + } + } + let order_uids = solution.order_ids().copied().collect(); self.persistence .store_order_events(order_uids, OrderEventLabel::Considered); From b31c69b263da420935c06acafbd872532c629cd8 Mon Sep 17 00:00:00 2001 From: sunce86 Date: Mon, 13 May 2024 12:47:46 +0200 Subject: [PATCH 04/11] add comment --- crates/autopilot/src/domain/settlement/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/autopilot/src/domain/settlement/mod.rs b/crates/autopilot/src/domain/settlement/mod.rs index 26fc8231d4..393393bc71 100644 --- a/crates/autopilot/src/domain/settlement/mod.rs +++ b/crates/autopilot/src/domain/settlement/mod.rs @@ -119,6 +119,8 @@ impl Settlement { } /// Build a settlement from a solved auction. + /// + /// JIT orders are not included in the settlement if this constructor is used. pub fn from_solution( solution: &run_loop::Solution, auction: &auction::Auction, From f7700f28c80f57f5dfd206ed23f3010824602adb Mon Sep 17 00:00:00 2001 From: sunce86 Date: Mon, 13 May 2024 22:08:36 +0200 Subject: [PATCH 05/11] calculate score instead of surplus --- crates/autopilot/src/domain/settlement/mod.rs | 7 +- crates/autopilot/src/run_loop.rs | 66 ++++++++++--------- 2 files changed, 39 insertions(+), 34 deletions(-) diff --git a/crates/autopilot/src/domain/settlement/mod.rs b/crates/autopilot/src/domain/settlement/mod.rs index 40aee104ec..71b246dbed 100644 --- a/crates/autopilot/src/domain/settlement/mod.rs +++ b/crates/autopilot/src/domain/settlement/mod.rs @@ -143,8 +143,9 @@ impl Settlement { } /// Build a settlement from a solved auction. - /// - /// JIT orders are not included in the settlement if this constructor is used. + /// + /// JIT orders are not included in the settlement if this constructor is + /// used. pub fn from_solution( solution: &run_loop::Solution, auction: &auction::Auction, @@ -300,7 +301,7 @@ pub enum Error { #[error("no auction id found in calldata")] MissingAuctionId, #[error("auction {0} failed encoding: {1}")] - Encoding(auction::Id, EncodingError), + Encoding(auction::Id, error::Encoding), } #[cfg(test)] diff --git a/crates/autopilot/src/run_loop.rs b/crates/autopilot/src/run_loop.rs index c98831614f..7b98167039 100644 --- a/crates/autopilot/src/run_loop.rs +++ b/crates/autopilot/src/run_loop.rs @@ -146,37 +146,6 @@ impl RunLoop { } }; - { - // Also calculate the surplus from the solution itself - // For now, don't actually use it for ranking, instead log differences to solver - // provided score for debugging purposes. - let auction_prices = auction - .prices - .iter() - .map(|(k, v)| { - ( - (*k).into(), - auction::Price::new(domain::eth::Ether(*v)).unwrap(), - ) - }) - .collect(); - let settlement = - domain::settlement::Settlement::from_solution(solution, &auction, auction_id) - .map(|s| s.native_surplus(&auction_prices)); - - if let Ok(Ok(surplus)) = settlement { - if surplus.0 != solution.score.get() { - tracing::warn!( - "surplus mismatch: solver provided score: {}, calculated surplus: {}", - solution.score.get(), - surplus.0 - ); - } - } else { - tracing::warn!(?settlement, "failed to calculate surplus"); - } - } - let order_uids = solution.order_ids().copied().collect(); self.persistence .store_order_events(order_uids, OrderEventLabel::Considered); @@ -232,6 +201,41 @@ impl RunLoop { } } + { + // Also calculate the score from the solution itself + // For now, don't actually use it for ranking, instead log differences to solver + // provided score for debugging purposes. + let auction_prices = auction + .prices + .iter() + .map(|(k, v)| { + ( + (*k).into(), + auction::Price::new(domain::eth::Ether(*v)).unwrap(), + ) + }) + .collect(); + let policies = fee_policies + .iter() + .map(|(uid, policies)| (*uid, policies.clone())) + .collect(); + let settlement = + domain::settlement::Settlement::from_solution(solution, &auction, auction_id) + .map(|s| s.score(&auction_prices, &policies)); + + if let Ok(Ok(score)) = settlement { + if score.0 != solution.score.get() { + tracing::warn!( + "score mismatch: solver provided score: {}, calculated score: {}", + solution.score.get(), + score.0 + ); + } + } else { + tracing::warn!(?settlement, "failed to calculate score"); + } + } + let competition_table = SolverCompetitionDB { auction_start_block: auction.block, competition_simulation_block, From 72d7d25082e78b76776c087c515b6e4abe92553f Mon Sep 17 00:00:00 2001 From: sunce86 Date: Mon, 13 May 2024 22:32:35 +0200 Subject: [PATCH 06/11] update --- crates/autopilot/src/run_loop.rs | 71 ++++++++++++++++---------------- 1 file changed, 36 insertions(+), 35 deletions(-) diff --git a/crates/autopilot/src/run_loop.rs b/crates/autopilot/src/run_loop.rs index 7b98167039..9689a8ed17 100644 --- a/crates/autopilot/src/run_loop.rs +++ b/crates/autopilot/src/run_loop.rs @@ -146,6 +146,42 @@ impl RunLoop { } }; + { + // Also calculate the score from the solution itself + // For now, don't actually use it for ranking, instead log differences to solver + // provided score for debugging purposes. + let auction_prices = auction + .prices + .iter() + .map(|(k, v)| { + ( + (*k).into(), + auction::Price::new(domain::eth::Ether(*v)).unwrap(), + ) + }) + .collect(); + let policies = auction + .orders + .iter() + .map(|order| (order.uid, order.protocol_fees.clone())) + .collect(); + let settlement = + domain::settlement::Settlement::from_solution(solution, &auction, auction_id) + .map(|s| s.score(&auction_prices, &policies)); + + if let Ok(Ok(score)) = settlement { + if score.0 != solution.score.get() { + tracing::warn!( + "score mismatch: solver provided score: {}, calculated score: {}", + solution.score.get(), + score.0 + ); + } + } else { + tracing::warn!(?settlement, "failed to calculate score"); + } + } + let order_uids = solution.order_ids().copied().collect(); self.persistence .store_order_events(order_uids, OrderEventLabel::Considered); @@ -201,41 +237,6 @@ impl RunLoop { } } - { - // Also calculate the score from the solution itself - // For now, don't actually use it for ranking, instead log differences to solver - // provided score for debugging purposes. - let auction_prices = auction - .prices - .iter() - .map(|(k, v)| { - ( - (*k).into(), - auction::Price::new(domain::eth::Ether(*v)).unwrap(), - ) - }) - .collect(); - let policies = fee_policies - .iter() - .map(|(uid, policies)| (*uid, policies.clone())) - .collect(); - let settlement = - domain::settlement::Settlement::from_solution(solution, &auction, auction_id) - .map(|s| s.score(&auction_prices, &policies)); - - if let Ok(Ok(score)) = settlement { - if score.0 != solution.score.get() { - tracing::warn!( - "score mismatch: solver provided score: {}, calculated score: {}", - solution.score.get(), - score.0 - ); - } - } else { - tracing::warn!(?settlement, "failed to calculate score"); - } - } - let competition_table = SolverCompetitionDB { auction_start_block: auction.block, competition_simulation_block, From 4354517bce3ed1056732b6d84e6208087aac578d Mon Sep 17 00:00:00 2001 From: sunce86 Date: Mon, 13 May 2024 23:49:53 +0200 Subject: [PATCH 07/11] reuse competition::Solution --- crates/autopilot/src/domain/settlement/mod.rs | 44 ++++++++++--------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/crates/autopilot/src/domain/settlement/mod.rs b/crates/autopilot/src/domain/settlement/mod.rs index 71b246dbed..b37c991658 100644 --- a/crates/autopilot/src/domain/settlement/mod.rs +++ b/crates/autopilot/src/domain/settlement/mod.rs @@ -6,11 +6,11 @@ use { boundary, domain::{ auction::{self, order}, + competition, eth, fee, OrderUid, }, - run_loop, }, ethcontract::{common::FunctionExt, tokens::Tokenize, U256}, std::collections::HashMap, @@ -147,7 +147,7 @@ impl Settlement { /// JIT orders are not included in the settlement if this constructor is /// used. pub fn from_solution( - solution: &run_loop::Solution, + solution: &competition::Solution, auction: &auction::Auction, auction_id: auction::Id, ) -> Result { @@ -162,42 +162,46 @@ impl Settlement { let order = auction_orders .get(order_uid) .ok_or(error::Solution::MissingOrder)?; - let sell_token = &order.sell_token; - let buy_token = &order.buy_token; + let sell_token = order.sell_token.into(); + let buy_token = order.buy_token.into(); trades.push(trade::Trade::new( order.uid, eth::Asset { - token: (*sell_token).into(), + token: sell_token, amount: order.sell_amount.into(), }, eth::Asset { - token: (*buy_token).into(), + token: buy_token, amount: order.buy_amount.into(), }, order.side, match order.side { - order::Side::Sell => traded.sell_amount.into(), - order::Side::Buy => traded.buy_amount.into(), + order::Side::Sell => traded.sell.0.into(), + order::Side::Buy => traded.buy.0.into(), }, trade::Prices { uniform: trade::ClearingPrices { - sell: *solution - .clearing_prices() - .get(sell_token) - .ok_or(error::Solution::MissingClearingPrice)?, - buy: *solution - .clearing_prices() - .get(buy_token) - .ok_or(error::Solution::MissingClearingPrice)?, + sell: solution + .prices() + .get(&sell_token) + .ok_or(error::Solution::MissingClearingPrice)? + .get() + .0, + buy: solution + .prices() + .get(&buy_token) + .ok_or(error::Solution::MissingClearingPrice)? + .get() + .0, }, custom: trade::ClearingPrices { sell: match order.side { - order::Side::Sell => traded.buy_amount, - order::Side::Buy => traded.sell_amount, + order::Side::Sell => traded.buy.into(), + order::Side::Buy => traded.sell.into(), }, buy: match order.side { - order::Side::Sell => traded.sell_amount, - order::Side::Buy => traded.buy_amount, + order::Side::Sell => traded.sell.into(), + order::Side::Buy => traded.buy.into(), }, }, }, From 219a1e3ac3bc7d6d31665800746d90b51c056dce Mon Sep 17 00:00:00 2001 From: sunce86 Date: Tue, 14 May 2024 00:23:07 +0200 Subject: [PATCH 08/11] fix comments --- crates/autopilot/src/domain/settlement/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/autopilot/src/domain/settlement/mod.rs b/crates/autopilot/src/domain/settlement/mod.rs index b37c991658..0b2284aa7c 100644 --- a/crates/autopilot/src/domain/settlement/mod.rs +++ b/crates/autopilot/src/domain/settlement/mod.rs @@ -142,10 +142,10 @@ impl Settlement { Ok(Self { trades, auction_id }) } - /// Build a settlement from a solved auction. + /// Build a settlement from a `competition::Solution`. /// - /// JIT orders are not included in the settlement if this constructor is - /// used. + /// Since `competition::Solution` does not contain JIT orders, the resulting + /// settlement might be a subset of it's onchain observable twin. pub fn from_solution( solution: &competition::Solution, auction: &auction::Auction, From 4afdda32a4d007502ff0469417282636976c4d87 Mon Sep 17 00:00:00 2001 From: sunce86 Date: Wed, 15 May 2024 11:13:46 +0200 Subject: [PATCH 09/11] fix bug for buy orders --- crates/autopilot/src/domain/settlement/mod.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/crates/autopilot/src/domain/settlement/mod.rs b/crates/autopilot/src/domain/settlement/mod.rs index 0b2284aa7c..cf5a1144f2 100644 --- a/crates/autopilot/src/domain/settlement/mod.rs +++ b/crates/autopilot/src/domain/settlement/mod.rs @@ -195,14 +195,17 @@ impl Settlement { .0, }, custom: trade::ClearingPrices { - sell: match order.side { - order::Side::Sell => traded.buy.into(), - order::Side::Buy => traded.sell.into(), - }, - buy: match order.side { - order::Side::Sell => traded.sell.into(), - order::Side::Buy => traded.buy.into(), - }, + // settlement contract uses this formula to convert executed amounts: + // SELL order: executedBuyAmount = executed * sellPrice / buyPrice; + // BUY order: executedSellAmount = executed * buyPrice / sellPrice; + // + // With an example of converting 1 GNO for 100 DAI: + // SELL order: executedBuyAmount = 1 * 100 / 1 = 100 => + // (sellPrice = 100, buyPrice = 1) + // BUY order: executedSellAmount = 100 * 1 / 100 = 1 => + // (sellPrice = 100, buyPrice = 1) + sell: traded.buy.into(), + buy: traded.sell.into(), }, }, )); From aba9925051b5fa57832fa993ee06e7d2d72ba43e Mon Sep 17 00:00:00 2001 From: sunce86 Date: Wed, 15 May 2024 11:17:12 +0200 Subject: [PATCH 10/11] small cr fix --- crates/autopilot/src/domain/settlement/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/autopilot/src/domain/settlement/mod.rs b/crates/autopilot/src/domain/settlement/mod.rs index cf5a1144f2..40c23c6f61 100644 --- a/crates/autopilot/src/domain/settlement/mod.rs +++ b/crates/autopilot/src/domain/settlement/mod.rs @@ -186,13 +186,13 @@ impl Settlement { .get(&sell_token) .ok_or(error::Solution::MissingClearingPrice)? .get() - .0, + .into(), buy: solution .prices() .get(&buy_token) .ok_or(error::Solution::MissingClearingPrice)? .get() - .0, + .into(), }, custom: trade::ClearingPrices { // settlement contract uses this formula to convert executed amounts: From 8f6b35f0726c271437f5e67eb9f6735e8e4d9f35 Mon Sep 17 00:00:00 2001 From: sunce86 Date: Fri, 19 Jul 2024 09:59:02 +0200 Subject: [PATCH 11/11] small fixes due to merged code --- .../src/domain/settlement/solution/mod.rs | 20 ++++++------------- crates/autopilot/src/run_loop.rs | 14 ++----------- 2 files changed, 8 insertions(+), 26 deletions(-) diff --git a/crates/autopilot/src/domain/settlement/solution/mod.rs b/crates/autopilot/src/domain/settlement/solution/mod.rs index ce7a3f3cea..322c2d1d80 100644 --- a/crates/autopilot/src/domain/settlement/solution/mod.rs +++ b/crates/autopilot/src/domain/settlement/solution/mod.rs @@ -147,18 +147,10 @@ impl Solution { let order = auction_orders .get(order_uid) .ok_or(error::Solution::MissingOrder(*order_uid))?; - let sell_token = order.sell_token.into(); - let buy_token = order.buy_token.into(); trades.push(trade::Trade::new( order.uid, - eth::Asset { - token: sell_token, - amount: order.sell_amount.into(), - }, - eth::Asset { - token: buy_token, - amount: order.buy_amount.into(), - }, + order.sell, + order.buy, order.side, match order.side { order::Side::Sell => traded.sell.0.into(), @@ -168,14 +160,14 @@ impl Solution { uniform: trade::ClearingPrices { sell: solution .prices() - .get(&sell_token) - .ok_or(error::Solution::MissingClearingPrice(sell_token))? + .get(&order.sell.token) + .ok_or(error::Solution::MissingClearingPrice(order.sell.token))? .get() .into(), buy: solution .prices() - .get(&buy_token) - .ok_or(error::Solution::MissingClearingPrice(buy_token))? + .get(&order.buy.token) + .ok_or(error::Solution::MissingClearingPrice(order.buy.token))? .get() .into(), }, diff --git a/crates/autopilot/src/run_loop.rs b/crates/autopilot/src/run_loop.rs index 40518f19b9..2811cbb4a5 100644 --- a/crates/autopilot/src/run_loop.rs +++ b/crates/autopilot/src/run_loop.rs @@ -3,7 +3,7 @@ use { database::competition::Competition, domain::{ self, - auction::{self, order::Class}, + auction::order::Class, competition::{ SolutionError, {self}, @@ -150,16 +150,6 @@ impl RunLoop { // Also calculate the score from the solution itself // For now, don't actually use it for ranking, instead log differences to solver // provided score for debugging purposes. - let auction_prices = auction - .prices - .iter() - .map(|(k, v)| { - ( - (*k).into(), - auction::Price::new(domain::eth::Ether(*v)).unwrap(), - ) - }) - .collect(); let policies = auction .orders .iter() @@ -168,7 +158,7 @@ impl RunLoop { let settlement_solution = domain::settlement::Solution::from_competition_solution( solution, &auction, auction_id, ) - .map(|s| s.score(&auction_prices, &policies)); + .map(|s| s.score(&auction.prices, &policies)); if let Ok(Ok(score)) = settlement_solution { if score != solution.score() {