Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[babylon] babylon stake outputs #7334

Closed
wants to merge 14 commits into from
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

{{
config(
schema = 'babylon_btc'
,alias = 'finality_providers'
,materialized = 'table'
,post_hook='{{ expose_spells(\'["bitcoin"]\',
"project",
"babylon",
\'["maybeYonas", "pyor_xyz"]\') }}'
)
}}

select
json_extract_scalar(j, '$.active_delegations') as active_delegations,
json_extract_scalar(j, '$.active_tvl') as active_tvl,
json_extract_scalar(j, '$.btc_pk') as btc_pk,
json_extract_scalar(j, '$.commission') as commission,
-- json_extract_scalar(j, '$.description') as description,
json_extract_scalar(j, '$.description.details') as details,
json_extract_scalar(j, '$.description.identity') as identity,
json_extract_scalar(j, '$.description.moniker') as moniker,
json_extract_scalar(j, '$.description.security_contact') as security_contact,
json_extract_scalar(j, '$.description.website') as website,
json_extract_scalar(j, '$.total_delegations') as total_delegations,
json_extract_scalar(j, '$.total_tvl') as total_tvl
from unnest(
cast(
json_extract(
cast(json_parse(
http_get('https://staking-api.babylonlabs.io/v1/finality-providers?pagination_key='))
as json),
'$.data'
) as array(json)
)
-- )
) as t(j)
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
{{ config(
schema = 'babylon_btc',
alias = 'staking_outputs',
materialized = 'incremental',
file_format = 'delta',
incremental_strategy = 'merge',
unique_key = ['tx_id'],
post_hook='{{ expose_spells(\'["bitcoin"]\',
"project",
"babylon",
\'["maybeYonas", "pyor_xyz"]\') }}'
)
}}

with
latest_block as (
select max(block_height) as max_block
from {{ source('bitcoin', 'transactions') }}
),
{% if is_incremental() %}
max_timestamp as (
select max(block_time) as max_block_time
from {{this}}
),
{% endif %}
outputs as (
select
o.block_time,
o.block_height,
o.tx_id,
o.value,
o.address,
o.type,
o.script_hex,
o.index
from {{ source('bitcoin', 'outputs') }} o
where block_time > date'2024-08-22'
and block_height >= 857909
{% if is_incremental() %}
and block_time > (select max_block_time from max_timestamp)

union all
select
o.block_time,
o.block_height,
o.tx_id,
o.value,
o.address,
o.type,
o.script_hex,
0 as index
from {{this}} o
where unstake_tx_id is null
{% endif %}
),
inputs as (
select
i.tx_id,
i.spent_tx_id,
i.block_time,
i.address
from {{ source('bitcoin', 'inputs') }} i
where block_time > date'2024-08-22'
and block_height >= 857909
{% if is_incremental() %}
and block_time > (select max_block_time from max_timestamp)
{% endif %}
),
transactions as (
select id,index
from {{ source('bitcoin', 'transactions') }}
where block_time > date'2024-08-22'
and block_height >= 857909
{% if is_incremental() %}
and block_time > (select max_block_time from max_timestamp)

union all
select
i.tx_id as id,
i.index
from {{this}} i
where unstake_tx_id is null
{% endif %}
),
restaking_txs as (
select
o.tx_id,

substr(o.script_hex,7,1) as version,
substr(o.script_hex,8,32) as staker,
substr(o.script_hex,40,32) as finality_provider,
substr(o.script_hex,72,2) as stakingtime,

t.index
-- stakingtime()
from outputs o
join transactions t
on o.tx_id = t.id
-- and block_height = 853843
-- and tx_id = 0x45f9cbcc4b4a5b58feef62e8462199c6079d42e24130f155594c9af8904e0f0d
where substr(script_hex, 1,6) = 0x6a4762626e31
),
restake_info as (
select
o.block_time,
o.block_height,
o.tx_id,
o.value,
o.address,
o.type,
r.version,
r.staker,
r.finality_provider,
r.stakingtime,
r.index,
d.moniker as finality_provider_name,

i.tx_id as unstake_tx_id,
i.block_time as unstake_block_time,
o.script_hex
from outputs o
join restaking_txs r
on o.tx_id = r.tx_id
left join {{ref('babylon_bitcoin_finality_providers')}} d
on r.finality_provider = from_hex('0x'||d.btc_pk)
left join inputs i
on o.tx_id = i.spent_tx_id
and o.address = i.address
where o.index = 0
),
data as (
select i.*,
sum(value) over(order by block_height,index) as total_btc,
case
when i.block_height < l.max_block-6
then 'confirmed'
else 'pending'
end as status
from restake_info i,
latest_block l
)

select
block_time,
block_height,
tx_id,
value,
address,
type,
version,
staker,
finality_provider,
stakingtime,
index,
finality_provider_name,
unstake_tx_id,
unstake_block_time,
total_btc,
status,
case when
total_btc - value < 1000
or
(block_height >= 864790 and block_height <= 864799)
or
(block_height >= 874088 and block_height <= 875087)
then 'accepted'
else 'overflow'
end as babylon_acceptance,
case
when unstake_tx_id is not null
then 'unstaked'
else 'staked'
end as babylon_status,
script_hex
from data
117 changes: 117 additions & 0 deletions dbt_subprojects/daily_spellbook/models/babylon/bitcoin/schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
version: 2

models:
- name: babylon_bitcoin_finality_providers

meta:
blockchain: bitcoin
sector: restaking
project: babylon
contributors: maybeYonas, pyor_xyz

