From d774949ca2bc58d420f1656408d2d4509bec7318 Mon Sep 17 00:00:00 2001 From: drethereum <71602799+drethereum@users.noreply.github.com> Date: Tue, 25 Jun 2024 16:47:51 -0600 Subject: [PATCH] AN-4881/agent-activity (#178) * initial models * docs * docs and tests * overview docs * updates for service reads * coalesce * row num * heal * inc * remove test * rebuild * remove docs --- .../silver_olas__getservice_reads.sql | 153 +++++++++++++++++ .../silver_olas__getservice_reads.yml | 17 ++ .../silver_olas__registry_metadata.sql | 146 ++++++++++++++++ .../silver_olas__registry_metadata.yml | 19 +++ ...ilver_olas__registry_metadata_complete.sql | 35 ++++ ...ilver_olas__registry_metadata_complete.yml | 16 ++ .../metadata/silver_olas__registry_reads.sql | 121 ++++++++++++++ .../metadata/silver_olas__registry_reads.yml | 19 +++ .../silver_olas__create_service_multisigs.sql | 53 ++++++ .../silver_olas__create_service_multisigs.yml | 24 +++ .../olas/silver_olas__service_event_logs.sql | 57 +++++++ .../olas/silver_olas__service_event_logs.yml | 28 ++++ .../silver_olas__service_registrations.sql | 158 ++++++++++++++++++ .../silver_olas__service_registrations.yml | 29 ++++ 14 files changed, 875 insertions(+) create mode 100644 models/silver/protocols/olas/metadata/silver_olas__getservice_reads.sql create mode 100644 models/silver/protocols/olas/metadata/silver_olas__getservice_reads.yml create mode 100644 models/silver/protocols/olas/metadata/silver_olas__registry_metadata.sql create mode 100644 models/silver/protocols/olas/metadata/silver_olas__registry_metadata.yml create mode 100644 models/silver/protocols/olas/metadata/silver_olas__registry_metadata_complete.sql create mode 100644 models/silver/protocols/olas/metadata/silver_olas__registry_metadata_complete.yml create mode 100644 models/silver/protocols/olas/metadata/silver_olas__registry_reads.sql create mode 100644 models/silver/protocols/olas/metadata/silver_olas__registry_reads.yml create mode 100644 models/silver/protocols/olas/silver_olas__create_service_multisigs.sql create mode 100644 models/silver/protocols/olas/silver_olas__create_service_multisigs.yml create mode 100644 models/silver/protocols/olas/silver_olas__service_event_logs.sql create mode 100644 models/silver/protocols/olas/silver_olas__service_event_logs.yml create mode 100644 models/silver/protocols/olas/silver_olas__service_registrations.sql create mode 100644 models/silver/protocols/olas/silver_olas__service_registrations.yml diff --git a/models/silver/protocols/olas/metadata/silver_olas__getservice_reads.sql b/models/silver/protocols/olas/metadata/silver_olas__getservice_reads.sql new file mode 100644 index 00000000..6cb6a16b --- /dev/null +++ b/models/silver/protocols/olas/metadata/silver_olas__getservice_reads.sql @@ -0,0 +1,153 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = 'getservice_reads_id', + full_refresh = false, + tags = ['curated'] +) }} + +WITH service_contracts AS ( + + SELECT + contract_address, + service_id AS registry_id, + MAX(block_number) AS block_number + FROM + {{ ref('silver_olas__service_registrations') }} + +{% if is_incremental() %} +WHERE + _inserted_timestamp >= ( + SELECT + MAX(_inserted_timestamp) - INTERVAL '12 hours' + FROM + {{ this }} + ) + AND CONCAT( + contract_address, + '-', + registry_id + ) NOT IN ( + SELECT + CONCAT( + contract_address, + '-', + function_input + ) + FROM + {{ this }} + ) +{% endif %} +GROUP BY + 1, + 2 +), +function_sigs AS ( + SELECT + '0xef0e239b' AS function_sig, + 'getService' AS function_name +), +inputs AS ( + SELECT + contract_address, + block_number, + function_sig, + function_name, + registry_id AS function_input, + CONCAT( + function_sig, + LPAD( + SUBSTR(utils.udf_int_to_hex(function_input), 3), + 64, + 0) + ) AS DATA + FROM + service_contracts + JOIN function_sigs + ON 1 = 1 + ), + contract_reads AS ( + SELECT + contract_address, + block_number, + function_sig, + function_name, + function_input, + DATA, + utils.udf_json_rpc_call( + 'eth_call', + [{ 'to': contract_address, 'from': null, 'data': data }, utils.udf_int_to_hex(block_number) ] + ) AS rpc_request, + live.udf_api( + 'POST', + CONCAT( + '{service}', + '/', + '{Authentication}' + ),{}, + rpc_request, + 'Vault/prod/base/quicknode/mainnet' + ) AS read_output, + SYSDATE() AS _inserted_timestamp + FROM + inputs + ), + reads_flat AS ( + SELECT + read_output, + read_output :data :id :: STRING AS read_id, + read_output :data :result :: STRING AS read_result, + SPLIT( + read_id, + '-' + ) AS read_id_object, + regexp_substr_all(SUBSTR(read_result, 3, len(read_result)), '.{64}') AS segmented_read, + utils.udf_hex_to_int( + VALUE :: STRING + ) AS decoded_read, + function_sig, + function_name, + function_input, + DATA, + contract_address, + block_number, + _inserted_timestamp + FROM + contract_reads, + LATERAL FLATTEN( + input => segmented_read + ) + ), + reads_final AS ( + SELECT + read_output, + read_id, + read_result, + read_id_object, + segmented_read, + function_sig, + function_name, + function_input, + DATA, + contract_address, + block_number, + _inserted_timestamp, + ARRAY_AGG(TRY_TO_NUMBER(decoded_read)) AS reads_array, + ARRAY_SLICE(reads_array, 9, ARRAY_SIZE(reads_array)) AS agent_ids + FROM + reads_flat + GROUP BY + ALL) + SELECT + *, + {{ dbt_utils.generate_surrogate_key( + ['contract_address','function_input'] + ) }} AS getservice_reads_id, + SYSDATE() AS inserted_timestamp, + SYSDATE() AS modified_timestamp, + '{{ invocation_id }}' AS _invocation_id + FROM + reads_final + WHERE + agent_ids IS NOT NULL + AND agent_ids :: STRING <> '[]' diff --git a/models/silver/protocols/olas/metadata/silver_olas__getservice_reads.yml b/models/silver/protocols/olas/metadata/silver_olas__getservice_reads.yml new file mode 100644 index 00000000..10d22ac6 --- /dev/null +++ b/models/silver/protocols/olas/metadata/silver_olas__getservice_reads.yml @@ -0,0 +1,17 @@ +version: 2 +models: + - name: silver_olas__getservice_reads + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - GETSERVICE_READS_ID + columns: + - name: BLOCK_NUMBER + tests: + - not_null + - name: FUNCTION_INPUT + tests: + - not_null + - name: AGENT_IDS + tests: + - not_null \ No newline at end of file diff --git a/models/silver/protocols/olas/metadata/silver_olas__registry_metadata.sql b/models/silver/protocols/olas/metadata/silver_olas__registry_metadata.sql new file mode 100644 index 00000000..843fce62 --- /dev/null +++ b/models/silver/protocols/olas/metadata/silver_olas__registry_metadata.sql @@ -0,0 +1,146 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = 'registry_metadata_id', + full_refresh = false, + tags = ['curated'] +) }} + +WITH new_records AS ( + + SELECT + block_number, + contract_address, + function_input AS registry_id, + token_uri_link, + _inserted_timestamp, + ROW_NUMBER() over ( + ORDER BY + contract_address, + registry_id + ) AS row_num + FROM + {{ ref('silver_olas__registry_reads') }} + +{% if is_incremental() %} +WHERE + _inserted_timestamp > ( + SELECT + MAX(_inserted_timestamp) + FROM + {{ this }} + ) + OR + CONCAT( + contract_address, + '-', + registry_id + ) IN ( + SELECT + CONCAT( + contract_address, + '-', + registry_id + ) + FROM + {{ this }} + WHERE + NAME IS NULL + ) +{% endif %} +), +uri_calls AS ( + SELECT + block_number, + contract_address, + registry_id, + token_uri_link, + live.udf_api(token_uri_link) AS resp, + _inserted_timestamp + FROM + new_records + WHERE + row_num <= 100 + UNION ALL + SELECT + block_number, + contract_address, + registry_id, + token_uri_link, + live.udf_api(token_uri_link) AS resp, + _inserted_timestamp + FROM + new_records + WHERE + row_num > 100 + AND row_num <= 200 + UNION ALL + SELECT + block_number, + contract_address, + registry_id, + token_uri_link, + live.udf_api(token_uri_link) AS resp, + _inserted_timestamp + FROM + new_records + WHERE + row_num > 200 +), +response AS ( + SELECT + resp, + block_number, + contract_address, + registry_id, + token_uri_link, + resp :data :attributes [0] :trait_type :: STRING AS trait_type, + resp :data :attributes [0] :value :: STRING AS trait_value, + REPLACE( + resp :data :code_uri :: STRING, + 'ipfs://', + 'https://gateway.autonolas.tech/ipfs/' + ) AS code_uri_link, + resp :data :description :: STRING AS description, + CASE + WHEN resp :data :image :: STRING ILIKE 'ipfs://%' THEN REPLACE( + resp :data :image :: STRING, + 'ipfs://', + 'https://gateway.autonolas.tech/ipfs/' + ) + WHEN resp :data :image :: STRING NOT ILIKE '%://%' THEN CONCAT( + 'https://gateway.autonolas.tech/ipfs/', + resp :data :image :: STRING + ) + ELSE resp :data :image :: STRING + END AS image_link, + resp :data :name :: STRING AS NAME, + _inserted_timestamp + FROM + uri_calls +) +SELECT + resp, + block_number, + contract_address, + registry_id, + token_uri_link, + trait_type, + trait_value, + code_uri_link, + description, + image_link, + NAME, + _inserted_timestamp, + {{ dbt_utils.generate_surrogate_key( + ['contract_address','registry_id'] + ) }} AS registry_metadata_id, + SYSDATE() AS inserted_timestamp, + SYSDATE() AS modified_timestamp, + '{{ invocation_id }}' AS _invocation_id +FROM + response +WHERE + resp :: STRING NOT ILIKE '%merkledag: not found%' + AND resp :: STRING NOT ILIKE '%tuple index out of range%' + AND resp :: STRING NOT ILIKE '%"error":%' diff --git a/models/silver/protocols/olas/metadata/silver_olas__registry_metadata.yml b/models/silver/protocols/olas/metadata/silver_olas__registry_metadata.yml new file mode 100644 index 00000000..0b4afd35 --- /dev/null +++ b/models/silver/protocols/olas/metadata/silver_olas__registry_metadata.yml @@ -0,0 +1,19 @@ +version: 2 +models: + - name: silver_olas__registry_metadata + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - REGISTRY_METADATA_ID + columns: + - name: BLOCK_NUMBER + tests: + - not_null + - name: CONTRACT_ADDRESS + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: 0[xX][0-9a-fA-F]+ + - name: REGISTRY_ID + tests: + - not_null diff --git a/models/silver/protocols/olas/metadata/silver_olas__registry_metadata_complete.sql b/models/silver/protocols/olas/metadata/silver_olas__registry_metadata_complete.sql new file mode 100644 index 00000000..f6c5ff9d --- /dev/null +++ b/models/silver/protocols/olas/metadata/silver_olas__registry_metadata_complete.sql @@ -0,0 +1,35 @@ +{{ config( + materialized = 'view' +) }} + +SELECT + m.name, + m.description, + m.registry_id, + m.contract_address, + CASE + WHEN m.contract_address = '0x3c1ff68f5aa342d296d4dee4bb1cacca912d95fe' THEN 'Service' + END AS registry_type, + m.trait_type, + m.trait_value, + m.code_uri_link, + m.image_link, + s.agent_ids, + m.registry_metadata_id, + m.inserted_timestamp, + GREATEST( + COALESCE( + m.modified_timestamp, + '1970-01-01' :: TIMESTAMP + ), + COALESCE( + s.modified_timestamp, + '1970-01-01' :: TIMESTAMP + ) + ) AS modified_timestamp +FROM + {{ ref('silver_olas__registry_metadata') }} + m + LEFT JOIN {{ ref('silver_olas__getservice_reads') }} + s + ON m.registry_id = s.function_input \ No newline at end of file diff --git a/models/silver/protocols/olas/metadata/silver_olas__registry_metadata_complete.yml b/models/silver/protocols/olas/metadata/silver_olas__registry_metadata_complete.yml new file mode 100644 index 00000000..9dbda4de --- /dev/null +++ b/models/silver/protocols/olas/metadata/silver_olas__registry_metadata_complete.yml @@ -0,0 +1,16 @@ +version: 2 +models: + - name: silver_olas__registry_metadata_complete + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - REGISTRY_METADATA_ID + columns: + - name: CONTRACT_ADDRESS + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: 0[xX][0-9a-fA-F]+ + - name: REGISTRY_ID + tests: + - not_null \ No newline at end of file diff --git a/models/silver/protocols/olas/metadata/silver_olas__registry_reads.sql b/models/silver/protocols/olas/metadata/silver_olas__registry_reads.sql new file mode 100644 index 00000000..2c0df30b --- /dev/null +++ b/models/silver/protocols/olas/metadata/silver_olas__registry_reads.sql @@ -0,0 +1,121 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = 'registry_reads_id', + full_refresh = false, + tags = ['curated'] +) }} + +WITH service_contracts AS ( + + SELECT + contract_address, + service_id AS registry_id, + MAX(block_number) AS block_number + FROM + {{ ref('silver_olas__service_registrations') }} + +{% if is_incremental() %} +WHERE + _inserted_timestamp >= ( + SELECT + MAX(_inserted_timestamp) - INTERVAL '12 hours' + FROM + {{ this }} + ) + AND CONCAT( + contract_address, + '-', + registry_id + ) NOT IN ( + SELECT + CONCAT( + contract_address, + '-', + function_input + ) + FROM + {{ this }} + ) +{% endif %} +GROUP BY + 1, + 2 +), +function_sigs AS ( + SELECT + '0xc87b56dd' AS function_sig, + 'tokenURI' AS function_name +), +inputs AS ( + SELECT + contract_address, + block_number, + function_sig, + function_name, + registry_id AS function_input, + CONCAT( + function_sig, + LPAD( + SUBSTR(utils.udf_int_to_hex(function_input), 3), + 64, + 0) + ) AS DATA + FROM + service_contracts + JOIN function_sigs + ON 1 = 1 + ), + contract_reads AS ( + SELECT + contract_address, + block_number, + function_sig, + function_name, + function_input, + DATA, + utils.udf_json_rpc_call( + 'eth_call', + [{ 'to': contract_address, 'from': null, 'data': data }, utils.udf_int_to_hex(block_number) ] + ) AS rpc_request, + live.udf_api( + 'POST', + CONCAT( + '{service}', + '/', + '{Authentication}' + ),{}, + rpc_request, + 'Vault/prod/base/quicknode/mainnet' + ) AS read_output, + SYSDATE() AS _inserted_timestamp + FROM + inputs + ) + SELECT + read_output, + read_output :data :id :: STRING AS read_id, + read_output :data :result :: STRING AS read_result, + SPLIT( + read_id, + '-' + ) AS read_id_object, + function_sig, + function_name, + function_input, + DATA, + utils.udf_hex_to_string(SUBSTR(read_result, 131)) AS token_uri_link, + contract_address, + block_number, + _inserted_timestamp, + {{ dbt_utils.generate_surrogate_key( + ['contract_address','function_input'] + ) }} AS registry_reads_id, + SYSDATE() AS inserted_timestamp, + SYSDATE() AS modified_timestamp, + '{{ invocation_id }}' AS _invocation_id + FROM + contract_reads + WHERE + token_uri_link IS NOT NULL + AND LENGTH(token_uri_link) <> 0 diff --git a/models/silver/protocols/olas/metadata/silver_olas__registry_reads.yml b/models/silver/protocols/olas/metadata/silver_olas__registry_reads.yml new file mode 100644 index 00000000..09e3858b --- /dev/null +++ b/models/silver/protocols/olas/metadata/silver_olas__registry_reads.yml @@ -0,0 +1,19 @@ +version: 2 +models: + - name: silver_olas__registry_reads + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - REGISTRY_READS_ID + columns: + - name: BLOCK_NUMBER + tests: + - not_null + - name: CONTRACT_ADDRESS + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: 0[xX][0-9a-fA-F]+ + - name: TOKEN_URI_LINK + tests: + - not_null \ No newline at end of file diff --git a/models/silver/protocols/olas/silver_olas__create_service_multisigs.sql b/models/silver/protocols/olas/silver_olas__create_service_multisigs.sql new file mode 100644 index 00000000..8f848528 --- /dev/null +++ b/models/silver/protocols/olas/silver_olas__create_service_multisigs.sql @@ -0,0 +1,53 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = 'block_number', + cluster_by = ['block_timestamp::DATE'], + tags = ['curated','reorg'] +) }} + +SELECT + block_number, + block_timestamp, + tx_hash, + origin_function_signature, + origin_from_address, + origin_to_address, + contract_address, + event_index, + topics [0] :: STRING AS topic_0, + topics [1] :: STRING AS topic_1, + topics [2] :: STRING AS topic_2, + topics [3] :: STRING AS topic_3, + 'CreateMultisigWithAgents' AS event_name, + DATA, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + TRY_TO_NUMBER( + utils.udf_hex_to_int( + topic_1 + ) + ) AS id, + CONCAT('0x', SUBSTR(topic_2, 27, 40)) AS multisig_address, + _log_id, + _inserted_timestamp, + {{ dbt_utils.generate_surrogate_key( + ['tx_hash','event_index'] + ) }} AS create_service_multisigs_id, + SYSDATE() AS inserted_timestamp, + SYSDATE() AS modified_timestamp, + '{{ invocation_id }}' AS _invocation_id +FROM + {{ ref('silver__logs') }} +WHERE + contract_address = '0x3c1ff68f5aa342d296d4dee4bb1cacca912d95fe' --Service Registry (AUTONOLAS-SERVICE-V1) + AND topic_0 = '0x2d53f895cd5faf3cddba94a25c2ced2105885b5b37450ff430ffa3cbdf332c74' --CreateMultisigWithAgents + AND tx_status = 'SUCCESS' + +{% if is_incremental() %} +AND _inserted_timestamp >= ( + SELECT + MAX(_inserted_timestamp) - INTERVAL '12 hours' + FROM + {{ this }} +) +{% endif %} diff --git a/models/silver/protocols/olas/silver_olas__create_service_multisigs.yml b/models/silver/protocols/olas/silver_olas__create_service_multisigs.yml new file mode 100644 index 00000000..8a53dda2 --- /dev/null +++ b/models/silver/protocols/olas/silver_olas__create_service_multisigs.yml @@ -0,0 +1,24 @@ +version: 2 +models: + - name: silver_olas__create_service_multisigs + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - _LOG_ID + columns: + - name: BLOCK_NUMBER + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: TX_HASH + tests: + - not_null + - name: MULTISIG_ADDRESS + tests: + - dbt_expectations.expect_column_values_to_match_regex: + regex: 0[xX][0-9a-fA-F]+ + - name: ID + tests: + - not_null diff --git a/models/silver/protocols/olas/silver_olas__service_event_logs.sql b/models/silver/protocols/olas/silver_olas__service_event_logs.sql new file mode 100644 index 00000000..23102e7b --- /dev/null +++ b/models/silver/protocols/olas/silver_olas__service_event_logs.sql @@ -0,0 +1,57 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = 'block_number', + cluster_by = ['block_timestamp::DATE'], + tags = ['curated','reorg'] +) }} + +WITH service_multisigs AS ( + + SELECT + DISTINCT multisig_address, + id + FROM + {{ ref('silver_olas__create_service_multisigs') }} +) +SELECT + d.block_number, + d.block_timestamp, + d.tx_hash, + d.origin_function_signature, + d.origin_from_address, + d.origin_to_address, + d.contract_address, + d.event_index, + s.multisig_address, + s.id AS service_id, + d.topics [0] :: STRING AS topic_0, + d.topics [1] :: STRING AS topic_1, + d.topics [2] :: STRING AS topic_2, + d.topics [3] :: STRING AS topic_3, + d.data, + regexp_substr_all(SUBSTR(d.data, 3, len(d.data)), '.{64}') AS segmented_data, + d._log_id, + d._inserted_timestamp, + {{ dbt_utils.generate_surrogate_key( + ['d.tx_hash','d.event_index'] + ) }} AS service_event_logs_id, + SYSDATE() AS inserted_timestamp, + SYSDATE() AS modified_timestamp, + '{{ invocation_id }}' AS _invocation_id +FROM + {{ ref('silver__logs') }} + d + INNER JOIN service_multisigs s + ON d.origin_to_address = s.multisig_address +WHERE + d.tx_status = 'SUCCESS' + +{% if is_incremental() %} +AND d._inserted_timestamp >= ( + SELECT + MAX(_inserted_timestamp) - INTERVAL '12 hours' + FROM + {{ this }} +) +{% endif %} diff --git a/models/silver/protocols/olas/silver_olas__service_event_logs.yml b/models/silver/protocols/olas/silver_olas__service_event_logs.yml new file mode 100644 index 00000000..186c61c0 --- /dev/null +++ b/models/silver/protocols/olas/silver_olas__service_event_logs.yml @@ -0,0 +1,28 @@ +version: 2 +models: + - name: silver_olas__service_event_logs + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - _LOG_ID + columns: + - name: BLOCK_NUMBER + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: TX_HASH + tests: + - not_null + - name: MULTISIG_ADDRESS + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: 0[xX][0-9a-fA-F]+ + - name: SERVICE_ID + tests: + - not_null + - name: DATA + tests: + - not_null \ No newline at end of file diff --git a/models/silver/protocols/olas/silver_olas__service_registrations.sql b/models/silver/protocols/olas/silver_olas__service_registrations.sql new file mode 100644 index 00000000..87e17514 --- /dev/null +++ b/models/silver/protocols/olas/silver_olas__service_registrations.sql @@ -0,0 +1,158 @@ +{{ config( + materialized = 'incremental', + incremental_strategy = 'delete+insert', + unique_key = 'block_number', + cluster_by = ['block_timestamp::DATE'], + tags = ['curated','reorg'] +) }} + +WITH registry_evt AS ( + + SELECT + block_number, + block_timestamp, + tx_hash, + origin_function_signature, + origin_from_address, + origin_to_address, + contract_address, + event_index, + topics [0] :: STRING AS topic_0, + topics [1] :: STRING AS topic_1, + topics [2] :: STRING AS topic_2, + topics [3] :: STRING AS topic_3, + CASE + WHEN topic_0 = '0xb34c1e02384201736eb4693b9b173306cb41bff12f15894dea5773088e9a3b1c' THEN 'CreateService' + WHEN topic_0 = '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef' THEN 'Transfer' + END AS event_name, + DATA, + regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data, + _log_id, + _inserted_timestamp + FROM + {{ ref('silver__logs') }} + WHERE + contract_address = '0x3c1ff68f5aa342d296d4dee4bb1cacca912d95fe' --Service Registry (AUTONOLAS-SERVICE-V1) + AND topic_0 IN ( + '0xb34c1e02384201736eb4693b9b173306cb41bff12f15894dea5773088e9a3b1c', + --CreateService (for services) + '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef' --Transfer + ) + AND tx_status = 'SUCCESS' + +{% if is_incremental() %} +AND _inserted_timestamp >= ( + SELECT + MAX(_inserted_timestamp) - INTERVAL '12 hours' + FROM + {{ this }} +) +{% endif %} +), +transfers AS ( + SELECT + block_number, + block_timestamp, + tx_hash, + origin_function_signature, + origin_from_address, + origin_to_address, + contract_address, + event_index, + topic_0, + topic_1, + topic_2, + topic_3, + event_name, + DATA, + segmented_data, + CONCAT('0x', SUBSTR(topic_1, 27, 40)) AS from_address, + CONCAT('0x', SUBSTR(topic_2, 27, 40)) AS to_address, + TRY_TO_NUMBER( + utils.udf_hex_to_int( + topic_3 + ) + ) AS id, + _log_id, + _inserted_timestamp + FROM + registry_evt + WHERE + event_name = 'Transfer' +), +multisigs AS ( + SELECT + DISTINCT multisig_address, + id, + contract_address + FROM + {{ ref('silver_olas__create_service_multisigs') }} + qualify(ROW_NUMBER() over (PARTITION BY multisig_address + ORDER BY + block_timestamp DESC)) = 1 --get latest service multisig address +), +services AS ( + SELECT + r.block_number, + r.block_timestamp, + r.tx_hash, + r.origin_function_signature, + r.origin_from_address, + r.origin_to_address, + r.contract_address, + r.event_index, + r.topic_0, + r.topic_1, + r.topic_2, + r.topic_3, + r.event_name, + r.data, + r.segmented_data, + TRY_TO_NUMBER(utils.udf_hex_to_int(r.topic_1)) AS service_id, + CONCAT( + '0x', + r.segmented_data [0] :: STRING + ) AS config_hash, + t.from_address, + t.to_address AS owner_address, + m.multisig_address, + r._log_id, + r._inserted_timestamp + FROM + registry_evt r + LEFT JOIN transfers t + ON r.tx_hash = t.tx_hash + AND r.contract_address = t.contract_address + AND service_id = t.id + LEFT JOIN multisigs m + ON r.contract_address = m.contract_address + AND service_id = m.id + WHERE + r.event_name = 'CreateService' +) +SELECT + block_number, + block_timestamp, + tx_hash, + origin_function_signature, + origin_from_address, + origin_to_address, + contract_address, + event_index, + event_name, + owner_address, + multisig_address, + service_id, + config_hash, + _log_id, + _inserted_timestamp, + {{ dbt_utils.generate_surrogate_key( + ['tx_hash','event_index'] + ) }} AS service_registration_id, + SYSDATE() AS inserted_timestamp, + SYSDATE() AS modified_timestamp, + '{{ invocation_id }}' AS _invocation_id +FROM + services qualify(ROW_NUMBER() over (PARTITION BY _log_id +ORDER BY + _inserted_timestamp DESC)) = 1 diff --git a/models/silver/protocols/olas/silver_olas__service_registrations.yml b/models/silver/protocols/olas/silver_olas__service_registrations.yml new file mode 100644 index 00000000..6967f37c --- /dev/null +++ b/models/silver/protocols/olas/silver_olas__service_registrations.yml @@ -0,0 +1,29 @@ +version: 2 +models: + - name: silver_olas__service_registrations + tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - _LOG_ID + columns: + - name: BLOCK_NUMBER + tests: + - not_null + - name: BLOCK_TIMESTAMP + tests: + - not_null + - name: TX_HASH + tests: + - not_null + - name: OWNER_ADDRESS + tests: + - not_null + - dbt_expectations.expect_column_values_to_match_regex: + regex: 0[xX][0-9a-fA-F]+ + - name: MULTISIG_ADDRESS + tests: + - dbt_expectations.expect_column_values_to_match_regex: + regex: 0[xX][0-9a-fA-F]+ + - name: SERVICE_ID + tests: + - not_null \ No newline at end of file