Skip to content

Commit

Permalink
Init: Referrals sector (#4519)
Browse files Browse the repository at this point in the history
* init referral sector

* remove col

* add dummy schemas

* fix comma

* add block_date and block_month

* make incremental instead of view

* syntax fix

* fix

* fix

* fix test

* fix null comparison

* add is_referral

* add is_referral

* fix is_referral

* adapt sector spell structure

* fix config

* combine sound within the macro

* fix macro nesting

* fix macro nesting

* materialize top level

* materialize top level

* switch to prices.usd for performance

* switch to prices.usd for performance

* improve zora logic

* more zora fixes

* more zora fixes

* more zora fixes

* review comments

* review comments

* fix typo

---------

Co-authored-by: jeff <102681548+jeff-dude@users.noreply.github.com>
  • Loading branch information
0xRobin and jeff-dude authored Oct 20, 2023
1 parent de42b73 commit 3a35a6e
Show file tree
Hide file tree
Showing 19 changed files with 732 additions and 0 deletions.
18 changes: 18 additions & 0 deletions macros/models/_sector/add_tx_from_and_to.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% macro add_tx_from_and_to(
model_cte
,blockchain
)
%}

select
model.*
, tx."from" as tx_from
, tx."to" as tx_to
from {{model_cte}} model
inner join {{source(blockchain, 'transactions')}} tx
on model.block_number = tx.block_number
and model.tx_hash = tx.hash
{% if is_incremental() %}
where {{incremental_predicate('tx.block_time')}}
{% endif %}
{% endmacro %}
42 changes: 42 additions & 0 deletions macros/models/_sector/referral/enrich_referral_rewards.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{% macro enrich_referral_rewards(model)%}

