-
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.
Merge pull request #5 from cowprotocol/payment_due
Payment Due query
- Loading branch information
Showing
2 changed files
with
48 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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
[sqlfluff:templater:jinja:context] | ||
start='2024-08-01 12:00' | ||
end='2024-08-02 12:00' | ||
fee_computation_start='2024-08-01 12:00' | ||
fee_computation_end='2024-08-02 12:00' | ||
billing_date='2024-08-01 12:00' |
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,45 @@ | ||
-- Computes the weekly payment due for each connected builder | ||
-- Parameters: | ||
-- {{fee_computation_start}} - the start timestamp for the per block fee computation subquery | ||
-- {{fee_computation_end}} - the end timestamp for the per block fee computation subquery | ||
-- {{billing_date}} - the date at which the query is run | ||
|
||
WITH block_range AS ( | ||
SELECT | ||
MIN(number) AS start_block, | ||
MAX(number) + 1 AS end_block -- range is exclusive | ||
FROM ethereum.blocks | ||
WHERE | ||
time >= TIMESTAMP '{{billing_date}}' - INTERVAL '7' DAY | ||
AND time < TIMESTAMP '{{billing_date}}' | ||
), | ||
|
||
final_fee AS ( | ||
SELECT avg_block_fee_wei | ||
FROM "query_4002039(start='{{fee_computation_start}}', end='{{fee_computation_end}}')" | ||
), | ||
|
||
-- selects the count of blocks each builder won in the billing period | ||
builder_blocks AS ( | ||
SELECT | ||
label, | ||
billing_address, | ||
COUNT(*) AS blocks_won | ||
FROM ethereum.raw_0004 | ||
INNER JOIN query_4001804 AS info | ||
ON | ||
(CONTAINS(info.extra_data, FROM_HEX(block.extraData)) OR CONTAINS(info.builder_addresses, FROM_HEX(block.miner))) --noqa: RF01 | ||
AND blockNumber >= start_block | ||
AND blockNumber < end_block | ||
AND blockNumber >= (SELECT start_block FROM block_range) | ||
AND blockNumber < (SELECT end_block FROM block_range) | ||
GROUP BY 1, 2 | ||
) | ||
|
||
SELECT | ||
b.*, | ||
avg_block_fee_wei AS weekly_fee, | ||
blocks_won * avg_block_fee_wei AS amount_due_wei, | ||
blocks_won * avg_block_fee_wei / 1e18 AS amount_due_eth | ||
FROM builder_blocks AS b | ||
CROSS JOIN final_fee |