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

Add multiple price feeds option #65

Open
wants to merge 21 commits into
base: main
Choose a base branch
from

Conversation

harisang
Copy link
Contributor

@harisang harisang commented Nov 13, 2024

This PR adds the option to use multiple price feeds as follows:

  • at most 4 price feeds are considered, and the approx_percentile(0.5) the method for computing the median that is presented in this article is used to compute a price based on these price feeds. This replaces what has been now only the dune price.

In case all price feeds return null, we continue to use the fallbacks that have been used up till now

@harisang harisang requested a review from fhenneke November 14, 2024 07:25
Copy link
Contributor

@fhenneke fhenneke left a comment

Choose a reason for hiding this comment

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

The changes generally make sense for testing other price feeds.

One issue is that the query is a bit too complicated for me now. (Maybe already was too complicated before.) Since it is essential to the accounting, it has to easy to understand what the query does.

For example, the handling of missing values seems a bit indirect: first, some price feeds are combined and an approximate median is computed. If that is missing, yet another price feed is used. So there seem to be different tiers of price feeds as well. But that is not made explicit anywhere. And only the Dune price feed is used for some things (intrinsic prices, native token prices) that other price feeds could theoretically also be used for.

cowprotocol/accounting/slippage/raw_slippage_4059683.sql Outdated Show resolved Hide resolved
@@ -43,6 +82,35 @@ precise_prices as (
group by 1, 2, 3
),

multiple_price_feeds_pre as (
Copy link
Contributor

Choose a reason for hiding this comment

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

This name does not help me understand what this table means. You can just select from a union in the next table. If it is a conceptually important table to you, the name should be changed.

@@ -25,8 +25,47 @@ with token_times as (
group by 1, 2
),

imported_price_feed as (
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add some comments on what those tables mean, or on the meaning of some tables in the context of the full query.

@harisang harisang requested a review from fhenneke November 20, 2024 18:01
@@ -135,5 +187,5 @@ native_token_prices as (
)

select * from prices
union all
union distinct
Copy link
Contributor

Choose a reason for hiding this comment

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

Why was this change required? Before, the entries in the prices table were always distinct from entries in the native_token_prices table since the token address was different. Does this still hold?

If not, there might be additional issues due to a token having multiple prices at the same time, leading to duplicate entries when joining.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tbh i didn't think too much about it and it just looked more correct to me (in the sense that if you provide a prices table where native tokens are included, then you should not use the corresponding native_token_prices entries). In the current state, i don't think it can ever happen that the two table have common entries so i will switch back to union all

@fhenneke fhenneke dismissed their stale review November 21, 2024 11:00

This dude cannot be trusted!

@fhenneke
Copy link
Contributor

fhenneke commented Dec 4, 2024

Is there a way to test this query?

@fleupold fleupold added the E:9.1 Streamline mainnet payouts process See https://github.com/cowprotocol/pm/issues/80 for details label Dec 6, 2024
@harisang harisang force-pushed the add_additional_price_feed_to_slippage branch from 5ea81c5 to 1fa83a6 Compare January 31, 2025 01:45
Copy link

github-actions bot commented Jan 31, 2025

@harisang
Copy link
Contributor Author

For the median part of the code, i added this "test"
https://dune.com/queries/4646457

@fhenneke
Copy link
Contributor

fhenneke commented Feb 4, 2025

Do you have a test query on dune for some of the final queries, e.g. raw slippage from query 4059683?

I still do not quite get what the final design would be and what the intermediate testing would look
like. Do we just run every query twice and compare? Or have one more query which calls slippage with different price feed parameters? Will we remove the price feed parameter at some point? How does it help with fixing prices?

@harisang
Copy link
Contributor Author

harisang commented Feb 5, 2025

So, i forked basically all queries that this PR changes as follows:

There are some minor changes in some queries so that their output is more interesting for testing purposes

@harisang
Copy link
Contributor Author

harisang commented Feb 5, 2025

I still do not quite get what the final design would be and what the intermediate testing would look
like. Do we just run every query twice and compare? Or have one more query which calls slippage with different price feed parameters? Will we remove the price feed parameter at some point? How does it help with fixing prices?

My goal would be to eventually use the "new price feed". What this will be is not clear yet. Currently it is the median of some price feeds; we could add more price feeds or take some other function of the existing ones, or average out over longer periods of time, etc etc. But the goal is to replace the Dune price feed eventually.

The problem itself is a bit underspecified, that's why i don't have a clear answer to "how does it help with fixing prices".

A concrete first goal for me would be to kill outliers, i.e., not have to manually exclude txs from the accounting because slippage is extremely off.

@fhenneke
Copy link
Contributor

fhenneke commented Feb 5, 2025

I checked the raw slippage per transaction query and there seems to be a significant difference between the old and new query with the dune price feed (e.g. for transaction 0x89aae16f7b5650fb4f4cc59fe1bb63d6c622a205a4ac8b344db748e800ce7a47 or 0x8f941c8512d281efee3e76f5d388a76af40990812478c5840b3dda538c3f3d9e). And the differences between the two price feeds in the new query seems small (but thta would kind of be expected).
old:
image
new with dune price feed:
image
new with median price feed:
image

Do you know where those differences are coming from?

@fhenneke
Copy link
Contributor

fhenneke commented Feb 5, 2025

I think the issue is with using prices per unit instead of prices per atom. E.g. for token 0x9de1c3d446237ab9baff74127eb4f303802a2683, the Dune table does not contain a price nor does it contain information on decimals. The original reworked slippage accounting intentionally used prices per atom which could still be computed from exchange rates. The rework in this PR moves to prices per unit, which depends on having information on decimals. The corresponding contribution to slippage is thus ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E:9.1 Streamline mainnet payouts process See https://github.com/cowprotocol/pm/issues/80 for details
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants