flow-models/models/silver/silver__swaps_events.sql
Jack Forgash b9d6ef2823
AN-2310/rewrite swaps (#82)
* add trades and swaps

* revert old swaps model and branch into new

* swaps v2 still some bugs

* dup token0 issue

* swaps2

* final

* tweak

* del todo

* incr
2022-11-08 11:07:42 -07:00

65 lines
965 B
SQL

{{ config(
materialized = 'incremental',
cluster_by = ['_inserted_timestamp::DATE'],
unique_key = "CONCAT_WS('-', tx_id, event_index)",
incremental_strategy = 'delete+insert'
) }}
WITH swap_contracts AS (
SELECT
*
FROM
{{ ref('silver__contract_labels') }}
WHERE
contract_name LIKE '%SwapPair%'
),
swaps_txs AS (
SELECT
*
FROM
{{ ref('silver__events_final') }}
WHERE
event_contract IN (
SELECT
event_contract
FROM
swap_contracts
)
{% if is_incremental() %}
AND _inserted_timestamp >= (
SELECT
MAX(_inserted_timestamp)
FROM
{{ this }}
)
{% endif %}
),
swap_events AS (
SELECT
*
FROM
{{ ref('silver__events_final') }}
WHERE
tx_id IN (
SELECT
tx_id
FROM
swaps_txs
)
{% if is_incremental() %}
AND _inserted_timestamp >= (
SELECT
MAX(_inserted_timestamp)
FROM
{{ this }}
)
{% endif %}
)
SELECT
*
FROM
swap_events