-
Notifications
You must be signed in to change notification settings - Fork 2
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
Fix CoW AMM CoW surplus query #60
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,78 @@ | ||
-- This query computes how much surplus has been provided to CoW AMMs, when trading with other user orders | ||
-- as part of a CoW. For that, a CoW detector query is used (4025739(). Finally, the query computes the | ||
-- distribution of an amount {{budget}} of COW tokens to solvers, proportionally to the surplus generated | ||
-- distribution of an amount {{cow_budget}} of COW tokens to solvers, proportionally to the surplus generated | ||
-- via CoWs and pushed to CoW AMMs. | ||
-- Parameters: | ||
-- {{start_time}} - the start date timestamp for the accounting period (inclusively) | ||
-- {{end_time}} - the end date timestamp for the accounting period (exclusively) | ||
-- {{blockchain}} -- the chain we are interested in | ||
-- {{budget}} -- the amount of COW that needs to be distributed | ||
-- {{cow_budget}} -- the amount of COW that needs to be distributed | ||
|
||
with cow_amm_surplus as ( | ||
with cow_surplus_per_batch_ethereum as ( | ||
select | ||
tx_hash, | ||
case | ||
when token_1_transfer_usd > 0 then token_1_transfer_usd + (token_1_balance_usd - token_1_transfer_usd) * token_2_transfer_usd / token_2_balance_usd | ||
else token_2_transfer_usd + (token_2_balance_usd - token_2_transfer_usd) * token_1_transfer_usd / token_1_balance_usd | ||
end as surplus | ||
from dune.cowprotocol.result_balancer_cow_amm_base_query_v_2 | ||
where istrade | ||
'ethereum' as blockchain, | ||
cow_per_batch.block_time, | ||
cow_per_batch.tx_hash, | ||
solvers.name as solver_name, | ||
naive_cow, -- fraction of batch volume traded within a CoW | ||
trades.surplus_usd as surplus_in_usd, -- surplus of the executed CoW AMM order, expressed in USD | ||
naive_cow * trades.surplus_usd as realized_cow_surplus_in_usd -- surplus of the CoW AMM that is assumed to be generated via a CoW. | ||
from "query_4025739(blockchain='ethereum',start_time='{{start_time}}',end_time='{{end_time}}')" as cow_per_batch | ||
inner join cow_protocol_ethereum.trades as trades on cow_per_batch.tx_hash = trades.tx_hash | ||
inner join cow_protocol_ethereum.batches as batches on cow_per_batch.tx_hash = batches.tx_hash | ||
inner join cow_protocol_ethereum.solvers as solvers on batches.solver_address = solvers.address and solvers.active | ||
where trades.trader in (select address from query_3959044 where blockchain = 'ethereum') | ||
), | ||
|
||
cow_surplus_per_batch as ( | ||
cow_surplus_per_batch_gnosis as ( | ||
select | ||
'gnosis' as blockchain, | ||
cow_per_batch.block_time, | ||
cow_per_batch.tx_hash, | ||
solver_address, | ||
solvers.name as solver_name, | ||
naive_cow, -- fraction of batch volume traded within a CoW | ||
surplus as surplus_in_usd, -- surplus of the executed CoW AMM order, expressed in USD | ||
naive_cow * surplus as realized_cow_surplus_in_usd -- surplus of the CoW AMM that is assumed to be generated via a CoW. | ||
from "query_4025739(blockchain='{{blockchain}}',start_time='{{start_time}}',end_time='{{end_time}}')" as cow_per_batch | ||
inner join cow_amm_surplus on cow_per_batch.tx_hash = cow_amm_surplus.tx_hash | ||
inner join cow_protocol_{{blockchain}}.batches as b on cow_per_batch.tx_hash = b.tx_hash | ||
trades.surplus_usd as surplus_in_usd, -- surplus of the executed CoW AMM order, expressed in USD | ||
naive_cow * trades.surplus_usd as realized_cow_surplus_in_usd -- surplus of the CoW AMM that is assumed to be generated via a CoW. | ||
from "query_4025739(blockchain='gnosis',start_time='{{start_time}}',end_time='{{end_time}}')" as cow_per_batch | ||
inner join cow_protocol_gnosis.trades as trades on cow_per_batch.tx_hash = trades.tx_hash | ||
inner join cow_protocol_gnosis.batches as batches on cow_per_batch.tx_hash = batches.tx_hash | ||
inner join cow_protocol_gnosis.solvers as solvers on batches.solver_address = solvers.address and solvers.active | ||
where trades.trader in (select address from query_3959044 where blockchain = 'gnosis') | ||
), | ||
|
||
cow_surplus_per_batch as ( | ||
select * from cow_surplus_per_batch_ethereum | ||
union all | ||
select * from cow_surplus_per_batch_gnosis | ||
), | ||
|
||
aggregate_results_per_solver as ( | ||
|
||
aggregate_result_per_solver as ( | ||
select | ||
name as solver_name, | ||
solver_name, | ||
sum(realized_cow_surplus_in_usd) as total_cow_surplus_in_usd | ||
from cow_surplus_per_batch | ||
inner join cow_protocol_{{blockchain}}.solvers as s on cow_surplus_per_batch.solver_address = s.address and s.active | ||
group by name | ||
group by solver_name | ||
), | ||
|
||
total_surplus as ( | ||
select sum(total_cow_surplus_in_usd) as total_surplus_in_usd from aggregate_results_per_solver | ||
|
||
---- final results | ||
|
||
reward_addresses as ( | ||
select | ||
solver as solver_address, | ||
reward_target, | ||
substring(solver_name, 6, 100) as solver_name | ||
from "query_1541516(end_time='{{end_time}}',vouch_cte_name='named_results')" | ||
), | ||
|
||
final_results_per_solver as ( | ||
select | ||
arps.solver_name, | ||
total_cow_surplus_in_usd, | ||
{{budget}} * arps.total_cow_surplus_in_usd / ts.total_surplus_in_usd as total_cow_reward | ||
from aggregate_results_per_solver as arps cross join total_surplus as ts | ||
select distinct | ||
a.solver_name, | ||
b.reward_target, | ||
a.total_cow_surplus_in_usd, | ||
{{cow_budget}} * a.total_cow_surplus_in_usd / (select sum(total_cow_surplus_in_usd) from aggregate_result_per_solver) as total_cow_reward | ||
from aggregate_result_per_solver as a | ||
inner join reward_addresses as b on a.solver_name = b.solver_name | ||
) | ||
|
||
select * from {{results}} | ||
select * from {{results}} where total_cow_reward > 0 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why are we not including arbitrum AMMs here? I think they also qualify for the bounty.
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 is easy to add that if it qualifies. Let me double check with @harisang.
I mostly tried to stick to what the previous query did. Note, that the query previously used was not the same as the one under version control. It explicitly used tables for ethereum and gnosis, but not arbitrum.
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 added a table for arbitrum. Interestingly, there is no difference in total surplus. I briefly checked the intermediate tables and they seem to work. So I think it is fine to merge this.