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 dr .tx_id 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+ dr .tx_id
42+ ),
43+ current_epoch AS (
244 SELECT
345 Max (NO) AS no
446 FROM
@@ -36,11 +78,74 @@ total_drep_votes AS (
3678 WHERE
3779 voter_role = ' DRep'
3880),
81+ total_stake_controlled_by_dreps AS (
82+ SELECT
83+ SUM (dd .amount )::bigint AS total
84+ FROM
85+ drep_distr dd
86+ ),
87+ total_registered_direct_voters AS (
88+ SELECT
89+ COUNT (DISTINCT dh .raw ) AS unique_direct_voters
90+ FROM
91+ drep_registration dr
92+ JOIN
93+ drep_hash dh
94+ ON
95+ dr .drep_hash_id = dh .id
96+ LEFT JOIN
97+ voting_anchor va
98+ ON
99+ dr .voting_anchor_id = va .id
100+ WHERE
101+ dr .deposit > 0
102+ AND va .url IS NULL
103+ ),
39104total_registered_dreps AS (
105+ SELECT
106+ count (DISTINCT dh .raw ) AS unique_registrations
107+ FROM
108+ drep_registration dr
109+ JOIN
110+ drep_hash dh
111+ ON
112+ dr .drep_hash_id = dh .id
113+ WHERE
114+ dr .deposit > 0
115+ ),
116+ total_active_dreps AS (
40117 SELECT
41- count (* ) AS count
118+ count (DISTINCT drep_hash_raw) AS unique_active_drep_registrations
119+ FROM
120+ RankedDRep
121+ WHERE
122+ epoch_no >= (SELECT epoch_no FROM active_drep_boundary_epoch)
123+ AND rank = 1
124+ ),
125+ total_inactive_dreps AS (
126+ SELECT
127+ total_registered_dreps .unique_registrations - total_active_dreps .unique_active_drep_registrations AS total_inactive_dreps
42128 FROM
43- drep_hash
129+ total_registered_dreps
130+ CROSS JOIN
131+ total_active_dreps
132+ ),
133+ total_active_cip119_compliant_dreps AS (
134+ SELECT
135+ count (DISTINCT drep_hash_raw) AS unique_active_cip119_compliant_drep_registrations
136+ FROM
137+ RankedDRep
138+ JOIN
139+ voting_anchor va on va .id = RankedDRep .voting_anchor_id
140+ JOIN off_chain_vote_data ocvd on ocvd .voting_anchor_id = va .id
141+ JOIN off_chain_vote_drep_data ocvdd on ocvdd .off_chain_vote_data_id = ocvd .id
142+ WHERE
143+ -- given_name is the only compulsory field in CIP-119
144+ ocvdd .given_name IS NOT NULL
145+ AND
146+ epoch_no >= (SELECT epoch_no FROM active_drep_boundary_epoch)
147+ AND
148+ rank = 1
44149),
45150always_abstain_voting_power AS (
46151 SELECT
@@ -63,15 +168,20 @@ always_no_confidence_voting_power AS (
63168 drep_hash .view = ' drep_always_no_confidence' ORDER BY epoch_no DESC LIMIT 1 ), 0 ) AS amount
64169)
65170SELECT
66- current_epoch .no ,
171+ current_epoch .no as epoch_no ,
67172 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 ,
173+ unique_delegators .count as unique_delegators,
174+ total_delegations .count as total_delegations,
175+ total_gov_action_proposals .count as total_gov_action_proposals,
176+ total_drep_votes .count as total_drep_votes,
177+ total_registered_dreps .unique_registrations as total_registered_dreps,
178+ total_stake_controlled_by_dreps .total as total_stake_controlled_by_dreps,
179+ total_active_dreps .unique_active_drep_registrations as total_active_dreps,
180+ total_inactive_dreps .total_inactive_dreps as total_inactive_dreps,
181+ total_active_cip119_compliant_dreps .unique_active_cip119_compliant_drep_registrations as total_active_cip119_compliant_dreps,
182+ total_registered_direct_voters .unique_direct_voters as total_registered_direct_voters,
183+ always_abstain_voting_power .amount as always_abstain_voting_power,
184+ always_no_confidence_voting_power .amount as always_no_confidence_voting_power,
75185 network_name
76186FROM
77187 current_epoch
81191 CROSS JOIN total_gov_action_proposals
82192 CROSS JOIN total_drep_votes
83193 CROSS JOIN total_registered_dreps
194+ CROSS JOIN total_stake_controlled_by_dreps
195+ CROSS JOIN total_active_dreps
196+ CROSS JOIN total_inactive_dreps
197+ CROSS JOIN total_active_cip119_compliant_dreps
198+ CROSS JOIN total_registered_direct_voters
84199 CROSS JOIN always_abstain_voting_power
85200 CROSS JOIN always_no_confidence_voting_power
86- CROSS JOIN meta;
201+ CROSS JOIN meta
202+ GROUP BY
203+ current_epoch .no ,
204+ current_block .block_no ,
205+ unique_delegators .count ,
206+ total_delegations .count ,
207+ total_gov_action_proposals .count ,
208+ total_drep_votes .count ,
209+ total_registered_dreps .unique_registrations ,
210+ total_stake_controlled_by_dreps .total ,
211+ total_active_dreps .unique_active_drep_registrations ,
212+ total_inactive_dreps .total_inactive_dreps ,
213+ total_active_cip119_compliant_dreps .unique_active_cip119_compliant_drep_registrations ,
214+ total_registered_direct_voters .unique_direct_voters ,
215+ always_abstain_voting_power .amount ,
216+ always_no_confidence_voting_power .amount ,
217+ network_name;
218+
0 commit comments