Skip to content

Commit

Permalink
Add zksync transactions to safes (duneanalytics#4939)
Browse files Browse the repository at this point in the history
* Add query for zksync transaction spell

* Fixed typo

* Removed extra with

* Fix where clause

* Fixed typo

* Assigned missing data types

* Add missing column names

* Updated zksync schema

* Added zksync transactions to cross-chain spell

* Fixed duplicate issue

* Fixed duplicate issue

* Added schema and replaced left join with inner joint

* Add uniqueness test

* Update safe_zksync_transactions.sql

Updated gas variables and removed join on singleton.

* Update safe_zksync_schema.yml

Updated schema with gas variables

* Update safe_zksync_transactions.sql

Fixed gas variables

* Update safe_zksync_transactions.sql

Fixed type error.

* Update safe_zksync_transactions.sql

Cleaned up code and where clauses

* Update safe_zksync_schema.yml

Fixed uniqueness test

* Update safe_zksync_transactions.sql

Add unique key for test

* Update safe_zksync_schema.yml

Remove tests (added in transaction query directly)

* Update safe_zksync_transactions.sql

removed duplicated setting

* Update safe_zksync_schema.yml

Updated schema with unique_key and test

* Update safe_zksync_transactions.sql

Fix unique key
  • Loading branch information
alicecha authored Dec 15, 2023
1 parent 700c59e commit 7017495
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 4 deletions.
4 changes: 2 additions & 2 deletions models/safe/safe_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ models:

- name: safe_transactions_all
meta:
blockchain: arbitrum, avalanche_c, base, bnb, celo, ethereum, fantom, gnosis, goerli, optimism, polygon
blockchain: arbitrum, avalanche_c, base, bnb, celo, ethereum, fantom, gnosis, goerli, optimism, polygon, zksync
project: safe
contributors: kryptaki
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']
tags: ['arbitrum', 'avalanche_c', 'bnb', 'celo','ethereum', 'fantom', 'gnosis', 'goerli', 'optimism', 'polygon', 'safe', 'zksync']
description: "Safe transactions"
columns:
- *blockchain
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"]\',
post_hook='{{ expose_spells(\'["arbitrum","avalanche_c","base","bnb","celo","ethereum","fantom","gnosis","goerli","optimism","polygon","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_zksync_transactions')
] %}


Expand Down
73 changes: 72 additions & 1 deletion models/safe/zksync/safe_zksync_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,75 @@ models:
description: "Safe contract address"
tests:
- unique
- not_null
- not_null

- name: safe_zksync_transactions
meta:
blockchain: zksync
project: safe
contributors: kryptaki
freshness:
warn_after: { count: 12, period: hour }
error_after: { count: 24, period: hour }
config:
tags: ['safe', 'zksync']
description: "Safe transactions"
columns:
- *blockchain
- *block_date
- *block_month
- *block_time
- &block_number
name: block_number
description: "Number of block"
- *tx_hash
- name: address
description: "Safe contract address"
- &to
name: to
description: "Null - should be singleton address"
- &value
name: value
description: "Value of transaction"
- &gas
name: gas
description: "Gas limit set for transaction"
- &execution_gas_used
name: execution_gas_used
description: "Execution gas used during transaction, for more details see https://dune.com/docs/data-tables/raw/evm/traces/?h=traces#gas-used-in-traces"
- &total_gas_used
name: total_gas_used
description: "Total gas used during transaction"
- &tx_index
name: tx_index
description: "Transaction index"
- &sub_traces
name: sub_traces
description: "Null - retrievable in traces"
- *trace_address
- &success
name: success
description: "Success state of transaction"
- &error
name: error
description: "Error of transaction if any"
- &code
name: code
description: "Code"
- &input
name: input
description: "Input data"
- &output
name: output
description: "Output data"
- &method
name: method
description: "Function method"
- &trace_tx_hash
name: trace_tx_hash
description: "Tx hash to retrieve traces"
- &unique_key
name: unique_key
description: unique row identifier
tests:
- unique
106 changes: 106 additions & 0 deletions models/safe/zksync/safe_zksync_transactions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
{{
config(
materialized='incremental',
schema = 'safe_zksync',
alias = 'transactions',
partition_by = ['block_month'],
file_format ='delta',
incremental_strategy='merge',
unique_key = ['unique_key'],
post_hook='{{ expose_spells(\'["zksync"]\',
"project",
"safe",
\'["kryptaki"]\') }}'
)
}}

--add missing columns, remove unneeded columns from traces

with transactions as (
select
'zksync' as blockchain,
try_cast(date_trunc('day', t.block_time) as date) as block_date,
CAST(date_trunc('month', t.block_time) as DATE) as block_month,
t.block_time,
t.block_number,
t.hash as tx_hash,
s.address,
null as "to", --for other chains, this is the singleton address but on zksync this is the recipient address. Leaving this blank for now for consistency.
t.gas_used as total_gas_used,
t.index as tx_index,
t.success,
t.data as input,
null as "output",
case
when bytearray_substring(t.data, 1, 4) = 0x6a761202 then 'execTransaction'
else 'unknown'
end as method
from {{ source('zksync', 'transactions') }} t
join {{ ref('safe_zksync_safes') }} s
on s.address = t.to --zksync execTransactions recorded as tx "to" the Safe, not "from"
where
bytearray_substring(t.data, 1, 4) = 0x6a761202 -- execTransaction
and t.success = true
{% if not is_incremental() %}
and t.block_time > TIMESTAMP '2023-09-01' -- for initial query optimisation
{% endif %}
{% if is_incremental() %}
and t.block_time > date_trunc('day', now() - interval '7' day)
{% endif %}
),

traces as (
select
tr.block_number,
tr.tx_hash,
s.address,
tr.value,
tr.sub_traces, --may introduce some duplicates, not sure (there are duplicates for ETH transfers to Safes)
tr.trace_address, --see above
tr.error,
tr.code,
tr.gas,
tr.gas_used AS execution_gas_used,
tr.success
from {{ source('zksync', 'traces') }} tr
join {{ ref('safe_zksync_safes') }} s
on s.address = tr."from"
where
bytearray_length(bytearray_ltrim(tr.to)) > 4 AND
{% if not is_incremental() %}
tr.block_time > TIMESTAMP '2023-09-01' -- for initial query optimisation
{% endif %}
{% if is_incremental() %}
tr.block_time > date_trunc('day', now() - interval '7' day)
{% endif %}
)

select
t.blockchain,
t.block_date,
t.block_month,
t.block_time,
t.block_number,
t.tx_hash,
t.address,
cast(t.to as varbinary) as "to", --for other chains, this is the singleton address, but not available in zksync.transactions or zksync.traces so keeping as null for consistency
tr.value, --get value from traces (0 in transactions table)
tr.gas,
tr.execution_gas_used,
t.total_gas_used,
t.tx_index,
tr.sub_traces,
tr.trace_address,
t.success,
tr.error,
tr.code,
t.input, --get input from transactions (because contains the methodID)
cast(t.output as varbinary) as "output",
t.method,
tr.tx_hash as trace_tx_hash, --save the trace_tx_hash to match back on
{{dbt_utils.generate_surrogate_key(["t.tx_hash", "array_join(tr.trace_address, ',')", "gas"])}} as unique_key
from transactions t
inner join traces tr ON
tr.block_number = t.block_number
AND tr.success = t.success
AND tr.address = t.address --matching on safe address

0 comments on commit 7017495

Please sign in to comment.