Skip to content
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

Prioritize orders in SingleOrderSolvers #762

Merged
merged 12 commits into from
Nov 16, 2022

Conversation

MartinquaXD
Copy link
Contributor

@MartinquaXD MartinquaXD commented Nov 10, 2022

Fixes #753

With the release of the limit order feature we expect that we have to handle a lot more orders. The SingleOrderSolver (aptly named) can only solve a single order at a time and every order takes some amount of time. Because the SingleOrderSolvers can be quite slow solving an entire Auction is already time boxed.
To improve the chances to work on orders that can actually be settled and make the most of our the time we have we want to prioritize orders which are generously priced; meaning that they want to sell a lot and buy a little (denominated in ETH).

Since the auction already gives us prices for all the traded tokens in the order we can simply use those to compute each order's native sell_amount and buy_amount and prioritize the orders based on their ratio.
Note that we had issues in the past with deterministic order ordering because all SingleOrderSolvers would get stuck on "bad" orders. That's why we don't simply sort the orders by their price but assign a weight (based on its price) to each order and pick randomly from the weighted orders. That effectively gives us a semi-random ordering where orders which are generously priced have a higher chance to get solved early than orders with close to market pricing.

Test Plan

Added unit test but it's ignored because it could fail randomly with a very low probability.

@MartinquaXD MartinquaXD requested a review from a team as a code owner November 10, 2022 11:45
Copy link
Contributor

@nlordell nlordell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good to me. I feel like this is a relatively high-risk change. Maybe we should monitor it more closely over the next few weeks.

Copy link
Contributor

@vkgnosis vkgnosis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with the current state but also have a potential improvement.

The current relative priority is weak. An order with a price ratio of 0.9 is very likely not settlable, yet it's probability of being chosen relative to an order of price ratio 1.1 isn't that different. To improve this we could run the weight through another function to make it more extreme.

One way to do this could be squaring the value (x**2, or x**3, etc) and to prevent extreme outliers cap between [0.01, 10].

@MartinquaXD
Copy link
Contributor Author

To improve this we could run the weight through another function to make it more extreme.

I agree. I already asked the solver team this morning if they could come up with some sensible weight function.

@harisang
Copy link
Contributor

harisang commented Nov 14, 2022

I don't have a good intuition about what kind of weights would work, so I guess the x^2 suggestion above could work.

This also depends on whether we want to have this probability scale non-linearly or not. E.g., one could also try a piece-wise linear function and just differentiate between the > 1 and < 1 ratios. One such function could be the following:

f(x) = 10x + c, if x > 1.1
f(x) = 5x + d, if 0.9 < x < 1.1
f(x) = x, if x < 0.9

where c and d are set such that the function is continuous.

Copy link
Contributor

@nlordell nlordell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool! Can we maybe add some TRACE logs to list the order UIDs of the randomized orders? This would allow us to increase on observability into the randomization process if we want to debug it.

@MartinquaXD
Copy link
Contributor Author

I added the trace logs:

