From ee157e4053302518cef4ea62a504b12012360e9c Mon Sep 17 00:00:00 2001 From: soispoke <66172107+soispoke@users.noreply.github.com> Date: Mon, 18 Jul 2022 15:42:13 +0700 Subject: [PATCH] Fix uniqueness issue looksrare/opensea trades (#1294) Brief comments on the purpose of your changes: *For Dune Engine V2* I've checked that: * [ ] I tested the query on dune.com after compiling the model with dbt compile (compiled queries are written to the target directory) * [ ] I used "refs" to reference other models in this repo and "sources" to reference raw or decoded tables * [ ] if adding a new model, I added a test * [ ] the filename is unique and ends with .sql * [ ] each sql file is a select statement and has only one view, table or function defined * [ ] column names are `lowercase_snake_cased` When you are ready for a review, tag duneanalytics/data-experience. We will re-open your forked pull request as an internal pull request. Then your spells will run in dbt and the logs will be avaiable in Github Actions DBT Slim CI. This job will only run the models and tests changed by your PR compared to the production project. --- .../ethereum/looksrare_ethereum_events.sql | 13 +++++++------ .../opensea/ethereum/opensea_v1_ethereum_events.sql | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/spellbook/models/looksrare/ethereum/looksrare_ethereum_events.sql b/spellbook/models/looksrare/ethereum/looksrare_ethereum_events.sql index 466f14783fd..7a09d05ab76 100644 --- a/spellbook/models/looksrare/ethereum/looksrare_ethereum_events.sql +++ b/spellbook/models/looksrare/ethereum/looksrare_ethereum_events.sql @@ -8,7 +8,7 @@ }} WITH looks_rare AS ( - SELECT + SELECT ask.evt_block_time AS block_time, ask.tokenId::string AS token_id, ask.amount AS number_of_items, @@ -118,7 +118,7 @@ SELECT DISTINCT WHEN agg.name is NULL AND erc.value_unique is NULL AND erc.count_erc > 1 THEN erc.count_erc WHEN tokens.standard = 'erc1155' THEN erc.value_unique WHEN tokens.standard = 'erc721' THEN erc.count_erc - ELSE (SELECT + ELSE COALESCE((SELECT count(1)::bigint cnt FROM {{ source('erc721_ethereum','evt_transfer') }} erc721 WHERE erc721.evt_tx_hash = tx_hash @@ -127,9 +127,9 @@ SELECT DISTINCT count(1)::bigint cnt FROM {{ source('erc1155_ethereum','evt_transfersingle') }} erc1155 WHERE erc1155.evt_tx_hash = tx_hash - ) END AS number_of_items, + ), 0) END AS number_of_items, looks_rare.category as trade_category, - evt_type, + CASE WHEN evt_type is NULL THEN 'Other' ELSE evt_type END as evt_type, seller, buyer, looks_rare.price / power(10,erc20.decimals) AS amount_original, @@ -153,7 +153,7 @@ SELECT DISTINCT royalty_fee / looks_rare.price * 100 as royalty_fee_percentage, royalty_fee_receive_address, royalty_fee_currency_symbol, - tx_hash || '-' || token_id || '-' || seller || '-' || looks_rare.evt_index::string || '-' || evt_type as unique_trade_id + 'looksrare' || '-' || tx_hash || '-' || token_id::string || '-' || seller::string || '-' || COALESCE(erc.contract_address, nft_contract_address) || '-' || looks_rare.evt_index::string || '-' || COALESCE(evt_type::string, 'Other') || '-' || COALESCE(case when erc.value_unique::string is null then '0' ELSE '1' end, '1') as unique_trade_id FROM looks_rare INNER JOIN {{ source('ethereum','transactions') }} tx ON tx_hash = tx.hash AND tx.block_time > '2022-01-01' @@ -164,7 +164,8 @@ LEFT JOIN {{ source('prices', 'usd') }} p ON p.minute = date_trunc('minute', loo AND p.contract_address = currency_contract AND p.blockchain ='ethereum' LEFT JOIN {{ ref('tokens_ethereum_erc20') }} erc20 ON erc20.contract_address = currency_contract +WHERE number_of_items >= 1 {% if is_incremental() %} -- this filter will only be applied on an incremental run -WHERE looks_rare.block_time >= (select max(block_time) from {{ this }}) +AND looks_rare.block_time >= (select max(block_time) from {{ this }}) {% endif %} \ No newline at end of file diff --git a/spellbook/models/opensea/ethereum/opensea_v1_ethereum_events.sql b/spellbook/models/opensea/ethereum/opensea_v1_ethereum_events.sql index 2986fa084a4..7942268dc77 100644 --- a/spellbook/models/opensea/ethereum/opensea_v1_ethereum_events.sql +++ b/spellbook/models/opensea/ethereum/opensea_v1_ethereum_events.sql @@ -158,7 +158,7 @@ SELECT DISTINCT (wa.fees / wa.amount_original * 100)::string AS royalty_fee_percentage, wa.fee_receive_address as royalty_fee_receive_address, wa.fee_currency_symbol as royalty_fee_currency_symbol, - 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 + '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 LEFT JOIN erc_transfers ON erc_transfers.evt_tx_hash = wa.call_tx_hash AND (wa.token_id = erc_transfers.token_id_erc