1- WITH current_epoch AS (
1+ WITH DRepActivity AS (
2+ SELECT
3+ drep_activity AS drep_activity,
4+ epoch_no AS epoch_no
5+ FROM
6+ epoch_param
7+ WHERE
8+ epoch_no IS NOT NULL
9+ ORDER BY
10+ epoch_no DESC
11+ LIMIT 1
12+ ),
13+ active_drep_boundary_epoch AS (
14+ SELECT
15+ epoch_no - drep_activity AS epoch_no
16+ FROM
17+ DRepActivity
18+ ),
19+ RankedDRep AS (
20+ SELECT
21+ dh .raw AS drep_hash_raw,
22+ b .epoch_no ,
23+ dr .deposit ,
24+ dr .voting_anchor_id ,
25+ ROW_NUMBER() OVER (PARTITION BY dh .raw ORDER BY b .epoch_no DESC ) AS rank
26+ FROM
27+ drep_hash dh
28+ JOIN
29+ drep_registration dr ON dh .id = dr .drep_hash_id
30+ JOIN
31+ tx t ON dr .tx_id = t .id
32+ JOIN
33+ block b ON t .block_id = b .id
34+ WHERE
35+ dr .deposit >= 0
36+ GROUP BY
37+ dh .raw ,
38+ b .epoch_no ,
39+ dr .voting_anchor_id ,
40+ dr .deposit
41+ ),
42+ current_epoch AS (
243 SELECT
344 Max (NO) AS no
445 FROM
@@ -36,11 +77,70 @@ total_drep_votes AS (
3677 WHERE
3778 voter_role = ' DRep'
3879),
80+ total_stake_controlled_by_dreps AS (
81+ SELECT
82+ SUM (dd .amount )::bigint AS total
83+ FROM
84+ drep_distr dd
85+ ),
86+ total_registered_direct_voters AS (
87+ SELECT
88+ COUNT (DISTINCT dh .raw ) AS unique_direct_voters
89+ FROM
90+ drep_registration dr
91+ JOIN
92+ drep_hash dh
93+ ON
94+ dr .drep_hash_id = dh .id
95+ LEFT JOIN
96+ voting_anchor va
97+ ON
98+ dr .voting_anchor_id = va .id
99+ WHERE
100+ dr .deposit > 0
101+ AND va .url IS NULL
102+ ),
39103total_registered_dreps AS (
104+ SELECT
105+ count (DISTINCT dh .raw ) AS unique_registrations
106+ FROM
107+ drep_registration dr
108+ JOIN
109+ drep_hash dh
110+ ON
111+ dr .drep_hash_id = dh .id
112+ WHERE
113+ dr .deposit > 0
114+ ),
115+ total_active_dreps AS (
40116 SELECT
41- count (* ) AS count
117+ count (DISTINCT drep_hash_raw) AS unique_active_drep_registrations
118+ FROM
119+ RankedDRep
120+ WHERE
121+ epoch_no >= (SELECT epoch_no FROM active_drep_boundary_epoch)
122+ AND rank = 1
123+ ),
124+ total_inactive_dreps AS (
125+ SELECT
126+ total_registered_dreps .unique_registrations - total_active_dreps .unique_active_drep_registrations AS total_inactive_dreps
42127 FROM
43- drep_hash
128+ total_registered_dreps
129+ CROSS JOIN
130+ total_active_dreps
131+ ),
132+ total_active_cip119_compliant_dreps AS (
133+ SELECT
134+ count (DISTINCT drep_hash_raw) AS unique_active_cip119_compliant_drep_registrations
135+ FROM
136+ RankedDRep
137+ JOIN
138+ voting_anchor va on va .id = RankedDRep .voting_anchor_id
139+ JOIN off_chain_vote_data ocvd on ocvd .voting_anchor_id = va .id
140+ JOIN off_chain_vote_drep_data ocvdd on ocvdd .off_chain_vote_data_id = ocvd .id
141+ WHERE
142+ -- given_name is the only compulsory field in CIP-119
143+ ocvdd .given_name IS NOT NULL
44144),
45145always_abstain_voting_power AS (
46146 SELECT
@@ -63,15 +163,20 @@ always_no_confidence_voting_power AS (
63163 drep_hash .view = ' drep_always_no_confidence' ORDER BY epoch_no DESC LIMIT 1 ), 0 ) AS amount
64164)
65165SELECT
66- current_epoch .no ,
166+ current_epoch .no as epoch_no ,
67167 current_block .block_no ,
68- unique_delegators .count ,
69- total_delegations .count ,
70- total_gov_action_proposals .count ,
71- total_drep_votes .count ,
72- total_registered_dreps .count ,
73- always_abstain_voting_power .amount ,
74- always_no_confidence_voting_power .amount ,
168+ unique_delegators .count as unique_delegators,
169+ total_delegations .count as total_delegations,
170+ total_gov_action_proposals .count as total_gov_action_proposals,
171+ total_drep_votes .count as total_drep_votes,
172+ total_registered_dreps .unique_registrations as total_registered_dreps,
173+ total_stake_controlled_by_dreps .total as total_stake_controlled_by_dreps,
174+ total_active_dreps .unique_active_drep_registrations as total_active_dreps,
175+ total_inactive_dreps .total_inactive_dreps as total_inactive_dreps,
176+ total_active_cip119_compliant_dreps .unique_active_cip119_compliant_drep_registrations as total_active_cip119_compliant_dreps,
177+ total_registered_direct_voters .unique_direct_voters as total_registered_direct_voters,
178+ always_abstain_voting_power .amount as always_abstain_voting_power,
179+ always_no_confidence_voting_power .amount as always_no_confidence_voting_power,
75180 network_name
76181FROM
77182 current_epoch
81186 CROSS JOIN total_gov_action_proposals
82187 CROSS JOIN total_drep_votes
83188 CROSS JOIN total_registered_dreps
189+ CROSS JOIN total_stake_controlled_by_dreps
190+ CROSS JOIN total_active_dreps
191+ CROSS JOIN total_inactive_dreps
192+ CROSS JOIN total_active_cip119_compliant_dreps
193+ CROSS JOIN total_registered_direct_voters
84194 CROSS JOIN always_abstain_voting_power
85195 CROSS JOIN always_no_confidence_voting_power
86- CROSS JOIN meta;
196+ CROSS JOIN meta
197+ GROUP BY
198+ current_epoch .no ,
199+ current_block .block_no ,
200+ unique_delegators .count ,
201+ total_delegations .count ,
202+ total_gov_action_proposals .count ,
203+ total_drep_votes .count ,
204+ total_registered_dreps .unique_registrations ,
205+ total_stake_controlled_by_dreps .total ,
206+ total_active_dreps .unique_active_drep_registrations ,
207+ total_inactive_dreps .total_inactive_dreps ,
208+ total_active_cip119_compliant_dreps .unique_active_cip119_compliant_drep_registrations ,
209+ total_registered_direct_voters .unique_direct_voters ,
210+ always_abstain_voting_power .amount ,
211+ always_no_confidence_voting_power .amount ,
212+ network_name;
213+
0 commit comments