Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define protocol fee params and forward to driver #2098

Merged
merged 25 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions crates/autopilot/src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ pub struct Arguments {
)]
pub solve_deadline: Duration,

/// Describes how the protocol fee should be calculated.
#[clap(flatten)]
pub fee_policy: FeePolicy,

/// Time interval in days between each cleanup operation of the
/// `order_events` database table.
#[clap(long, env, default_value = "1", value_parser = duration_from_days)]
Expand Down Expand Up @@ -284,6 +288,7 @@ impl std::fmt::Display for Arguments {
writeln!(f, "score_cap: {}", self.score_cap)?;
display_option(f, "shadow", &self.shadow)?;
writeln!(f, "solve_deadline: {:?}", self.solve_deadline)?;
writeln!(f, "fee_policy: {}", self.fee_policy)?;
writeln!(
f,
"order_events_cleanup_interval: {:?}",
Expand All @@ -298,6 +303,51 @@ impl std::fmt::Display for Arguments {
}
}

#[derive(clap::Parser, Clone)]
pub struct FeePolicy {
/// How much of the order's price improvement over max(limit price,
/// best_bid) should be taken as a protocol fee.
#[clap(
long,
env,
default_value = None,
value_parser = shared::arguments::parse_percentage_factor
)]
pub price_improvement_factor: Option<f64>,
sunce86 marked this conversation as resolved.
Show resolved Hide resolved

/// How much of the order's volume should be taken as a protocol fee.
#[clap(
long,
env,
default_value = None,
value_parser = shared::arguments::parse_percentage_factor
)]
pub volume_factor: Option<f64>,
fleupold marked this conversation as resolved.
Show resolved Hide resolved

/// Should protocol fees be collected or skipped for limit orders with
/// in-market price at the time of order creation.
sunce86 marked this conversation as resolved.
Show resolved Hide resolved
#[clap(long, env, action = clap::ArgAction::Set, default_value = "true")]
pub skip_in_market_orders: bool,
sunce86 marked this conversation as resolved.
Show resolved Hide resolved

/// Should protocol fees be collected or skipped for TWAP limit orders.
#[clap(long, env, action = clap::ArgAction::Set, default_value = "true")]
pub skip_twap_orders: bool,
sunce86 marked this conversation as resolved.
Show resolved Hide resolved
}

impl std::fmt::Display for FeePolicy {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"price_improvement_factor: {:?}, volume_factor: {:?}, skip_in_market_orders: {}, \
skip_twap_orders: {}",
self.price_improvement_factor,
self.volume_factor,
self.skip_in_market_orders,
self.skip_twap_orders
)
}
}

