Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nft_mints macro #4608

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
190 changes: 190 additions & 0 deletions macros/models/_sector/nft/nft_mints.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
{% macro nft_mints(
blockchain,
src_contracts,
src_traces,
src_transactions,
src_erc20_evt_transfer,
nft_transfers,
nft_aggregators,
tokens_nft,
default_currency_symbol,
default_currency_contract,
addresses_defi = null
)%}

with

namespaces as (
select
address,
min_by(namespace, created_at) as namespace
from {{ src_contracts }}
group by 1
),

nfts_per_tx as (
select
tx_hash,
sum(cast(amount as double)) as nfts_minted_in_tx -- for some erc1155 uint256 is not enough
from {{ nft_transfers }}
where "from" = 0x0000000000000000000000000000000000000000
{% if is_incremental () %}
and {{ incremental_predicate('block_time') }}
{% endif %}
group by 1
),

nft_mints as (
select
nft_mints.block_time as block_time,
cast(date_trunc('day', nft_mints.block_time) as date) as block_date,
cast(date_trunc('month', nft_mints.block_time) as date) as block_month,
nft_mints.block_number,
nft_mints.token_id,
nft_mints.token_standard,
case
when nft_mints.amount = uint256 '1' then 'Single Item Mint'
else 'Bundle Mint'
end as trade_type,
nft_mints.amount as number_of_items,
'Mint' as trade_category,
'Mint' as evt_type,
nft_mints."from" as seller, -- needed? always burn address
nft_mints.to as buyer,
nft_mints.amount / case when nft_count.nfts_minted_in_tx = 0 then 1 else nft_count.nfts_minted_in_tx end as mint_ratio,
nft_mints.contract_address as nft_contract_address,
nft_mints.tx_hash,
nft_mints.evt_index,
tx.to as project_contract_address,
tx."from" as tx_from,
tx.to as tx_to
from {{ nft_transfers }} nft_mints
join nfts_per_tx nft_count on nft_count.tx_hash = nft_mints.tx_hash
join {{ src_transactions }} tx on nft_mints.block_time = tx.block_time and nft_mints.tx_hash = tx.hash
{% if is_incremental () %}
and {{ incremental_predicate('tx.block_time') }}
{% endif %}
where 1 = 1
and nft_mints."from" = 0x0000000000000000000000000000000000000000
{% if is_incremental () %}
and {{ incremental_predicate('nft_mints.block_time') }}
{% endif %}
),

nft_mint_with_native as (
select
nft_mints.block_time,
nft_mints.tx_hash,
nft_mints.token_id,
{{ default_currency_contract }} as contract_address,
'{{ default_currency_symbol }}' as symbol,
sum(coalesce(cast(trc.value as double), 0) * nft_mints.mint_ratio) as amount_raw,
sum((coalesce(cast(trc.value as double), 0) / power(10, 18)) * nft_mints.mint_ratio) as amount_original,
sum((coalesce(cast(trc.value as double), 0) / power(10, 18)) * nft_mints.mint_ratio * pu_native.price) as amount_usd
from nft_mints
join {{ src_traces }} trc on nft_mints.block_time = trc.block_time
and nft_mints.tx_hash = trc.tx_hash
and nft_mints.buyer = trc."from"
{% if is_incremental () %}
and {{ incremental_predicate('trc.block_time') }}
{% endif %}
left join {{ source('prices', 'usd') }} pu_native on pu_native.blockchain = '{{blockchain}}'
and pu_native.minute = date_trunc('minute', trc.block_time)
and pu_native.contract_address = {{ default_currency_contract }}
{% if is_incremental () %}
and {{ incremental_predicate('pu_native.minute') }}
{% endif %}
where 1 = 1
and (trc.call_type not in ('delegatecall', 'callcode', 'staticcall') or trc.call_type is null)
and cast(trc.value as double) > 0
and trc.success
group by 1,2,3
),

