Skip to content

Commit bdc59f3

Browse files
committed
feat(#2010): add more DRep metrics to network metrics endpoint
1 parent af1ca81 commit bdc59f3

File tree

7 files changed

+197
-35
lines changed

7 files changed

+197
-35
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ changes.
1212

1313
### Added
1414

15-
-
15+
- Add more useful metrics to the backend GET /network/metrics endpoint [Issue 2010](https://github.com/IntersectMBO/govtool/issues/2010)
1616

1717
### Fixed
1818

govtool/backend/sql/get-network-metrics.sql

Lines changed: 139 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,45 @@
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+
),
39103
total_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
),
45145
always_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
)
65165
SELECT
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
76181
FROM
77182
current_epoch
@@ -81,6 +186,28 @@ FROM
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+

govtool/backend/src/VVA/API.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,11 @@ getNetworkMetrics = do
437437
, getNetworkMetricsResponseTotalGovernanceActions = networkMetricsTotalGovernanceActions
438438
, getNetworkMetricsResponseTotalDRepVotes = networkMetricsTotalDRepVotes
439439
, getNetworkMetricsResponseTotalRegisteredDReps = networkMetricsTotalRegisteredDReps
440+
, getNetworkMetricsResponseTotalStakeControlledByDReps = networkMetricsTotalStakeControlledByDReps
441+
, getNetworkMetricsResponseTotalActiveDReps = networkMetricsTotalActiveDReps
442+
, getNetworkMetricsResponseTotalInactiveDReps = networkMetricsTotalInactiveDReps
443+
, getNetworkMetricsResponseTotalActiveCIP119CompliantDReps = networkMetricsTotalActiveCIP119CompliantDReps
444+
, getNetworkMetricsResponseTotalRegisteredDirectVoters = networkMetricsTotalRegisteredDirectVoters
440445
, getNetworkMetricsResponseAlwaysAbstainVotingPower = networkMetricsAlwaysAbstainVotingPower
441446
, getNetworkMetricsResponseAlwaysNoConfidenceVotingPower = networkMetricsAlwaysNoConfidenceVotingPower
442447
, getNetworkMetricsResponseNetworkName = networkMetricsNetworkName

govtool/backend/src/VVA/API/Types.hs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -878,17 +878,22 @@ instance ToSchema DelegationResponse where
878878

