Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
0xnirmal committed Mar 18, 2024
1 parent 3ab2efb commit 3007d23
Show file tree
Hide file tree
Showing 1,360 changed files with 39,609 additions and 0 deletions.
1 change: 1 addition & 0 deletions .user.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
id: 96b3d18d-1691-46de-bc05-ff2b647d55dc
46 changes: 46 additions & 0 deletions dbt_project.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Name your project! Project names should contain only lowercase characters
# and underscores. A good package name should reflect your organization's
# name or the intended use of these models
name: "artemis_dbt"
version: "1.0.0"
config-version: 2

# This setting configures which "profile" dbt uses for this project.
profile: "snowflake-dagster"

# These configurations specify where dbt should look for different types of files.
# The `source-paths` config, for example, states that models in this project can be
# found in the "models/" directory. You probably won't need to change these!
model-paths: ["./models"]
analysis-paths: ["./analyses"]
test-paths: ["./tests"]
seed-paths: ["./seeds"]
macro-paths: ["./macros"]
snapshot-paths: ["./snapshots"]

target-path: "target" # directory which will store compiled SQL files
clean-targets: # directories to be removed by `dbt clean`
- "target"
- "dbt_packages"

# Configuring models
# Full documentation: https://docs.getdbt.com/docs/configuring-models

# In this example config, we tell dbt to build all models in the example/ directory
# as tables. These settings can be overridden in the individual model files
# using the `{{ config(...) }}` macro.
models:
artemis_dbt:
metrics:
contracts:
+snowflake_warehouse: "DAILY_L1"
staging:
address_balances:
+snowflake_warehouse: "BALANCES_MD"
defillama:
+snowflake_warehouse: "DAILY_DEFILLAMA"
bam_models:
+snowflake_warehouse: "DAILY_BAM"

vars:
dbt_date:time_zone: "Etc/UTC"
Empty file added macros/.gitkeep
Empty file.
54 changes: 54 additions & 0 deletions macros/address_balances/address_balances_by_token.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{% macro address_balances(chain) %}
with
credit_debit as (
select address, contract_address, block_timestamp, credit as flow
from pc_dbt_db.prod.fact_{{ chain }}_address_credit_by_token
{% if is_incremental() %}
where block_timestamp > (select max(block_timestamp) from {{ this }})
{% endif %}
union all
select address, contract_address, block_timestamp, debit as flow
from pc_dbt_db.prod.fact_{{ chain }}_address_debit_by_token
{% if is_incremental() %}
where block_timestamp > (select max(block_timestamp) from {{ this }})
{% endif %}
),
credit_debits_and_latest_balances as (
select address, contract_address, block_timestamp, flow
from credit_debit
{% if is_incremental() %}
union all
select address, contract_address, block_timestamp, token_balance as flow
from prod.dim_{{ chain }}_current_balances
{% endif %}
)
select
address,
contract_address,
block_timestamp,
sum(flow) over (
partition by contract_address, address order by block_timestamp
) as balance_token
from credit_debits_and_latest_balances

{% endmacro %}


{% macro address_balances_with_flipside_ez(chain) %}
select
user_address as address,
case
when contract_address is null then 'native_token' else contract_address
end as contract_address,
block_timestamp,
case
when contract_address is null then current_bal else current_bal_unadj
end as balance_token
from {{ chain }}_flipside.core.ez_balance_deltas
where
to_date(block_timestamp) < to_date(sysdate())
{% if is_incremental() %}
and block_timestamp
>= (select dateadd('day', -3, max(block_timestamp)) from {{ this }})
{% endif %}
{% endmacro %}
110 changes: 110 additions & 0 deletions macros/address_balances/address_credits.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
{% macro address_credits(chain, wrapped_native_token_address, native_token_address) %}

select
to_address as address,
contract_address,
block_timestamp,
cast(raw_amount as float) as credit,
amount_usd as credit_usd,
tx_hash,
null as trace_index,
event_index
from {{ chain }}_flipside.core.ez_token_transfers
where
to_address <> lower('0x0000000000000000000000000000000000000000')
and to_date(block_timestamp) < to_date(sysdate())
{% if is_incremental() %}
and block_timestamp
>= (select dateadd('day', -3, max(block_timestamp)) from {{ this }})
{% endif %}