nft_mint_with_erc20 as (
select
nft_mints.block_time,
nft_mints.tx_hash,
nft_mints.token_id,
erc20.contract_address,
pu_erc20.symbol,
sum(coalesce(cast(erc20.value as double), 0) * nft_mints.mint_ratio) as amount_raw,
sum((coalesce(cast(erc20.value as double), 0) / power(10, 18)) * nft_mints.mint_ratio) as amount_original,
sum((coalesce(cast(erc20.value as double), 0) / power(10, 18)) * nft_mints.mint_ratio * pu_erc20.price) as amount_usd
from nft_mints
join {{ src_erc20_evt_transfer }} erc20 on nft_mints.block_time = erc20.evt_block_time
and nft_mints.tx_hash = erc20.evt_tx_hash
and nft_mints.buyer = erc20."from"
{% if is_incremental () %}
and {{ incremental_predicate('erc20.evt_block_time') }}
{% endif %}
left join {{ source('prices', 'usd') }} pu_erc20 on pu_erc20.blockchain = '{{blockchain}}'
and pu_erc20.minute = date_trunc('minute', erc20.evt_block_time)
and erc20.contract_address = pu_erc20.contract_address
{% if is_incremental () %}
and {{ incremental_predicate('pu_erc20.minute') }}
{% endif %}
where 1 = 1
and cast(erc20.value as double) > 0
group by 1,2,3,4,5
)

select
'{{blockchain}}' as blockchain,
coalesce(ec.namespace, 'Unknown') as project,
coalesce(tok.name, 'Unknown') as collection,
'' as version,
nft_mints.block_time,
nft_mints.block_date,
nft_mints.block_month,
nft_mints.block_number,
nft_mints.token_id,
nft_mints.token_standard,
nft_mints.trade_type,
nft_mints.number_of_items,
nft_mints.trade_category,
nft_mints.evt_type,
nft_mints.seller,
nft_mints.buyer,
nft_mints.nft_contract_address,
nft_mints.tx_hash,
nft_mints.evt_index,
nft_mints.project_contract_address,
nft_mints.tx_from,
nft_mints.tx_to,
coalesce(mint_erc20.amount_raw, mint_native.amount_raw, 0) as amount_raw,
coalesce(mint_erc20.amount_original, mint_native.amount_original, 0) as amount_original,
coalesce(mint_erc20.amount_usd, mint_native.amount_usd, 0) as amount_usd,
coalesce(mint_erc20.symbol, mint_native.symbol, '{{ default_currency_symbol }}') as currency_symbol,
coalesce(mint_erc20.contract_address, mint_native.contract_address, {{ default_currency_contract }}) as currency_contract,
agg.name as aggregator_name,
agg.contract_address as aggregator_address,
cast(0 as uint256) as platform_fee_amount_raw,
cast(0 as double) as platform_fee_amount,
cast(0 as double) as platform_fee_amount_usd,
cast(0 as double) as platform_fee_percentage,
cast(null as varbinary) as royalty_fee_receive_address,
'0' as royalty_fee_currency_symbol,
cast(0 as uint256) as royalty_fee_amount_raw,
cast(0 as double) as royalty_fee_amount,
cast(0 as double) as royalty_fee_amount_usd,
cast(0 as double) as royalty_fee_percentage
from nft_mints
left join nft_mint_with_native mint_native on nft_mints.block_time = mint_native.block_time
and nft_mints.tx_hash = mint_native.tx_hash
and nft_mints.token_id = mint_native.token_id
left join nft_mint_with_erc20 mint_erc20 on nft_mints.block_time = mint_erc20.block_time
and nft_mints.tx_hash = mint_erc20.tx_hash
and nft_mints.token_id = mint_erc20.token_id
left join {{ nft_aggregators }} agg on nft_mints.tx_to = agg.contract_address
left join {{ tokens_nft }} tok on nft_mints.nft_contract_address = tok.contract_address
left join namespaces ec on nft_mints.tx_to = ec.address
where 1 = 1
{% if addresses_defi %}
and nft_mints.nft_contract_address not in (select address from {{ addresses_defi }})
{% endif %}
{% if is_incremental () %}
and {{ incremental_predicate('nft_mints.block_time') }}
{% endif %}

