From 833829f3561da51400a0ffd7cc5ebd785ac83911 Mon Sep 17 00:00:00 2001 From: Austin Date: Mon, 2 Dec 2024 12:44:10 -0500 Subject: [PATCH] fix --- .../evm/silver/core/silver_evm__traces2.sql | 155 +++++++++++++++++- 1 file changed, 146 insertions(+), 9 deletions(-) diff --git a/models/evm/silver/core/silver_evm__traces2.sql b/models/evm/silver/core/silver_evm__traces2.sql index be2b551..4fa83a6 100644 --- a/models/evm/silver/core/silver_evm__traces2.sql +++ b/models/evm/silver/core/silver_evm__traces2.sql @@ -6,13 +6,150 @@ incremental_predicates = [fsc_evm.standard_predicate()], cluster_by = ['modified_timestamp::DATE','partition_key'], full_refresh = false, - tags = ['core'], - enabled = false -) }} -{{ fsc_evm.silver_traces_v1( - full_reload_start_block = 80000000, - full_reload_blocks = 5000000, - use_partition_key = true, - schema_name = 'bronze_evm', - sei_traces_mode = true + tags = ['core'] ) }} + +{% if execute %} + {% set max_inserted_query %} + + SELECT + DATEADD('minute', -5, MAX(_inserted_timestamp)) + FROM + {{ this }} + + {% endset %} + {% set max_ins = run_query(max_inserted_query) [0] [0] %} + {% if not max_ins or max_ins == 'None' %} + {% set max_ins = '2099-01-01' %} + {% endif %} + + {% set query_blocks %} + CREATE + OR REPLACE temporary TABLE silver_evm.silver_evm__traces2_intermediate_tmp AS + SELECT + block_number, + partition_key, + VALUE :array_index :: INT AS tx_position, + DATA :result AS full_traces, + DATA :txHash :: STRING AS tx_hash, + _inserted_timestamp + FROM + +{% if is_incremental() %} +{{ ref('bronze_evm__streamline_traces') }} +{% else %} + {{ ref('bronze_evm__streamline_fr_traces') }} +{% endif %} + +where DATA :result IS NOT NULL + +{% if is_incremental() %} +and _inserted_timestamp >= '{{max_ins}}' +{% endif %} + +qualify(ROW_NUMBER() over (PARTITION BY block_number, tx_position, tx_hash ORDER BY _inserted_timestamp DESC)) = 1 + +{% endset %} +{% do run_query( + query_blocks +) %} +{% endif %} + +WITH flatten_traces AS ( + SELECT + block_number, + tx_hash, + partition_key, + IFF( + path IN ( + 'result', + 'result.value', + 'result.type', + 'result.to', + 'result.input', + 'result.gasUsed', + 'result.gas', + 'result.from', + 'result.output', + 'result.error', + 'result.revertReason', + 'result.time', + 'gasUsed', + 'gas', + 'type', + 'to', + 'from', + 'value', + 'input', + 'error', + 'output', + 'time', + 'revertReason' + ), + 'ORIGIN', + REGEXP_REPLACE(REGEXP_REPLACE(path, '[^0-9]+', '_'), '^_|_$', '') + ) AS trace_address, + _inserted_timestamp, + OBJECT_AGG( + key, + VALUE + ) AS trace_json, + CASE + WHEN trace_address = 'ORIGIN' THEN NULL + WHEN POSITION( + '_' IN trace_address + ) = 0 THEN 'ORIGIN' + ELSE REGEXP_REPLACE( + trace_address, + '_[0-9]+$', + '', + 1, + 1 + ) + END AS parent_trace_address, + SPLIT( + trace_address, + '_' + ) AS trace_address_array + FROM + silver_evm.silver_evm__traces2_intermediate_tmp txs, + TABLE( + FLATTEN( + input => PARSE_JSON( + txs.full_traces + ), + recursive => TRUE + ) + ) f + WHERE + f.index IS NULL + AND f.key != 'calls' + AND f.path != 'result' + GROUP BY + block_number, + tx_hash, + partition_key, + trace_address, + _inserted_timestamp +) +SELECT + block_number, + tx_hash, + trace_address, + parent_trace_address, + trace_address_array, + trace_json, + partition_key, + _inserted_timestamp, + {{ dbt_utils.generate_surrogate_key( + ['block_number'] + + ['tx_hash'] + + ['trace_address'] + ) }} AS traces_id, + SYSDATE() AS inserted_timestamp, + SYSDATE() AS modified_timestamp, + '{{ invocation_id }}' AS _invocation_id +FROM + flatten_traces qualify(ROW_NUMBER() over(PARTITION BY traces_id +ORDER BY + _inserted_timestamp DESC)) = 1 \ No newline at end of file