Skip to content

Commit

Permalink
rework comments
Browse files Browse the repository at this point in the history
  • Loading branch information
m-lord-renkse committed Oct 15, 2024
1 parent b364db7 commit 06596b8
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 45 deletions.
16 changes: 15 additions & 1 deletion crates/driver/src/tests/cases/jit_orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::{
ab_order,
ab_solution,
test_solver,
ExpectedOrderAmounts,
Test,
},
},
Expand Down Expand Up @@ -62,6 +63,18 @@ async fn protocol_fee_test_case(test_case: TestCase) {
.buy_amount(test_case.execution.solver.buy);
let pool = ab_adjusted_pool(quote);
let solver_fee = test_case.execution.driver.sell / 100;
// Amounts expected to be returned by the driver after fee processing
let jit_order_expected_amounts = if test_case.is_surplus_capturing_jit_order {
ExpectedOrderAmounts {
sell: test_case.execution.solver.sell,
buy: test_case.execution.solver.buy,
}
} else {
ExpectedOrderAmounts {
sell: test_case.solution.jit_order.order.sell_amount,
buy: test_case.solution.jit_order.order.buy_amount,
}
};

let jit_order = setup::JitOrder {
order: ab_order()
Expand All @@ -70,7 +83,8 @@ async fn protocol_fee_test_case(test_case: TestCase) {
.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(),
.no_surplus()
.expected_amounts(jit_order_expected_amounts),
};

let order = ab_order()
Expand Down
39 changes: 22 additions & 17 deletions crates/driver/src/tests/setup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use {
DEFAULT_SURPLUS_FACTOR,
ETH_ORDER_AMOUNT,
},
hex_address,
setup::blockchain::{Blockchain, Trade},
},
},
Expand Down Expand Up @@ -1236,29 +1237,33 @@ impl<'a> SolveOk<'a> {
// Since JIT orders don't have UID at creation time, we need to search for
// matching token pair
for expected in jit_orders.iter() {
let mut exist = false;
for trade in trades.values() {
let u256 = |value: &serde_json::Value| {
eth::U256::from_dec_str(value.as_str().unwrap()).unwrap()
};
let sell_token = trade.get("sellToken").unwrap().to_string();
let buy_token = trade.get("buyToken").unwrap().to_string();

if sell_token == expected.order.sell_token && buy_token == expected.order.buy_token
{
assert!(is_approximately_equal(
expected.order.sell_amount,
u256(trade.get("executedSell").unwrap())
));
assert!(is_approximately_equal(
expected.order.buy_amount.unwrap(),
u256(trade.get("executedBuy").unwrap())
));
}
exist |= self.exist_jit_order(trade, expected)
}
assert!(exist, "JIT order {expected:?} not found");
}
self
}

/// Find for a JIT order, given specific token pair and buy/sell amount,
/// return true if the JIT order was found
fn exist_jit_order(&self, trade: &serde_json::Value, expected: &JitOrder) -> bool {
let u256 =
|value: &serde_json::Value| eth::U256::from_dec_str(value.as_str().unwrap()).unwrap();
let sell_token = trade.get("sellToken").unwrap().to_string();
let sell_token = sell_token.trim_matches('"');
let buy_token = trade.get("buyToken").unwrap().to_string();
let buy_token = buy_token.trim_matches('"');
let sell_amount = u256(trade.get("executedSell").unwrap());
let buy_amount = u256(trade.get("executedBuy").unwrap());

sell_token == hex_address(self.blockchain.get_token(&expected.order.sell_token))
&& buy_token == hex_address(self.blockchain.get_token(&expected.order.buy_token))
&& expected.order.expected_amounts.clone().unwrap().sell == sell_amount
&& expected.order.expected_amounts.clone().unwrap().buy == buy_amount
}

/// Check that the solution contains the expected orders.
pub fn orders(self, orders: &[Order]) -> Self {
let solution = self.solution();
Expand Down
62 changes: 35 additions & 27 deletions crates/driver/src/tests/setup/solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,20 +197,24 @@ impl Solver {
})
},
));
prices_json.insert(
config
.blockchain
.get_token_wrapped(fulfillment.quoted_order.order.sell_token),
fulfillment.execution.buy.to_string(),
);
prices_json.insert(
config
.blockchain
.get_token_wrapped(fulfillment.quoted_order.order.buy_token),
(fulfillment.execution.sell
- fulfillment.quoted_order.order.surplus_fee())
.to_string(),
);
assert!(prices_json
.insert(
config
.blockchain
.get_token_wrapped(fulfillment.quoted_order.order.sell_token),
fulfillment.execution.buy.to_string(),
)
.is_none());
assert!(prices_json
.insert(
config
.blockchain
.get_token_wrapped(fulfillment.quoted_order.order.buy_token),
(fulfillment.execution.sell
- fulfillment.quoted_order.order.surplus_fee())
.to_string(),
)
.is_none());
{
// trades have optional field `fee`
let order = if config.quote {
Expand Down Expand Up @@ -275,19 +279,23 @@ impl Solver {
.expected_surplus_capturing_jit_order_owners
.contains(&jit.quoted_order.order.owner)
{
prices_json.insert(
config
.blockchain
.get_token_wrapped(jit.quoted_order.order.sell_token),
jit.execution.buy.to_string(),
);
prices_json.insert(
config
.blockchain
.get_token_wrapped(jit.quoted_order.order.buy_token),
(jit.execution.sell - jit.quoted_order.order.surplus_fee())
.to_string(),
);
assert!(prices_json
.insert(
config
.blockchain
.get_token_wrapped(jit.quoted_order.order.sell_token),
jit.execution.buy.to_string(),
)
.is_none());
assert!(prices_json
.insert(
config
.blockchain
.get_token_wrapped(jit.quoted_order.order.buy_token),
(jit.execution.sell - jit.quoted_order.order.surplus_fee())
.to_string(),
)
.is_none());
}
{
let executed_amount = match jit.quoted_order.order.executed {
Expand Down

0 comments on commit 06596b8

Please sign in to comment.