From efedfb14c3338fc68f6514e9a9ae8a5fecc3a36d Mon Sep 17 00:00:00 2001 From: Alan Ghobadi Date: Wed, 31 Aug 2022 07:14:00 +0200 Subject: [PATCH] Use inner joins instead of left joins on transactions (#1465) * Use inner joins instead of left joins on transactions * Handle the fees for opensea differently * Change sudoswap join on traces to inner * add incremental filter on wyvern_call_data Co-authored-by: soispoke <66172107+soispoke@users.noreply.github.com> --- .../ethereum/opensea_v1_ethereum_events.sql | 8 ++++++-- .../ethereum/seaport_ethereum_transfers.sql | 8 ++++---- .../ethereum/sudoswap_ethereum_events.sql | 18 +++++++++--------- models/x2y2/ethereum/x2y2_ethereum_events.sql | 8 ++++---- 4 files changed, 23 insertions(+), 19 deletions(-) diff --git a/models/opensea/ethereum/opensea_v1_ethereum_events.sql b/models/opensea/ethereum/opensea_v1_ethereum_events.sql index 24c18ba496a..74ced85625b 100644 --- a/models/opensea/ethereum/opensea_v1_ethereum_events.sql +++ b/models/opensea/ethereum/opensea_v1_ethereum_events.sql @@ -59,6 +59,9 @@ WHERE (addrs[3] = '0x5b3256965e7c3cf26e11fcaf296dfc8807c01073' OR addrs[10] = '0x5b3256965e7c3cf26e11fcaf296dfc8807c01073') AND call_success = true +{% if is_incremental() %} +AND call_block_time >= date_trunc("day", now() - interval '1 week') +{% endif %} ), wyvern_all as @@ -88,7 +91,8 @@ SELECT AND fees.trace_address = wc.call_trace_address {% if is_incremental() %} AND fees.block_time >= date_trunc("day", now() - interval '1 week') - {% endif %} + WHERE wc.call_block_time >= date_trunc("day", now() - interval '1 week') + {% endif %} ), erc_transfers as @@ -177,7 +181,7 @@ SELECT DISTINCT wa.fee_currency_symbol as royalty_fee_currency_symbol, 'opensea' || '-' || wa.call_tx_hash || '-' || coalesce(wa.token_id, token_id_erc, '') || '-' || wa.seller || '-' || coalesce(evt_index::string, '') || '-' || coalesce(wa.call_trace_address::string,'') as unique_trade_id FROM wyvern_all wa -LEFT JOIN {{ source('ethereum','transactions') }} tx ON wa.call_tx_hash = tx.hash +INNER JOIN {{ source('ethereum','transactions') }} tx ON wa.call_tx_hash = tx.hash {% if is_incremental() %} and tx.block_time >= date_trunc("day", now() - interval '1 week') {% endif %} diff --git a/models/seaport/ethereum/seaport_ethereum_transfers.sql b/models/seaport/ethereum/seaport_ethereum_transfers.sql index fdcc32be211..e029ab59efb 100644 --- a/models/seaport/ethereum/seaport_ethereum_transfers.sql +++ b/models/seaport/ethereum/seaport_ethereum_transfers.sql @@ -210,7 +210,7 @@ with p1_call as ( string) as unique_trade_id, a.zone from p1_txn_level a - left join {{ source('ethereum','transactions') }} tx + inner join {{ source('ethereum','transactions') }} tx on tx.hash = a.tx_hash {% if not is_incremental() %} and tx.block_number > 14801608 @@ -415,7 +415,7 @@ with p1_call as ( string) as unique_trade_id, a.zone from p2_transfer_level a - left join {{ source('ethereum','transactions') }} tx + inner join {{ source('ethereum','transactions') }} tx on tx.hash = a.tx_hash {% if not is_incremental() %} and tx.block_number > 14801608 @@ -645,7 +645,7 @@ with p1_call as ( string) as unique_trade_id, a.zone from p3_txn_level a - left join {{ source('ethereum','transactions') }} tx + inner join {{ source('ethereum','transactions') }} tx on tx.hash = a.tx_hash {% if not is_incremental() %} and tx.block_number > 14801608 @@ -850,7 +850,7 @@ with p1_call as ( string) as unique_trade_id, a.zone from p4_transfer_level a - left join {{ source('ethereum','transactions') }} tx + inner join {{ source('ethereum','transactions') }} tx on tx.hash = a.tx_hash {% if not is_incremental() %} and tx.block_number > 14801608 diff --git a/models/sudoswap/ethereum/sudoswap_ethereum_events.sql b/models/sudoswap/ethereum/sudoswap_ethereum_events.sql index b737bf7a93f..395bd581de8 100644 --- a/models/sudoswap/ethereum/sudoswap_ethereum_events.sql +++ b/models/sudoswap/ethereum/sudoswap_ethereum_events.sql @@ -155,7 +155,7 @@ WITH , sb.ownerfee , sb.protocolfee FROM swaps_w_fees sb - LEFT JOIN {{ source('ethereum', 'traces') }} tr + INNER JOIN {{ source('ethereum', 'traces') }} tr ON tr.type = 'call' AND tr.call_type = 'call' AND tr.tx_hash = sb.call_tx_hash @@ -237,6 +237,14 @@ WITH , tx.from as tx_from , tx.to as tx_to FROM swaps_cleaned sc + INNER JOIN {{ source('ethereum', 'transactions') }} tx + ON tx.hash=sc.tx_hash + {% if is_incremental() %} + AND tx.block_time >= date_trunc("day", now() - interval '1 week') + {% endif %} + {% if not is_incremental() %} + AND tx.block_time >= '2022-4-1' + {% endif %} LEFT JOIN {{ source('prices', 'usd') }} pu ON pu.blockchain='ethereum' AND date_trunc('minute', pu.minute)=date_trunc('minute', sc.block_time) AND symbol = 'WETH' @@ -247,14 +255,6 @@ WITH AND pu.minute >= '2022-4-1' {% endif %} --add in `pu.contract_address = sc.currency_address` in the future when ERC20 pairs are added in. - LEFT JOIN {{ source('ethereum', 'transactions') }} tx - ON tx.hash=sc.tx_hash - {% if is_incremental() %} - AND tx.block_time >= date_trunc("day", now() - interval '1 week') - {% endif %} - {% if not is_incremental() %} - AND tx.block_time >= '2022-4-1' - {% endif %} LEFT JOIN nft_ethereum_aggregators agg ON agg.contract_address = tx.to --assumes aggregator is the top level call. Will need to change this to check for agg calls in internal traces later on. LEFT JOIN tokens_ethereum_nft tokens ON nft_contract_address = tokens.contract_address ), diff --git a/models/x2y2/ethereum/x2y2_ethereum_events.sql b/models/x2y2/ethereum/x2y2_ethereum_events.sql index ebf0485f43c..a8fcee7c9f1 100644 --- a/models/x2y2/ethereum/x2y2_ethereum_events.sql +++ b/models/x2y2/ethereum/x2y2_ethereum_events.sql @@ -189,6 +189,10 @@ SELECT 'ethereum' AS blockchain END AS royalty_fee_currency_symbol , 'x2y2-' || txs.tx_hash || '-' || txs.nft_contract_address || txs.token_id || '-' || txs.seller || '-' || txs.evt_index || 'Trade' AS unique_trade_id FROM all_x2y2_txs txs +INNER JOIN {{ source('ethereum','transactions') }} et ON et.hash=txs.tx_hash + {% if is_incremental() %} + AND et.block_time >= date_trunc("day", now() - interval '1 week') + {% endif %} LEFT JOIN {{ source('prices','usd') }} pu ON pu.blockchain='ethereum' AND date_trunc('minute', pu.minute)=date_trunc('minute', txs.block_time) AND (pu.contract_address=txs.currency_contract @@ -196,10 +200,6 @@ LEFT JOIN {{ source('prices','usd') }} pu ON pu.blockchain='ethereum' {% if is_incremental() %} AND pu.minute >= date_trunc("day", now() - interval '1 week') {% endif %} -LEFT JOIN {{ source('ethereum','transactions') }} et ON et.hash=txs.tx_hash - {% if is_incremental() %} - AND et.block_time >= date_trunc("day", now() - interval '1 week') - {% endif %} LEFT JOIN {{ source('erc721_ethereum','evt_transfer') }} erct ON txs.project_contract_address=erct.contract_address AND erct.evt_tx_hash=txs.tx_hash AND erct.tokenId=txs.token_id