Skip to content

Commit 6c07faf

Browse files
0xRobinsoispoke
andauthored
Add Archipelago market to nft.trades (#1473)
* add archipelago trades to nft.trades * whitespace and newlines * requested changes Co-authored-by: soispoke <[email protected]>
1 parent 5b5315d commit 6c07faf

12 files changed

+709
-7
lines changed

dbt_project.yml

+8
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@ models:
148148
+schema: tornado_cash
149149
+materialized: view
150150

151+
archipelago:
152+
+schema: archipelago
153+
+materialized: view
154+
ethereum:
155+
+schema: archipelago_ethereum
156+
+materialized: view
157+
151158
integration_test:
152159
+schema: integration_test
153160
+materialized: view
@@ -216,6 +223,7 @@ seeds:
216223
token_bought_amount: float
217224
token_sold_address: string
218225
token_sold_amount: float
226+
219227
sudoswap:
220228
+enabled: true
221229
+schema: test_data

macros/alter_table_properties.sql

+30
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,33 @@ ALTER VIEW tokens_optimism.erc20 SET TBLPROPERTIES('dune.public'='true',
641641
'dune.data_explorer.contributors'='["msilb7", "chuxinh"]');
642642
{% endset %}
643643

644+
{% set archipelago_ethereum_events %}
645+
ALTER TABLE archipelago_ethereum.events SET TBLPROPERTIES ('dune.public'='true',
646+
'dune.data_explorer.blockchains'='["ethereum"]',
647+
'dune.data_explorer.category'='abstraction',
648+
'dune.data_explorer.abstraction.type'='project',
649+
'dune.data_explorer.abstraction.name'='archipelago',
650+
'dune.data_explorer.contributors'='["0xRob"]');
651+
{% endset %}
652+
653+
{% set archipelago_ethereum_trades %}
654+
ALTER VIEW archipelago_ethereum.trades SET TBLPROPERTIES ('dune.public'='true',
655+
'dune.data_explorer.blockchains'='["ethereum"]',
656+
'dune.data_explorer.category'='abstraction',
657+
'dune.data_explorer.abstraction.type'='project',
658+
'dune.data_explorer.abstraction.name'='archipelago',
659+
'dune.data_explorer.contributors'='["0xRob"]');
660+
{% endset %}
661+
662+
{% set archipelago_ethereum_fees %}
663+
ALTER VIEW archipelago_ethereum.fees SET TBLPROPERTIES ('dune.public'='true',
664+
'dune.data_explorer.blockchains'='["ethereum"]',
665+
'dune.data_explorer.category'='abstraction',
666+
'dune.data_explorer.abstraction.type'='project',
667+
'dune.data_explorer.abstraction.name'='archipelago',
668+
'dune.data_explorer.contributors'='["0xRob"]');
669+
{% endset %}
670+
644671

645672
{% do run_query(balances_ethereum_erc20_day) %}
646673
{% do run_query(balances_ethereum_erc20_hour) %}
@@ -712,6 +739,9 @@ ALTER VIEW tokens_optimism.erc20 SET TBLPROPERTIES('dune.public'='true',
712739
{% do run_query(uniswap_v3_optimism_pools) %}
713740
{% do run_query(tokens_optimism_nft) %}
714741
{% do run_query(tokens_optimism_erc20) %}
742+
{% do run_query(archipelago_ethereum_events) %}
743+
{% do run_query(archipelago_ethereum_trades) %}
744+
{% do run_query(archipelago_ethereum_fees) %}
715745

716746
{% do log("Tables generated", info=True) %}
717747
{%- else -%}

macros/optimize_tables.sql

+6-1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ OPTIMIZE uniswap_v2_ethereum.trades;
8383
OPTIMIZE x2y2_ethereum.events;
8484
{% endset %}
8585

86+
{% set archipelago_ethereum_events %}
87+
OPTIMIZE archipelago_ethereum.events;
88+
{% endset %}
89+
8690

8791
{% do run_query(looksrare_ethereum_events) %}
8892
{% do run_query(magiceden_solana_events) %}
@@ -103,5 +107,6 @@ OPTIMIZE x2y2_ethereum.events;
103107
{% do run_query(uniswap_v1_ethereum_trades) %}
104108
{% do run_query(uniswap_v2_ethereum_trades) %}
105109
{% do run_query(x2y2_ethereum_events) %}
110+
{% do run_query(archipelago_ethereum_events) %}
106111
{% do log("Tables Optimized", info=True) %}
107-
{% endmacro %}
112+
{% endmacro %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
{{ config(
2+
alias = 'events',
3+
partition_by = ['block_date'],
4+
materialized = 'incremental',
5+
file_format = 'delta',
6+
incremental_strategy = 'merge',
7+
unique_key = ['block_date', 'unique_trade_id']
8+
)
9+
}}
10+
11+
WITH
12+
trade_events as (
13+
SELECT
14+
contract_address as project_contract_address
15+
, evt_block_number as block_number
16+
, evt_block_time as block_time
17+
, evt_tx_hash as tx_hash
18+
, buyer
19+
, seller
20+
, cost as amount_raw
21+
, currency as currency_contract
22+
, tradeId as unique_trade_id
23+
FROM {{ source('archipelago_ethereum','ArchipelagoMarket_evt_Trade') }}
24+
{% if is_incremental() %}
25+
WHERE evt_block_time >= date_trunc("day", now() - interval '1 week')
26+
{% endif %}
27+
{% if not is_incremental() %}
28+
WHERE evt_block_time >= '2022-6-20'
29+
{% endif %}
30+
31+
),
32+
33+
token_events as (
34+
SELECT
35+
evt_block_number as block_number
36+
, evt_block_time as block_time
37+
, evt_tx_hash as tx_hash
38+
, tokenAddress as nft_contract_address
39+
, tokenId as token_id
40+
, tradeId as unique_trade_id
41+
FROM {{ source('archipelago_ethereum','ArchipelagoMarket_evt_TokenTrade') }}
42+
{% if is_incremental() %}
43+
WHERE evt_block_time >= date_trunc("day", now() - interval '1 week')
44+
{% endif %}
45+
{% if not is_incremental() %}
46+
WHERE evt_block_time >= '2022-6-20'
47+
{% endif %}
48+
49+
),
50+
51+
fee_events as (
52+
SELECT
53+
evt_block_number as block_number
54+
, evt_block_time as block_time
55+
, evt_tx_hash as tx_hash
56+
, amount as fee_amount_raw
57+
, currency as fee_currency_contract
58+
, recipient as fee_receive_address
59+
, micros / pow(10,4) as fee_percentage
60+
, tradeId as unique_trade_id
61+
, case when (
62+
upper(recipient) = upper('0xA76456bb6aBC50FB38e17c042026bc27a95C3314')
63+
or upper(recipient) = upper('0x1fC12C9f68A6B0633Ba5897A40A8e61ed9274dC9')
64+
) then true else false end
65+
as is_protocol_fee
66+
FROM {{ source('archipelago_ethereum','ArchipelagoMarket_evt_RoyaltyPayment') }}
67+
{% if is_incremental() %}
68+
WHERE evt_block_time >= date_trunc("day", now() - interval '1 week')
69+
{% endif %}
70+
{% if not is_incremental() %}
71+
WHERE evt_block_time >= '2022-6-20'
72+
{% endif %}
73+
74+
),
75+
76+
tokens_ethereum_nft as (
77+
SELECT
78+
*
79+
FROM {{ ref('tokens_ethereum_nft') }}
80+
),
81+
82+
nft_ethereum_aggregators as (
83+
SELECT
84+
*
85+
FROM {{ ref('nft_ethereum_aggregators') }}
86+
),
87+
88+
-- enrichments
89+
90+
trades_with_nft_and_tx as (
91+
select
92+
e.*
93+
, t.nft_contract_address
94+
, t.token_id
95+
, tx.from as tx_from
96+
, tx.to as tx_to
97+
from trade_events e
98+
inner join token_events t
99+
ON e.block_number = t.block_number and e.unique_trade_id = t.unique_trade_id
100+
inner join {{ source('ethereum', 'transactions') }} tx
101+
ON e.block_number = tx.block_number and e.tx_hash = tx.hash
102+
{% if is_incremental() %}
103+
AND tx.block_time >= date_trunc("day", now() - interval '1 week')
104+
{% endif %}
105+
{% if not is_incremental() %}
106+
AND tx.block_time >= '2022-6-20'
107+
{% endif %}
108+
),
109+
110+
platform_fees as (
111+
select
112+
block_number
113+
,sum(fee_amount_raw) as platform_fee_amount_raw
114+
,sum(fee_percentage) as platform_fee_percentage
115+
,unique_trade_id
116+
from fee_events
117+
where is_protocol_fee
118+
group by block_number,unique_trade_id
119+
),
120+
121+
royalty_fees as (
122+
select
123+
block_number
124+
,sum(fee_amount_raw) as royalty_fee_amount_raw
125+
,sum(fee_percentage) as royalty_fee_percentage
126+
,null::string as royalty_fee_receive_address -- we have multiple address so have to null this field
127+
,unique_trade_id
128+
from fee_events
129+
where not is_protocol_fee
130+
group by block_number,unique_trade_id
131+
),
132+
133+
134+
trades_with_fees as (
135+
select
136+
t.*
137+
, pf.platform_fee_amount_raw
138+
, pf.platform_fee_percentage
139+
, rf.royalty_fee_amount_raw
140+
, rf.royalty_fee_percentage
141+
, rf.royalty_fee_receive_address
142+
from trades_with_nft_and_tx t
143+
left join platform_fees pf
144+
ON t.block_number = pf.block_number and t.unique_trade_id = pf.unique_trade_id
145+
left join royalty_fees rf
146+
ON t.block_number = rf.block_number and t.unique_trade_id = rf.unique_trade_id
147+
148+
),
149+
150+
trades_with_price as (
151+
select
152+
t.*
153+
, p.symbol as currency_symbol
154+
, amount_raw/pow(10, p.decimals) as amount_original
155+
, amount_raw/pow(10, p.decimals)*p.price as amount_usd
156+
, platform_fee_amount_raw/pow(10, p.decimals) as platform_fee_amount
157+
, platform_fee_amount_raw/pow(10, p.decimals)*p.price as platform_fee_amount_usd
158+
, royalty_fee_amount_raw/pow(10, p.decimals) as royalty_fee_amount
159+
, royalty_fee_amount_raw/pow(10, p.decimals)*p.price as royalty_fee_amount_usd
160+
, p.symbol as royalty_fee_currency_symbol
161+
from trades_with_fees t
162+
left join {{ source('prices', 'usd') }} p ON p.blockchain='ethereum'
163+
AND p.symbol = 'WETH' -- currently we only have ETH trades
164+
AND date_trunc('minute', p.minute)=date_trunc('minute', t.block_time)
165+
{% if is_incremental() %}
166+
AND p.minute >= date_trunc("day", now() - interval '1 week')
167+
{% endif %}
168+
{% if not is_incremental() %}
169+
AND p.minute >= '2022-4-1'
170+
{% endif %}
171+
),
172+
173+
trades_enhanced as (
174+
select
175+
t.*
176+
, nft.standard as token_standard
177+
, nft.name as collection
178+
, agg.contract_address as aggregator_address
179+
, agg.name as aggregator_name
180+
from trades_with_price t
181+
left join tokens_ethereum_nft nft
182+
ON nft_contract_address = nft.contract_address
183+
left join nft_ethereum_aggregators agg
184+
ON tx_to = agg.contract_address
185+
)
186+
187+
188+
SELECT
189+
'ethereum' as blockchain
190+
, 'archipelago' as project
191+
, 'v1' as version
192+
, TRY_CAST(date_trunc('DAY', block_time) AS date) AS block_date
193+
, block_time
194+
, block_number
195+
, token_id
196+
, token_standard
197+
, 1 as number_of_items
198+
, 'Single Item Trade' as trade_type
199+
, case when tx_from = seller then 'Sell' else 'Buy' end as trade_category
200+
, 'Trade' as evt_type
201+
, seller
202+
, buyer
203+
, amount_raw
204+
, amount_original
205+
, amount_usd
206+
, currency_symbol
207+
, currency_contract
208+
, project_contract_address
209+
, nft_contract_address
210+
, collection
211+
, tx_hash
212+
, tx_from
213+
, tx_to
214+
, aggregator_address
215+
, aggregator_name
216+
, platform_fee_amount
217+
, platform_fee_amount_raw
218+
, platform_fee_amount_usd
219+
, platform_fee_percentage
220+
, royalty_fee_amount
221+
, royalty_fee_amount_usd
222+
, royalty_fee_amount_raw
223+
, royalty_fee_currency_symbol
224+
, royalty_fee_receive_address -- null here
225+
, royalty_fee_percentage
226+
, unique_trade_id
227+
from trades_enhanced
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{{
2+
config(
3+
alias='fees')
4+
}}
5+
6+
SELECT
7+
blockchain,
8+
project,
9+
version,
10+
block_time,
11+
token_id,
12+
collection,
13+
platform_fee_amount_raw,
14+
platform_fee_amount,
15+
platform_fee_amount_usd,
16+
platform_fee_percentage,
17+
royalty_fee_amount_raw,
18+
royalty_fee_amount,
19+
royalty_fee_amount_usd,
20+
royalty_fee_percentage,
21+
royalty_fee_receive_address,
22+
royalty_fee_currency_symbol,
23+
token_standard,
24+
trade_type,
25+
number_of_items,
26+
trade_category,
27+
evt_type,
28+
seller,
29+
buyer,
30+
nft_contract_address,
31+
project_contract_address,
32+
aggregator_name,
33+
aggregator_address,
34+
tx_hash,
35+
block_number,
36+
tx_from,
37+
tx_to,
38+
unique_trade_id
39+
FROM ({{ ref('archipelago_ethereum_events') }})

0 commit comments

Comments
 (0)