Skip to content

Commit

Permalink
Adjusted values
Browse files Browse the repository at this point in the history
  • Loading branch information
squadgazzz committed Mar 6, 2024
1 parent 758ebde commit 3a80fe6
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 46 deletions.
115 changes: 69 additions & 46 deletions crates/driver/src/tests/cases/protocol_fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use crate::{
},
};

const DEFAULT_SURPLUS_FACTOR: u64 = 2;

struct TestCase {
order_side: order::Side,
fee_policy: FeePolicy,
Expand All @@ -41,6 +43,7 @@ async fn protocol_fee_test_case(test_case: TestCase) {
buy: test_case.executed_buy_amount,
};
let order = ab_order()
.surplus(DEFAULT_SURPLUS_FACTOR.into())
.kind(order::Kind::Limit)
.sell_amount(test_case.order_sell_amount)
.side(test_case.order_side)
Expand All @@ -67,16 +70,18 @@ async fn surplus_protocol_fee_buy_order_not_capped() {
// high enough so we don't get capped by volume fee
max_volume_factor: 1.0,
};
let order_buy_amount = 40000000000000000000u128.into();
let order_sell_amount = 50000000000000000000u128.into();
let test_case = TestCase {
order_side: order::Side::Buy,
fee_policy,
order_sell_amount: 50000000000000000000u128.into(),
order_sell_amount,
network_fee: Some(10000000000000000000u128.into()),
quote_sell_amount: 50000000000000000000u128.into(),
quote_buy_amount: 40000000000000000000u128.into(),
executed: 40000000000000000000u128.into(),
executed_sell_amount: 100000000000000000000u128.into(),
executed_buy_amount: 40000000000000000000u128.into(),
quote_sell_amount: order_sell_amount,
quote_buy_amount: order_buy_amount,
executed: order_buy_amount,
executed_sell_amount: 75000000000000000000u128.into(),
executed_buy_amount: order_buy_amount,
};

protocol_fee_test_case(test_case).await;
Expand All @@ -90,16 +95,18 @@ async fn surplus_protocol_fee_sell_order_not_capped() {
// high enough so we don't get capped by volume fee
max_volume_factor: 1.0,
};
let order_sell_amount = 50000000000000000000u128.into();
let order_buy_amount = 40000000000000000000u128.into();
let test_case = TestCase {
order_side: order::Side::Sell,
fee_policy,
order_sell_amount: 50000000000000000000u128.into(),
order_sell_amount,
network_fee: Some(10000000000000000000u128.into()),
quote_sell_amount: 50000000000000000000u128.into(),
quote_buy_amount: 40000000000000000000u128.into(),
executed: 40000000000000000000u128.into(),
executed_sell_amount: 50000000000000000000u128.into(),
executed_buy_amount: 20000000002000000000u128.into(),
quote_sell_amount: order_sell_amount,
quote_buy_amount: order_buy_amount,
executed: order_buy_amount,
executed_sell_amount: order_sell_amount,
executed_buy_amount: 30000000000000000000u128.into(),
};

protocol_fee_test_case(test_case).await;
Expand Down Expand Up @@ -189,23 +196,31 @@ async fn volume_protocol_fee_sell_order() {
protocol_fee_test_case(test_case).await;
}

// Price Improvement policy fee tests.
// Out of market order could be defined as:
// (order.sell + order.fee) * quote.buy < (quote.sell + quote.fee) * order.buy
// In the following tests Limit orders are used only, where order fee is 0. The
// amount values are adjusted to respect the definition.

#[tokio::test]
#[ignore]
async fn price_improvement_fee_buy_out_of_market_order() {
let order_sell_amount = 50000000000000000000u128.into();
let order_buy_amount = 40000000000000000000u128.into();
let quote_network_fee = 20000000000000000000u128.into();
let fee_policy = FeePolicy::PriceImprovement {
factor: 0.5,
max_volume_factor: 1.0,
quote: PriceImprovementQuote {
sell_amount: 50000000000000000000u128.into(),
buy_amount: 35000000000000000000u128.into(),
network_fee: 20000000000000000000u128.into(),
sell_amount: order_sell_amount,
// Since order surplus factor is 2.0, order's buy amount becomes x0.5 from the original
// value. Quote's buy amount is selected to be equal to the adjusted order's
// buy amount.
buy_amount: order_buy_amount / DEFAULT_SURPLUS_FACTOR,
// Quote's fee is high enough to make the order's conditions better.
network_fee: quote_network_fee,
},
};
// (order.sell + order.fee) * quote.buy < (quote.sell + quote.fee) * order.buy
// (45 + 10) * 35 < (50 + 20) * 35
// 1925 < 2450
let order_sell_amount = 45000000000000000000u128.into();
let order_buy_amount = 35000000000000000000u128.into();
let test_case = TestCase {
order_side: order::Side::Buy,
fee_policy,
Expand All @@ -214,7 +229,9 @@ async fn price_improvement_fee_buy_out_of_market_order() {
quote_sell_amount: order_sell_amount,
quote_buy_amount: order_buy_amount,
executed: order_buy_amount,
executed_sell_amount: 57500000000000000000u128.into(),
// Executed values should be the same as for the surplus policy fee.
// ???
executed_sell_amount: 75000000000000000000u128.into(),
executed_buy_amount: order_buy_amount,
};

Expand All @@ -224,20 +241,20 @@ async fn price_improvement_fee_buy_out_of_market_order() {
#[tokio::test]
#[ignore]
async fn price_improvement_fee_sell_out_of_market_order() {
let order_sell_amount = 50000000000000000000u128.into();
let order_buy_amount = 40000000000000000000u128.into();
let fee_policy = FeePolicy::PriceImprovement {
factor: 0.5,
max_volume_factor: 1.0,
quote: PriceImprovementQuote {
sell_amount: 50000000000000000000u128.into(),
buy_amount: 35000000000000000000u128.into(),
// Since order surplus factor is 2.0, order's sell amount becomes x2 from the original
// value. Quote's sell amount is selected to be equal to the adjusted order's
// buy amount.
sell_amount: order_sell_amount * DEFAULT_SURPLUS_FACTOR,
buy_amount: order_buy_amount,
network_fee: 20000000000000000000u128.into(),
},
};
// (order.sell + order.fee) * quote.buy < (quote.sell + quote.fee) * order.buy
// (50 + 10) * 35 < (50 + 20) * 40
// 2100 < 2800
let order_sell_amount = 50000000000000000000u128.into();
let order_buy_amount = 40000000000000000000u128.into();
let network_fee = 10000000000000000000u128.into();
let test_case = TestCase {
order_side: order::Side::Sell,
Expand All @@ -248,7 +265,9 @@ async fn price_improvement_fee_sell_out_of_market_order() {
quote_buy_amount: order_buy_amount,
executed: order_sell_amount - network_fee,
executed_sell_amount: order_sell_amount,
executed_buy_amount: 32500000000000000000u128.into(),
// executed values should be the same as for the surplus policy fee
// ???
executed_buy_amount: 30000000000000000000u128.into(),
};

protocol_fee_test_case(test_case).await;
Expand All @@ -261,25 +280,28 @@ async fn price_improvement_fee_buy_in_market_order() {
factor: 0.5,
max_volume_factor: 1.0,
quote: PriceImprovementQuote {
sell_amount: 45000000000000000000u128.into(),
buy_amount: 35000000000000000000u128.into(),
network_fee: 10000000000000000000u128.into(),
sell_amount: 50000000000000000000u128.into(),
// Since order surplus factor is 2.0, order's buy amount will be x0.5.
// To make it inside market price, the quote buy amount should be higher than the
// adjusted order's value.
buy_amount: 40000000000000000000u128.into(),
network_fee: 20000000000000000000u128.into(),
},
};
// (order.sell + order.fee) * quote.buy < (quote.sell + quote.fee) * order.buy
// (50 + 15) * 35 < (45 + 10) * 35
// 2275 < 1925
let order_sell_amount = 50000000000000000000u128.into();
let order_buy_amount = 35000000000000000000u128.into();
let order_buy_amount = 40000000000000000000u128.into();
let network_fee = 10000000000000000000u128.into();
let test_case = TestCase {
order_side: order::Side::Buy,
fee_policy,
order_sell_amount,
network_fee: Some(15000000000000000000u128.into()),
network_fee: Some(network_fee),
quote_sell_amount: order_sell_amount,
quote_buy_amount: order_buy_amount,
executed: order_buy_amount,
executed_sell_amount: 52500000000000000000u128.into(),
// quote.sell_amount + factor * quote.network_fee
// ???
executed_sell_amount: 60000000000000000000u128.into(),
executed_buy_amount: order_buy_amount,
};

Expand All @@ -293,17 +315,17 @@ async fn price_improvement_fee_sell_in_market_order() {
factor: 0.5,
max_volume_factor: 1.0,
quote: PriceImprovementQuote {
sell_amount: 50000000000000000000u128.into(),
// Since order surplus factor is 2.0, order's sell amount will be x2.
// To make it inside market price, the quote sell amount should be lower than the
// adjusted order's value.
sell_amount: 500000000000000000000u128.into(),
buy_amount: 40000000000000000000u128.into(),
network_fee: 10000000000000000000u128.into(),
network_fee: 20000000000000000000u128.into(),
},
};
// (order.sell + order.fee) * quote.buy < (quote.sell + quote.fee) * order.buy
// (50 + 15) * 40 < (50 + 10) * 35
// 2600 < 2100
let order_sell_amount: eth::U256 = 50000000000000000000u128.into();
let order_buy_amount: eth::U256 = 35000000000000000000u128.into();
let network_fee = 15000000000000000000u128.into();
let order_buy_amount: eth::U256 = 40000000000000000000u128.into();
let network_fee = 10000000000000000000u128.into();
let test_case = TestCase {
order_side: order::Side::Sell,
fee_policy,
Expand All @@ -313,7 +335,8 @@ async fn price_improvement_fee_sell_in_market_order() {
quote_buy_amount: order_buy_amount,
executed: order_sell_amount - network_fee,
executed_sell_amount: order_sell_amount,
executed_buy_amount: 34166666666666666667u128.into(),
// ???
executed_buy_amount: 30000000000000000000u128.into(),
};

protocol_fee_test_case(test_case).await;
Expand Down
7 changes: 7 additions & 0 deletions crates/driver/src/tests/setup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,13 @@ impl Order {
}
}

pub fn surplus(self, surplus_factor: eth::U256) -> Self {
Self {
surplus_factor,
..self
}
}

/// Mark this order as internalizable.
pub fn internalize(self) -> Self {
Self {
Expand Down

0 comments on commit 3a80fe6

Please sign in to comment.