Skip to content

Commit

Permalink
Sell order adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
squadgazzz committed Mar 13, 2024
1 parent 257d023 commit 36617c8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
49 changes: 40 additions & 9 deletions crates/driver/src/tests/cases/protocol_fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,13 @@ async fn protocol_fee_test_case(test_case: TestCase) {
Some(executed) => Partial::Yes { executed },
None => Partial::No,
};
let solver_fee = test_case.execution.driver.sell / 100;
// Target amount to be executed by the solver in case of partially fillable
// order
let executed = match partial {
Partial::Yes { .. } => match test_case.order.side {
order::Side::Buy => Some(test_case.execution.solver.buy),
order::Side::Sell => Some(test_case.execution.solver.sell),
order::Side::Sell => Some(test_case.execution.solver.sell - solver_fee),
},
Partial::No => None,
};
Expand All @@ -93,7 +94,7 @@ async fn protocol_fee_test_case(test_case: TestCase) {
// Expected amounts already account for network fee, so it doesn't matter for the math.
// However, it cannot be zero, otherwise the order would be perceived as a StaticFee orders (which cannot have Protocol Fees)
// todo: can be cleaned up after https://github.com/cowprotocol/services/issues/2507
.solver_fee(Some(test_case.execution.driver.sell / 100))
.solver_fee(Some(solver_fee))
.side(test_case.order.side)
.fee_policy(test_case.fee_policy)
.executed(executed)
Expand Down Expand Up @@ -147,7 +148,37 @@ async fn surplus_protocol_fee_buy_order_not_capped() {

#[tokio::test]
#[ignore]
async fn surplus_protocol_fee_buy_partial_order() {
async fn surplus_protocol_fee_sell_order_not_capped() {
let fee_policy = Policy::Surplus {
factor: 0.5,
// high enough so we don't get capped by volume fee
max_volume_factor: 1.0,
};
let test_case = TestCase {
fee_policy,
order: Order {
sell_amount: 50.ether().into_wei(),
buy_amount: 40.ether().into_wei(),
side: order::Side::Sell,
},
execution: Execution {
// 20 ETH surplus, half of which gets captured by the protocol
solver: Amounts {
sell: 50.ether().into_wei(),
buy: 60.ether().into_wei(),
},
driver: Amounts {
sell: 50.ether().into_wei(),
buy: 50.ether().into_wei(),
},
},
};
protocol_fee_test_case(test_case).await;
}

#[tokio::test]
#[ignore]
async fn surplus_protocol_fee_partial_buy_order_not_capped() {
let fee_policy = Policy::Surplus {
factor: 0.5,
// high enough so we don't get capped by volume fee
Expand Down Expand Up @@ -179,7 +210,7 @@ async fn surplus_protocol_fee_buy_partial_order() {

#[tokio::test]
#[ignore]
async fn surplus_protocol_fee_sell_order_not_capped() {
async fn surplus_protocol_fee_partial_sell_order_not_capped() {
let fee_policy = Policy::Surplus {
factor: 0.5,
// high enough so we don't get capped by volume fee
Expand All @@ -193,14 +224,14 @@ async fn surplus_protocol_fee_sell_order_not_capped() {
side: order::Side::Sell,
},
execution: Execution {
// 20 ETH surplus, half of which gets captured by the protocol
// 6 ETH surplus, half of which gets captured by the protocol
solver: Amounts {
sell: 50.ether().into_wei(),
buy: 60.ether().into_wei(),
sell: 25.ether().into_wei(),
buy: 26.ether().into_wei(),
},
driver: Amounts {
sell: 50.ether().into_wei(),
buy: 50.ether().into_wei(),
sell: 25.ether().into_wei(),
buy: 23.ether().into_wei(),
},
},
};
Expand Down
10 changes: 8 additions & 2 deletions crates/driver/src/tests/setup/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl QuotedOrder {
.order
.executed
// Since `executed` is a target amount
.map(|executed_sell| self.buy * executed_sell / self.sell)
.map(|executed_sell| self.buy * (executed_sell + self.order.solver_fee.unwrap_or_default()) / self.sell)
.unwrap_or(self.buy),
};
match self.order.side {
Expand All @@ -151,7 +151,11 @@ impl QuotedOrder {
/// Calculates sell amount that should be received from a driver
pub fn driver_sell_amount(&self) -> eth::U256 {
let executed_sell = match self.order.side {
order::Side::Sell => self.order.executed.unwrap_or(self.sell),
order::Side::Sell => self
.order
.executed
.map(|sell| sell + self.order.solver_fee.unwrap_or_default())
.unwrap_or(self.sell),
order::Side::Buy => self
.order
.executed
Expand Down Expand Up @@ -606,6 +610,8 @@ impl Blockchain {
),
// todo: simplify it
(order::Side::Sell, _) => {
// let sell_amount = order.executed.map(|sell| sell +
// order.solver_fee.unwrap_or_default()).unwrap_or(order.sell_amount);
let sell_amount = order.executed.unwrap_or(order.sell_amount);
(
sell_amount,
Expand Down

0 comments on commit 36617c8

Please sign in to comment.