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 5 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
24 changes: 24 additions & 0 deletions crates/autopilot/src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,24 @@ pub struct Arguments {
value_parser = shared::arguments::duration_from_seconds,
)]
pub solve_deadline: Duration,

/// How much of the order's surplus should be taken as a protocol fee.
#[clap(
long,
env,
default_value = "0",
value_parser = shared::arguments::parse_percentage_factor
)]
pub protocol_fee_factor: f64,

/// Cap protocol fee with a percentage of the order's volume.
#[clap(
long,
env,
default_value = "0",
value_parser = shared::arguments::parse_percentage_factor
)]
pub protofol_fee_volume_cap_factor: f64,
sunce86 marked this conversation as resolved.
Show resolved Hide resolved
}

impl std::fmt::Display for Arguments {
Expand Down Expand Up @@ -270,6 +288,12 @@ 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, "protocol_fee_factor: {}", self.protocol_fee_factor)?;
writeln!(
f,
"protofol_fee_volume_cap_factor: {}",
self.protofol_fee_volume_cap_factor
)?;
Ok(())
}
}
11 changes: 11 additions & 0 deletions crates/autopilot/src/driver_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ pub mod solve {
pub app_data: AppDataHash,
#[serde(flatten)]
pub signature: Signature,
pub protocol_fee: ProtocolFee,
sunce86 marked this conversation as resolved.
Show resolved Hide resolved
}

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

#[serde_as]
#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ProtocolFee {
sunce86 marked this conversation as resolved.
Show resolved Hide resolved
/// Percentage of the order's surplus should be taken as a protocol fee.
pub factor: f64,
/// Cap protocol fee with a percentage of the order's volume.
pub volume_cap_factor: f64,
}

#[derive(Clone, Debug, Default, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Response {
Expand Down
9 changes: 9 additions & 0 deletions crates/autopilot/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use {
Postgres,
},
driver_api::Driver,
driver_model::solve::ProtocolFee,
event_updater::{EventUpdater, GPv2SettlementContract},
protocol,
run_loop::RunLoop,
Expand Down Expand Up @@ -619,6 +620,10 @@ pub async fn run(args: Arguments) {
score_cap: args.score_cap,
max_settlement_transaction_wait: args.max_settlement_transaction_wait,
solve_deadline: args.solve_deadline,
protocol_fee_params: ProtocolFee {
factor: args.protocol_fee_factor,
volume_cap_factor: args.protofol_fee_volume_cap_factor,
},
};
run.run_forever().await;
unreachable!("run loop exited");
Expand Down Expand Up @@ -680,6 +685,10 @@ async fn shadow_mode(args: Arguments) -> ! {
trusted_tokens,
args.score_cap,
args.solve_deadline,
ProtocolFee {
factor: args.protocol_fee_factor,
volume_cap_factor: args.protofol_fee_volume_cap_factor,
},
);
shadow.run_forever().await;

Expand Down
13 changes: 12 additions & 1 deletion crates/autopilot/src/run_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use {
driver_model::{
reveal::{self, Request},
settle,
solve::{self, Class},
solve::{self, Class, ProtocolFee},
},
solvable_orders::SolvableOrdersCache,
},
Expand Down Expand Up @@ -54,6 +54,7 @@ pub struct RunLoop {
pub score_cap: U256,
pub max_settlement_transaction_wait: Duration,
pub solve_deadline: Duration,
pub protocol_fee_params: ProtocolFee,
}