2022-11-16T13:34:28.788Z TRACE auction{id=3573685 run=0}: solver::solver::single_order_solver: prioritized orders name="1Inch" orders=[Limit Order OrderUid(0xf1d335487cc76316ec51795f98a9de0927f1da762cb43e9b2bc65b69c11d8bab189d81b32b847d94b12317c2fa64199d7cf506bb6374ecd1), Limit Order OrderUid(0xefadbf110bd2e8c14786795055df0cc241b930b6794e1479333e64522cf59bd0b00098ba6eedaed1d4ab31e7fa14cb969ccce6536374f618), Limit Order OrderUid(0xc019444bf2291849f8ac0d81441eae8d60a14bd2ad9f421f345d967b34e7fb84937a0c4697e5551ba3a50f8beb279ed0a71d08b56374f888), Limit Order OrderUid(0xbe99f8cee93dec848741910c9309386bd9af3644e5f746664ae376a716e4604affab14b181409170378471b13ff2bff5be012c64637506aa), Limit Order OrderUid(0x76da8cd66c3a6e4df2838cf07eddf0a71e15ad323435a294c1b2e52527da496c3dac271d1b36a434880c527a678b6487ac9c1f8c6374ed5f), Limit Order OrderUid(0xe1e88cc0c66d5cd061701cfaa555dab149c1d3eebc043133b1e19f264b0595df6880f5334158980ecfded17ae18b455efce1c0b56374f892), Limit Order OrderUid(0xc028ac80b4bc9926633d636e752aecca37e2dd606cb550549d777eaec2294ed0b0a9d7d6db9d58fd1cf528353a746d4b126c8b136374f889)]
2022-11-16T13:34:28.791Z TRACE auction{id=3573685 run=0}: solver::solver::single_order_solver: prioritized orders name="ParaSwap" orders=[Limit Order OrderUid(0xf1d335487cc76316ec51795f98a9de0927f1da762cb43e9b2bc65b69c11d8bab189d81b32b847d94b12317c2fa64199d7cf506bb6374ecd1), Limit Order OrderUid(0x76da8cd66c3a6e4df2838cf07eddf0a71e15ad323435a294c1b2e52527da496c3dac271d1b36a434880c527a678b6487ac9c1f8c6374ed5f), Limit Order OrderUid(0xefadbf110bd2e8c14786795055df0cc241b930b6794e1479333e64522cf59bd0b00098ba6eedaed1d4ab31e7fa14cb969ccce6536374f618), Limit Order OrderUid(0xc019444bf2291849f8ac0d81441eae8d60a14bd2ad9f421f345d967b34e7fb84937a0c4697e5551ba3a50f8beb279ed0a71d08b56374f888), Limit Order OrderUid(0xc028ac80b4bc9926633d636e752aecca37e2dd606cb550549d777eaec2294ed0b0a9d7d6db9d58fd1cf528353a746d4b126c8b136374f889), Limit Order OrderUid(0xe1e88cc0c66d5cd061701cfaa555dab149c1d3eebc043133b1e19f264b0595df6880f5334158980ecfded17ae18b455efce1c0b56374f892), Limit Order OrderUid(0xbe99f8cee93dec848741910c9309386bd9af3644e5f746664ae376a716e4604affab14b181409170378471b13ff2bff5be012c64637506aa)]
2022-11-16T13:34:28.794Z TRACE auction{id=3573685 run=0}: solver::solver::single_order_solver: prioritized orders name="0x" orders=[Limit Order OrderUid(0xefadbf110bd2e8c14786795055df0cc241b930b6794e1479333e64522cf59bd0b00098ba6eedaed1d4ab31e7fa14cb969ccce6536374f618), Limit Order OrderUid(0xc028ac80b4bc9926633d636e752aecca37e2dd606cb550549d777eaec2294ed0b0a9d7d6db9d58fd1cf528353a746d4b126c8b136374f889), Limit Order OrderUid(0x76da8cd66c3a6e4df2838cf07eddf0a71e15ad323435a294c1b2e52527da496c3dac271d1b36a434880c527a678b6487ac9c1f8c6374ed5f), Limit Order OrderUid(0xbe99f8cee93dec848741910c9309386bd9af3644e5f746664ae376a716e4604affab14b181409170378471b13ff2bff5be012c64637506aa), Limit Order OrderUid(0xf1d335487cc76316ec51795f98a9de0927f1da762cb43e9b2bc65b69c11d8bab189d81b32b847d94b12317c2fa64199d7cf506bb6374ecd1), Limit Order OrderUid(0xe1e88cc0c66d5cd061701cfaa555dab149c1d3eebc043133b1e19f264b0595df6880f5334158980ecfded17ae18b455efce1c0b56374f892), Limit Order OrderUid(0xc019444bf2291849f8ac0d81441eae8d60a14bd2ad9f421f345d967b34e7fb84937a0c4697e5551ba3a50f8beb279ed0a71d08b56374f888)]

@MartinquaXD MartinquaXD enabled auto-merge (squash) November 16, 2022 13:45
@MartinquaXD MartinquaXD merged commit 943665a into main Nov 16, 2022
@MartinquaXD MartinquaXD deleted the prioritize-viable-orders-in-single-order-solver branch November 16, 2022 13:50
@github-actions github-actions bot locked and limited conversation to collaborators Nov 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement heuristics to prioritize orders in SingleOrderSolvers
5 participants