select
r.blockchain
,r.project
,r.version
,r.block_number
,r.block_time
,r.block_date
,r.block_month
,r.tx_hash
,r.category
,r.referrer_address
,coalesce(r.referee_address, r.tx_from) as referee_address
,r.currency_contract
,r.reward_amount_raw
,r.project_contract_address
,r.sub_tx_id
,(r.referrer_address != 0x0000000000000000000000000000000000000000) as is_referral
,r.tx_from
,r.tx_to
,r.reward_amount_raw/pow(10,coalesce(erc.decimals,18)) as reward_amount
,r.reward_amount_raw/pow(10,coalesce(erc.decimals,18))*p.price as reward_amount_usd
from {{model}} r
left join {{ref('tokens_erc20')}} erc
on erc.blockchain = r.blockchain
and erc.contract_address = r.currency_contract
left join {{source('prices','usd')}} p
on p.minute = date_trunc('minute',r.block_time)
and (
(p.blockchain = r.blockchain
and p.contract_address = r.currency_contract)
or (r.currency_contract = {{var("ETH_ERC20_ADDRESS")}}
and p.symbol = 'ETH' and p.blockchain is null)
)
{% if is_incremental() %}
and {{incremental_predicate('p.minute')}}
{% endif %}
{% if is_incremental() %}
where {{incremental_predicate('r.block_time')}}
{% endif %}
{% endmacro %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{% macro rabbithole_referral_rewards(
blockchain
,QuestFactory_evt_MintFeePaid
)
%}

with model as (
select
'{{blockchain}}' as blockchain
,'rabbithole' as project
,'v2' as version
,evt_block_number as block_number
,evt_block_time as block_time
,cast(date_trunc('day',evt_block_time) as date) as block_date
,cast(date_trunc('month',evt_block_time) as date) as block_month
,evt_tx_hash as tx_hash
,'Quest' as category
,referrerAddress as referrer_address
,cast(null as varbinary) as referee_address -- will be overwritten as tx_from
,{{var('ETH_ERC20_ADDRESS')}} as currency_contract
,referrerAmountWei as reward_amount_raw
,contract_address as project_contract_address -- the drop contract
,evt_index as sub_tx_id
from {{QuestFactory_evt_MintFeePaid}}
{% if is_incremental() %}
where {{incremental_predicate('evt_block_time')}}
{% endif %}
)

{{ add_tx_from_and_to('model', blockchain) }}
{% endmacro %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{% macro soundxyz_referral_rewards(
blockchain
,evt_Minted_models
)
%}


with model as (
{% for evt_Minted in evt_Minted_models %}
select
'{{blockchain}}' as blockchain
,'soundxyz' as project
,'v1' as version
,evt_block_number as block_number
,evt_block_time as block_time
,cast(date_trunc('day',evt_block_time) as date) as block_date
,cast(date_trunc('month',evt_block_time) as date) as block_month
,evt_tx_hash as tx_hash
,'NFT' as category
,affiliate as referrer_address
,buyer as referee_address -- will be overwritten as tx_from
,{{var('ETH_ERC20_ADDRESS')}} as currency_contract
,affiliateFee as reward_amount_raw
,contract_address as project_contract_address -- the drop contract
,evt_index as sub_tx_id
from {{evt_Minted}}
{% if is_incremental() %}
where {{incremental_predicate('evt_block_time')}}
{% endif %}
{% if not loop.last %}
UNION ALL
{% endif %}
{% endfor %}
)
{{ add_tx_from_and_to('model', blockchain) }}
{% endmacro %}
39 changes: 39 additions & 0 deletions macros/models/_sector/referral/platforms/zora_referral_rewards.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{% macro zora_referral_rewards(
blockchain
,ProtocolRewards_evt_RewardsDeposit
)
%}

select
'{{blockchain}}' as blockchain
,'zora' as project
,'v1' as version
,evt_block_number as block_number
,evt_block_time as block_time
,cast(date_trunc('day',evt_block_time) as date) as block_date
,cast(date_trunc('month',evt_block_time) as date) as block_month
,evt_tx_hash as tx_hash
,'NFT' as category
,case
when mintReferralReward = uint256 '0'
or mintReferral = zora
or mintReferral = tx."from"
then 0x0000000000000000000000000000000000000000 else mintReferral end as referrer_address
,cast(null as varbinary) as referee_address -- will be overwritten as tx_from
,{{ var("ETH_ERC20_ADDRESS") }} as currency_contract
,mintReferralReward as reward_amount_raw
,e."from" as project_contract_address -- the drop contract
,evt_index as sub_tx_id
,tx."from" as tx_from
,tx.to as tx_to
from {{ProtocolRewards_evt_RewardsDeposit}} e
inner join {{source(blockchain, 'transactions')}} tx
on evt_block_number = tx.block_number
and evt_tx_hash = tx.hash
{% if is_incremental() %}
and {{incremental_predicate('tx.block_time')}}
{% endif %}
{% if is_incremental() %}
where {{incremental_predicate('evt_block_time')}}
{% endif %}
{% endmacro %}
74 changes: 74 additions & 0 deletions models/_sector/referral/rewards/_schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
version: 2

models:

- name: referral_rewards
meta:
blockchain: ethereum, solana, bnb, optimism, arbitrum, polygon
sector: nft
contributors: soispoke, hildobby, ilemi, 0xRob, cat, umer_h_adil
config:
tags: ['nft', 'opensea', 'looksrare', 'x2y2', 'magiceden', 'sudoswap', 'foundation', 'element', 'zora', 'ethereum', 'bnb', 'solana', 'events', 'polygon', 'optimism', 'arbitrum', 'rarible', 'aavegotchi', 'oneplanet', 'fractal']
description: >
NFT mints
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- project
- tx_hash
- sub_tx_id
columns:
- name: blockchain
description: "Blockchain"
- name: project
description: "Project"
tests:
- not_null
- name: version
description: "Project version"
- name: block_time
description: "UTC event block time"
- name: block_date
description: "UTC event block date"
- name: block_month
description: "UTC event block month"
- name: block_month
description: "block number"
- name: tx_hash
description: "transaction hash"
tests:
- not_null
- name: tx_from
description: "transaction sender"
- name: tx_to
description: "transaction receiver"
- name: category
description: "project category (NFT/AMM/Staking/...)"
- name: referrer_address
description: "address of the person making the referral"
- name: referee_address
description: "address of the person being referred"
- name: is_referral
description: "whether or not the a referral was made (and payed out)"
tests:
- accepted_values:
values: [ true, false ]
quote: false
- name: currency_contract
description: "contract address of the currency"
- name: reward_amount_raw
description: "raw reward amount"
- name: reward_amount
description: "reward amount corrected with the correct decimals"
- name: reward_amount_usd
description: "USD value of the referral reward at time of execution"
tests:
- dbt_utils.accepted_range:
max_value: 1000000 # $1m is an arbitrary number, intended to flag outlier amounts early
- name: project_contract_address
description: "project contract address"
- name: sub_tx_id
description: "id to distinguish multiple rewards in 1 transaction. Often the event index"
tests:
- not_null

Loading

0 comments on commit 3a35a6e

Please sign in to comment.