-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test: JIT orders surplus support for cow amm (#2699)
# Description Tests for the JIT orders surplus support PR: #2682
- Loading branch information
1 parent
0ed1c76
commit 681faa0
Showing
8 changed files
with
666 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
use crate::{ | ||
domain::{ | ||
competition::{order, order::Side}, | ||
eth, | ||
}, | ||
tests::{ | ||
self, | ||
cases::{is_approximately_equal, EtherExt}, | ||
setup::{ | ||
self, | ||
ab_adjusted_pool, | ||
ab_liquidity_quote, | ||
ab_order, | ||
ab_solution, | ||
test_solver, | ||
Test, | ||
}, | ||
}, | ||
}; | ||
|
||
struct Amounts { | ||
sell: eth::U256, | ||
buy: eth::U256, | ||
} | ||
|
||
struct Execution { | ||
// The executed net-amounts (including network fee) reported by the solver | ||
solver: Amounts, | ||
// The executed net-amounts (including network and protocol) reported by the driver | ||
driver: Amounts, | ||
} | ||
|
||
struct Order { | ||
sell_amount: eth::U256, | ||
buy_amount: eth::U256, | ||
side: order::Side, | ||
} | ||
|
||
struct JitOrder { | ||
order: Order, | ||
} | ||
|
||
struct Solution { | ||
jit_order: JitOrder, | ||
expected_score: eth::U256, | ||
} | ||
|
||
struct TestCase { | ||
order: Order, | ||
execution: Execution, | ||
is_surplus_capturing_jit_order: bool, | ||
solution: Solution, | ||
} | ||
|
||
#[cfg(test)] | ||
async fn protocol_fee_test_case(test_case: TestCase) { | ||
let test_name = format!("JIT Order: {:?}", test_case.order.side); | ||
// Adjust liquidity pools so that the order is executable at the amounts | ||
// expected from the solver. | ||
let quote = ab_liquidity_quote() | ||
.sell_amount(test_case.execution.solver.sell) | ||
.buy_amount(test_case.execution.solver.buy); | ||
let pool = ab_adjusted_pool(quote); | ||
let solver_fee = test_case.execution.driver.sell / 100; | ||
|
||
let jit_order = setup::JitOrder { | ||
order: ab_order() | ||
.kind(order::Kind::Limit) | ||
.sell_amount(test_case.solution.jit_order.order.sell_amount) | ||
.buy_amount(test_case.solution.jit_order.order.buy_amount) | ||
.solver_fee(Some(solver_fee)) | ||
.side(test_case.solution.jit_order.order.side) | ||
.no_surplus(), | ||
}; | ||
|
||
let order = ab_order() | ||
.kind(order::Kind::Limit) | ||
.sell_amount(test_case.order.sell_amount) | ||
.buy_amount(test_case.order.buy_amount) | ||
.solver_fee(Some(solver_fee)) | ||
.side(test_case.order.side) | ||
.partial(0.into()) | ||
.no_surplus(); | ||
|
||
let solver = test_solver(); | ||
let test: Test = tests::setup() | ||
.name(test_name) | ||
.pool(pool) | ||
.jit_order(jit_order.clone()) | ||
.order(order.clone()) | ||
.solution(ab_solution()) | ||
.surplus_capturing_jit_order_owners( | ||
test_case | ||
.is_surplus_capturing_jit_order | ||
.then(|| vec![solver.address()]) | ||
.unwrap_or_default(), | ||
) | ||
.solvers(vec![solver]) | ||
.done() | ||
.await; | ||
|
||
let result = test.solve().await.ok(); | ||
assert!(is_approximately_equal( | ||
result.score(), | ||
test_case.solution.expected_score, | ||
)); | ||
} | ||
|
||
#[tokio::test] | ||
#[ignore] | ||
async fn surplus_protocol_fee_jit_order_from_surplus_capturing_owner_not_capped() { | ||
let test_case = TestCase { | ||
order: Order { | ||
sell_amount: 50.ether().into_wei(), | ||
buy_amount: 40.ether().into_wei(), | ||
side: Side::Buy, | ||
}, | ||
execution: Execution { | ||
// 20 ETH surplus in sell token (after network fee), half of which is kept by the | ||
// protocol | ||
solver: Amounts { | ||
sell: 30.ether().into_wei(), | ||
buy: 40.ether().into_wei(), | ||
}, | ||
driver: Amounts { | ||
sell: 40.ether().into_wei(), | ||
buy: 40.ether().into_wei(), | ||
}, | ||
}, | ||
is_surplus_capturing_jit_order: true, | ||
solution: Solution { | ||
jit_order: JitOrder { | ||
order: Order { | ||
sell_amount: 50.ether().into_wei(), | ||
buy_amount: 40.ether().into_wei(), | ||
side: Side::Buy, | ||
}, | ||
}, | ||
// Score is 20 x 2 since there are two orders with score 20 (user order + JIT order) | ||
expected_score: 40.ether().into_wei(), | ||
}, | ||
}; | ||
|
||
protocol_fee_test_case(test_case).await; | ||
} | ||
|
||
#[tokio::test] | ||
#[ignore] | ||
async fn surplus_protocol_fee_jit_order_not_capped() { | ||
let test_case = TestCase { | ||
order: Order { | ||
sell_amount: 50.ether().into_wei(), | ||
buy_amount: 40.ether().into_wei(), | ||
side: Side::Buy, | ||
}, | ||
execution: Execution { | ||
// 20 ETH surplus in sell token (after network fee), half of which is kept by the | ||
// protocol | ||
solver: Amounts { | ||
sell: 30.ether().into_wei(), | ||
buy: 40.ether().into_wei(), | ||
}, | ||
driver: Amounts { | ||
sell: 40.ether().into_wei(), | ||
buy: 40.ether().into_wei(), | ||
}, | ||
}, | ||
is_surplus_capturing_jit_order: false, | ||
solution: Solution { | ||
jit_order: JitOrder { | ||
order: Order { | ||
sell_amount: 50.ether().into_wei(), | ||
buy_amount: 40.ether().into_wei(), | ||
side: Side::Buy, | ||
}, | ||
}, | ||
// Score is 20 since the JIT order is not from a surplus capturing owner | ||
expected_score: 20.ether().into_wei(), | ||
}, | ||
}; | ||
|
||
protocol_fee_test_case(test_case).await; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.