Skip to content

Commit

Permalink
Add CoW AMM surplus bounty query (#43)
Browse files Browse the repository at this point in the history
* sync dashboard query with what is on Dune

* add cow amm surplus bounty query

* remove irrelevant changes

* address some comments

* minor changes

* bug fix

* another bug fix

* table renaming
  • Loading branch information
harisang authored Sep 18, 2024
1 parent 3ae11d1 commit b940fe9
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cowamm/.sqlfluff
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@ start='2024-08-08 00:00'
token_a='0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'
token_b='0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'
cow_amm='0xf08d4dea369c456d26a3168ff0024b904f2d8b91'
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
56 changes: 56 additions & 0 deletions cowamm/cow_amm_cow_with_users_surplus_query_4031724.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
-- 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
-- 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

with cow_amm_surplus 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
),

cow_surplus_per_batch as (
select
cow_per_batch.block_time,
cow_per_batch.tx_hash,
solver_address,
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
),

aggregate_results_per_solver as (
select
name as 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
),

total_surplus as (
select sum(total_cow_surplus_in_usd) as total_surplus_in_usd from aggregate_results_per_solver
),

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 * from {{results}}

0 comments on commit b940fe9

Please sign in to comment.