879879
data GetNetworkMetricsResponse
880880
= GetNetworkMetricsResponse
881-
{ getNetworkMetricsResponseCurrentTime :: UTCTime
882-
, getNetworkMetricsResponseCurrentEpoch :: Integer
883-
, getNetworkMetricsResponseCurrentBlock :: Integer
884-
, getNetworkMetricsResponseUniqueDelegators :: Integer
885-
, getNetworkMetricsResponseTotalDelegations :: Integer
886-
, getNetworkMetricsResponseTotalGovernanceActions :: Integer
887-
, getNetworkMetricsResponseTotalDRepVotes :: Integer
888-
, getNetworkMetricsResponseTotalRegisteredDReps :: Integer
889-
, getNetworkMetricsResponseAlwaysAbstainVotingPower :: Integer
890-
, getNetworkMetricsResponseAlwaysNoConfidenceVotingPower :: Integer
891-
, getNetworkMetricsResponseNetworkName :: Text
881+
{ getNetworkMetricsResponseCurrentTime :: UTCTime
882+
, getNetworkMetricsResponseCurrentEpoch :: Integer
883+
, getNetworkMetricsResponseCurrentBlock :: Integer
884+
, getNetworkMetricsResponseUniqueDelegators :: Integer
885+
, getNetworkMetricsResponseTotalDelegations :: Integer
886+
, getNetworkMetricsResponseTotalGovernanceActions :: Integer
887+
, getNetworkMetricsResponseTotalDRepVotes :: Integer
888+
, getNetworkMetricsResponseTotalRegisteredDReps :: Integer
889+
, getNetworkMetricsResponseTotalStakeControlledByDReps :: Integer
890+
, getNetworkMetricsResponseTotalActiveDReps :: Integer
891+
, getNetworkMetricsResponseTotalInactiveDReps :: Integer
892+
, getNetworkMetricsResponseTotalActiveCIP119CompliantDReps :: Integer
893+
, getNetworkMetricsResponseTotalRegisteredDirectVoters :: Integer
894+
, getNetworkMetricsResponseAlwaysAbstainVotingPower :: Integer
895+
, getNetworkMetricsResponseAlwaysNoConfidenceVotingPower :: Integer
896+
, getNetworkMetricsResponseNetworkName :: Text
892897
}
893898

894899
deriveJSON (jsonOptions "getNetworkMetricsResponse") ''GetNetworkMetricsResponse
@@ -903,6 +908,11 @@ exampleGetNetworkMetricsResponse =
903908
<> "\"totalGovernanceActions\": 0,"
904909
<> "\"totalDRepVotes\": 0,"
905910
<> "\"totalRegisteredDReps\": 0,"
911+
<> "\"totalStakeControlledByDReps\": 0,"
912+
<> "\"totalActiveDReps\": 0,"
913+
<> "\"totalInactiveDReps\": 0,"
914+
<> "\"totalActiveCIP119CompliantDReps\": 0,"
915+
<> "\"totalRegisteredDirectVoters\": 0,"
906916
<> "\"alwaysAbstainVotingPower\": 0,"
907917
<> "\"alwaysNoConfidenceVotingPower\": 0,"
908918
<> "\"networkName\": \"Mainnet\"}"

govtool/backend/src/VVA/Network.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ networkMetrics = withPool $ \conn -> do
4242
, total_gov_action_proposals
4343
, total_drep_votes
4444
, total_registered_dreps
45+
, total_stake_controlled_by_dreps
46+
, total_active_dreps
47+
, total_inactive_dreps
48+
, total_active_cip119_compliant_dreps
49+
, total_registered_direct_voters
4550
, always_abstain_voting_power
4651
, always_no_confidence_voting_power
4752
, network_name
@@ -54,6 +59,11 @@ networkMetrics = withPool $ \conn -> do
5459
total_gov_action_proposals
5560
total_drep_votes
5661
total_registered_dreps
62+
total_stake_controlled_by_dreps
63+
total_active_dreps
64+
total_inactive_dreps
65+
total_active_cip119_compliant_dreps
66+
total_registered_direct_voters
5767
always_abstain_voting_power
5868
always_no_confidence_voting_power
5969
network_name

govtool/backend/src/VVA/Types.hs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -202,17 +202,22 @@ data CacheEnv
202202

203203
data NetworkMetrics
204204
= NetworkMetrics
205-
{ networkMetricsCurrentTime :: UTCTime
206-
, networkMetricsCurrentEpoch :: Integer
207-
, networkMetricsCurrentBlock :: Integer
208-
, networkMetricsUniqueDelegators :: Integer
209-
, networkMetricsTotalDelegations :: Integer
210-
, networkMetricsTotalGovernanceActions :: Integer
211-
, networkMetricsTotalDRepVotes :: Integer
212-
, networkMetricsTotalRegisteredDReps :: Integer
213-
, networkMetricsAlwaysAbstainVotingPower :: Integer
214-
, networkMetricsAlwaysNoConfidenceVotingPower :: Integer
215-
, networkMetricsNetworkName :: Text
205+
{ networkMetricsCurrentTime :: UTCTime
206+
, networkMetricsCurrentEpoch :: Integer
207+
, networkMetricsCurrentBlock :: Integer
208+
, networkMetricsUniqueDelegators :: Integer
209+
, networkMetricsTotalDelegations :: Integer
210+
, networkMetricsTotalGovernanceActions :: Integer
211+
, networkMetricsTotalDRepVotes :: Integer
212+
, networkMetricsTotalRegisteredDReps :: Integer
213+
, networkMetricsTotalStakeControlledByDReps :: Integer
214+
, networkMetricsTotalActiveDReps :: Integer
215+
, networkMetricsTotalInactiveDReps :: Integer
216+
, networkMetricsTotalActiveCIP119CompliantDReps :: Integer
217+
, networkMetricsTotalRegisteredDirectVoters :: Integer
218+
, networkMetricsAlwaysAbstainVotingPower :: Integer
219+
, networkMetricsAlwaysNoConfidenceVotingPower :: Integer
220+
, networkMetricsNetworkName :: Text
216221
}
217222

218223
data Delegation

govtool/frontend/src/models/api.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ export type NetworkMetrics = {
6868
totalGovernanceActions: number;
6969
totalDRepVotes: number;
7070
totalRegisteredDReps: number;
71+
totalStakeControlledByDReps: number;
72+
totalActiveDReps: number;
73+
totalInactiveDReps: number;
74+
totalActiveCIP119CompliantDReps: number;
75+
totalRegisteredDirectVoters: number;
7176
alwaysAbstainVotingPower: number;
7277
alwaysNoConfidenceVotingPower: number;
7378
networkName: string;

0 commit comments

Comments
 (0)