From 4de8b9e0f14a802735c28466866a752ba74ca9b5 Mon Sep 17 00:00:00 2001 From: Carl Cervone <42869436+ccerv1@users.noreply.github.com> Date: Wed, 19 Jun 2024 12:48:45 -0400 Subject: [PATCH] dbt: identify contracts deployed by factories (#1675) * dbt: identify contracts deployed by factories * refactor int_contracts_by_project * materialize int_contracts_by_project as table --------- Co-authored-by: Reuven V. Gonzales --- .../directory/int_all_artifacts.sql | 39 ++++++-- .../directory/int_contracts_by_project.sql | 88 ++++++++++++++----- 2 files changed, 96 insertions(+), 31 deletions(-) diff --git a/warehouse/dbt/models/intermediate/directory/int_all_artifacts.sql b/warehouse/dbt/models/intermediate/directory/int_all_artifacts.sql index 4b47f191d..564726ecd 100644 --- a/warehouse/dbt/models/intermediate/directory/int_all_artifacts.sql +++ b/warehouse/dbt/models/intermediate/directory/int_all_artifacts.sql @@ -19,9 +19,7 @@ with ossd_artifacts as ( artifact_name, artifact_url from {{ ref("int_artifacts_in_ossd_by_project") }} - where - artifact_type != 'DEPLOYER' - and artifact_source != 'ANY_EVM' + where artifact_type not in ('DEPLOYER', 'CONTRACT') ), verified_deployers as ( @@ -50,7 +48,7 @@ verified_contracts as ( from {{ ref("int_contracts_by_project") }} ), -all_normalized_artifacts as ( +onchain_artifacts as ( select project_id, artifact_id, @@ -60,7 +58,7 @@ all_normalized_artifacts as ( artifact_namespace, artifact_name, artifact_url - from ossd_artifacts + from verified_deployers union all select project_id, @@ -71,7 +69,34 @@ all_normalized_artifacts as ( artifact_namespace, artifact_name, artifact_url - from verified_deployers + from verified_contracts +), + +other_artifacts as ( + select + project_id, + artifact_id, + artifact_source_id, + artifact_source, + artifact_type, + artifact_namespace, + artifact_name, + artifact_url + from ossd_artifacts + where artifact_id not in (select artifact_id from onchain_artifacts) +), + +all_normalized_artifacts as ( + select + project_id, + artifact_id, + artifact_source_id, + artifact_source, + artifact_type, + artifact_namespace, + artifact_name, + artifact_url + from other_artifacts union all select project_id, @@ -82,7 +107,7 @@ all_normalized_artifacts as ( artifact_namespace, artifact_name, artifact_url - from verified_contracts + from onchain_artifacts ) select distinct diff --git a/warehouse/dbt/models/intermediate/directory/int_contracts_by_project.sql b/warehouse/dbt/models/intermediate/directory/int_contracts_by_project.sql index 0190e3808..2171a42c9 100644 --- a/warehouse/dbt/models/intermediate/directory/int_contracts_by_project.sql +++ b/warehouse/dbt/models/intermediate/directory/int_contracts_by_project.sql @@ -1,35 +1,75 @@ -with contracts as ( +{{ + config( + materialized="table" + ) +}} +with contracts_in_ossd as ( select - block_timestamp, - transaction_hash, - network, - deployer_address, - contract_address - from {{ ref('int_derived_contracts') }} + project_id, + artifact_source as network, + artifact_name as contract_address + from {{ ref('int_artifacts_in_ossd_by_project') }} + where artifact_type = 'CONTRACT' ), -deployers as ( +derived_contracts_by_project as ( select - project_id, - artifact_name as deployer_address, - artifact_source as network - from {{ ref('int_deployers_by_project') }} + deployers_by_project.project_id, + derived_contracts.network, + derived_contracts.contract_address + from {{ ref('int_derived_contracts') }} as derived_contracts + left join {{ ref('int_deployers_by_project') }} as deployers_by_project + on + derived_contracts.deployer_address = deployers_by_project.artifact_name + and derived_contracts.network = deployers_by_project.artifact_source + where deployers_by_project.artifact_name is not null +), + +unified_contracts as ( + select distinct * + from ( + select + project_id, + network, + contract_address + from contracts_in_ossd + union all + select + project_id, + network, + contract_address + from derived_contracts_by_project + ) ), discovered_contracts as ( select - deployers.project_id, - contracts.block_timestamp, - contracts.transaction_hash, - contracts.network, - contracts.deployer_address, - contracts.contract_address - from contracts - left join deployers + unified_contracts.project_id, + unified_contracts.network, + factories.contract_address + from {{ ref('int_factories') }} as factories + left join unified_contracts on - contracts.deployer_address = deployers.deployer_address - and contracts.network = deployers.network - where deployers.deployer_address is not null + factories.factory_address = unified_contracts.contract_address + and factories.network = unified_contracts.network + where unified_contracts.project_id is not null +), + +contracts_by_project as ( + select distinct * + from ( + select + project_id, + network, + contract_address + from discovered_contracts + union all + select + project_id, + network, + contract_address + from unified_contracts + ) ) select @@ -39,4 +79,4 @@ select contract_address as artifact_source_id, LOWER(network) as artifact_namespace, contract_address as artifact_name -from discovered_contracts +from contracts_by_project