Skip to content

Commit

Permalink
Add zkevm to safe models (#5758)
Browse files Browse the repository at this point in the history
* Add v1.4.1 singletons

L1 singleton 0x41675C099F32341bf84BFc5382aF534df5C7461a
L2 singleton 0x29fcB43b46531BcA003ddC8FCB67FFE91900C76

* Change goerli test

* Declare new Safe models for zkevm

* Implement Safe models for zkevm

* Update crosschain models with zkevm data

Models updated are:
- safe_native_transfers.all.sql -> Added safe_zkevm_matic_transfers
- safe_safes_all.sql -> Added safe_zkevm_safes
- safe_transactions_all.sql -> safe_zkevm_transactions

* Bugfix: Joining correctly on celo transactions for safe_celo_transactions spell

* Fix project start for safe_polygon_matic_transfers.sql

* rename correctly Safe zkevm transactions spell

* Add zkevm base source

* Implement correctly base source

# Conflicts:
#	sources/_base_sources/zkevm_base_sources.yml

* Fix zkevm schema

* Trigger run again

* Remove singletons spell

* Add singletons spell

* add decoded source tables

* Fix duplicate anchor

* Update sources/safe/zkevm/safe_zkevm_sources.yml

Co-authored-by: Huang Geyang <Sukebeta@outlook.com>

* Adapt test

* Update models/safe/zkevm/safe_zkevm_matic_transfers.sql

Co-authored-by: Huang Geyang <Sukebeta@outlook.com>

* Update models/safe/zkevm/safe_zkevm_matic_transfers.sql

Co-authored-by: Huang Geyang <Sukebeta@outlook.com>

* Add schema to zkevm Safe data models

* Adapt pot_hook section

* Use same incremental interval in union

* Set project start date

* Add unique combo of columns test

* remove unique_key column on zkevm_transactions

* Revert "remove unique_key column on zkevm_transactions"

This reverts commit 5e44197.

* Delelete unique key on safe_zkevm_transacions

* Update models/safe/zkevm/safe_zkevm_matic_transfers.sql

Co-authored-by: Huang Geyang <Sukebeta@outlook.com>

* Add increment join

Following suggestion of Hosuke

* Address merge conflicts

* Address one more merge conflict

* Address one more merge conflict

* Quickfix: Change block_time incremental join

* Update models/safe/zkevm/safe_zkevm_transactions.sql

Co-authored-by: jeff-dude <102681548+jeff-dude@users.noreply.github.com>

* Update models/safe/zkevm/safe_zkevm_transactions.sql

Co-authored-by: jeff-dude <102681548+jeff-dude@users.noreply.github.com>

* Address feedback of jeff

* Move unique combo test to safe txs spell

* Update models/safe/zkevm/safe_zkevm_schema.yml

Co-authored-by: jeff-dude <102681548+jeff-dude@users.noreply.github.com>

---------

Co-authored-by: Huang Geyang <Sukebeta@outlook.com>
Co-authored-by: jeff-dude <102681548+jeff-dude@users.noreply.github.com>
  • Loading branch information
3 people authored Apr 24, 2024
1 parent 2efee82 commit 64b3cbc
Show file tree
Hide file tree
Showing 13 changed files with 444 additions and 14 deletions.
2 changes: 1 addition & 1 deletion models/safe/celo/safe_celo_transactions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ join {{ ref('safe_celo_safes') }} s
on s.address = tr."from"
join {{ ref('safe_celo_singletons') }} ss
on tr.to = ss.address
join {{ source('ethereum', 'transactions') }} et
join {{ source('celo', 'transactions') }} et
on tr.tx_hash = et.hash
and tr.block_number = et.block_number
where bytearray_substring(tr.input, 1, 4) in (
Expand Down
3 changes: 2 additions & 1 deletion models/safe/safe_native_transfers_all.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{ config(
schema = 'safe',
alias = 'native_transfers_all',
post_hook='{{ expose_spells(\'["arbitrum","avalanche_c","base","bnb","celo","ethereum","gnosis","goerli","optimism","polygon","zksync"]\',
post_hook='{{ expose_spells(\'["arbitrum","avalanche_c","base","bnb","celo","ethereum","gnosis","goerli","optimism","polygon","zkevm","zksync"]\',
"project",
"safe",
\'["kryptaki", "danielpartida"]\') }}'
Expand All @@ -19,6 +19,7 @@
,ref('safe_goerli_eth_transfers')
,ref('safe_optimism_eth_transfers')
,ref('safe_polygon_matic_transfers')
,ref('safe_zkevm_matic_transfers')
,ref('safe_zksync_eth_transfers')
] %}

Expand Down
3 changes: 2 additions & 1 deletion models/safe/safe_safes_all.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{ config(
schema = 'safe',
alias = 'safes_all',
post_hook='{{ expose_spells(\'["arbitrum","avalanche_c","base","bnb","celo","ethereum","fantom","gnosis","goerli","optimism","polygon","zksync"]\',
post_hook='{{ expose_spells(\'["arbitrum","avalanche_c","base","bnb","celo","ethereum","fantom","gnosis","goerli","optimism","polygon","zkevm","zksync"]\',
"project",
"safe",
\'["tschubotz", "danielpartida", "kryptaki"]\') }}'
Expand All @@ -20,6 +20,7 @@
,ref('safe_goerli_safes')
,ref('safe_optimism_safes')
,ref('safe_polygon_safes')
,ref('safe_zkevm_safes')
,ref('safe_zksync_safes')
] %}

Expand Down
16 changes: 8 additions & 8 deletions models/safe/safe_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ version: 2
models:
- name: safe_safes_all
meta:
blockchain: arbitrum, avalanche_c, base, bnb, celo, ethereum, fantom, gnosis, goerli, optimism, polygon, zksync
blockchain: arbitrum, avalanche_c, base, bnb, celo, ethereum, fantom, gnosis, goerli, optimism, polygon, zkevm, zksync
project: safe
contributors: tschubotz, danielpartida, kryptaki
config:
tags: ['arbitrum', 'avalanche_c', 'bnb', 'celo','ethereum', 'fantom', 'gnosis', 'goerli', 'optimism', 'polygon', 'safe','zksync']
tags: ['arbitrum', 'avalanche_c', 'bnb', 'celo','ethereum', 'fantom', 'gnosis', 'goerli', 'optimism', 'polygon', 'safe', 'zkevm', 'zksync']
description: >
Lists all Safes across chains.
columns:
Expand All @@ -32,14 +32,14 @@ models:

- name: safe_transactions_all
meta:
blockchain: arbitrum, avalanche_c, base, bnb, celo, ethereum, fantom, gnosis, goerli, optimism, polygon, zksync
blockchain: arbitrum, avalanche_c, base, bnb, celo, ethereum, fantom, gnosis, goerli, optimism, polygon, zkevm, zksync
project: safe
contributors: kryptaki
contributors: kryptaki, danielpartida
freshness:
warn_after: { count: 12, period: hour }
error_after: { count: 24, period: hour }
config:
tags: ['arbitrum', 'avalanche_c', 'bnb', 'celo','ethereum', 'fantom', 'gnosis', 'goerli', 'optimism', 'polygon', 'safe', 'zksync']
tags: ['arbitrum', 'avalanche_c', 'bnb', 'celo','ethereum', 'fantom', 'gnosis', 'goerli', 'optimism', 'polygon', 'safe', 'zkevm', 'zksync']
description: "Safe transactions"
columns:
- *blockchain
Expand Down Expand Up @@ -94,14 +94,14 @@ models:

- name: safe_native_transfers_all
meta:
blockchain: arbitrum, avalanche_c, base, bnb, celo, ethereum, gnosis, goerli, optimism, polygon, zksync
blockchain: arbitrum, avalanche_c, base, bnb, celo, ethereum, gnosis, goerli, optimism, polygon, zkevm, zksync
project: safe
contributors: tschubotz
contributors: kryptaki, tschubotz, danielpartida
freshness:
warn_after: { count: 12, period: hour }
error_after: { count: 24, period: hour }
config:
tags: ['arbitrum', 'avalanche_c', 'bnb', 'celo','ethereum', 'gnosis', 'goerli', 'optimism', 'polygon', 'safe', 'zksync']
tags: ['arbitrum', 'avalanche_c', 'bnb', 'celo','ethereum', 'gnosis', 'goerli', 'optimism', 'polygon', 'safe', 'zkevm', 'zksync']
description: "Native gas token transfers into or out of Safes"
tests:
- dbt_utils.unique_combination_of_columns:
Expand Down
3 changes: 2 additions & 1 deletion models/safe/safe_transactions_all.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{ config(
schema = 'safe',
alias = 'transactions_all',
post_hook='{{ expose_spells(\'["arbitrum","avalanche_c","base","bnb","celo","ethereum","fantom","gnosis","goerli","optimism","polygon","zksync"]\',
post_hook='{{ expose_spells(\'["arbitrum","avalanche_c","base","bnb","celo","ethereum","fantom","gnosis","goerli","optimism","polygon","zkevm","zksync"]\',
"project",
"safe",
\'["kryptaki", "danielpartida"]\') }}'
Expand All @@ -20,6 +20,7 @@
,ref('safe_goerli_transactions')
,ref('safe_optimism_transactions')
,ref('safe_polygon_transactions')
,ref('safe_zkevm_transactions')
,ref('safe_zksync_transactions')
] %}

Expand Down
82 changes: 82 additions & 0 deletions models/safe/zkevm/safe_zkevm_matic_transfers.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{{
config(
materialized='incremental',
schema = 'safe_zkevm',
alias = 'matic_transfers',
partition_by = ['block_month'],
unique_key = ['block_date', 'address', 'tx_hash', 'trace_address'],
on_schema_change='fail',
file_format ='delta',
incremental_strategy='merge',
post_hook = '{{ expose_spells(
blockchains = \'["zkevm"]\',
spell_type = "project",
spell_name = "safe",
contributors = \'["danielpartida"]\') }}'
)
}}

{% set project_start_date = '2023-09-01' %}

select
t.*,
p.price * t.amount_raw / 1e18 AS amount_usd

from (
select
'zkevm' as blockchain,
'MATIC' as symbol,
s.address,
try_cast(date_trunc('day', et.block_time) as date) as block_date,
CAST(date_trunc('month', et.block_time) as DATE) as block_month,
et.block_time,
-CAST(et.value AS INT256) as amount_raw,
et.tx_hash,
array_join(et.trace_address, ',') as trace_address
from {{ source('zkevm', 'traces') }} et
join {{ ref('safe_zkevm_safes') }} s on et."from" = s.address
and et."from" != et.to -- exclude calls to self to guarantee unique key property
and et.success = true
and (lower(et.call_type) not in ('delegatecall', 'callcode', 'staticcall') or et.call_type is null)
and et.value > UINT256 '0' -- et.value is uint256 type
{% if not is_incremental() %}
where et.block_time > TIMESTAMP '{{project_start_date}}' -- for initial query optimisation
{% endif %}
{% if is_incremental() %}
-- to prevent potential counterfactual safe deployment issues we take a bigger interval
where et.block_time > date_trunc('day', now() - interval '10' day)
{% endif %}

union all

select
'zkevm' as blockchain,
'MATIC' as symbol,
s.address,
try_cast(date_trunc('day', et.block_time) as date) as block_date,
CAST(date_trunc('month', et.block_time) as DATE) as block_month,
et.block_time,
CAST(et.value AS INT256) as amount_raw,
et.tx_hash,
array_join(et.trace_address, ',') as trace_address
from {{ source('zkevm', 'traces') }} et
join {{ ref('safe_zkevm_safes') }} s on et.to = s.address
and et."from" != et.to -- exclude calls to self to guarantee unique key property
and et.success = true
and (lower(et.call_type) not in ('delegatecall', 'callcode', 'staticcall') or et.call_type is null)
and et.value > UINT256 '0' -- et.value is uint256 type
{% if not is_incremental() %}
where et.block_time > TIMESTAMP '{{project_start_date}}' -- for initial query optimisation
{% endif %}
{% if is_incremental() %}
-- to prevent potential counterfactual safe deployment issues we take a bigger interval
where et.block_time > date_trunc('day', now() - interval '10' day)
{% endif %}
) t

left join {{ source('prices', 'usd') }} p on p.blockchain is null
{% if is_incremental() %}
and {{ incremental_predicate('p.minute') }}
{% endif %}
and p.symbol = t.symbol
and p.minute = date_trunc('minute', t.block_time)
49 changes: 49 additions & 0 deletions models/safe/zkevm/safe_zkevm_safes.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{{
config(
materialized='incremental',
schema = 'safe_zkevm',
alias= 'safes',
partition_by = ['block_month'],
unique_key = ['block_date', 'address'],
on_schema_change='fail',
file_format ='delta',
incremental_strategy='merge',
post_hook = '{{ expose_spells(
blockchains = \'["zkevm"]\',
spell_type = "project",
spell_name = "safe",
contributors = \'["danielpartida"]\') }}'
)
}}

select
'zkevm' as blockchain,
et."from" as address,
case
when et.to = 0xd9db270c1b5e3bd161e8c8503c55ceabee709552 then '1.3.0'
when et.to = 0x3e5c63644e683549055b9be8653de26e0b4cd36e then '1.3.0L2'
when et.to = 0x41675C099F32341bf84BFc5382aF534df5C7461a then '1.4.1'
when et.to = 0x29fcB43b46531BcA003ddC8FCB67FFE91900C762 then '1.4.1L2'
else 'unknown'
end as creation_version,
try_cast(date_trunc('day', et.block_time) as date) as block_date,
CAST(date_trunc('month', et.block_time) as DATE) as block_month,
et.block_time as creation_time,
et.tx_hash
from {{ source('zkevm', 'traces') }} et
join {{ ref('safe_zkevm_singletons') }} s
on et.to = s.address
where et.success = true
and et.call_type = 'delegatecall' -- delegatecall to singleton is Safe (proxy) address
and bytearray_substring(et.input, 1, 4) in (
0x0ec78d9e, -- setup method v0.1.0
0xa97ab18a, -- setup method v1.0.0
0xb63e800d -- setup method v1.1.0, v1.1.1, v1.2.0, v1.3.0, v1.3.0L2, v1.4.1, v.1.4.1L2
)
and et.gas_used > 10000 -- to ensure the setup call was successful. excludes e.g. setup calls with missing params that fallback
{% if not is_incremental() %}
and et.block_time > TIMESTAMP '2023-09-01' -- for initial query optimisation
{% endif %}
{% if is_incremental() %}
and et.block_time > date_trunc('day', now() - interval '7' day)
{% endif %}
Loading

0 comments on commit 64b3cbc

Please sign in to comment.