config:
tags: ['babylon', 'bitcon']
description: "json dump of finality providers from babylon api -> 'https://staking-api.babylonlabs.io/v1/finality-providers?pagination_key='"
data_tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- btc_pk
columns:
- name: active_delegations
description: "active delegations"
- name: active_tvl
description: "active tvl"
- name: btc_pk
description: "private key of the finality provider, serves are unique id"
- name: commission
description: "% charged by the finality providers on rewards ?"
- name: details
description: "description of finality provider"
- name: identity
description: "another unique id ?"
- name: moniker
description: "name of finality provider"
- name: security_contact
description: "email to send 'wen airdrop'"
- name: website
description: "website of finality provider"
- name: total_delegations
description: "total delegations"
- name: total_tvl
description: "total tvl"

- name: babylon_bitcoin_staking_outputs

meta:
blockchain: bitcoin
sector: restaking
project: babylon
contributors: maybeYonas, pyor_xyz

config:
tags: ['babylon', 'bitcon']
description: "all babylon bitcoin staking outputs and their statuses"
data_tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- tx_id
- check_seed:
seed_file: ref('babylon_bitcoin_staking_outputs_seed')
match_columns:
- tx_id
check_columns:
- block_time
- block_height
- value
- address
- type
- version
- staker
- finality_provider
- stakingtime
- index
- unstake_tx_id
- unstake_block_time
- status
- babylon_acceptance
- babylon_status

columns:
- name: block_time
description: ""
- name: block_height
description: ""
- name: tx_id
description: ""
- name: value
description: ""
- name: address
description: ""
- name: type
description: ""
- name: version
description: ""
- name: staker
description: ""
- name: finality_provider
description: ""
- name: stakingtime
description: ""
- name: index
description: ""
- name: finality_provider_name
description: ""
- name: unstake_tx_id
description: ""
- name: unstake_block_time
description: ""
- name: total_btc
description: ""
- name: status
description: ""
- name: babylon_acceptance
description: ""
- name: babylon_status
description: ""
- name: script_hex
description: ""
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
block_time,block_height,tx_id,value,address,type,version,staker,finality_provider,stakingtime,index,unstake_tx_id,unstake_block_time,status,babylon_acceptance,babylon_status
2024-08-22 13:53:33.000 UTC,857916,0x35447d44090cba9576059c6851c21934c4e39187de4c701f852e25a6b5f031e4,0.05,bc1pzww2cq5c2dsf6ysf59ztcpq3hqhtx56xskvhyx07c8cj3psm4wws9hpws0,witness_v1_taproot,0x00,0xf3576d06e72b728dc8f74f4527ad363e8d0d2f484b926361864eec950479f48e,0x0f5c19935a08f661a1c4dfeb5e51ce7f0cfcf4d2eeb405fe4c7d7bd668fc85e4,0xfa00,2347,0xb9b493f2ea1f492641d6ed2a27f510bb0b644def0c9135ddf00c155179a684ed,2024-09-05 14:21:53.000 UTC,confirmed,overflow,unstaked
2024-08-22 13:53:33.000 UTC,857916,0xe6b4a76ad90214e50cb290da3e467f393f52bd7fcedc35587cac3c731f7f3ae4,0.05,bc1p3y6glg7tjnmvpu0hu4u4f09uhtz3phjdxgxlgvlv7pz05m8p476st0nyrh,witness_v1_taproot,0x00,0x47a76a0b9faab338351b844df017207e361bba7dc37d0428d1e5fd8ac426ca5b,0x0f5c19935a08f661a1c4dfeb5e51ce7f0cfcf4d2eeb405fe4c7d7bd668fc85e4,0xfa00,2348,0xf38960db423feb08b95d9124aeb7a8e62321c11fd09520702cca775289897541,2024-09-05 15:40:34.000 UTC,confirmed,overflow,unstaked
2024-12-11 06:57:37.000 UTC,874220,0xc809535a9b76d4ec255c096c41ce2d18037e8b059884e93f11624b5c2e15d57a,0.69583785,bc1qqws0734qnlxhrygtvw5y7vxywfeqdqq7gpvccs,witness_v0_keyhash,0x00,0x73aceffb9a3ce79fa352a23211541e8748259071548213a901040e4e6a59fa77,0x289719ef19e455816e474588007f1824df7f6b1bace7decf8bb1c435e4cb849e,0xfa00,34,0x07c6cbeabd7dcb3039db5dfc19ab6058ce30d25ff337ae7a5859052a28ed993d,2024-12-13 05:26:29.000 UTC,confirmed,accepted,unstaked
2024-12-11 07:04:39.000 UTC,874223,0xffe790f4ee53e53bba8014775d3538938c2ca37559f9ab36e7a5d15118b49588,0.005,bc1pqrugear5fz6r2dpsq748v5klpvggkwf0820qlgj3fy4rqyg5wlpq3fr6rs,witness_v1_taproot,0x00,0xeea88f3cc59307847c900795fdba771b2e35db1650b4358418bbaf0cfd00f965,0x2877658a1e7608fe84543a75ca879334bb8f2e7aecae71adb7767192bda50739,0xfa00,900,0x96b5ea74f8c02a618920cbdebe867f888d11d7f38351eb1fa859b264f6615cdf,2024-12-11 11:02:32.000 UTC,confirmed,accepted,unstaked
22 changes: 22 additions & 0 deletions dbt_subprojects/daily_spellbook/seeds/babylon/bitcoin/schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: 2

seeds:
- name: babylon_bitcoin_staking_outputs_seed
config:
column_types:
block_time: timestamp
block_height: bigint
tx_id: varbinary
value: double
address: varchar
type: varchar
version: varbinary
staker: varbinary
finality_provider: varbinary
stakingtime: varbinary
index: integer
unstake_tx_id: varbinary
unstake_block_time: timestamp
status: varchar
babylon_acceptance: varchar
babylon_status: varchar
Loading