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

Fix CoW AMM CoW surplus query #60

Merged
merged 3 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cowamm/.sqlfluff
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ blockchain=ethereum
start_time='2024-08-20 00:00:00'
end_time='2024-08-27 00:00:00'
results=final_results_per_solver,cow_surplus_per_batch
budget=30000
cow_budget=30000
82 changes: 52 additions & 30 deletions cowamm/cow_amm_cow_with_users_surplus_query_4031724.sql
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 (
Copy link
Contributor

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.

Copy link
Contributor Author

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.

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 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.

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
Loading