-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Partially fillable orders in driver tests #2512
Changes from 6 commits
938f21b
c1fa5b5
0ff04bf
578ab42
f176d45
5075047
257d023
9c9bf73
2f844ef
72ff73a
227db4e
8ebca7a
8c212ec
2ac00e7
d685908
38469a3
e955a1e
7929000
20cfba2
9d4bfbb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,6 +131,40 @@ impl QuotedOrder { | |
} | ||
} | ||
|
||
/// Calculates buy amount that should be received from a driver | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does this mean? Why is this necessary and different to e.g. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed |
||
pub fn driver_buy_amount(&self) -> eth::U256 { | ||
let executed_buy = match self.order.side { | ||
order::Side::Buy => self.order.executed.unwrap_or(self.buy), | ||
order::Side::Sell => self | ||
.order | ||
.executed | ||
// Since `executed` is a target amount | ||
.map(|executed_sell| self.buy * executed_sell / self.sell) | ||
.unwrap_or(self.buy), | ||
}; | ||
match self.order.side { | ||
order::Side::Buy => executed_buy, | ||
order::Side::Sell => executed_buy / self.order.surplus_factor, | ||
} | ||
} | ||
|
||
/// 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::Buy => self | ||
.order | ||
.executed | ||
// Since `executed` is a target amount | ||
.map(|executed_buy| self.sell * executed_buy / self.buy) | ||
.unwrap_or(self.sell), | ||
}; | ||
match self.order.side { | ||
order::Side::Buy => executed_sell * self.order.surplus_factor, | ||
order::Side::Sell => executed_sell, | ||
} | ||
} | ||
|
||
/// The UID of the order. | ||
pub fn order_uid(&self, blockchain: &Blockchain) -> tests::boundary::OrderUid { | ||
self.boundary(blockchain).uid() | ||
|
@@ -561,7 +595,7 @@ impl Blockchain { | |
/// Compute the execution of an order given the available liquidity | ||
pub fn execution(&self, order: &Order) -> Execution { | ||
let pair = self.find_pair(order); | ||
let (sell, buy) = match (order.side, order.buy_amount) { | ||
let (sell, buy) = match (order.side, order.executed.or(order.buy_amount)) { | ||
// For buy order with explicitly specified amounts, use the buy amount | ||
(order::Side::Buy, Some(buy_amount)) => ( | ||
pair.pool.in_given_out(Asset { | ||
|
@@ -570,6 +604,17 @@ impl Blockchain { | |
}), | ||
buy_amount, | ||
), | ||
// todo: simplify it | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please do. This conditional is now very hard to read. I think we should have two conditional, one checks if there is an explicit amount set (in which case we use it, using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Applied your suggestion, thanks! |
||
(order::Side::Sell, _) => { | ||
let sell_amount = order.executed.unwrap_or(order.sell_amount); | ||
( | ||
sell_amount, | ||
pair.pool.out_given_in(Asset { | ||
amount: sell_amount, | ||
token: order.sell_token, | ||
}), | ||
) | ||
} | ||
// Otherwise assume the full sell amount to compute the execution | ||
(_, _) => ( | ||
order.sell_amount, | ||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -128,8 +128,9 @@ pub struct Order { | |||
/// buy amount is divided depends on the order side. This is necessary to | ||||
/// keep the solution scores positive. | ||||
pub surplus_factor: eth::U256, | ||||
/// Override the executed amount of the order. Useful for testing liquidity | ||||
/// orders. Otherwise [`execution_diff`] is probably more suitable. | ||||
/// Override the executed target amount of the order. Useful for testing | ||||
/// liquidity orders. Otherwise [`execution_diff`] is probably more | ||||
/// suitable. | ||||
Comment on lines
+131
to
+133
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are not testing liquidity order though, are we? I wonder if executed is even needed or if we can instead use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mean to pass order's full amount into this function?
That doesn't work since |
||||
pub executed: Option<eth::U256>, | ||||
/// Provides explicit expected order executed amounts. | ||||
pub expected_amounts: Option<ExpectedOrderAmounts>, | ||||
|
@@ -253,6 +254,14 @@ impl Order { | |||
} | ||||
} | ||||
|
||||
pub fn partial(self, partial: Partial) -> Self { | ||||
Self { partial, ..self } | ||||
} | ||||
|
||||
pub fn executed(self, executed: Option<eth::U256>) -> Self { | ||||
Self { executed, ..self } | ||||
} | ||||
|
||||
fn surplus_fee(&self) -> eth::U256 { | ||||
match self.kind { | ||||
order::Kind::Limit => self.solver_fee.unwrap_or_default(), | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we simplify this example by using more round fractions (e.g. have the order be ratiod 1:1 or 1:2 and partially match it 50%)?
This way it's easy to see the surplus (here you need to do some non-trivial math)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simplified the amounts.