Skip to content

Commit

Permalink
Blur & Fantasy trades (#6091)
Browse files Browse the repository at this point in the history
* create fantasy trades

* make fantasy.trades view

* fix

* fix

* fix

* fix project name

* add blur trades

* fix

* fix

---------

Co-authored-by: Alan Ghobadi <[email protected]>
  • Loading branch information
hildobby and aalan3 authored Jun 11, 2024
1 parent e7df88a commit 8d4db5e
Show file tree
Hide file tree
Showing 10 changed files with 331 additions and 2 deletions.
51 changes: 51 additions & 0 deletions models/_sector/nft/trades/chains/blast/nft_blast_base_trades.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{{ config(
schema = 'nft_blast',
alias = 'base_trades',
materialized = 'view'
)
}}


{% set nft_models = [
ref('fantasy_blast_base_trades')
, ref('blur_blast_base_trades')
] %}


SELECT * FROM (
{% for nft_model in nft_models %}
SELECT
blockchain,
project,
project_version,
cast(date_trunc('day', block_time) as date) as block_date,
cast(date_trunc('month', block_time) as date) as block_month,
block_time,
block_number,
tx_hash,
project_contract_address,
trade_category, --buy/sell/swap
trade_type, --primary/secondary
buyer,
seller,
nft_contract_address,
nft_token_id,
nft_amount, -- always 1 for erc721
price_raw,
currency_contract,
platform_fee_amount_raw,
royalty_fee_amount_raw,
platform_fee_address, -- optional
royalty_fee_address, -- optional
sub_tx_trade_id,
tx_from,
tx_to,
tx_data_marker,
row_number() over (partition by tx_hash, sub_tx_trade_id order by tx_hash) as duplicates_rank -- duplicates protection
FROM {{ nft_model }}
{% if not loop.last %}
UNION ALL
{% endif %}
{% endfor %}
)
where duplicates_rank = 1
30 changes: 30 additions & 0 deletions models/_sector/nft/trades/chains/blast/platforms/_schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
version: 2

models:
- name: fantasy_blast_base_trades
meta:
blockchain: blast
sector: nft
project: fantasy
contributors: hildobby
config:
tags: ['blast', 'nft', 'trades', 'fantasy']
description: "Fantasy base trades"
tests:
- check_columns_nft_base_trades
- dbt_utils.unique_combination_of_columns:
combination_of_columns: ['block_number','tx_hash','sub_tx_trade_id']

- name: blur_blast_base_trades
meta:
blockchain: blast
sector: nft
project: fantasy
contributors: hildobby
config:
tags: ['blast', 'nft', 'trades', 'blur']
description: "Blur on blast base trades"
tests:
- check_columns_nft_base_trades
- dbt_utils.unique_combination_of_columns:
combination_of_columns: ['block_number','tx_hash','sub_tx_trade_id']
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
{{ config(
schema = 'blur_blast',
alias = 'base_trades',
materialized = 'incremental',
file_format = 'delta',
incremental_strategy = 'merge',
unique_key = ['block_number','tx_hash','sub_tx_trade_id'],
)
}}

{% set blur_blast_start_date = '2024-04-10' %}

WITH blur_trades AS (
SELECT evt_tx_hash AS tx_hash
, bytearray_to_uint256(bytearray_substring(cast(collectionPriceSide as varbinary),2,11)) AS price_raw
, evt_block_time AS block_time
, evt_block_number AS block_number
, NULL AS fee_side
, evt_index
, contract_address AS project_contract_address
, bytearray_to_bigint(bytearray_substring(cast(collectionPriceSide as varbinary),1,1)) AS order_type
, bytearray_substring(cast(collectionPriceSide as varbinary),13,20) AS nft_contract_address
, orderHash AS order_hash
, bytearray_to_uint256(bytearray_substring(cast(tokenIdListingIndexTrader as varbinary),1,11)) AS nft_token_id
, bytearray_substring(cast(tokenIdListingIndexTrader as varbinary),13,20) AS trader
, double '0' AS fee
, NULL AS royalty_fee_address
FROM {{ source('blur_blast','BlurExchangeV2_evt_Execution721Packed') }}
{% if is_incremental() %}
WHERE {{incremental_predicate('evt_block_time')}}
{% else %}
WHERE evt_block_time >= TIMESTAMP '{{blur_blast_start_date}}'
{% endif %}

UNION ALL

SELECT evt_tx_hash AS tx_hash
, bytearray_to_uint256(bytearray_substring(cast(collectionPriceSide as varbinary),2,11)) AS price_raw
, evt_block_time AS block_time
, evt_block_number AS block_number
, 'maker' AS fee_side
, evt_index
, contract_address AS project_contract_address
, bytearray_to_bigint(bytearray_substring(cast(collectionPriceSide as varbinary),1,1)) AS order_type
, bytearray_substring(cast(collectionPriceSide as varbinary),13,20) AS nft_contract_address
, orderHash AS order_hash
, bytearray_to_uint256(bytearray_substring(cast(tokenIdListingIndexTrader as varbinary),1,11)) AS nft_token_id
, bytearray_substring(cast(tokenIdListingIndexTrader as varbinary),13,20) AS trader
, CAST(bitwise_right_shift(makerFeeRecipientRate, 160) AS double)/10000 AS fee
, bytearray_substring(cast(makerFeeRecipientRate as varbinary),13,20) AS royalty_fee_address
FROM {{ source('blur_blast','BlurExchangeV2_evt_Execution721MakerFeePacked') }}
{% if is_incremental() %}
WHERE {{incremental_predicate('evt_block_time')}}
{% else %}
WHERE evt_block_time >= TIMESTAMP '{{blur_blast_start_date}}'
{% endif %}

UNION ALL

SELECT evt_tx_hash AS tx_hash
, bytearray_to_uint256(bytearray_substring(cast(collectionPriceSide as varbinary),2,11)) AS price_raw
, evt_block_time AS block_time
, evt_block_number AS block_number
, 'taker' AS fee_side
, evt_index
, contract_address AS project_contract_address
, bytearray_to_bigint(bytearray_substring(cast(collectionPriceSide as varbinary),1,1)) AS order_type
, bytearray_substring(cast(collectionPriceSide as varbinary),13,20) AS nft_contract_address
, orderHash AS order_hash
, bytearray_to_uint256(bytearray_substring(cast(tokenIdListingIndexTrader as varbinary),1,11)) AS nft_token_id
, bytearray_substring(cast(tokenIdListingIndexTrader as varbinary),13,20) AS trader
, CAST(bitwise_right_shift(takerFeeRecipientRate, 160) AS double)/10000 AS fee
, bytearray_substring(cast(takerFeeRecipientRate as varbinary),13,20) AS royalty_fee_address
FROM {{ source('blur_blast','BlurExchangeV2_evt_Execution721TakerFeePacked') }}
{% if is_incremental() %}
WHERE {{incremental_predicate('evt_block_time')}}
{% else %}
WHERE evt_block_time >= TIMESTAMP '{{blur_blast_start_date}}'
{% endif %}
)

, trades_final AS (
SELECT 'blast' as blockchain
, 'blur' as project
, 'v2' as project_version
, bt.block_time
, bt.block_number
, bt.tx_hash
, bt.evt_index AS sub_tx_trade_id
, CASE WHEN bt.order_type = 1 THEN 'Sell' ELSE 'Buy' END AS trade_category
, 'secondary' AS trade_type
, CASE WHEN bt.order_type = 1 THEN bt.trader ELSE txs."from" END AS buyer
, CASE WHEN bt.order_type = 0 THEN bt.trader ELSE txs."from" END AS seller
, bt.nft_contract_address
, bt.nft_token_id AS nft_token_id
, UINT256 '1' AS nft_amount
, bt.price_raw
, CASE WHEN bt.order_type = 0 THEN 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2 ELSE 0x0000000000a39bb272e79075ade125fd351887ac END AS currency_contract
, bt.project_contract_address
, uint256 '0' AS platform_fee_amount_raw
, CAST(NULL AS varbinary) AS platform_fee_address
, CAST(ROUND(bt.price_raw * bt.fee) AS UINT256) AS royalty_fee_amount_raw
, bt.royalty_fee_address
FROM blur_trades bt
-- todo: remove the join on transactions here
INNER JOIN {{ source('blast', 'transactions') }} txs ON txs.block_number=bt.block_number
AND txs.hash=bt.tx_hash
{% if is_incremental() %}
AND {{incremental_predicate('txs.block_time')}}
{% else %}
AND txs.block_time >= TIMESTAMP '{{blur_blast_start_date}}'
{% endif %}
)

{{ add_nft_tx_data('trades_final', 'blast') }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
{{ config(
schema = 'fantasy_blast',
alias = 'base_trades',
materialized = 'incremental',
file_format = 'delta',
incremental_strategy = 'merge',
unique_key = ['block_number','tx_hash','sub_tx_trade_id']
)
}}

{% set project_start_date = '2024-05-01' %}

WITH trades AS (
SELECT evt_block_time AS block_time
, evt_block_date AS block_date
, CAST(JSON_EXTRACT_SCALAR(sell, '$.tokenId') AS UINT256) AS nft_token_id
, UINT256 '1' AS nft_amount
, 'Buy' AS trade_category
, from_hex(JSON_EXTRACT_SCALAR(sell, '$.trader')) AS seller
, buyer
, CAST(JSON_EXTRACT_SCALAR(sell, '$.price') AS UINT256) AS price_raw
, from_hex(JSON_EXTRACT_SCALAR(sell, '$.paymentToken')) AS currency_contract
, from_hex(JSON_EXTRACT_SCALAR(sell, '$.collection')) AS nft_contract_address
, evt_tx_hash AS tx_hash
, contract_address AS project_contract_address
, evt_block_number AS block_number
, evt_index AS sub_tx_trade_id
, evt_tx_from AS tx_from
, evt_tx_to AS tx_to
FROM {{ source('fantasy_blast', 'Exchange_evt_Buy')}}
{% if not is_incremental() %}
WHERE evt_block_time >= TIMESTAMP '{{project_start_date}}'
{% endif %}
{% if is_incremental() %}
WHERE {{ incremental_predicate('evt_block_time') }}
{% endif %}

UNION ALL

SELECT evt_block_time AS block_time
, evt_block_date AS block_date
, CAST(JSON_EXTRACT_SCALAR(buyOrder, '$.tokenId') AS UINT256) AS nft_token_id
, UINT256 '1' AS nft_amount
, 'Sell' AS trade_category
, seller
, from_hex(JSON_EXTRACT_SCALAR(buyOrder, '$.trader')) AS buyer
, CAST(JSON_EXTRACT_SCALAR(buyOrder, '$.price') AS UINT256) AS price_raw
, from_hex(JSON_EXTRACT_SCALAR(buyOrder, '$.paymentToken')) AS currency_contract
, from_hex(JSON_EXTRACT_SCALAR(buyOrder, '$.collection')) AS nft_contract_address
, evt_tx_hash AS tx_hash
, contract_address AS project_contract_address
, evt_block_number AS block_number
, evt_index AS sub_tx_trade_id
, evt_tx_from AS tx_from
, evt_tx_to AS tx_to
FROM {{ source('fantasy_blast', 'Exchange_evt_Sell')}}
{% if not is_incremental() %}
WHERE evt_block_time >= TIMESTAMP '{{project_start_date}}'
{% endif %}
{% if is_incremental() %}
WHERE {{ incremental_predicate('evt_block_time') }}
{% endif %}
)

, trades_final AS (
SELECT 'blast' AS blockchain
, 'fantasy' AS project
, 'v1' AS project_version
, block_time
, block_date
, date_trunc('month', block_time) AS block_month
, nft_token_id
, 'secondary' AS trade_type
, nft_amount
, trade_category
, seller
, buyer
, price_raw
, currency_contract
, nft_contract_address
, tx_hash
, project_contract_address
, block_number
, sub_tx_trade_id
--, tx_from
--, tx_to
, CAST(0.015*CAST(price_raw AS double) AS UINT256) AS platform_fee_amount_raw
, CAST(0.015*CAST(price_raw AS double) AS UINT256) AS royalty_fee_amount_raw
, CAST(NULL AS VARBINARY) AS royalty_fee_address
, 0x8ab15fe88a00b03724ac91ee4ee1f998064f2e31 AS platform_fee_address
FROM trades
)

{{ add_nft_tx_data('trades_final', 'base') }}
1 change: 1 addition & 0 deletions models/_sector/nft/trades/nft_base_trades.sql
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
,ref('nft_avalanche_c_base_trades')
,ref('nft_linea_base_trades')
,ref('nft_zora_base_trades')
,ref('nft_blast_base_trades')
] %}

