Skip to content

Commit

Permalink
Add Archipelago market to nft.trades (#1473)
Browse files Browse the repository at this point in the history
* add archipelago trades to nft.trades

* whitespace and newlines

* requested changes

Co-authored-by: soispoke <[email protected]>
  • Loading branch information
0xRobin and soispoke authored Sep 5, 2022
1 parent 5b5315d commit 6c07faf
Show file tree
Hide file tree
Showing 12 changed files with 709 additions and 7 deletions.
8 changes: 8 additions & 0 deletions dbt_project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ models:
+schema: tornado_cash
+materialized: view

archipelago:
+schema: archipelago
+materialized: view
ethereum:
+schema: archipelago_ethereum
+materialized: view

integration_test:
+schema: integration_test
+materialized: view
Expand Down Expand Up @@ -216,6 +223,7 @@ seeds:
token_bought_amount: float
token_sold_address: string
token_sold_amount: float

sudoswap:
+enabled: true
+schema: test_data
Expand Down
30 changes: 30 additions & 0 deletions macros/alter_table_properties.sql
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,33 @@ ALTER VIEW tokens_optimism.erc20 SET TBLPROPERTIES('dune.public'='true',
'dune.data_explorer.contributors'='["msilb7", "chuxinh"]');
{% endset %}

{% set archipelago_ethereum_events %}
ALTER TABLE archipelago_ethereum.events SET TBLPROPERTIES ('dune.public'='true',
'dune.data_explorer.blockchains'='["ethereum"]',
'dune.data_explorer.category'='abstraction',
'dune.data_explorer.abstraction.type'='project',
'dune.data_explorer.abstraction.name'='archipelago',
'dune.data_explorer.contributors'='["0xRob"]');
{% endset %}

{% set archipelago_ethereum_trades %}
ALTER VIEW archipelago_ethereum.trades SET TBLPROPERTIES ('dune.public'='true',
'dune.data_explorer.blockchains'='["ethereum"]',
'dune.data_explorer.category'='abstraction',
'dune.data_explorer.abstraction.type'='project',
'dune.data_explorer.abstraction.name'='archipelago',
'dune.data_explorer.contributors'='["0xRob"]');
{% endset %}

{% set archipelago_ethereum_fees %}
ALTER VIEW archipelago_ethereum.fees SET TBLPROPERTIES ('dune.public'='true',
'dune.data_explorer.blockchains'='["ethereum"]',
'dune.data_explorer.category'='abstraction',
'dune.data_explorer.abstraction.type'='project',
'dune.data_explorer.abstraction.name'='archipelago',
'dune.data_explorer.contributors'='["0xRob"]');
{% endset %}


{% do run_query(balances_ethereum_erc20_day) %}
{% do run_query(balances_ethereum_erc20_hour) %}
Expand Down Expand Up @@ -712,6 +739,9 @@ ALTER VIEW tokens_optimism.erc20 SET TBLPROPERTIES('dune.public'='true',
{% do run_query(uniswap_v3_optimism_pools) %}
{% do run_query(tokens_optimism_nft) %}
{% do run_query(tokens_optimism_erc20) %}
{% do run_query(archipelago_ethereum_events) %}
{% do run_query(archipelago_ethereum_trades) %}
{% do run_query(archipelago_ethereum_fees) %}

{% do log("Tables generated", info=True) %}
{%- else -%}
Expand Down
7 changes: 6 additions & 1 deletion macros/optimize_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ OPTIMIZE uniswap_v2_ethereum.trades;
OPTIMIZE x2y2_ethereum.events;
{% endset %}

{% set archipelago_ethereum_events %}
OPTIMIZE archipelago_ethereum.events;
{% endset %}


{% do run_query(looksrare_ethereum_events) %}
{% do run_query(magiceden_solana_events) %}
Expand All @@ -103,5 +107,6 @@ OPTIMIZE x2y2_ethereum.events;
{% do run_query(uniswap_v1_ethereum_trades) %}
{% do run_query(uniswap_v2_ethereum_trades) %}
{% do run_query(x2y2_ethereum_events) %}
{% do run_query(archipelago_ethereum_events) %}
{% do log("Tables Optimized", info=True) %}
{% endmacro %}
{% endmacro %}
227 changes: 227 additions & 0 deletions models/archipelago/ethereum/archipelago_ethereum_events.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
{{ config(
alias = 'events',
partition_by = ['block_date'],
materialized = 'incremental',
file_format = 'delta',
incremental_strategy = 'merge',
unique_key = ['block_date', 'unique_trade_id']
)
}}

WITH
trade_events as (
SELECT
contract_address as project_contract_address
, evt_block_number as block_number
, evt_block_time as block_time
, evt_tx_hash as tx_hash
, buyer
, seller
, cost as amount_raw
, currency as currency_contract
, tradeId as unique_trade_id
FROM {{ source('archipelago_ethereum','ArchipelagoMarket_evt_Trade') }}
{% if is_incremental() %}
WHERE evt_block_time >= date_trunc("day", now() - interval '1 week')
{% endif %}
{% if not is_incremental() %}
WHERE evt_block_time >= '2022-6-20'
{% endif %}

),

token_events as (
SELECT
evt_block_number as block_number
, evt_block_time as block_time
, evt_tx_hash as tx_hash
, tokenAddress as nft_contract_address
, tokenId as token_id
, tradeId as unique_trade_id
FROM {{ source('archipelago_ethereum','ArchipelagoMarket_evt_TokenTrade') }}
{% if is_incremental() %}
WHERE evt_block_time >= date_trunc("day", now() - interval '1 week')
{% endif %}
{% if not is_incremental() %}
WHERE evt_block_time >= '2022-6-20'
{% endif %}

),

fee_events as (
SELECT
evt_block_number as block_number
, evt_block_time as block_time
, evt_tx_hash as tx_hash
, amount as fee_amount_raw
, currency as fee_currency_contract
, recipient as fee_receive_address
, micros / pow(10,4) as fee_percentage
, tradeId as unique_trade_id
, case when (
upper(recipient) = upper('0xA76456bb6aBC50FB38e17c042026bc27a95C3314')
or upper(recipient) = upper('0x1fC12C9f68A6B0633Ba5897A40A8e61ed9274dC9')
) then true else false end
as is_protocol_fee
FROM {{ source('archipelago_ethereum','ArchipelagoMarket_evt_RoyaltyPayment') }}
{% if is_incremental() %}
WHERE evt_block_time >= date_trunc("day", now() - interval '1 week')
{% endif %}
{% if not is_incremental() %}
WHERE evt_block_time >= '2022-6-20'
{% endif %}

),

tokens_ethereum_nft as (
SELECT
*
FROM {{ ref('tokens_ethereum_nft') }}
),

nft_ethereum_aggregators as (
SELECT
*
FROM {{ ref('nft_ethereum_aggregators') }}
),

-- enrichments

trades_with_nft_and_tx as (
select
e.*
, t.nft_contract_address
, t.token_id
, tx.from as tx_from
, tx.to as tx_to
from trade_events e
inner join token_events t
ON e.block_number = t.block_number and e.unique_trade_id = t.unique_trade_id
inner join {{ source('ethereum', 'transactions') }} tx
ON e.block_number = tx.block_number and e.tx_hash = tx.hash
{% if is_incremental() %}
AND tx.block_time >= date_trunc("day", now() - interval '1 week')
{% endif %}
{% if not is_incremental() %}
AND tx.block_time >= '2022-6-20'
{% endif %}
),

platform_fees as (
select
block_number
,sum(fee_amount_raw) as platform_fee_amount_raw
,sum(fee_percentage) as platform_fee_percentage
,unique_trade_id
from fee_events
where is_protocol_fee
group by block_number,unique_trade_id
),

royalty_fees as (
select
block_number
,sum(fee_amount_raw) as royalty_fee_amount_raw
,sum(fee_percentage) as royalty_fee_percentage
,null::string as royalty_fee_receive_address -- we have multiple address so have to null this field
,unique_trade_id
from fee_events
where not is_protocol_fee
group by block_number,unique_trade_id
),


trades_with_fees as (
select
t.*
, pf.platform_fee_amount_raw
, pf.platform_fee_percentage
, rf.royalty_fee_amount_raw
, rf.royalty_fee_percentage
, rf.royalty_fee_receive_address
from trades_with_nft_and_tx t
left join platform_fees pf
ON t.block_number = pf.block_number and t.unique_trade_id = pf.unique_trade_id
left join royalty_fees rf
ON t.block_number = rf.block_number and t.unique_trade_id = rf.unique_trade_id

),

trades_with_price as (
select
t.*
, p.symbol as currency_symbol
, amount_raw/pow(10, p.decimals) as amount_original
, amount_raw/pow(10, p.decimals)*p.price as amount_usd
, platform_fee_amount_raw/pow(10, p.decimals) as platform_fee_amount
, platform_fee_amount_raw/pow(10, p.decimals)*p.price as platform_fee_amount_usd
, royalty_fee_amount_raw/pow(10, p.decimals) as royalty_fee_amount
, royalty_fee_amount_raw/pow(10, p.decimals)*p.price as royalty_fee_amount_usd
, p.symbol as royalty_fee_currency_symbol
from trades_with_fees t
left join {{ source('prices', 'usd') }} p ON p.blockchain='ethereum'
AND p.symbol = 'WETH' -- currently we only have ETH trades
AND date_trunc('minute', p.minute)=date_trunc('minute', t.block_time)
{% if is_incremental() %}
AND p.minute >= date_trunc("day", now() - interval '1 week')
{% endif %}
{% if not is_incremental() %}
AND p.minute >= '2022-4-1'
{% endif %}
),

trades_enhanced as (
select
t.*
, nft.standard as token_standard
, nft.name as collection
, agg.contract_address as aggregator_address
, agg.name as aggregator_name
from trades_with_price t
left join tokens_ethereum_nft nft
ON nft_contract_address = nft.contract_address
left join nft_ethereum_aggregators agg
ON tx_to = agg.contract_address
)


SELECT
'ethereum' as blockchain
, 'archipelago' as project
, 'v1' as version
, TRY_CAST(date_trunc('DAY', block_time) AS date) AS block_date
, block_time
, block_number
, token_id
, token_standard
, 1 as number_of_items
, 'Single Item Trade' as trade_type
, case when tx_from = seller then 'Sell' else 'Buy' end as trade_category
, 'Trade' as evt_type
, seller
, buyer
, amount_raw
, amount_original
, amount_usd
, currency_symbol
, currency_contract
, project_contract_address
, nft_contract_address
, collection
, tx_hash
, tx_from
, tx_to
, aggregator_address
, aggregator_name
, platform_fee_amount
, platform_fee_amount_raw
, platform_fee_amount_usd
, platform_fee_percentage
, royalty_fee_amount
, royalty_fee_amount_usd
, royalty_fee_amount_raw
, royalty_fee_currency_symbol
, royalty_fee_receive_address -- null here
, royalty_fee_percentage
, unique_trade_id
from trades_enhanced
39 changes: 39 additions & 0 deletions models/archipelago/ethereum/archipelago_ethereum_fees.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{{
config(
alias='fees')
}}

SELECT
blockchain,
project,
version,
block_time,
token_id,
collection,
platform_fee_amount_raw,
platform_fee_amount,
platform_fee_amount_usd,
platform_fee_percentage,
royalty_fee_amount_raw,
royalty_fee_amount,
royalty_fee_amount_usd,
royalty_fee_percentage,
royalty_fee_receive_address,
royalty_fee_currency_symbol,
token_standard,
trade_type,
number_of_items,
trade_category,
evt_type,
seller,
buyer,
nft_contract_address,
project_contract_address,
aggregator_name,
aggregator_address,
tx_hash,
block_number,
tx_from,
tx_to,
unique_trade_id
FROM ({{ ref('archipelago_ethereum_events') }})
Loading

0 comments on commit 6c07faf

Please sign in to comment.