Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds deployer tables #1130

Merged
merged 10 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading