-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Migrate NFT beta base_trades #3694
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
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
958fd09
migrate all NFT base_trades beta models
0xRobin 3cccbef
migrate all NFT base_trades beta models
0xRobin a002846
migrate seeds
0xRobin 8116d00
add collectionswap_ethereum_pools
0xRobin fc0a919
actually migrate the pools
0xRobin f2cc70b
fix tests
0xRobin a2eac84
fix test
0xRobin 5fbb999
fix blur
0xRobin 9994f01
Merge branch 'main' into migrate-nft-beta
0xRobin 7ecdcff
fix sudoswap
0xRobin aebe51a
fix sudoswap seed
0xRobin 5c9d7c3
Update models/_sector/nft/trades/ethereum/platforms/archipelago_ether…
0xRobin 8be72b7
Update models/_sector/nft/trades/ethereum/platforms/x2y2_ethereum_bas…
0xRobin f7b53d6
Merge branch 'main' into migrate-nft-beta
0xRobin fd68402
fix element base trades legacy
0xRobin 4072f29
migrate nft trades beta setup
0xRobin ac127ae
re-apply duplicate fix
0xRobin bc2052d
re-apply duplicate fix
0xRobin 70a4e40
Merge branch 'migrate-nft-beta-setup' into migrate-nft-beta
0xRobin 187431b
fixes to trades_beta
0xRobin 1282088
fix macro refs
0xRobin 219b3a9
update downstream refs
0xRobin f6a9c0b
fix legacy parent
0xRobin 23caf1e
partition by month
0xRobin c642271
fix tests
0xRobin 466feb3
Merge branch 'main' into migrate-nft-beta
0xRobin 66b3ad2
Merge branch 'main' into migrate-nft-beta
0xRobin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
{% macro enrich_trades_legacy(blockchain='', models=[], transactions_model=null, tokens_nft_model=null, tokens_erc20_model=null, prices_model=null, aggregators=null, aggregator_markers=null) %} | ||
-- Macro to apply the NFT trades enrichment(s) to base models | ||
-- 1. add transaction information | ||
-- 2. add NFT token information | ||
-- 3. add ERC20 token information + handle ERC20 decimals | ||
-- 4. handle USD columns | ||
-- 5. add aggregator columns | ||
-- 6. fix buyer or seller for aggregator txs | ||
-- 7. calculate platform and royalty rates | ||
-- 8. deduplicate based on sub_tx_trade_id | ||
|
||
WITH base_union as ( | ||
{% for nft_model in models %} | ||
SELECT | ||
'{{ blockchain }}' as blockchain, | ||
'{{ nft_model[0] }}' as project, | ||
'{{ nft_model[1] }}' as project_version, | ||
block_date, | ||
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, | ||
row_number() over (partition by tx_hash, sub_tx_trade_id order by tx_hash) as duplicates_rank | ||
FROM {{ nft_model[2] }} | ||
{% if is_incremental() %} | ||
WHERE block_time >= date_trunc("day", now() - interval '1 week') | ||
{% endif %} | ||
{% if not loop.last %} | ||
UNION ALL | ||
{% endif %} | ||
{% endfor %} | ||
), | ||
|
||
enrichments as ( | ||
SELECT | ||
base.blockchain, | ||
base.project, | ||
base.project_version, | ||
base.block_date, | ||
base.block_time, | ||
base.block_number, | ||
base.tx_hash, | ||
base.sub_tx_trade_id, | ||
base.project_contract_address, | ||
base.trade_category, | ||
base.trade_type, | ||
case when base.buyer = coalesce(agg1.contract_address,agg2.contract_address) then tx.from else base.buyer end as buyer, | ||
case when base.seller = coalesce(agg1.contract_address,agg2.contract_address) then tx.from else base.seller end as seller, | ||
base.nft_contract_address, | ||
base.nft_token_id, | ||
base.nft_amount, | ||
base.price_raw, | ||
base.currency_contract, | ||
base.platform_fee_amount_raw, | ||
base.royalty_fee_amount_raw, | ||
base.platform_fee_address, | ||
base.royalty_fee_address, | ||
tx.from as tx_from, | ||
tx.to as tx_to, | ||
nft.name as nft_collection, | ||
nft.standard as nft_standard, | ||
coalesce(erc20.symbol,p.symbol) as currency_symbol, | ||
base.price_raw/pow(10,coalesce(erc20.decimals,p.decimals,18)) as price, | ||
base.platform_fee_amount_raw/pow(10,coalesce(erc20.decimals,p.decimals,18)) as platform_fee_amount, | ||
base.royalty_fee_amount_raw/pow(10,coalesce(erc20.decimals,p.decimals,18)) as royalty_fee_amount, | ||
base.price_raw/pow(10,coalesce(erc20.decimals,p.decimals,18))*p.price as price_usd, | ||
base.platform_fee_amount_raw/pow(10,coalesce(erc20.decimals,p.decimals,18))*p.price as platform_fee_amount_usd, | ||
base.royalty_fee_amount_raw/pow(10,coalesce(erc20.decimals,p.decimals,18))*p.price as royalty_fee_amount_usd, | ||
coalesce(cast(100*base.platform_fee_amount_raw/base.price_raw as double),cast(0.0 as double)) as platform_fee_percentage, | ||
coalesce(cast(100*base.royalty_fee_amount_raw/base.price_raw as double),cast(0.0 as double)) as royalty_fee_percentage, | ||
coalesce(agg1.contract_address,agg2.contract_address) as aggregator_address, | ||
{% if aggregator_markers != null %} | ||
coalesce(agg_mark.aggregator_name, agg1.name, agg2.name) as aggregator_name | ||
{% else %} | ||
coalesce(agg1.name,agg2.name) as aggregator_name | ||
{% endif %} | ||
FROM base_union base | ||
INNER JOIN {{ transactions_model }} tx | ||
ON tx.block_number = base.block_number | ||
AND tx.hash = base.tx_hash | ||
{% if is_incremental() %} | ||
AND tx.block_time >= date_trunc("day", now() - interval '1 week') | ||
{% endif %} | ||
LEFT JOIN {{ tokens_nft_model }} nft | ||
ON nft.contract_address = base.nft_contract_address | ||
LEFT JOIN {{ tokens_erc20_model }} erc20 | ||
ON erc20.contract_address = base.currency_contract | ||
LEFT JOIN {{ prices_model }} p | ||
ON p.blockchain = base.blockchain | ||
AND p.contract_address = base.currency_contract | ||
AND p.minute = date_trunc('minute',base.block_time) | ||
{% if is_incremental() %} | ||
AND p.minute >= date_trunc("day", now() - interval '1 week') | ||
{% endif %} | ||
LEFT JOIN {{ aggregators }} agg1 | ||
ON (base.buyer = agg1.contract_address | ||
OR base.seller = agg1.contract_address) | ||
LEFT JOIN {{ aggregators }} agg2 | ||
ON agg1.contract_address is null -- only match if agg1 produces no matches, this prevents duplicates | ||
AND tx.to = agg2.contract_address | ||
{% if aggregator_markers != null %} | ||
LEFT JOIN {{ aggregator_markers }} agg_mark | ||
ON RIGHT(tx.data, agg_mark.hash_marker_size) = agg_mark.hash_marker | ||
{% endif %} | ||
WHERE base.duplicates_rank = 1 | ||
) | ||
|
||
|
||
select * from enrichments | ||
{% endmacro %} |
123 changes: 123 additions & 0 deletions
123
macros/models/_sector/nft/platforms/element_v1_base_trades.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
-- Element NFT trades (re-usable macro for all chains) | ||
{% macro element_v1_base_trades(erc721_sell_order_filled, erc721_buy_order_filled, erc1155_sell_order_filled, erc1155_buy_order_filled) %} | ||
|
||
|
||
SELECT | ||
cast(DATE_TRUNC('month', evt_block_time) as date) AS block_date, | ||
evt_block_time AS block_time, | ||
evt_block_number AS block_number, | ||
'Buy' AS trade_category, | ||
'secondary' AS trade_type, | ||
erc721Token AS nft_contract_address, | ||
cast(erc721TokenId as uint256) AS nft_token_id, | ||
uint256 '1' AS nft_amount, | ||
taker AS buyer, | ||
maker AS seller, | ||
cast(erc20TokenAmount AS UINT256) AS price_raw, | ||
CASE | ||
WHEN erc20Token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee THEN 0x0000000000000000000000000000000000000000 | ||
ELSE erc20Token | ||
END AS currency_contract, | ||
cast(0 AS UINT256) AS platform_fee_amount_raw, | ||
cast(0 AS UINT256) AS royalty_fee_amount_raw, | ||
cast(NULL AS VARBINARY) AS platform_fee_address, | ||
cast(NULL AS VARBINARY) AS royalty_fee_address, | ||
contract_address AS project_contract_address, | ||
evt_tx_hash AS tx_hash, | ||
evt_index AS sub_tx_trade_id | ||
FROM {{ erc721_sell_order_filled }} | ||
{% if is_incremental() %} | ||
WHERE evt_block_time >= date_trunc('day', now() - interval '7' day) | ||
{% endif %} | ||
|
||
UNION ALL | ||
|
||
SELECT | ||
cast(date_trunc('month', evt_block_time) as date) AS block_date, | ||
evt_block_time AS block_time, | ||
evt_block_number AS block_number, | ||
'Sell' AS trade_category, | ||
'secondary' AS trade_type, | ||
erc721Token AS nft_contract_address, | ||
cast(erc721TokenId as uint256) AS nft_token_id, | ||
uint256 '1' AS nft_amount, | ||
maker AS buyer, | ||
taker AS seller, | ||
cast(erc20TokenAmount AS UINT256) AS price_raw, | ||
CASE | ||
WHEN erc20Token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee THEN 0x0000000000000000000000000000000000000000 | ||
ELSE erc20Token | ||
END AS currency_contract, | ||
cast(0 AS UINT256) AS platform_fee_amount_raw, | ||
cast(0 AS UINT256) AS royalty_fee_amount_raw, | ||
cast(NULL AS VARBINARY) AS platform_fee_address, | ||
cast(NULL AS VARBINARY) AS royalty_fee_address, | ||
contract_address AS project_contract_address, | ||
evt_tx_hash AS tx_hash, | ||
evt_index AS sub_tx_trade_id | ||
FROM {{ erc721_buy_order_filled }} | ||
{% if is_incremental() %} | ||
WHERE evt_block_time >= date_trunc('day', now() - interval '7' day) | ||
{% endif %} | ||
|
||
UNION ALL | ||
|
||
SELECT | ||
cast(date_trunc('month', evt_block_time) as date) AS block_date, | ||
evt_block_time AS block_time, | ||
evt_block_number AS block_number, | ||
'Buy' AS trade_category, | ||
'secondary' AS trade_type, | ||
erc1155Token AS nft_contract_address, | ||
cast(erc1155TokenId as uint256) AS nft_token_id, | ||
uint256 '1' AS nft_amount, | ||
taker AS buyer, | ||
maker AS seller, | ||
cast(erc20FillAmount AS UINT256) AS price_raw, | ||
CASE | ||
WHEN erc20Token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee THEN 0x0000000000000000000000000000000000000000 | ||
ELSE erc20Token | ||
END AS currency_contract, | ||
cast(0 AS UINT256) AS platform_fee_amount_raw, | ||
cast(0 AS UINT256) AS royalty_fee_amount_raw, | ||
cast(NULL AS VARBINARY) AS platform_fee_address, | ||
cast(NULL AS VARBINARY) AS royalty_fee_address, | ||
contract_address AS project_contract_address, | ||
evt_tx_hash AS tx_hash, | ||
evt_index AS sub_tx_trade_id | ||
FROM {{ erc1155_buy_order_filled }} | ||
{% if is_incremental() %} | ||
WHERE evt_block_time >= date_trunc('day', now() - interval '7' day) | ||
{% endif %} | ||
|
||
UNION ALL | ||
|
||
SELECT | ||
cast(date_trunc('month', evt_block_time) as date) AS block_date, | ||
evt_block_time AS block_time, | ||
evt_block_number AS block_number, | ||
'Buy' AS trade_category, | ||
'secondary' AS trade_type, | ||
erc1155Token AS nft_contract_address, | ||
cast(erc1155TokenId as uint256) AS nft_token_id, | ||
uint256 '1' AS nft_amount, | ||
maker AS buyer, | ||
taker AS seller, | ||
cast(erc20FillAmount AS UINT256) AS price_raw, | ||
CASE | ||
WHEN erc20Token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee THEN 0x0000000000000000000000000000000000000000 | ||
ELSE erc20Token | ||
END AS currency_contract, | ||
cast(0 AS UINT256) AS platform_fee_amount_raw, | ||
cast(0 AS UINT256) AS royalty_fee_amount_raw, | ||
cast(NULL AS VARBINARY) AS platform_fee_address, | ||
cast(NULL AS VARBINARY) AS royalty_fee_address, | ||
contract_address AS project_contract_address, | ||
evt_tx_hash AS tx_hash, | ||
evt_index AS sub_tx_trade_id | ||
FROM {{ erc1155_sell_order_filled }} | ||
{% if is_incremental() %} | ||
WHERE evt_block_time >= date_trunc('day', now() - interval '7' day) | ||
{% endif %} | ||
|
||
{% endmacro %} |
2 changes: 1 addition & 1 deletion
2
...ls/_sector/nft/element_v1_base_trades.sql → ...atforms/element_v1_base_trades_legacy.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.