From dec2182c21d33ac74400f151fae15042ed28e5cf Mon Sep 17 00:00:00 2001 From: Mike Stepanovic Date: Tue, 22 Jul 2025 08:21:13 -0600 Subject: [PATCH] base kittypunch silver models --- .../defi/silver_evm__punchswap_v2_swaps.sql | 108 ++++++++++++++++++ .../defi/silver_evm__punchswap_v3_swaps.sql | 93 +++++++++++++++ .../defi/silver_evm__stablekitty_swaps.sql | 84 ++++++++++++++ 3 files changed, 285 insertions(+) create mode 100644 models/evm/silver/defi/silver_evm__punchswap_v2_swaps.sql create mode 100644 models/evm/silver/defi/silver_evm__punchswap_v3_swaps.sql create mode 100644 models/evm/silver/defi/silver_evm__stablekitty_swaps.sql diff --git a/models/evm/silver/defi/silver_evm__punchswap_v2_swaps.sql b/models/evm/silver/defi/silver_evm__punchswap_v2_swaps.sql new file mode 100644 index 0000000..c4ac699 --- /dev/null +++ b/models/evm/silver/defi/silver_evm__punchswap_v2_swaps.sql @@ -0,0 +1,108 @@ +{{ config( + materialized = 'incremental', + unique_key = 'punchswap_v2_swap_id', + incremental_strategy = 'merge', + merge_exclude_columns = ['inserted_timestamp'], + cluster_by = ['block_timestamp::date', 'modified_timestamp::date'], + tags = ['scheduled_non_core'] +) }} + +WITH transactions AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + FROM_ADDRESS as trader, + TO_ADDRESS as router_address, + ORIGIN_FUNCTION_SIGNATURE as function_sig, + VALUE as native_value, + TX_SUCCEEDED as tx_succeeded + FROM {{ ref('core_evm__fact_transactions') }} + WHERE TO_ADDRESS = '0xf45afe28fd5519d5f8c1d4787a4d5f724c0efa4d' -- PunchSwap V2 Router + AND TX_SUCCEEDED = true + AND ORIGIN_FUNCTION_SIGNATURE IN ( + '0x38ed1739', -- swapExactTokensForTokens + '0x7ff36ab5', -- swapExactETHForTokens + '0x18cbafe5', -- swapExactTokensForETH + '0x8803dbee', -- swapTokensForExactTokens + '0xfb3bdb41' -- swapTokensForExactETH + ) + +{% if is_incremental() %} +AND block_timestamp >= ( + SELECT MAX(block_timestamp) + FROM {{ this }} +) +{% endif %} +), +events AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + contract_address, + event_index, + topic_0, + topic_1, + topic_2, + topic_3, + DATA + FROM {{ ref('core_evm__fact_event_logs') }} + WHERE tx_hash IN (SELECT tx_hash FROM transactions) + AND topic_0 = '0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822' -- Swap event +), +swap_events AS ( + SELECT + e.tx_hash, + e.block_number, + e.block_timestamp, + e.contract_address as pool_address, + e.event_index, + '0x' || SUBSTR(t.trader, 3) as trader, + t.function_sig, + t.native_value, + CASE + WHEN t.function_sig = '0x7ff36ab5' THEN 'ETH_TO_TOKEN' + WHEN t.function_sig = '0x18cbafe5' THEN 'TOKEN_TO_ETH' + ELSE 'TOKEN_TO_TOKEN' + END as swap_type, + utils.udf_hex_to_int(SUBSTR(e.DATA, 1, 64)) as amount0_in, + utils.udf_hex_to_int(SUBSTR(e.DATA, 65, 64)) as amount1_in, + utils.udf_hex_to_int(SUBSTR(e.DATA, 129, 64)) as amount0_out, + utils.udf_hex_to_int(SUBSTR(e.DATA, 193, 64)) as amount1_out + FROM events e + JOIN transactions t ON e.tx_hash = t.tx_hash +) +SELECT + block_number AS block_height, + block_timestamp, + tx_hash AS tx_id, + event_index AS swap_index, + pool_address AS swap_contract, + 'PunchSwap V2' AS platform, + trader, + CASE + WHEN swap_type = 'ETH_TO_TOKEN' THEN native_value + ELSE amount0_in + END AS token_in_amount, + CASE + WHEN swap_type = 'ETH_TO_TOKEN' THEN '0x0000000000000000000000000000000000000000' + WHEN amount0_in > 0 THEN '0x' || SUBSTR(topic_1, 27) + ELSE '0x' || SUBSTR(topic_2, 27) + END AS token_in_contract, + NULL AS token_in_destination, + CASE + WHEN swap_type = 'TOKEN_TO_ETH' THEN native_value + ELSE amount0_out + END AS token_out_amount, + CASE + WHEN swap_type = 'TOKEN_TO_ETH' THEN '0x0000000000000000000000000000000000000000' + WHEN amount0_out > 0 THEN '0x' || SUBSTR(topic_1, 27) + ELSE '0x' || SUBSTR(topic_2, 27) + END AS token_out_contract, + NULL AS token_out_source, + {{ dbt_utils.generate_surrogate_key(['tx_hash', 'event_index']) }} AS punchswap_v2_swap_id, + SYSDATE() AS inserted_timestamp, + SYSDATE() AS modified_timestamp, + '{{ invocation_id }}' AS invocation_id +FROM swap_events diff --git a/models/evm/silver/defi/silver_evm__punchswap_v3_swaps.sql b/models/evm/silver/defi/silver_evm__punchswap_v3_swaps.sql new file mode 100644 index 0000000..35aa3f2 --- /dev/null +++ b/models/evm/silver/defi/silver_evm__punchswap_v3_swaps.sql @@ -0,0 +1,93 @@ +{{ config( + materialized = 'incremental', + unique_key = 'punchswap_v3_swap_id', + incremental_strategy = 'merge', + merge_exclude_columns = ['inserted_timestamp'], + cluster_by = ['block_timestamp::date', 'modified_timestamp::date'], + tags = ['scheduled_non_core'] +) }} + +WITH transactions AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + FROM_ADDRESS as trader, + TO_ADDRESS as router_address, + ORIGIN_FUNCTION_SIGNATURE as function_sig, + VALUE as native_value, + TX_SUCCEEDED as tx_succeeded + FROM {{ ref('core_evm__fact_transactions') }} + WHERE TO_ADDRESS = '0xf331959366032a634c7cacf5852fe01ffdb84af0' -- PunchSwap V3 Factory + AND TX_SUCCEEDED = true + AND ORIGIN_FUNCTION_SIGNATURE IS NOT NULL + +{% if is_incremental() %} +AND block_timestamp >= ( + SELECT MAX(block_timestamp) + FROM {{ this }} +) +{% endif %} +), +events AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + contract_address, + event_index, + topic_0, + topic_1, + topic_2, + topic_3, + DATA + FROM {{ ref('core_evm__fact_event_logs') }} + WHERE tx_hash IN (SELECT tx_hash FROM transactions) + AND topic_0 = '0xc42079f94a6350d7e6235f29174924f928cc2ac818eb64fed8004e115fbcca67' -- Swap event (V3) +), + +swap_events AS ( + SELECT + e.tx_hash, + e.block_number, + e.block_timestamp, + e.contract_address as pool_address, + e.event_index, + '0x' || SUBSTR(t.trader, 3) as trader, + t.function_sig, + t.native_value, + utils.udf_hex_to_int(SUBSTR(e.DATA, 1, 64)) as sender, + utils.udf_hex_to_int(SUBSTR(e.DATA, 65, 64)) as recipient, + utils.udf_hex_to_int(SUBSTR(e.DATA, 129, 64)) as amount0, + utils.udf_hex_to_int(SUBSTR(e.DATA, 193, 64)) as amount1, + utils.udf_hex_to_int(SUBSTR(e.DATA, 257, 64)) as sqrt_price_x96, + utils.udf_hex_to_int(SUBSTR(e.DATA, 321, 64)) as liquidity, + utils.udf_hex_to_int(SUBSTR(e.DATA, 385, 64)) as tick + FROM events e + JOIN transactions t ON e.tx_hash = t.tx_hash +) +SELECT + block_number AS block_height, + block_timestamp, + tx_hash AS tx_id, + event_index AS swap_index, + pool_address AS swap_contract, + 'PunchSwap V3' AS platform, + trader, + CASE + WHEN amount0 > 0 THEN amount0 + ELSE amount1 + END AS token_in_amount, + NULL AS token_in_contract, + NULL AS token_in_destination, + CASE + WHEN amount0 < 0 THEN ABS(amount0) + ELSE ABS(amount1) + END AS token_out_amount, + NULL AS token_out_contract, + NULL AS token_out_source, + {{ dbt_utils.generate_surrogate_key(['tx_hash', 'event_index']) }} AS punchswap_v3_swap_id, + SYSDATE() AS inserted_timestamp, + SYSDATE() AS modified_timestamp, + '{{ invocation_id }}' AS invocation_id +FROM swap_events diff --git a/models/evm/silver/defi/silver_evm__stablekitty_swaps.sql b/models/evm/silver/defi/silver_evm__stablekitty_swaps.sql new file mode 100644 index 0000000..58bfc90 --- /dev/null +++ b/models/evm/silver/defi/silver_evm__stablekitty_swaps.sql @@ -0,0 +1,84 @@ +{{ config( + materialized = 'incremental', + unique_key = 'stablekitty_swap_id', + incremental_strategy = 'merge', + merge_exclude_columns = ['inserted_timestamp'], + cluster_by = ['block_timestamp::date', 'modified_timestamp::date'], + tags = ['scheduled_non_core'] +) }} + +WITH transactions AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + FROM_ADDRESS as trader, + TO_ADDRESS as router_address, + ORIGIN_FUNCTION_SIGNATURE as function_sig, + VALUE as native_value, + TX_SUCCEEDED as tx_succeeded + FROM {{ ref('core_evm__fact_transactions') }} + WHERE TO_ADDRESS = '0x09d35647cedc6725696e330be485ccc0d3385819' -- StableKitty Router + AND TX_SUCCEEDED = true + AND ORIGIN_FUNCTION_SIGNATURE = '0xfd44959c' -- exchange function + +{% if is_incremental() %} +AND block_timestamp >= ( + SELECT MAX(block_timestamp) + FROM {{ this }} +) +{% endif %} +), +events AS ( + SELECT + tx_hash, + block_number, + block_timestamp, + contract_address, + event_index, + topic_0, + topic_1, + topic_2, + topic_3, + DATA + FROM {{ ref('core_evm__fact_event_logs') }} + WHERE tx_hash IN (SELECT tx_hash FROM transactions) + AND topic_0 = '0x8b3e96f2b889fa771c53c981b40daf005f63f637f1869f707052d15a3dd97140' -- TokenExchange event +), +exchange_events AS ( + SELECT + e.tx_hash, + e.block_number, + e.block_timestamp, + e.contract_address as pool_address, + e.event_index, + '0x' || SUBSTR(t.trader, 3) as trader, + t.function_sig, + t.native_value, + utils.udf_hex_to_int(SUBSTR(e.DATA, 1, 64)) as buyer, + utils.udf_hex_to_int(SUBSTR(e.DATA, 65, 64)) as sold_id, + utils.udf_hex_to_int(SUBSTR(e.DATA, 129, 64)) as tokens_sold, + utils.udf_hex_to_int(SUBSTR(e.DATA, 193, 64)) as bought_id, + utils.udf_hex_to_int(SUBSTR(e.DATA, 257, 64)) as tokens_bought + FROM events e + JOIN transactions t ON e.tx_hash = t.tx_hash +) +SELECT + block_number AS block_height, + block_timestamp, + tx_hash AS tx_id, + event_index AS swap_index, + pool_address AS swap_contract, + 'StableKitty' AS platform, + trader, + tokens_sold AS token_in_amount, + CONCAT('0x', LPAD(HEX(sold_id), 40, '0')) AS token_in_contract, + NULL AS token_in_destination, + tokens_bought AS token_out_amount, + CONCAT('0x', LPAD(HEX(bought_id), 40, '0')) AS token_out_contract, + NULL AS token_out_source, + {{ dbt_utils.generate_surrogate_key(['tx_hash', 'event_index']) }} AS stablekitty_swap_id, + SYSDATE() AS inserted_timestamp, + SYSDATE() AS modified_timestamp, + '{{ invocation_id }}' AS invocation_id +FROM exchange_events