-
Notifications
You must be signed in to change notification settings - Fork 0
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
Refactor slippage query #31
Conversation
this file should have all information required for debugging unusual silppage
for compatibility with the old query
cowprotocol/accounting/slippage/slippage_per_solver_4070065.sql
Outdated
Show resolved
Hide resolved
cowprotocol/accounting/slippage/slippage_per_solver_4070065.sql
Outdated
Show resolved
Hide resolved
cowprotocol/accounting/slippage/slippage_per_transaction_4070059.sql
Outdated
Show resolved
Hide resolved
cowprotocol/accounting/slippage/slippage_per_transaction_4070059.sql
Outdated
Show resolved
Hide resolved
-- Results of the query are filtered to not include batches from excluded_batches. | ||
-- Batches are also excluded if there is a non-zero imbalance and no value (in native atomes). | ||
|
||
with excluded_batches as ( |
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.
I think this should be moved to the slippage_per_solver
query.
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.
One goal was to have a query per transaction which is exactly what we use for total slippage to base an unusual slippage query on.
If you think that is not a requirement and having a discrepancy there if fine, I can move excluded batches somewhere else.
Maybe the better distinction is to merge the three queries slippage breakdown, slippage per transaction, slippage per solver into
- raw slippage (including breakdown and aggregation per transaction)
- slippage (with filtering, per transaction and per solver)
The first query would then be for debugging and could also leave out solver names, the second would be compatible with actual payment.
select | ||
s.block_time, | ||
s.tx_hash, | ||
solver_address, |
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.
i guess solver_address could be skipped here as it is not really relevant to the main functionality of the query. And also causes this inner join with the batches table which looked a bit confusing at first.
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.
It was useful for me to have a query with results per transaction and where I could filter on solver address. A cleaner way might be what I proposed in the response above.
on s.tx_hash = b.tx_hash | ||
where s.tx_hash not in (select tx_hash from excluded_batches) | ||
group by 1, 2, 3 | ||
having bool_and(slippage_native_atom is not null or slippage_atoms = 0) |
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 you explain how this bool_and
works here? And why is the second condition in the parenthesis needed? Sorry for my ignorance
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.
I also did not know this before I saw it being used in the old slippage query.
bool_and
is an aggregating expression, similar to sum
. it checks that all of the expressions (iterating over rows within a group, same as with sum
), i.e. the logical and
of all the entries.
Here it is used to check that for each entry in the slippage breakdown slippage_wei is not null
, i.e., we have a value for slippage in wei due to a price being available, or slippage_atoms = 0
, i.e., there is no slippage so we also do not need a price. This excludes all settlements where a price is missing for a token for which we need a price.
- rename transfer_type to type - rename fee_data to raw_fee_data - reorder columns for consistency - fix some comments - rename price to price_unit - rename slippage_native_atom to slippage_wei
- rename type to fee_type or slippage_type - allow more intuitive ordering of columns
…59.sql Co-authored-by: Haris Angelidakis <64154020+harisang@users.noreply.github.com>
I joined the new and old slippage query to check differences, https://dune.com/queries/4083184. In cases where both queries report a value, they are almost identical (~1e-10).
|
where block_time >= cast('{{start_time}}' as timestamp) and block_time < cast('{{end_time}}' as timestamp) | ||
), | ||
|
||
protocol_fee_balance_changes as ( |
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.
The table name, just looking at this query, is a bit strange. Same for the next one
select * from "query_4064601(blockchain='{{blockchain}}',start_time='{{start_time}}',end_time='{{end_time}}')" | ||
), | ||
|
||
raw_slippage_breakdown as ( |
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.
what do we mean by "raw" here?
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.
I want to differentiate between slippage values without much processing ("raw_slippage", for debugging) and slippage values after processing (just "slippage", what is really used later on).
Maybe "unfiltered" would be better. Or just slippage_breakdown and slippage_per_transaction and name the query "slippage_breakdown"?
Do you have a different naming suggestion?
Looks good!
Shouldn't with then remove that change from here? |
I will remove them now. They were useful for testing on Gnosis since otherwise wrong slippage would have dominated everything. |
This reverts commit 9961053.
This PR refactors the slippage query. It separates different parts of the slippage computation, simplifies debugging unusual slippage, and prepares for moving some of the slippage computation offline.
Query layout
The general layout of the query is as follows (dashed lines and rectangles correspond to queries which do not yet exist):
'protocol_fee'
and'network_fee'
. Protocol fees could in future PRs be separated into protocol fees and partner fees, and, combined with other prices, could give totals for protocol and parner fees.The changes to the main slippage query can be observed here.
Except for the empty fee table, all of the queries should work on other chains as well. Missing fee data currently means that network and protocol fees are treated as slippage, which is fine for network fees but incorrect for protocol fees.
Differences to old slippage query
This is mostly but not quite a replacement of the old slippage query. To exceptions:
Fix of balances changes queryWorking on slippage revealed an issue with the balance changes query: unlike on mainnet, it is not required on Gnosis to have special treatment for sDAI. All balance changes are accompanied by transfer events. Looking at deposit and withdraw events is not required.The change is also independently addresses in #41.(Updated with modified design.)