Skip to content

Commit

Permalink
Explicit fee amounts
Browse files Browse the repository at this point in the history
  • Loading branch information
squadgazzz committed Mar 5, 2024
1 parent 38135e8 commit 2a20508
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 46 deletions.
23 changes: 18 additions & 5 deletions crates/driver/src/domain/competition/solution/trade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,16 @@ impl Fulfillment {
}),
};

let executed_with_fee = order::TargetAmount(
executed
.0
.checked_add(fee.0)
.ok_or(Error::InvalidExecutedAmount)?,
let option = executed.0.checked_add(fee.0);
tracing::info!("newlog option={:?}", option);
let executed_with_fee =
order::TargetAmount(option.ok_or(Error::InvalidExecutedAmount)?);
tracing::info!(
"newlog executed_with_fee == order.target()={:?}, executed_with_fee={:?}, \
order.target()={:?}",
executed_with_fee == order.target(),
executed_with_fee,
order.target()
);
match order.partial {
order::Partial::Yes { available } => executed_with_fee <= available,
Expand Down Expand Up @@ -258,8 +263,16 @@ impl Jit {
// the target amount. Otherwise, the executed amount must be equal to the target
// amount.
let is_valid = if order.partially_fillable {
tracing::info!(
"newlog executed <= order.target()={:?}",
executed <= order.target()
);
executed <= order.target()
} else {
tracing::info!(
"newlog executed == order.target()={:?}",
executed == order.target()
);
executed == order.target()
};
if is_valid {
Expand Down
68 changes: 29 additions & 39 deletions crates/driver/src/tests/cases/protocol_fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct TestCase {
order_side: order::Side,
fee_policy: FeePolicy,
order_sell_amount: eth::U256,
solver_fee: Option<eth::U256>,
network_fee: Option<eth::U256>,
quote_sell_amount: eth::U256,
quote_buy_amount: eth::U256,
executed: eth::U256,
Expand All @@ -44,7 +44,7 @@ async fn protocol_fee_test_case(test_case: TestCase) {
.kind(order::Kind::Limit)
.sell_amount(test_case.order_sell_amount)
.side(test_case.order_side)
.solver_fee(test_case.solver_fee)
.solver_fee(test_case.network_fee)
.fee_policy(test_case.fee_policy)
.executed(test_case.executed)
.expected_amounts(expected_amounts);
Expand All @@ -71,7 +71,7 @@ async fn surplus_protocol_fee_buy_order_not_capped() {
order_side: order::Side::Buy,
fee_policy,
order_sell_amount: 50000000000000000000u128.into(),
solver_fee: Some(10000000000000000000u128.into()),
network_fee: Some(10000000000000000000u128.into()),
quote_sell_amount: 50000000000000000000u128.into(),
quote_buy_amount: 40000000000000000000u128.into(),
executed: 40000000000000000000u128.into(),
Expand All @@ -94,7 +94,7 @@ async fn surplus_protocol_fee_sell_order_not_capped() {
order_side: order::Side::Sell,
fee_policy,
order_sell_amount: 50000000000000000000u128.into(),
solver_fee: Some(10000000000000000000u128.into()),
network_fee: Some(10000000000000000000u128.into()),
quote_sell_amount: 50000000000000000000u128.into(),
quote_buy_amount: 40000000000000000000u128.into(),
executed: 40000000000000000000u128.into(),
Expand All @@ -117,7 +117,7 @@ async fn surplus_protocol_fee_buy_order_capped() {
order_side: order::Side::Buy,
fee_policy,
order_sell_amount: 50000000000000000000u128.into(),
solver_fee: Some(10000000000000000000u128.into()),
network_fee: Some(10000000000000000000u128.into()),
quote_sell_amount: 50000000000000000000u128.into(),
quote_buy_amount: 40000000000000000000u128.into(),
executed: 40000000000000000000u128.into(),
Expand All @@ -140,7 +140,7 @@ async fn surplus_protocol_fee_sell_order_capped() {
order_side: order::Side::Sell,
fee_policy,
order_sell_amount: 50000000000000000000u128.into(),
solver_fee: Some(10000000000000000000u128.into()),
network_fee: Some(10000000000000000000u128.into()),
quote_sell_amount: 50000000000000000000u128.into(),
quote_buy_amount: 40000000000000000000u128.into(),
executed: 40000000000000000000u128.into(),
Expand All @@ -159,7 +159,7 @@ async fn volume_protocol_fee_buy_order() {
order_side: order::Side::Buy,
fee_policy,
order_sell_amount: 50000000000000000000u128.into(),
solver_fee: Some(10000000000000000000u128.into()),
network_fee: Some(10000000000000000000u128.into()),
quote_sell_amount: 50000000000000000000u128.into(),
quote_buy_amount: 40000000000000000000u128.into(),
executed: 40000000000000000000u128.into(),
Expand All @@ -178,7 +178,7 @@ async fn volume_protocol_fee_sell_order() {
order_side: order::Side::Sell,
fee_policy,
order_sell_amount: 50000000000000000000u128.into(),
solver_fee: Some(10000000000000000000u128.into()),
network_fee: Some(10000000000000000000u128.into()),
quote_sell_amount: 50000000000000000000u128.into(),
quote_buy_amount: 40000000000000000000u128.into(),
executed: 40000000000000000000u128.into(),
Expand All @@ -192,16 +192,13 @@ async fn volume_protocol_fee_sell_order() {
#[tokio::test]
#[ignore]
async fn price_improvement_fee_buy_out_of_market_order() {
let quote_sell_amount = 50000000000000000000u128.into();
let quote_buy_amount = 35000000000000000000u128.into();
let fee: eth::U256 = 1000000000000000000u128.into();
let fee_policy = FeePolicy::PriceImprovement {
factor: 0.5,
max_volume_factor: 1.0,
quote: PriceImprovementQuote {
sell_amount: quote_sell_amount,
buy_amount: quote_buy_amount,
fee,
sell_amount: 50000000000000000000u128.into(),
buy_amount: 35000000000000000000u128.into(),
fee: 1000000000000000000u128.into(),
},
};
let order_sell_amount = 50000000000000000000u128.into();
Expand All @@ -210,7 +207,7 @@ async fn price_improvement_fee_buy_out_of_market_order() {
order_side: order::Side::Buy,
fee_policy,
order_sell_amount,
solver_fee: Some(fee),
network_fee: Some(2000000000000000000u128.into()),
quote_sell_amount: order_sell_amount,
quote_buy_amount: order_buy_amount,
executed: order_buy_amount,
Expand All @@ -224,28 +221,26 @@ async fn price_improvement_fee_buy_out_of_market_order() {
#[tokio::test]
#[ignore]
async fn price_improvement_fee_sell_out_of_market_order() {
let quote_sell_amount = 50000000000000000000u128.into();
let quote_buy_amount = 35000000000000000000u128.into();
let fee: eth::U256 = 1000000000000000000u128.into();
let fee_policy = FeePolicy::PriceImprovement {
factor: 0.5,
max_volume_factor: 1.0,
quote: PriceImprovementQuote {
sell_amount: quote_sell_amount,
buy_amount: quote_buy_amount,
fee,
sell_amount: 50000000000000000000u128.into(),
buy_amount: 35000000000000000000u128.into(),
fee: 1000000000000000000u128.into(),
},
};
let order_sell_amount = 50000000000000000000u128.into();
let order_buy_amount = 40000000000000000000u128.into();
let network_fee = 2000000000000000000u128.into();
let test_case = TestCase {
order_side: order::Side::Sell,
fee_policy,
order_sell_amount,
solver_fee: Some(fee),
network_fee: Some(network_fee),
quote_sell_amount: order_sell_amount,
quote_buy_amount: order_buy_amount,
executed: order_sell_amount - fee,
executed: order_sell_amount - network_fee,
executed_sell_amount: order_sell_amount,
executed_buy_amount: 37156862745098039215u128.into(),
};
Expand All @@ -256,25 +251,22 @@ async fn price_improvement_fee_sell_out_of_market_order() {
#[tokio::test]
#[ignore]
async fn price_improvement_fee_buy_in_market_order() {
let quote_sell_amount: eth::U256 = 50000000000000000000u128.into();
let quote_buy_amount: eth::U256 = 40000000000000000000u128.into();
let fee = 1000000000000000000u128.into();
let fee_policy = FeePolicy::PriceImprovement {
factor: 0.5,
max_volume_factor: 1.0,
quote: PriceImprovementQuote {
sell_amount: quote_sell_amount,
buy_amount: quote_buy_amount,
fee,
sell_amount: 50000000000000000000u128.into(),
buy_amount: 40000000000000000000u128.into(),
fee: 1000000000000000000u128.into(),
},
};
let order_sell_amount = 50000000000000000000u128.into();
let order_buy_amount = 35000000000000000000u128.into();
let test_case = TestCase {
order_side: order::Side::Buy,
fee_policy,
order_sell_amount: quote_sell_amount,
solver_fee: Some(fee),
order_sell_amount,
network_fee: Some(2000000000000000000u128.into()),
quote_sell_amount: order_sell_amount,
quote_buy_amount: order_buy_amount,
executed: order_buy_amount,
Expand All @@ -288,28 +280,26 @@ async fn price_improvement_fee_buy_in_market_order() {
#[tokio::test]
#[ignore]
async fn price_improvement_fee_sell_in_market_order() {
let quote_sell_amount: eth::U256 = 50000000000000000000u128.into();
let quote_buy_amount: eth::U256 = 40000000000000000000u128.into();
let fee = 1000000000000000000u128.into();
let fee_policy = FeePolicy::PriceImprovement {
factor: 0.5,
max_volume_factor: 1.0,
quote: PriceImprovementQuote {
sell_amount: quote_sell_amount,
buy_amount: quote_buy_amount,
fee,
sell_amount: 50000000000000000000u128.into(),
buy_amount: 40000000000000000000u128.into(),
fee: 1000000000000000000u128.into(),
},
};
let order_sell_amount: eth::U256 = 50000000000000000000u128.into();
let order_buy_amount: eth::U256 = 35000000000000000000u128.into();
let network_fee = 20000000000000000000u128.into();
let test_case = TestCase {
order_side: order::Side::Sell,
fee_policy,
order_sell_amount,
solver_fee: Some(fee),
network_fee: Some(network_fee),
quote_sell_amount: order_sell_amount,
quote_buy_amount: order_buy_amount,
executed: order_sell_amount - fee,
executed: order_sell_amount - network_fee,
executed_sell_amount: order_sell_amount,
executed_buy_amount: order_buy_amount,
};
Expand Down
1 change: 1 addition & 0 deletions crates/driver/src/tests/setup/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ impl Blockchain {
amount: order.sell_amount,
token: order.sell_token,
});
println!("newlog executed_buy={:?}", executed_buy);
QuotedOrder {
order: order.clone(),
buy: executed_buy,
Expand Down
10 changes: 8 additions & 2 deletions crates/driver/src/tests/setup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1159,8 +1159,14 @@ impl<'a> SolveOk<'a> {
fulfillment.quoted_order.buy,
),
};
assert!(u256(trade.get("sellAmount").unwrap()) == expected_sell);
assert!(u256(trade.get("buyAmount").unwrap()) == expected_buy);
let u257 = u256(trade.get("sellAmount").unwrap());
let u258 = u256(trade.get("buyAmount").unwrap());
tracing::info!("newlog u257={:?}", u257);
tracing::info!("newlog expected_sell={:?}", expected_sell);
tracing::info!("newlog u258={:?}", u258);
tracing::info!("newlog expected_buy={:?}", expected_buy);
assert!(u257 == expected_sell);
assert!(u258 == expected_buy);
}
self
}
Expand Down

0 comments on commit 2a20508

Please sign in to comment.