union all

select
to_address as address,
'native_token' as contract_address,
block_timestamp,
amount as credit,
amount_usd as credit_usd,
tx_hash,
trace_index,
null as event_index
from {{ chain }}_flipside.core.ez_native_transfers
where
to_date(block_timestamp) < to_date(sysdate())
and to_address <> lower('0x0000000000000000000000000000000000000000')
{% if is_incremental() %}
and block_timestamp
>= (select dateadd('day', -3, max(block_timestamp)) from {{ this }})
{% endif %}

{% if wrapped_native_token_address is defined %}
union all
select
decoded_log:"dst"::string as address,
contract_address,
block_timestamp,
cast(decoded_log:"wad" as float) as credit,
null as credit_usd,
tx_hash,
null as trace_index,
event_index
from {{ chain }}_flipside.core.ez_decoded_event_logs
where
event_name = 'Deposit'
and contract_address = lower('{{ wrapped_native_token_address }}')
and to_date(block_timestamp) < to_date(sysdate())
and address <> lower('0x0000000000000000000000000000000000000000')
{% if is_incremental() %}
and block_timestamp
>= (select dateadd('day', -3, max(block_timestamp)) from {{ this }})
{% endif %}

{% endif %}
-- Need to include credits of old bridge deposits of eth to arbitrum. Only need to
-- do this
-- for full refreshes since the bridge is no longer used.
{% if chain == "arbitrum" and not is_incremental() %}
union all
select
origin_to_address as address,
'native_token' as contract_address,
block_timestamp,
amount as credit,
null as credit_usd,
tx_hash,
null as trace_index,
null as event_index
from ethereum_flipside.core.ez_native_transfers
where to_address = lower('0x011B6E24FfB0B5f5fCc564cf4183C5BBBc96D515')
{% endif %}

-- Some EVM Chains has a specific contract address for their native token (polygon)
-- When you bridge the native token from ethereum (deposit matic) it does not emit
-- a transfer event
-- (neither as a native transfer or a erc20 transfer)
-- so we need to seperately account for these events.
{% if native_token_address is defined %}
union all
select
decoded_log:"from"::string as address,
'native_token' as contract_address,
block_timestamp,
cast(decoded_log:"amount" as float) / pow(10, 18) as credit,
null as credit_usd,
tx_hash,
null as trace_index,
null as event_index
from {{ chain }}_flipside.core.ez_decoded_event_logs
where
event_name = 'Deposit'
and contract_address = lower('{{ native_token_address }}')
and to_date(block_timestamp) < to_date(sysdate())
and address <> lower('0x0000000000000000000000000000000000000000')
{% if is_incremental() %}
and block_timestamp
>= (select dateadd('day', -3, max(block_timestamp)) from {{ this }})
{% endif %}
{% endif %}
-- Not included is staking fees (need to do more research and would be chains specific)
{% endmacro %}
45 changes: 45 additions & 0 deletions macros/address_balances/address_credits_allium.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{% macro address_credits_allium(chain) %}

select
to_address as address,
'native_token' as contract_address,
block_timestamp,
cast(amount as float) as credit,
cast(usd_amount as float) as credit_usd,
transaction_hash as tx_hash,
unique_id
from {{ chain }}_allium.assets.trx_token_transfers
where
{% if chain == "tron" %}
-- Null Address on Tron
lower(to_address) <> lower('T9yD14Nj9j7xAB4dbGeiX9h8unkKHxuWwb')
{% endif %}
and to_date(block_timestamp) < to_date(sysdate())
and to_address <> from_address
{% if is_incremental() %}
and block_timestamp
>= (select dateadd('day', -3, max(block_timestamp)) from {{ this }})
{% endif %}
union all
select
to_address as address,
token_address as contract_address,
block_timestamp,
cast(raw_amount as float) as credit,
null as credit_usd,
transaction_hash as tx_hash,
unique_id
from {{ chain }}_allium.assets.trc20_token_transfers
where
{% if chain == "tron" %}
-- Null Address on Tron
lower(to_address) <> lower('T9yD14Nj9j7xAB4dbGeiX9h8unkKHxuWwb')
{% endif %}
{% if is_incremental() %}
and block_timestamp
>= (select dateadd('day', -7, max(block_timestamp)) from {{ this }})
{% endif %}
and to_date(block_timestamp) < to_date(sysdate())
and to_address <> from_address

