cowprotocol / explorer

https://explorer.cow.fi/
12 stars 8 forks source link

fix: fix order surplus percentage calculation #709

Closed anxolin closed 10 months ago

anxolin commented 10 months ago

DRAFT: I actually have some fundamental questions about the surplus calculation. So I will put this a draft until we get an agreement, then I will need to decide about sellOrders (and fix tests) https://cowservices.slack.com/archives/C035N0YEB6J/p1703098943291339

Summary

Surplus for buy orders is broken.

Look at this example. The numbers match to me on how we define the order: image

50% slippage, it means in this example we need to add to the sell amount half of the sell amount in this case 0.049 * 0.5 = 0.0245

Our buy order max sell amount is 0.0735

So far so good 😊

Imagine we execute the order, and we get exactly the quote (0.049)

The surplus sould be 50%, right?

However the explorer does this: (0.049-0.0735)/0.0735

So we think surplus is 33.33%

What is the mistake in the explorer?? I believe we need to divide by the received price, not by the limit price in this case.

But Order Surplus = How much less I spent / How much I received

Take this order as an example: 0x1f9b14a849a1a903eab355ae6240cebb7174f4168064014cb46641f02e2d90f779063d9173c09887d536924e2f6eadbabac099f5658318db

https://explorer.cow.fi/gc/orders/0x1f9b14a849a1a903eab355ae6240cebb7174f4168064014cb46641f02e2d90f779063d9173c09887d536924e2f6eadbabac099f5658318db?tab=overview

This PR: https://explorer-dev-git-fix-surplus-buy-orders-cowswap.vercel.app/gc/orders/0x1f9b14a849a1a903eab355ae6240cebb7174f4168064014cb46641f02e2d90f779063d9173c09887d536924e2f6eadbabac099f5658318db?tab=overview

context

https://cowservices.slack.com/archives/C0361CDD1FZ/p1703089134101259

Test

sell/buy orders should work same as before

vercel[bot] commented 10 months ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
explorer-dev ✅ Ready (Inspect) Visit Preview Dec 21, 2023 11:59am

📖 Storybook ↗︎

github-actions[bot] commented 10 months ago

Pull Request Test Coverage Report for Build 7287788967


Totals Coverage Status
Change from base Build 7248021046: 0.0%
Covered Lines: 878
Relevant Lines: 1237

💛 - Coveralls
elena-zh commented 10 months ago

This change requires fixes on the CoW Swap side as well: surplus percentages should match on Explorer and on order receipt image

anxolin commented 10 months ago

Thanks for adding sell orders. I was not 100% sure we want this formula for them as its discussed in https://cowservices.slack.com/archives/C035N0YEB6J/p1703098943291339

however, probably I also lean to this implementation of surplus