diff --git a/models/projects/metaplex/core/ez_metaplex_metrics.sql b/models/projects/metaplex/core/ez_metaplex_metrics.sql index e1666f5f..8d7b7d51 100644 --- a/models/projects/metaplex/core/ez_metaplex_metrics.sql +++ b/models/projects/metaplex/core/ez_metaplex_metrics.sql @@ -23,10 +23,9 @@ with date_spine as ( , buybacks as ( select date - , sum(buyback_usd) * -1 as buyback - , sum(buyback_native) * -1 as buyback_native + , buyback + , buyback_native from {{ ref("fact_metaplex_buybacks") }} - group by 1 ) , active_wallets as ( select diff --git a/models/staging/metaplex/fact_metaplex_buybacks.sql b/models/staging/metaplex/fact_metaplex_buybacks.sql index 58af2660..174ab6d9 100644 --- a/models/staging/metaplex/fact_metaplex_buybacks.sql +++ b/models/staging/metaplex/fact_metaplex_buybacks.sql @@ -3,47 +3,32 @@ snowflake_warehouse="METAPLEX" ) }} -WITH daily_balances AS ( - SELECT - DATE_TRUNC('day', block_timestamp) AS date, - account_address, - mint, - MAX(balance) AS ending_balance - FROM - {{ source('SOLANA_FLIPSIDE', 'fact_token_balances') }} - WHERE - owner = 'E7Hzc1cQwx5BgJa8hJGVuDF2G2f2penLrhiKU6nU53gK' - AND mint = 'METAewgxyPbgwsseH8T16a39CQ5VyVxZi9zXiDPY18m' - AND succeeded = TRUE - {% if is_incremental() %} - AND block_timestamp > (SELECT MAX(date) FROM {{ this }}) - {% endif %} - GROUP BY - DATE_TRUNC('day', block_timestamp), - account_address, - mint +WITH +buybacks AS ( + select + date_trunc('day', block_timestamp) as date + , amount + , mint as token_address + from solana_flipside.core.fact_transfers + where tx_to = 'E7Hzc1cQwx5BgJa8hJGVuDF2G2f2penLrhiKU6nU53gK' and tx_from = 'BBcPaj5v95nFFbXfgTebYyJDSY5HBCpARuRCLynVWimp' + and mint = 'METAewgxyPbgwsseH8T16a39CQ5VyVxZi9zXiDPY18m' + {% if is_incremental() %} + and block_timestamp >= (select dateadd('day', -5, max(date)) from {{ this }}) + {% else %} + and block_timestamp >= '2024-06-26' + {% endif %} ) , prices AS ( - SELECT - date(hour) AS date, - symbol, - avg(price) as price - FROM - {{source('SOLANA_FLIPSIDE_PRICE', 'ez_prices_hourly')}} - WHERE token_address = 'METAewgxyPbgwsseH8T16a39CQ5VyVxZi9zXiDPY18m' - GROUP BY 1, 2 + {{ get_coingecko_price_with_latest('metaplex') }} ) - SELECT b.date, - p.symbol, - b.ending_balance, - b.ending_balance - LAG(b.ending_balance) OVER (ORDER BY b.date DESC) AS buyback_native, - buyback_native * p.price AS buyback_usd + b.amount as buyback_native, + b.amount * p.price AS buyback FROM - daily_balances b + buybacks b LEFT JOIN prices p ON b.date = p.date ORDER BY - b.date DESC \ No newline at end of file + b.date DESC