{% endmacro %}
8 changes: 5 additions & 3 deletions models/_sector/nft/mints/_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@ models:

- name: nft_mints
meta:
blockchain: ethereum, solana, bnb, optimism, arbitrum, polygon
blockchain: ethereum, solana, bnb, optimism, arbitrum, polygon, celo
sector: nft
contributors: soispoke, hildobby, ilemi, 0xRob, cat, umer_h_adil
contributors: soispoke, hildobby, ilemi, 0xRob, cat, umer_h_adil, tomfutago
config:
tags: ['nft', 'opensea', 'looksrare', 'x2y2', 'magiceden', 'sudoswap', 'foundation', 'element', 'zora', 'ethereum', 'bnb', 'solana', 'events', 'polygon', 'optimism', 'arbitrum', 'rarible', 'aavegotchi', 'oneplanet', 'fractal']
tags: ['nft', 'opensea', 'looksrare', 'x2y2', 'magiceden', 'sudoswap', 'foundation', 'element', 'zora', 'ethereum', 'bnb', 'solana', 'events', 'polygon', 'optimism', 'arbitrum', 'celo', 'rarible', 'aavegotchi', 'oneplanet', 'fractal']
description: >
NFT mints
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- blockchain
- tx_hash
- evt_index
- token_id
- number_of_items
- currency_contract
columns:
- &blockchain
name: blockchain
Expand Down
33 changes: 32 additions & 1 deletion models/_sector/nft/mints/native/_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ models:
meta:
blockchain: ethereum
sector: nft
contributors: umer_h_adil, hildobby
contributors: umer_h_adil, hildobby, tomfutago
config:
tags: [ 'nft','ethereum' ]
description: >
Expand All @@ -18,6 +18,7 @@ models:
- evt_index
- token_id
- number_of_items
- currency_contract
columns:
- &tx_hash
name: tx_hash
Expand All @@ -39,6 +40,11 @@ models:
description: "number_of_items"
tests:
- not_null
- &currency_contract
name: currency_contract
description: "currency contract"
tests:
- not_null
- &amount_usd
name: amount_usd
description: "USD value of the trade at time of execution"
Expand Down Expand Up @@ -68,3 +74,28 @@ models:
- *token_id
- *number_of_items
- *amount_usd

- name: nft_celo_native_mints
meta:
blockchain: celo
sector: nft
contributors: tomfutago
config:
tags: [ 'nft','celo' ]
description: >
NFT native (i.e., non-platform) mints
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- tx_hash
- evt_index
- token_id
- number_of_items
- currency_contract
columns:
- *tx_hash
- *evt_index
- *token_id
- *number_of_items
- *currency_contract
- *amount_usd
29 changes: 29 additions & 0 deletions models/_sector/nft/mints/native/nft_celo_native_mints.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{{
config(
tags = ['dunesql'],
schema = 'nft_celo',
alias = alias('native_mints'),
partition_by = ['block_month'],
materialized = 'incremental',
file_format = 'delta',
incremental_strategy = 'merge',
unique_key = ['tx_hash','evt_index','token_id','number_of_items','currency_contract']
)
}}

{% set blockchain = 'celo' %}

{{
nft_mints(
blockchain = blockchain,
src_contracts = source(blockchain, 'contracts'),
src_traces = source(blockchain, 'traces'),
src_transactions = source(blockchain, 'transactions'),
src_erc20_evt_transfer = source('erc20_' ~ blockchain, 'evt_transfer'),
nft_transfers = ref('nft_' ~ blockchain ~ '_transfers'),
nft_aggregators = ref('nft_' ~ blockchain ~ '_aggregators'),
tokens_nft = ref('tokens_' ~ blockchain ~ '_nft'),
default_currency_symbol = 'CELO',
default_currency_contract = '0x471EcE3750Da237f93B8E339c536989b8978a438'
)
}}
Loading