Skip to content

Commit

Permalink
Adds deployer tables (#1130)
Browse files Browse the repository at this point in the history
* Adds deployer tables

* fix playground

* Add discovered deployers

* lint fixes

* more lint fixes

* lint fixes

* fix

* fix

* fix

* lint fixes
  • Loading branch information
ravenac95 authored Mar 26, 2024
1 parent 4158d5f commit cb2842d
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 2 deletions.
10 changes: 10 additions & 0 deletions warehouse/dbt/models/ethereum_sources.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
sources:
- name: ethereum

database: bigquery-public-data

schema: crypto_ethereum
tables:
- name: transactions
identifier: transactions

12 changes: 12 additions & 0 deletions warehouse/dbt/models/optimism_sources.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
sources:
- name: optimism

database: bigquery-public-data

schema: goog_blockchain_optimism_mainnet_us
tables:
- name: transactions
identifier: transactions

- name: logs
identifier: logs
13 changes: 13 additions & 0 deletions warehouse/dbt/models/playground/ethereum__transactions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{{
config(
materialized='table',
) if target.name in ['playground', 'dev'] else config(
enabled=false,
)
}}
SELECT *
FROM {{ source("ethereum", 'transactions') }}
WHERE block_timestamp >= TIMESTAMP_TRUNC(
TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL -14 DAY),
DAY
)
13 changes: 13 additions & 0 deletions warehouse/dbt/models/playground/optimism__logs.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{{
config(
materialized='table',
) if target.name in ['playground', 'dev'] else config(
enabled=false,
)
}}
SELECT *
FROM {{ source("optimism", 'logs') }}
WHERE block_timestamp >= TIMESTAMP_TRUNC(
TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL -14 DAY),
DAY
)
13 changes: 13 additions & 0 deletions warehouse/dbt/models/playground/optimism__transactions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{{
config(
materialized='table',
) if target.name in ['playground', 'dev'] else config(
enabled=false,
)
}}
SELECT *
FROM {{ source("optimism", 'transactions') }}
WHERE block_timestamp >= TIMESTAMP_TRUNC(
TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL -14 DAY),
DAY
)
34 changes: 34 additions & 0 deletions warehouse/dbt/models/staging/ethereum/stg_ethereum__deployers.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{{
config(
materialized='incremental',
partition_by={
"field": "block_timestamp",
"data_type": "timestamp",
"granularity": "day",
},
unique_id="transaction_hash",
on_schema_change="append_new_columns",
incremental_strategy="insert_overwrite"
) if target.name == 'production' else config(
materialized='table',
)
}}

SELECT
block_timestamp,
`hash` AS transaction_hash,
from_address AS deployer_address,
receipt_contract_address AS contract_address
FROM {{ source("ethereum", "transactions") }}
WHERE
to_address IS NULL
AND receipt_status = 1
AND receipt_contract_address IS NOT NULL
{% if is_incremental() %}
WHERE
TIMESTAMP_TRUNC(block_timestamp, DAY) >= (
SELECT TIMESTAMP_TRUNC(MAX(block_timestamp), DAY)
FROM {{ this }}
)
AND TIMESTAMP_TRUNC(block_timestamp, DAY) < CURRENT_TIMESTAMP()
{% endif %}
49 changes: 49 additions & 0 deletions warehouse/dbt/models/staging/optimism/stg_optimism__deployers.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{{
config(
materialized='incremental',
partition_by={
"field": "block_timestamp",
"data_type": "timestamp",
"granularity": "day",
},
unique_id="transaction_hash",
on_schema_change="append_new_columns",
incremental_strategy="insert_overwrite"
) if target.name == 'production' else config(
materialized='table',
)
}}
WITH {% if is_incremental() %} max_block_timestamp AS (
SELECT TIMESTAMP_TRUNC(MAX(block_timestamp), DAY)
FROM {{ this }}
),
{% endif %}
logs AS (
-- transactions
SELECT *
FROM {{ oso_source("optimism", "logs") }}
{% if is_incremental() %}
WHERE
TIMESTAMP_TRUNC(block_timestamp, DAY) >= (
SELECT * FROM max_block_timestamp
)
AND TIMESTAMP_TRUNC(block_timestamp, DAY) < CURRENT_TIMESTAMP()
{% endif %}
)

SELECT
t.block_timestamp AS block_timestamp,
t.transaction_hash AS transaction_hash,
t.from_address AS deployer_address,
l.address AS contract_address
FROM {{ oso_source("optimism", "transactions") }} AS t
INNER JOIN logs AS l
ON t.transaction_hash = l.transaction_hash
WHERE
t.to_address IS null
{% if is_incremental() %}
AND TIMESTAMP_TRUNC(block_timestamp, DAY) >= (
SELECT * FROM max_block_timestamp
)
AND TIMESTAMP_TRUNC(block_timestamp, DAY) < CURRENT_TIMESTAMP()
{% endif %}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ all_npm AS (
UNNEST(JSON_QUERY_ARRAY(projects.npm)) AS npm
),

all_blockchain AS (
ossd_blockchain AS (
SELECT
projects.id AS project_id,
UPPER(network) AS artifact_namespace,
Expand All @@ -55,14 +55,46 @@ all_blockchain AS (
UNNEST(JSON_VALUE_ARRAY(blockchains.tags)) AS tag
),

all_deployers AS (
SELECT
*,
'OPTIMISM' AS network
FROM {{ ref("stg_ethereum__deployers") }}
UNION ALL
SELECT
*,
'ETHEREUM' AS network
FROM {{ ref("stg_ethereum__deployers") }}
),

discovered_contracts AS (
SELECT
ob.project_id,
ob.artifact_namespace,
'CONTRACT' AS artifact_type,
ad.contract_address AS artifact_name,
ad.contract_address AS artifact_url,
ad.contract_address AS artifact_source_id
FROM ossd_blockchain AS ob
INNER JOIN all_deployers AS ad
ON
ob.artifact_source_id = ad.deployer_address
AND ob.artifact_namespace = ad.network
AND ob.artifact_type IN ('EOA', 'DEPLOYER', 'FACTORY')
),

all_artifacts AS (
SELECT *
FROM
all_repos
UNION ALL
SELECT *
FROM
all_blockchain
ossd_blockchain
UNION ALL
SELECT *
FROM
discovered_contracts
UNION ALL
SELECT *
FROM
Expand Down

0 comments on commit cb2842d

Please sign in to comment.