CryptoLionCopy of UST dominance
    Updated 2021-07-24
    WITH init as
    (SELECT
    date_trunc('day',terra.swaps.block_timestamp) as day,
    -- sum(msg_value:execute_msg:swap:offer_asset:amount / POW(10,6)) as swap_amount,
    -- msg_value:from_contract as swap_currency,
    sum(CASE WHEN offer_currency = 'UST' THEN offer_amount_usd ELSE 0 END) as swap_amount_UST,
    sum(CASE WHEN offer_currency <> 'UST' THEN offer_amount_usd ELSE 0 END) as swap_amount_other,
    -- swap_amount_UST/(swap_amount_UST + swap_amount_other) as percentage_UST,
    -- c.address_name as contract_label,
    count(terra.swaps.trader) as swaps,
    count(distinct terra.swaps.trader) as distinct_traders
    FROM terra.swaps
    INNER JOIN terra.msgs on terra.swaps.tx_id = terra.msgs.tx_id
    INNER JOIN terra.labels c
    ON msg_value:contract::string = c.address

    WHERE
    -- msg_value:execute_msg:swap IS NOT NULL
    terra.swaps.block_timestamp >= CURRENT_DATE - 90
    AND terra.swaps.tx_status = 'SUCCEEDED'
    AND offer_amount_usd > 0
    AND c.address_name LIKE 'Terraswap%'
    GROUP BY 1
    HAVING swap_amount_UST IS NOT NULL
    AND swap_amount_other IS NOT NULL
    ORDER BY 1 DESC)

    SELECT
    day,
    swap_amount_UST/(swap_amount_UST+swap_amount_other)*100 as percentage_ust,
    swaps,
    distinct_traders
    FROM init
    ORDER BY 1 DESC
    Run a query to Download Data