Skip to content

Commit

Permalink
Calculate score in autopilot domain (#2724)
Browse files Browse the repository at this point in the history
# Description
Related to #2719 (will not
fix it until unit test is updated 👇 )

Adds score calculation to `domain::Settlement` object, which will be
used to calculate the score during solver competition.

## How to test
Will update the `settlement` unit test to test with a settlement
transaction that contains multiple fee policies so the whole
functionality (surplus, multiple fee policies, score) is tested
together.

Will update the test in a separate PR. Now, only the function is merged
to unblock progress on other tasks for colocation.
  • Loading branch information
sunce86 authored May 13, 2024
1 parent 0642920 commit a80da50
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
27 changes: 26 additions & 1 deletion crates/autopilot/src/domain/settlement/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ use {
domain::{
auction::{self, order},
eth,
fee,
OrderUid,
},
},
ethcontract::{common::FunctionExt, tokens::Tokenize, U256},
std::collections::HashMap,
trade::Trade,
};

Expand All @@ -18,7 +21,6 @@ mod trade;

/// Settlement originated from a calldata of a settlement transaction.
#[derive(Debug)]
#[allow(dead_code)]
pub struct Settlement {
trades: Vec<Trade>,
/// Data that was appended to the regular call data of the `settle()` call
Expand All @@ -32,6 +34,29 @@ impl Settlement {
/// id.
const META_DATA_LEN: usize = 8;

pub fn auction_id(&self) -> auction::Id {
self.auction_id
}

pub fn score(
&self,
prices: &auction::Prices,
policies: &HashMap<OrderUid, Vec<fee::Policy>>,
) -> Result<eth::Ether, trade::Error> {
self.trades
.iter()
.map(|trade| {
trade.score(
prices,
policies
.get(trade.order_uid())
.map(|value| value.as_slice())
.unwrap_or_default(),
)
})
.sum()
}

pub fn native_surplus(&self, prices: &auction::Prices) -> Result<eth::Ether, trade::Error> {
self.trades
.iter()
Expand Down
9 changes: 5 additions & 4 deletions crates/autopilot/src/domain/settlement/trade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use {
};

#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct Trade {
order_uid: domain::OrderUid,
sell: eth::Asset,
Expand Down Expand Up @@ -43,16 +42,18 @@ impl Trade {
}
}

pub fn order_uid(&self) -> &domain::OrderUid {
&self.order_uid
}

/// CIP38 score defined as surplus + protocol fee
///
/// Denominated in NATIVE token
#[allow(dead_code)]
fn score(
pub fn score(
&self,
prices: &auction::Prices,
policies: &[fee::Policy],
) -> Result<eth::Ether, Error> {
tracing::debug!("Scoring trade {:?}", self);
Ok(self.native_surplus(prices)? + self.native_protocol_fee(prices, policies)?)
}

Expand Down

0 comments on commit a80da50

Please sign in to comment.