with base_union as (
Expand Down
2 changes: 1 addition & 1 deletion models/_sector/nft/trades/nft_trades.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
schema = 'nft',
alias = 'trades',
materialized = 'view',
post_hook='{{ expose_spells(\'["ethereum","solana","bnb","optimism","arbitrum","polygon","zksync"]\',
post_hook='{{ expose_spells(\'["ethereum","solana","bnb","optimism","arbitrum","polygon","zksync", "blast"]\',
"sector",
"nft",
\'["soispoke","0xRob", "hildobby"]\') }}')
Expand Down
10 changes: 10 additions & 0 deletions models/_sector/nft/trades/platform_views/_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,13 @@ models:
tags: ['nft', 'trades', 'zora']
description: "NFT trades view for zora"


- name: fantasy_trades_view
meta:
blockchain: ["blast"]
sector: nft
contributors: hildobby
config:
tags: ['nft', 'trades', 'blast']
description: "NFT trades view for fantasy"

15 changes: 15 additions & 0 deletions models/_sector/nft/trades/platform_views/fantasy_trades_view.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

{{ config(
schema = 'fantasy',
alias = 'trades',

materialized = 'view',
post_hook='{{ expose_spells(\'["blast"]\',
"project",
"fantasy",
\'["hildobby"]\') }}')
}}

SELECT *
FROM {{ ref('nft_trades') }}
WHERE project = 'fantasy'
2 changes: 1 addition & 1 deletion models/_sector/nft/trades/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ models:

- name: nft_trades
meta:
blockchain: ethereum, solana, bnb, optimism, arbitrum, polygon
blockchain: ethereum, solana, bnb, optimism, arbitrum, polygon, blast
sector: nft
contributors: soispoke, hildobby, ilemi, cat
config:
Expand Down
13 changes: 13 additions & 0 deletions sources/_sector/nft/trades/blast_sources.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: 2

sources:
- name: blur_blast
tables:
- name: BlurExchangeV2_evt_Execution721Packed
- name: BlurExchangeV2_evt_Execution721MakerFeePacked
- name: BlurExchangeV2_evt_Execution721TakerFeePacked

- name: fantasy_blast
tables:
- name: Exchange_evt_Buy
- name: Exchange_evt_Sell

0 comments on commit 8d4db5e

Please sign in to comment.