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

Remove Custom Price Order Sell Price from UCP #878

Merged
merged 6 commits into from
Dec 1, 2022

Conversation

sunce86
Copy link
Contributor

@sunce86 sunce86 commented Nov 30, 2022

Fixes #848 and potentially #849

Custom price orders (liquidity orders and limit orders) now store both sell_price and buy_price outside of the clearing price vector (as suggested by @MartinquaXD, thanks for clarifying the issue).

Copy link
Contributor

@MartinquaXD MartinquaXD left a comment

Choose a reason for hiding this comment

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

This patch looks correct to me. After thinking about it a bit more I believe initially only 1 token price was stored directly in the custom price trades as a gas optimization but I believe in this case it's better to have a sane implementation than saving tiny amounts of gas. Managing the Settlement and its encoding is already difficult enough. 👍

@nlordell
Copy link
Contributor

After thinking about it a bit more I believe initially only 1 token price was stored directly in the custom price trades as a gas optimization but I believe in this case it's better to have a sane implementation than saving tiny amounts of gas.

Yes. THe issue is we found that for solvers that would compress prices (reduce number of bits to represent prices for gas savings) we would get huge errors on what we wanted the price to actually be. Agree that this is the correct fix.

In theory, we could do something complicated like bitshift prices so that there are no leading 0's when multiplied with the highest amounts for maximum precision, but as you said, settlement encoding is already complicated enough.

let sell_token = order.data.sell_token;
let sell_price = order.data.buy_amount;
self.tokens.push(sell_token);
self.clearing_prices.insert(sell_token, sell_price);
Copy link
Contributor

Choose a reason for hiding this comment

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

For context the assumption is that this line added the buy_amount of a foreign liquidity order contained in a solution to the clearing price vector of the Settlement if that order's sell_token was not already part of the clearing prices. Because this price is not denominated in ETH and actually only makes sense in relation to the custom price associated to that order it can make the Settlement fail the price deviation check.

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.

Changes look correct to me. I would also change limit orders to use exact prices (or they will also run into the same precision issues as liquidity orders).

@sunce86
Copy link
Contributor Author

sunce86 commented Dec 1, 2022

I've updated the unit tests - moving to ready to review.
Also I have one additional change for merging the settlements...

@sunce86 sunce86 marked this pull request as ready for review December 1, 2022 17:01
@sunce86 sunce86 requested a review from a team as a code owner December 1, 2022 17:01
Ok(())
})
.collect::<Result<Vec<()>>>()?;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think there is no need to scale custom price orders anymore, since they are now completely decoupled from UCP.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think so as well.

Comment on lines +351 to +352
let adjusted_sell_price = buy_amount;
let adjusted_buy_price = sell_amount;
Copy link
Contributor

Choose a reason for hiding this comment

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

This always confuses me 🤣 - but I'm 99% sure its correct (you can never be 100% when it comes to prices and exchange rates...)

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.

Nice!

@nlordell
Copy link
Contributor

nlordell commented Dec 1, 2022

I didn't quite understand how this fixes #849 - which to my understanding means that the driver is doing price deviation checks for limit and liquidity orders (even if it is introducing the prices).

(To me this PR fixes #848).

@sunce86
Copy link
Contributor Author

sunce86 commented Dec 1, 2022

I didn't quite understand how this fixes #849 - which to my understanding means that the driver is doing price deviation checks for limit and liquidity orders (even if it is introducing the prices).

It fixes it because this bug was caused by foreign liquidity orders adding sell_token price to the UCP vector but it's value was not scaled leading to price violations when compared to other values from UCP vector. Since prices for liquidity/limit orders are no longer stored in UCP we skip this check. But this is just my assumption based on the @MartinquaXD investigation here.

Anyhow I will skip fixing #849 with this PR until I have a proof for it.

@sunce86 sunce86 merged commit 6988d10 into main Dec 1, 2022
@sunce86 sunce86 deleted the fix-liquidity-orders-price-bug branch December 1, 2022 22:57
@github-actions github-actions bot locked and limited conversation to collaborators Dec 1, 2022
@harisang
Copy link
Contributor

harisang commented Dec 2, 2022

Great, thanks for taking care of this!

As far as I understand, the prices determining the execution of a liquidity order are no longer stored in the UCP and, and if this means that these prices are not checked for max price deviation, then we are good to go with #849 as well.

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.

Fix bug when introducing prices for liquidity orders
5 participants