impl RunLoop {
Expand Down Expand Up @@ -302,6 +303,7 @@ impl RunLoop {
&self.market_makable_token_list.all(),
self.score_cap,
self.solve_deadline,
self.protocol_fee_params,
);
let request = &request;

Expand Down Expand Up @@ -449,6 +451,7 @@ pub fn solve_request(
trusted_tokens: &HashSet<H160>,
score_cap: U256,
time_limit: Duration,
protocol_fee_params: ProtocolFee,
) -> solve::Request {
solve::Request {
id,
Expand All @@ -474,6 +477,13 @@ pub fn solve_request(
.collect()
};
let order_is_untouched = remaining_order.executed_amount.is_zero();
let protocol_fee = match order.metadata.class {
OrderClass::Market => Default::default(),
OrderClass::Liquidity => Default::default(),
// todo https://github.com/cowprotocol/services/issues/2092
// skip protocol fee for limit orders with in-market price
OrderClass::Limit(_) => protocol_fee_params,
};
solve::Order {
uid: order.metadata.uid,
sell_token: order.data.sell_token,
Expand All @@ -499,6 +509,7 @@ pub fn solve_request(
class,
app_data: order.data.app_data,
signature: order.signature.clone(),
protocol_fee,
}
})
.collect(),
Expand Down
9 changes: 8 additions & 1 deletion crates/autopilot/src/shadow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
use {
crate::{
driver_api::Driver,
driver_model::{reveal, solve},
driver_model::{
reveal,
solve::{self, ProtocolFee},
},
protocol,
run_loop,
},
Expand Down Expand Up @@ -43,6 +46,7 @@ pub struct RunLoop {
block: u64,
score_cap: U256,
solve_deadline: Duration,
protocol_fee_params: ProtocolFee,
}

impl RunLoop {
Expand All @@ -52,6 +56,7 @@ impl RunLoop {
trusted_tokens: AutoUpdatingTokenList,
score_cap: U256,
solve_deadline: Duration,
protocol_fee_params: ProtocolFee,
) -> Self {
Self {
orderbook,
Expand All @@ -61,6 +66,7 @@ impl RunLoop {
block: 0,
score_cap,
solve_deadline,
protocol_fee_params,
}
}

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

Expand Down
13 changes: 13 additions & 0 deletions crates/driver/src/domain/competition/order/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub struct Order {
pub sell_token_balance: SellTokenBalance,
pub buy_token_balance: BuyTokenBalance,
pub signature: Signature,
pub protocol_fee: ProtocolFee,
}

/// An amount denominated in the sell token of an [`Order`].
Expand Down Expand Up @@ -375,6 +376,14 @@ impl From<Trader> for eth::Address {
}
}

#[derive(Debug, Clone)]
pub struct ProtocolFee {
/// Percentage of the order's surplus should be taken as a protocol fee.
pub factor: f64,
/// Cap protocol fee with a percentage of the order's volume.
pub volume_cap_factor: f64,
}

/// A just-in-time order. JIT orders are added at solving time by the solver to
/// generate a more optimal solution for the auction. Very similar to a regular
/// [`Order`].
Expand Down Expand Up @@ -453,6 +462,10 @@ mod tests {
data: Default::default(),
signer: Default::default(),
},
protocol_fee: ProtocolFee {
factor: 0.,
volume_cap_factor: 0.,
},
};

assert_eq!(
Expand Down
6 changes: 5 additions & 1 deletion crates/driver/src/domain/quote.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {
super::competition::auction,
super::competition::{auction, order::ProtocolFee},
crate::{
boundary,
domain::{
Expand Down Expand Up @@ -135,6 +135,10 @@ impl Order {
data: Default::default(),
signer: Default::default(),
},
protocol_fee: ProtocolFee {
factor: 0.,
volume_cap_factor: 0.,
},
}],
[
auction::Token {
Expand Down
12 changes: 12 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,10 @@ impl Auction {
data: order.signature.into(),
signer: order.owner.into(),
},
protocol_fee: competition::order::ProtocolFee {
factor: order.protocol_fee.factor,
volume_cap_factor: order.protocol_fee.volume_cap_factor,
},
})
.collect(),
self.tokens.into_iter().map(|token| {
Expand Down Expand Up @@ -234,6 +238,7 @@ struct Order {
signing_scheme: SigningScheme,
#[serde_as(as = "serialize::Hex")]
signature: Vec<u8>,
protocol_fee: ProtocolFee,
}

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

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct ProtocolFee {
pub factor: f64,
pub volume_cap_factor: f64,
}
6 changes: 5 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,11 @@ 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))),
"protocolFee": {
"factor": 0.,
"volumeCapFactor": 0.,
}
}));
}
for fulfillment in test.fulfillments.iter() {
Expand Down
Loading