hrst79dexalot main copy
    Updated 2023-07-12

    with
    price as (
    SELECT
    hour::date as date,
    symbol,
    decimals,
    TOKEN_ADDRESS,
    avg(price) as price
    from avalanche.core.fact_hourly_token_prices
    group by 1,2,3,4)

    select
    date_trunc('week', block_timestamp) as date,
    case when from_address = address then 'outflow'
    when to_address =address then 'inflow'
    else 'other' end as label,
    symbol,
    count(DISTINCT tx_hash) as txs,
    count(DISTINCT origin_from_address) as user,

    sum(case when label = 'inflow' then raw_amount*price/pow(10,decimals) else 0 end) as inflow_usd_volume,
    sum(case when label = 'outflow' then raw_amount*price/pow(10,decimals) else 0 end) as outflow_usd_volume,

    inflow_usd_volume - outflow_usd_volume as netflow_usd


    from avalanche.core.fact_token_transfers a
    join avalanche.core.dim_labels b on (a.from_address = b.address or a.to_address = b.address)
    left join price p on a.block_timestamp::date=p.date and a.contract_address=p.TOKEN_ADDRESS
    WHERE project_name='dexalot'
    and symbol is not null
    and date>= current_date - 180
    group by 1,2,3,p.date