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 block range, bonding pool and vouch queries #19

Merged
merged 13 commits into from
Sep 12, 2024
5 changes: 5 additions & 0 deletions cowprotocol/accounting/rewards/mainnet/.sqlfluff
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[sqlfluff:templater:jinja:context]
start_time='2024-08-20 00:00'
end_time='2024-08-27 00:00'
vouch_cte_name=named_results, valid_vouches
harisang marked this conversation as resolved.
Show resolved Hide resolved

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- Base query to convert accounting period to block range.
-- Convention throughout the accounting that is used is that we consider all auctions whose
-- block deadline is in the block range this query returns
-- Parameters:
-- {{start_time}} - the start date timestamp for the accounting period (inclusively)
-- {{end_time}} - the end date timestamp for the accounting period (exclusively)

select
min("number") as start_block,
max("number") as end_block
from ethereum.blocks
where time >= cast('{{start_time}}' as timestamp) and time < cast('{{end_time}}' as timestamp)
fhenneke marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-- Query that hardcodes all existing full bonding pools that
-- are currently valid at CoW Protocol.
with
full_bonding_pools as (
select
from_hex('0x8353713b6D2F728Ed763a04B886B16aAD2b16eBD') as pool_address,
'Gnosis' as pool_name,
from_hex('0x6c642cafcbd9d8383250bb25f67ae409147f78b2') as initial_funder
union distinct
select
from_hex('0x5d4020b9261F01B6f8a45db929704b0Ad6F5e9E6') as pool_address,
'CoW DAO' as pool_name,
from_hex('0x423cec87f19f0778f549846e0801ee267a917935') as initial_funder
union distinct
select
from_hex('0xC96569Dc132ebB6694A5f0b781B33f202Da8AcE8') as pool_address,
'Project Blanc' as pool_name,
from_hex('0xCa99e3Fc7B51167eaC363a3Af8C9A185852D1622') as initial_funder
)

select *
from
full_bonding_pools
Copy link
Contributor

Choose a reason for hiding this comment

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

We can add this as is to stay compatible with its current function.

An alternative, which I prefer, is to simplify it to just get the latest pool and reward address, irrespective of any invalidations later on. Solvers changing bonding pools or getting unvouched before running the script have been a pain.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just to clarify, are you saying we should simply fetch the latest vouching event of every solver, and then don't worry about the rest, as in case the solver is later unvouched, we will have in any case blacklisted as well and then that solver would have no effect in the competition?

Copy link
Contributor

Choose a reason for hiding this comment

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

As far as uses in solver rewards, I think we should only care about latest reward addresses (and bonding pools). If a solver gets blacklisted and removed, solver rewards should still be rewarded as if they had not been removed. Blacklisting should take care of barring solvers from gaining additional rewards if they are not eligible.

Other places might use that query differently. But for our main use case, this simplification should be useful.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah makes sense, will proceed with the simplification

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, i think i changed my mind on this again. This query also shows in the main dashboard, and i think it is nice to be able to see all "active" solvers, with their names etc. So if you don't mind, i would keep it as is and would actually even revert to the previous state where there are two views of the results, so that we can use the other view in the main dashboard.

Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
-- Query that fetches the list of solver addresses that are active
-- and are properly vouched for by a full bonding pool
-- Parameters:
-- {{end_time}} - the end date timestamp for the accounting period (exclusively)
with
fhenneke marked this conversation as resolved.
Show resolved Hide resolved
last_block_before_timestamp as (
select end_block from "query_3333356(start_time='2018-01-01 00:00',end_time='{{end_time}}')"
),

-- Query Logic Begins here!
vouches as (
select
evt_block_number,
evt_index,
solver,
cowRewardTarget as reward_target,
pool_address,
initial_funder,
True as active
from cow_protocol_ethereum.VouchRegister_evt_Vouch
inner join query_4056263
on
pool_address = bondingPool
and sender = initial_funder
where evt_block_number <= (select * from last_block_before_timestamp)
),

invalidations as (
select
evt_block_number,
evt_index,
solver,
Null as reward_target, -- This is just to align with vouches to take a union
pool_address,
initial_funder,
False as active
from cow_protocol_ethereum.VouchRegister_evt_InvalidateVouch
inner join query_4056263
on
pool_address = bondingPool
and sender = initial_funder
where evt_block_number <= (select * from last_block_before_timestamp)
),

-- Intermediate helper table
vouches_and_invalidations as (
select * from vouches
union distinct
select * from invalidations
),

-- At this point we have excluded all arbitrary vouches (i.e., those not from initial funders of recognized pools)
-- The next query ranks (solver, pool_address, initial_funder) by most recent (vouch or invalidation)
-- and yields as rank 1, the current "active" status of the triplet.
ranked_vouches as (
select
*,
rank() over (
partition by solver, pool_address, initial_funder
order by evt_block_number desc, evt_index desc
) as rk
from vouches_and_invalidations
),

-- This will contain all latest active vouches,
-- but could still contain solvers with multiplicity > 1 for different pools.
-- Rank here again by solver, and time.
current_active_vouches as (
select
*,
rank() over (
partition by solver
order by evt_block_number, evt_index
) as time_rank
from ranked_vouches
where
rk = 1
and active = True
),

-- To filter for the case of "same solver, different pool",
-- rank the current_active vouches and choose the earliest
valid_vouches as (
select
solver,
reward_target,
pool
from current_active_vouches
where time_rank = 1
)

select * from valid_vouches
Loading