|
| 1 | +{{ config( |
| 2 | + alias = 'events', |
| 3 | + partition_by = ['block_date'], |
| 4 | + materialized = 'incremental', |
| 5 | + file_format = 'delta', |
| 6 | + incremental_strategy = 'merge', |
| 7 | + unique_key = ['block_date', 'unique_trade_id'] |
| 8 | + ) |
| 9 | +}} |
| 10 | + |
| 11 | +WITH |
| 12 | + trade_events as ( |
| 13 | + SELECT |
| 14 | + contract_address as project_contract_address |
| 15 | + , evt_block_number as block_number |
| 16 | + , evt_block_time as block_time |
| 17 | + , evt_tx_hash as tx_hash |
| 18 | + , buyer |
| 19 | + , seller |
| 20 | + , cost as amount_raw |
| 21 | + , currency as currency_contract |
| 22 | + , tradeId as unique_trade_id |
| 23 | + FROM {{ source('archipelago_ethereum','ArchipelagoMarket_evt_Trade') }} |
| 24 | + {% if is_incremental() %} |
| 25 | + WHERE evt_block_time >= date_trunc("day", now() - interval '1 week') |
| 26 | + {% endif %} |
| 27 | + {% if not is_incremental() %} |
| 28 | + WHERE evt_block_time >= '2022-6-20' |
| 29 | + {% endif %} |
| 30 | + |
| 31 | + ), |
| 32 | + |
| 33 | + token_events as ( |
| 34 | + SELECT |
| 35 | + evt_block_number as block_number |
| 36 | + , evt_block_time as block_time |
| 37 | + , evt_tx_hash as tx_hash |
| 38 | + , tokenAddress as nft_contract_address |
| 39 | + , tokenId as token_id |
| 40 | + , tradeId as unique_trade_id |
| 41 | + FROM {{ source('archipelago_ethereum','ArchipelagoMarket_evt_TokenTrade') }} |
| 42 | + {% if is_incremental() %} |
| 43 | + WHERE evt_block_time >= date_trunc("day", now() - interval '1 week') |
| 44 | + {% endif %} |
| 45 | + {% if not is_incremental() %} |
| 46 | + WHERE evt_block_time >= '2022-6-20' |
| 47 | + {% endif %} |
| 48 | + |
| 49 | + ), |
| 50 | + |
| 51 | + fee_events as ( |
| 52 | + SELECT |
| 53 | + evt_block_number as block_number |
| 54 | + , evt_block_time as block_time |
| 55 | + , evt_tx_hash as tx_hash |
| 56 | + , amount as fee_amount_raw |
| 57 | + , currency as fee_currency_contract |
| 58 | + , recipient as fee_receive_address |
| 59 | + , micros / pow(10,4) as fee_percentage |
| 60 | + , tradeId as unique_trade_id |
| 61 | + , case when ( |
| 62 | + upper(recipient) = upper('0xA76456bb6aBC50FB38e17c042026bc27a95C3314') |
| 63 | + or upper(recipient) = upper('0x1fC12C9f68A6B0633Ba5897A40A8e61ed9274dC9') |
| 64 | + ) then true else false end |
| 65 | + as is_protocol_fee |
| 66 | + FROM {{ source('archipelago_ethereum','ArchipelagoMarket_evt_RoyaltyPayment') }} |
| 67 | + {% if is_incremental() %} |
| 68 | + WHERE evt_block_time >= date_trunc("day", now() - interval '1 week') |
| 69 | + {% endif %} |
| 70 | + {% if not is_incremental() %} |
| 71 | + WHERE evt_block_time >= '2022-6-20' |
| 72 | + {% endif %} |
| 73 | + |
| 74 | + ), |
| 75 | + |
| 76 | + tokens_ethereum_nft as ( |
| 77 | + SELECT |
| 78 | + * |
| 79 | + FROM {{ ref('tokens_ethereum_nft') }} |
| 80 | + ), |
| 81 | + |
| 82 | + nft_ethereum_aggregators as ( |
| 83 | + SELECT |
| 84 | + * |
| 85 | + FROM {{ ref('nft_ethereum_aggregators') }} |
| 86 | + ), |
| 87 | + |
| 88 | + -- enrichments |
| 89 | + |
| 90 | + trades_with_nft_and_tx as ( |
| 91 | + select |
| 92 | + e.* |
| 93 | + , t.nft_contract_address |
| 94 | + , t.token_id |
| 95 | + , tx.from as tx_from |
| 96 | + , tx.to as tx_to |
| 97 | + from trade_events e |
| 98 | + inner join token_events t |
| 99 | + ON e.block_number = t.block_number and e.unique_trade_id = t.unique_trade_id |
| 100 | + inner join {{ source('ethereum', 'transactions') }} tx |
| 101 | + ON e.block_number = tx.block_number and e.tx_hash = tx.hash |
| 102 | + {% if is_incremental() %} |
| 103 | + AND tx.block_time >= date_trunc("day", now() - interval '1 week') |
| 104 | + {% endif %} |
| 105 | + {% if not is_incremental() %} |
| 106 | + AND tx.block_time >= '2022-6-20' |
| 107 | + {% endif %} |
| 108 | + ), |
| 109 | + |
| 110 | + platform_fees as ( |
| 111 | + select |
| 112 | + block_number |
| 113 | + ,sum(fee_amount_raw) as platform_fee_amount_raw |
| 114 | + ,sum(fee_percentage) as platform_fee_percentage |
| 115 | + ,unique_trade_id |
| 116 | + from fee_events |
| 117 | + where is_protocol_fee |
| 118 | + group by block_number,unique_trade_id |
| 119 | + ), |
| 120 | + |
| 121 | + royalty_fees as ( |
| 122 | + select |
| 123 | + block_number |
| 124 | + ,sum(fee_amount_raw) as royalty_fee_amount_raw |
| 125 | + ,sum(fee_percentage) as royalty_fee_percentage |
| 126 | + ,null::string as royalty_fee_receive_address -- we have multiple address so have to null this field |
| 127 | + ,unique_trade_id |
| 128 | + from fee_events |
| 129 | + where not is_protocol_fee |
| 130 | + group by block_number,unique_trade_id |
| 131 | + ), |
| 132 | + |
| 133 | + |
| 134 | + trades_with_fees as ( |
| 135 | + select |
| 136 | + t.* |
| 137 | + , pf.platform_fee_amount_raw |
| 138 | + , pf.platform_fee_percentage |
| 139 | + , rf.royalty_fee_amount_raw |
| 140 | + , rf.royalty_fee_percentage |
| 141 | + , rf.royalty_fee_receive_address |
| 142 | + from trades_with_nft_and_tx t |
| 143 | + left join platform_fees pf |
| 144 | + ON t.block_number = pf.block_number and t.unique_trade_id = pf.unique_trade_id |
| 145 | + left join royalty_fees rf |
| 146 | + ON t.block_number = rf.block_number and t.unique_trade_id = rf.unique_trade_id |
| 147 | + |
| 148 | + ), |
| 149 | + |
| 150 | + trades_with_price as ( |
| 151 | + select |
| 152 | + t.* |
| 153 | + , p.symbol as currency_symbol |
| 154 | + , amount_raw/pow(10, p.decimals) as amount_original |
| 155 | + , amount_raw/pow(10, p.decimals)*p.price as amount_usd |
| 156 | + , platform_fee_amount_raw/pow(10, p.decimals) as platform_fee_amount |
| 157 | + , platform_fee_amount_raw/pow(10, p.decimals)*p.price as platform_fee_amount_usd |
| 158 | + , royalty_fee_amount_raw/pow(10, p.decimals) as royalty_fee_amount |
| 159 | + , royalty_fee_amount_raw/pow(10, p.decimals)*p.price as royalty_fee_amount_usd |
| 160 | + , p.symbol as royalty_fee_currency_symbol |
| 161 | + from trades_with_fees t |
| 162 | + left join {{ source('prices', 'usd') }} p ON p.blockchain='ethereum' |
| 163 | + AND p.symbol = 'WETH' -- currently we only have ETH trades |
| 164 | + AND date_trunc('minute', p.minute)=date_trunc('minute', t.block_time) |
| 165 | + {% if is_incremental() %} |
| 166 | + AND p.minute >= date_trunc("day", now() - interval '1 week') |
| 167 | + {% endif %} |
| 168 | + {% if not is_incremental() %} |
| 169 | + AND p.minute >= '2022-4-1' |
| 170 | + {% endif %} |
| 171 | + ), |
| 172 | + |
| 173 | + trades_enhanced as ( |
| 174 | + select |
| 175 | + t.* |
| 176 | + , nft.standard as token_standard |
| 177 | + , nft.name as collection |
| 178 | + , agg.contract_address as aggregator_address |
| 179 | + , agg.name as aggregator_name |
| 180 | + from trades_with_price t |
| 181 | + left join tokens_ethereum_nft nft |
| 182 | + ON nft_contract_address = nft.contract_address |
| 183 | + left join nft_ethereum_aggregators agg |
| 184 | + ON tx_to = agg.contract_address |
| 185 | + ) |
| 186 | + |
| 187 | + |
| 188 | +SELECT |
| 189 | + 'ethereum' as blockchain |
| 190 | + , 'archipelago' as project |
| 191 | + , 'v1' as version |
| 192 | + , TRY_CAST(date_trunc('DAY', block_time) AS date) AS block_date |
| 193 | + , block_time |
| 194 | + , block_number |
| 195 | + , token_id |
| 196 | + , token_standard |
| 197 | + , 1 as number_of_items |
| 198 | + , 'Single Item Trade' as trade_type |
| 199 | + , case when tx_from = seller then 'Sell' else 'Buy' end as trade_category |
| 200 | + , 'Trade' as evt_type |
| 201 | + , seller |
| 202 | + , buyer |
| 203 | + , amount_raw |
| 204 | + , amount_original |
| 205 | + , amount_usd |
| 206 | + , currency_symbol |
| 207 | + , currency_contract |
| 208 | + , project_contract_address |
| 209 | + , nft_contract_address |
| 210 | + , collection |
| 211 | + , tx_hash |
| 212 | + , tx_from |
| 213 | + , tx_to |
| 214 | + , aggregator_address |
| 215 | + , aggregator_name |
| 216 | + , platform_fee_amount |
| 217 | + , platform_fee_amount_raw |
| 218 | + , platform_fee_amount_usd |
| 219 | + , platform_fee_percentage |
| 220 | + , royalty_fee_amount |
| 221 | + , royalty_fee_amount_usd |
| 222 | + , royalty_fee_amount_raw |
| 223 | + , royalty_fee_currency_symbol |
| 224 | + , royalty_fee_receive_address -- null here |
| 225 | + , royalty_fee_percentage |
| 226 | + , unique_trade_id |
| 227 | +from trades_enhanced |
0 commit comments