fn duration_from_days(s: &str) -> Result<Duration, ParseFloatError> {
let days = s.parse::<f64>()?;
Ok(Duration::from_secs_f64(days * 86_400.0))
Expand Down
33 changes: 33 additions & 0 deletions crates/autopilot/src/driver_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ pub mod solve {
pub app_data: AppDataHash,
#[serde(flatten)]
pub signature: Signature,
/// The types of fees that will be collected by the protocol.
/// Multiple fees are applied in the order they are listed
pub fee_policies: Vec<FeePolicy>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -150,6 +153,36 @@ pub mod solve {
pub submission_address: H160,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
sunce86 marked this conversation as resolved.
Show resolved Hide resolved
#[serde(rename_all = "camelCase")]
pub enum FeePolicy {
/// If the order receives more than expected (positive deviation from
/// quoted amounts) pay the protocol a factor of the achieved
/// improvement. The fee is taken in `sell` token for `buy`
/// orders and in `buy` token for `sell` orders.
Comment on lines +149 to +150
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fhenneke, @olgafetisova is this comment true? I think protocol fees should be collected in CoW using some exchange rate (either the pre auction prices, post auction price, tbd)?

Note, that here we are documenting the semantics from the protocol's perspective (our driver implementation may collect the required amounts in buy or sell token, but that's an implementation detail of the driver). In what currency will the protocol later request the quote deviation from the solvers that receive the auction?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that we also save external prices for these tokens for each auctions so it is easy to convert.

QuoteDeviation {
sunce86 marked this conversation as resolved.
Show resolved Hide resolved
/// Factor of price improvement the protocol charges as a fee.
/// Price improvement is the difference between executed price and
/// limit price or quoted price (whichever is better)
///
/// E.g. if a user received 2000USDC for 1ETH while having been
/// quoted 1990USDC, their price improvement is 10USDC.
/// A factor of 0.5 requires the solver to pay 5USDC to
/// the protocol for settling this order.
factor: f64,
/// Cap protocol fee with a percentage of the order's volume.
volume_cap_factor: Option<f64>,
sunce86 marked this conversation as resolved.
Show resolved Hide resolved
},
/// How much of the order's volume should be taken as a protocol fee.
/// The fee is taken in `sell` token for `sell` orders and in `buy`
/// token for `buy` orders.
Comment on lines +166 to +167
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto - what currency does the protocol charge solvers the fee in?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you mentioned in the comment ☝️ , the protocol collects the protocol fees in different tokens. The question is, whether the conversion to ETH/COW/else should be done in the autopilot or later in the script for accounting.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The conversion will be done in the script similarly to how we do it in solver rewards script, using the prices table.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, but it will still be charged in some currency right? Solvers will receive a bill from us at the end of the week saying: order x got 100 USDT price improvement, please pay us y.

y will be some percentage of 100 USDT, but in which currency (USDT, COW, ETH)? This is important for someone consuming this model to know so that they can make sure they convert and keep the fees in the right currency if they want to be neutral.

Comment on lines +166 to +167
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fees get taken in the opposite token (compared to surplus fee). This is because it would allow us to easily implement the fee model in the driver without solvers having to know about it, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My initial idea was that we need to take the fee as some percent of the executed_amount which is always expressed in the sell token for sell orders and in buy token for buy orders, but this is still open and could change during implementation if we figure out we actually need something else.

Volume {
sunce86 marked this conversation as resolved.
Show resolved Hide resolved
/// Percentage of the order's volume should be taken as a protocol
/// fee.
factor: f64,
},
}

#[derive(Clone, Debug, Default, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Response {
Expand Down
2 changes: 2 additions & 0 deletions crates/autopilot/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,7 @@ pub async fn run(args: Arguments) {
max_settlement_transaction_wait: args.max_settlement_transaction_wait,
solve_deadline: args.solve_deadline,
in_flight_orders: Default::default(),
fee_policy: args.fee_policy,
};
run.run_forever().await;
unreachable!("run loop exited");
Expand Down Expand Up @@ -692,6 +693,7 @@ async fn shadow_mode(args: Arguments) -> ! {
trusted_tokens,
args.score_cap,
args.solve_deadline,
args.fee_policy,
);
shadow.run_forever().await;

Expand Down
29 changes: 28 additions & 1 deletion crates/autopilot/src/run_loop.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use {
crate::{
arguments,
database::{
competition::{Competition, ExecutedFee, OrderExecution},
Postgres,
Expand All @@ -8,7 +9,7 @@ use {
driver_model::{
reveal::{self, Request},
settle,
solve::{self, Class},
solve::{self, Class, FeePolicy},
},
solvable_orders::SolvableOrdersCache,
},
Expand Down Expand Up @@ -56,6 +57,7 @@ pub struct RunLoop {
pub max_settlement_transaction_wait: Duration,
pub solve_deadline: Duration,
pub in_flight_orders: Arc<Mutex<InFlightOrders>>,
pub fee_policy: arguments::FeePolicy,
}

impl RunLoop {
Expand Down Expand Up @@ -306,6 +308,7 @@ impl RunLoop {
&self.market_makable_token_list.all(),
self.score_cap,
self.solve_deadline,
self.fee_policy.clone(),
);
let request = &request;

Expand Down Expand Up @@ -488,6 +491,7 @@ pub fn solve_request(
trusted_tokens: &HashSet<H160>,
score_cap: U256,
time_limit: Duration,
fee_policy: arguments::FeePolicy,
) -> solve::Request {
solve::Request {
id,
Expand All @@ -513,6 +517,28 @@ pub fn solve_request(
.collect()
};
let order_is_untouched = remaining_order.executed_amount.is_zero();
let fee_policy = match (
sunce86 marked this conversation as resolved.
Show resolved Hide resolved
fee_policy.price_improvement_factor,
fee_policy.volume_factor,
sunce86 marked this conversation as resolved.
Show resolved Hide resolved
) {
(Some(factor), volume_cap_factor) => Some(FeePolicy::QuoteDeviation {
factor,
volume_cap_factor,
}),
(None, Some(factor)) => Some(FeePolicy::Volume { factor }),
(_, _) => None,
};

let fee_policies = match order.metadata.class {
OrderClass::Market => vec![],
OrderClass::Liquidity => vec![],
// todo https://github.com/cowprotocol/services/issues/2092
// skip protocol fee for limit orders with in-market price

// todo https://github.com/cowprotocol/services/issues/2115
// skip protocol fee for TWAP limit orders
OrderClass::Limit(_) => fee_policy.map(|policy| vec![policy]).unwrap_or(vec![]),
};
solve::Order {
uid: order.metadata.uid,
sell_token: order.data.sell_token,
Expand All @@ -538,6 +564,7 @@ pub fn solve_request(
class,
app_data: order.data.app_data,
signature: order.signature.clone(),
fee_policies,
}
})
.collect(),
Expand Down
10 changes: 9 additions & 1 deletion crates/autopilot/src/shadow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@

use {
crate::{
arguments::FeePolicy,
driver_api::Driver,
driver_model::{reveal, solve},
driver_model::{
reveal,
solve::{self},
},
protocol,
run_loop,
},
Expand Down Expand Up @@ -43,6 +47,7 @@ pub struct RunLoop {
block: u64,
score_cap: U256,
solve_deadline: Duration,
fee_policy: FeePolicy,
}

impl RunLoop {
Expand All @@ -52,6 +57,7 @@ impl RunLoop {
trusted_tokens: AutoUpdatingTokenList,
score_cap: U256,
solve_deadline: Duration,
fee_policy: FeePolicy,
) -> Self {
Self {
orderbook,
Expand All @@ -61,6 +67,7 @@ impl RunLoop {
block: 0,
score_cap,
solve_deadline,
fee_policy,
}
}

Expand Down Expand Up @@ -193,6 +200,7 @@ impl RunLoop {
&self.trusted_tokens.all(),
self.score_cap,
self.solve_deadline,
self.fee_policy.clone(),
);
let request = &request;

Expand Down
28 changes: 28 additions & 0 deletions crates/driver/src/domain/competition/order/fees.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#[derive(Clone, Debug)]
pub enum FeePolicy {
/// If the order receives more than expected (positive deviation from quoted
/// amounts) pay the protocol a factor of the achieved improvement.
/// The fee is taken in `sell` token for `buy` orders and in `buy`
/// token for `sell` orders.
QuoteDeviation {
/// Factor of price improvement the protocol charges as a fee.
/// Price improvement is the difference between executed price and
/// limit price or quoted price (whichever is better)
///
/// E.g. if a user received 2000USDC for 1ETH while having been quoted
/// 1990USDC, their price improvement is 10USDC. A factor of 0.5
/// requires the solver to pay 5USDC to the protocol for
/// settling this order.
factor: f64,
/// Cap protocol fee with a percentage of the order's volume.
volume_cap_factor: f64,
},
/// How much of the order's volume should be taken as a protocol fee.
/// The fee is taken in `sell` token for `sell` orders and in `buy`
/// token for `buy` orders.
Volume {
/// Percentage of the order's volume should be taken as a protocol
/// fee.
factor: f64,
},
}
8 changes: 7 additions & 1 deletion crates/driver/src/domain/competition/order/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub use signature::Signature;
use {
super::auction,
crate::{
Expand All @@ -9,7 +8,9 @@ use {
bigdecimal::Zero,
num::CheckedDiv,
};
pub use {fees::FeePolicy, signature::Signature};

pub mod fees;
pub mod signature;

/// An order in the auction.
Expand Down Expand Up @@ -39,6 +40,10 @@ pub struct Order {
pub sell_token_balance: SellTokenBalance,
pub buy_token_balance: BuyTokenBalance,
pub signature: Signature,
/// The types of fees the protocol collects from the winning solver.
/// Unless otherwise configured, the driver modifies solutions to take
/// sufficient fee in the form of positive slippage.
pub fee_policies: Vec<FeePolicy>,
}

/// An amount denominated in the sell token of an [`Order`].
Expand Down Expand Up @@ -453,6 +458,7 @@ mod tests {
data: Default::default(),
signer: Default::default(),
},
fee_policies: Default::default(),
};

assert_eq!(
Expand Down
1 change: 1 addition & 0 deletions crates/driver/src/domain/quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ impl Order {
data: Default::default(),
signer: Default::default(),
},
fee_policies: Default::default(),
}],
[
auction::Token {
Expand Down
24 changes: 24 additions & 0 deletions crates/driver/src/infra/api/routes/solve/dto/auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,22 @@ impl Auction {
data: order.signature.into(),
signer: order.owner.into(),
},
fee_policies: order
.fee_policies
.into_iter()
.map(|policy| match policy {
FeePolicy::QuoteDeviation {
factor,
volume_cap_factor,
} => competition::order::FeePolicy::QuoteDeviation {
factor,
volume_cap_factor,
},
FeePolicy::Volume { factor } => {
competition::order::FeePolicy::Volume { factor }
}
})
.collect(),
})
.collect(),
self.tokens.into_iter().map(|token| {
Expand Down Expand Up @@ -234,6 +250,7 @@ struct Order {
signing_scheme: SigningScheme,
#[serde_as(as = "serialize::Hex")]
signature: Vec<u8>,
fee_policies: Vec<FeePolicy>,
}

#[derive(Debug, Deserialize)]
Expand Down Expand Up @@ -287,3 +304,10 @@ enum Class {
Limit,
Liquidity,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
enum FeePolicy {
QuoteDeviation { factor: f64, volume_cap_factor: f64 },
Volume { factor: f64 },
}
13 changes: 12 additions & 1 deletion crates/driver/src/tests/setup/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,18 @@ pub fn solve_req(test: &Test) -> serde_json::Value {
},
"appData": "0x0000000000000000000000000000000000000000000000000000000000000000",
"signingScheme": "eip712",
"signature": format!("0x{}", hex::encode(quote.order_signature(&test.blockchain)))
"signature": format!("0x{}", hex::encode(quote.order_signature(&test.blockchain))),
"feePolicies": [{
"quoteDeviation": {
"factor": 0.5,
"volume_cap_factor": 0.06,
}
},
{
"volume": {
"factor": 0.1,
}
}],
}));
}
for fulfillment in test.fulfillments.iter() {
Expand Down
Loading