WITH all_first_appearance AS (
SELECT
tx_from,
MIN(block_timestamp) AS first_timestamp
FROM
solana.core.fact_transfers
GROUP BY
tx_from
),
period_first_appearance AS (
SELECT
aft.tx_from,
aft.first_timestamp,
ft.tx_to,
date_trunc('{{granularity}}', ft.block_timestamp) AS wk
FROM
all_first_appearance aft
JOIN
solana.core.fact_transfers ft ON aft.tx_from = ft.tx_from AND aft.first_timestamp = ft.block_timestamp
WHERE
ft.block_timestamp > current_date() - interval '{{periods}} {{granularity}}'
),
filtered_labels AS (
SELECT
pfa.tx_from,
pfa.tx_to,
pfa.wk,
dl.label_type
FROM
period_first_appearance pfa
JOIN
solana.core.dim_labels dl ON pfa.tx_to = dl.address
LEFT JOIN
solana.core.dim_labels dl2 ON pfa.tx_from = dl2.address
WHERE
dl.label_type IN ('defi', 'dex', 'nft')