-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add block range, bonding pool and vouch queries (#19)
* add block range, bonding pool and vouch queries * small fix * bug fixing * add description to block range query * add description to full bonding pools query * Add descritpion to vouch registry query * small updates * Update cowprotocol/accounting/rewards/mainnet/.sqlfluff Co-authored-by: Felix Henneke <[email protected]> * add hardcoded timestamps * add back alternative view * fix typo * bug fixes * minor edits --------- Co-authored-by: Felix Henneke <[email protected]>
- Loading branch information
Showing
4 changed files
with
146 additions
and
0 deletions.
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
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:00' | ||
end_time='2024-08-27 00:00:00' | ||
vouch_cte_name=valid_vouches,named_results | ||
|
12 changes: 12 additions & 0 deletions
12
...col/accounting/rewards/mainnet/block_number_interval_from_time_interval_query_3333356.sql
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 |
---|---|---|
@@ -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) |
23 changes: 23 additions & 0 deletions
23
cowprotocol/accounting/rewards/mainnet/full_bonding_pools_query_4056263.sql
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 |
---|---|---|
@@ -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 |
106 changes: 106 additions & 0 deletions
106
cowprotocol/accounting/rewards/mainnet/vouch_registry_query_1541516.sql
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 |
---|---|---|
@@ -0,0 +1,106 @@ | ||
-- 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 | ||
last_block_before_timestamp as ( | ||
select end_block from "query_3333356(start_time='2018-01-01 00: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_address | ||
from current_active_vouches | ||
where time_rank = 1 | ||
), | ||
|
||
named_results as ( | ||
select | ||
vv.solver, | ||
vv.reward_target, | ||
vv.pool_address, | ||
bp.pool_name, | ||
concat(environment, '-', s.name) as solver_name | ||
from valid_vouches as vv | ||
inner join cow_protocol_ethereum.solvers as s | ||
on vv.solver = s.address | ||
inner join query_4056263 as bp | ||
on vv.pool_address = bp.pool_address | ||
) | ||
|
||
select * from {{vouch_cte_name}} |