Skip to content

Commit ade5b78

Browse files
authored
feat(sqlmesh): create fee revenue models for oracles (#5321)
1 parent a7697ee commit ade5b78

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
MODEL (
2+
name oso.int_optimism_oracles_direct_fees_daily,
3+
description "Optimism oracles direct fees daily",
4+
dialect trino,
5+
kind incremental_by_time_range(
6+
time_column bucket_day,
7+
batch_size 60,
8+
batch_concurrency 1,
9+
lookback 14,
10+
forward_only true,
11+
on_destructive_change warn,
12+
),
13+
start @blockchain_incremental_start,
14+
cron '@daily',
15+
partitioned_by MONTH("bucket_day"),
16+
audits (
17+
has_at_least_n_rows(threshold := 0),
18+
),
19+
ignored_rules (
20+
"incrementalmustdefinenogapsaudit",
21+
"incrementalmusthaveforwardonly",
22+
),
23+
);
24+
25+
WITH events AS (
26+
SELECT
27+
DATE_TRUNC('DAY', block_timestamp) AS bucket_day,
28+
project_name AS oracle_name,
29+
to_address_trace AS oracle_address,
30+
gas_used_trace::DOUBLE / 1e18 * gas_price_tx::DOUBLE AS read_fees,
31+
transaction_hash
32+
FROM oso.int_optimism_static_calls_to_oracles
33+
WHERE block_timestamp BETWEEN @start_dt AND @end_dt
34+
)
35+
36+
SELECT
37+
bucket_day,
38+
oracle_name,
39+
oracle_address,
40+
SUM(read_fees) AS read_fees,
41+
COUNT(DISTINCT transaction_hash) AS transaction_count
42+
FROM events
43+
GROUP BY 1, 2, 3
44+
ORDER BY 1, 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
MODEL (
2+
name oso.int_optimism_oracles_indirect_fees_daily,
3+
description "Optimism oracles indirect fees daily",
4+
dialect trino,
5+
kind incremental_by_time_range(
6+
time_column bucket_day,
7+
batch_size 60,
8+
batch_concurrency 1,
9+
lookback 14,
10+
forward_only true,
11+
on_destructive_change warn,
12+
),
13+
start @blockchain_incremental_start,
14+
cron '@daily',
15+
partitioned_by MONTH("bucket_day"),
16+
audits (
17+
has_at_least_n_rows(threshold := 0),
18+
),
19+
ignored_rules (
20+
"incrementalmustdefinenogapsaudit",
21+
"incrementalmusthaveforwardonly",
22+
),
23+
);
24+
25+
WITH events AS (
26+
SELECT
27+
DATE_TRUNC('DAY', block_timestamp) AS bucket_day,
28+
project_name AS oracle_name,
29+
to_address_tx AS to_address,
30+
gas_used_tx::DOUBLE / 1e18 * gas_price_tx::DOUBLE AS l2_tx_fees,
31+
transaction_hash
32+
FROM oso.int_optimism_static_calls_to_oracles
33+
WHERE
34+
block_timestamp BETWEEN @start_dt AND @end_dt
35+
AND project_name IS NOT NULL
36+
),
37+
oracles_per_txn AS (
38+
SELECT
39+
transaction_hash,
40+
COUNT(DISTINCT oracle_name) AS num_oracles
41+
FROM events
42+
GROUP BY 1
43+
),
44+
amortized_events AS (
45+
SELECT
46+
bucket_day,
47+
oracle_name,
48+
to_address,
49+
SUM(l2_tx_fees / num_oracles) AS l2_tx_fees,
50+
SUM(1.0 / num_oracles) AS tx_count
51+
FROM events
52+
JOIN oracles_per_txn
53+
ON events.transaction_hash = oracles_per_txn.transaction_hash
54+
GROUP BY 1, 2, 3
55+
),
56+
apps AS (
57+
SELECT DISTINCT
58+
artifact_name,
59+
project_name AS to_project_name
60+
FROM oso.artifacts_by_project_v1
61+
WHERE
62+
project_source = 'OSS_DIRECTORY'
63+
AND project_namespace = 'oso'
64+
AND artifact_source = 'OPTIMISM'
65+
)
66+
67+
SELECT
68+
ae.bucket_day,
69+
ae.oracle_name,
70+
ae.to_address,
71+
apps.to_project_name,
72+
ae.l2_tx_fees,
73+
ae.tx_count
74+
FROM amortized_events AS ae
75+
LEFT JOIN apps
76+
ON ae.to_address = apps.artifact_name
77+
ORDER BY 1

0 commit comments

Comments
 (0)