gnosis-models/models/silver/defi/dex/honeyswap/silver_dex__honeyswap_pools.sql
drethereum d145a43b0e
AN-4280/heal-logic (#148)
* vars, readme, tx_success

* defi heal logic

* lookbacks on lps

* var format

* heal logic

* dex swaps heal

* pool name

* column names
2024-05-29 11:02:18 -06:00

57 lines
1.5 KiB
SQL

{{ config(
materialized = 'incremental',
incremental_strategy = 'delete+insert',
unique_key = 'pool_address',
tags = ['curated']
) }}
WITH pool_creation AS (
SELECT
block_number,
block_timestamp,
tx_hash,
event_index,
contract_address,
regexp_substr_all(SUBSTR(DATA, 3, len(DATA)), '.{64}') AS segmented_data,
CONCAT('0x', SUBSTR(topics [1] :: STRING, 27, 40)) AS token0,
CONCAT('0x', SUBSTR(topics [2] :: STRING, 27, 40)) AS token1,
CONCAT('0x', SUBSTR(segmented_data [0] :: STRING, 25, 40)) AS pool_address,
utils.udf_hex_to_int(
segmented_data [1] :: STRING
) :: INT AS pool_id,
_log_id,
_inserted_timestamp
FROM
{{ ref ('silver__logs') }}
WHERE
contract_address = LOWER('0xA818b4F111Ccac7AA31D0BCc0806d64F2E0737D7')
AND topics [0] :: STRING = '0x0d3648bd0f6ba80134a33ba9275ac585d9d315f0ad8355cddefde31afa28d0e9' --PairCreated
AND tx_status = 'SUCCESS'
{% if is_incremental() %}
AND _inserted_timestamp >= (
SELECT
MAX(_inserted_timestamp) - INTERVAL '12 hours'
FROM
{{ this }}
)
{% endif %}
)
SELECT
block_number,
block_timestamp,
tx_hash,
event_index,
contract_address,
token0,
token1,
pool_address,
pool_id,
_log_id,
_inserted_timestamp
FROM
pool_creation qualify(ROW_NUMBER() over (PARTITION BY pool_address
ORDER BY
_inserted_timestamp DESC)) = 1