Skip to content

Commit

Permalink
Merge pull request #5 from cowprotocol/payment_due
Browse files Browse the repository at this point in the history
Payment Due query
  • Loading branch information
fleupold authored Aug 26, 2024
2 parents 55e3b50 + e99fb97 commit 822caf7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
3 changes: 3 additions & 0 deletions mevblocker/fees/.sqlfluff
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'
45 changes: 45 additions & 0 deletions mevblocker/fees/payment_due_4005800.sql
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

0 comments on commit 822caf7

Please sign in to comment.