{% endmacro %}
84 changes: 84 additions & 0 deletions macros/address_balances/address_debits.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
{% macro address_debits(chain, wrapped_native_token_address) %}

select
from_address as address,
'native_token' as contract_address,
block_timestamp,
tx_fee * -1 as debit,
null as debit_usd,
tx_hash,
null as trace_index,
null as event_index
from {{ chain }}_flipside.core.fact_transactions as t
where
to_date(block_timestamp) < to_date(sysdate())
and from_address <> lower('0x0000000000000000000000000000000000000000')
{% if is_incremental() %}
and block_timestamp
>= (select dateadd('day', -3, max(block_timestamp)) from {{ this }})
{% endif %}

union all

select
from_address as address,
contract_address,
block_timestamp,
cast(raw_amount * -1 as float) as debit,
amount_usd * -1 as debit_usd,
tx_hash,
null as trace_index,
event_index
from {{ chain }}_flipside.core.ez_token_transfers
where
to_date(block_timestamp) < to_date(sysdate())
and from_address <> lower('0x0000000000000000000000000000000000000000')
{% if is_incremental() %}
and block_timestamp
>= (select dateadd('day', -3, max(block_timestamp)) from {{ this }})
{% endif %}

union all

select
from_address as address,
'native_token' as contract_address,
block_timestamp,
amount * -1 as debit,
amount_usd * -1 as debit_usd,
tx_hash,
trace_index,
null as event_index
from {{ chain }}_flipside.core.ez_native_transfers
where
to_date(block_timestamp) < to_date(sysdate())
and from_address <> lower('0x0000000000000000000000000000000000000000')
and from_address <> to_address
{% if is_incremental() %}
and block_timestamp
>= (select dateadd('day', -3, max(block_timestamp)) from {{ this }})
{% endif %}

{% if wrapped_native_token_address is defined %}
union all
select
decoded_log:"src"::string as address,
contract_address,
block_timestamp,
cast(decoded_log:"wad" as float) * -1 as debit,
null as debit_usd,
tx_hash,
null as trace_index,
event_index
from {{ chain }}_flipside.core.ez_decoded_event_logs
where
event_name = 'Withdrawal'
and contract_address = lower('{{ wrapped_native_token_address }}')
and to_date(block_timestamp) < to_date(sysdate())
and address <> lower('0x0000000000000000000000000000000000000000')
{% if is_incremental() %}
and block_timestamp
>= (select dateadd('day', -3, max(block_timestamp)) from {{ this }})
{% endif %}
{% endif %}
{% endmacro %}
45 changes: 45 additions & 0 deletions macros/address_balances/address_debits_allium.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{% macro address_debits_allium(chain) %}

select
from_address as address,
'native_token' as contract_address,
block_timestamp,
cast(amount * -1 as float) as debit,
cast(usd_amount * -1 as float) as debit_usd,
transaction_hash as tx_hash,
unique_id
from {{ chain }}_allium.assets.trx_token_transfers
where
{% if chain == "tron" %}
-- Null Address on Tron
lower(from_address) <> lower('T9yD14Nj9j7xAB4dbGeiX9h8unkKHxuWwb')
{% endif %}
and to_date(block_timestamp) < to_date(sysdate())
{% if is_incremental() %}
and block_timestamp
>= (select dateadd('day', -7, max(block_timestamp)) from {{ this }})
{% endif %}
and to_address <> from_address
union all
select
from_address as address,
token_address as contract_address,
block_timestamp,
cast(raw_amount * -1 as float) as debit,
null as debit_usd,
transaction_hash as tx_hash,
unique_id
from {{ chain }}_allium.assets.trc20_token_transfers
where
{% if chain == "tron" %}
-- Null Address on Tron
lower(from_address) <> lower('T9yD14Nj9j7xAB4dbGeiX9h8unkKHxuWwb')
{% endif %}
and to_date(block_timestamp) < to_date(sysdate())
{% if is_incremental() %}
and block_timestamp
>= (select dateadd('day', -7, max(block_timestamp)) from {{ this }})
{% endif %}
and to_address <> from_address

{% endmacro %}
Loading

0 comments on commit 3007d23

Please sign in to comment.