Skip to content

Commit 7c5d498

Browse files
committed
WIP: working latest voting anchor
1 parent cb6ad4f commit 7c5d498

File tree

1 file changed

+48
-44
lines changed

1 file changed

+48
-44
lines changed

govtool/backend/sql/list-dreps.sql

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ LatestVoteEpoch AS (
3737
lvp.rn = 1
3838
),
3939
RankedDRepRegistration AS (
40-
SELECT
40+
SELECT
4141
dr.id,
4242
dr.drep_hash_id,
4343
dr.deposit,
@@ -46,34 +46,52 @@ RankedDRepRegistration AS (
4646
tx.hash AS tx_hash
4747
FROM
4848
drep_registration dr
49-
JOIN tx ON tx.id = dr.tx_id
49+
JOIN tx ON tx.id = dr.tx_id
5050
),
5151
FetchError AS (
52-
SELECT
53-
fetch_error as message,
54-
voting_anchor_id
55-
FROM
56-
off_chain_vote_fetch_error
57-
WHERE
58-
fetch_time = (
59-
SELECT
60-
max(fetch_time)
61-
FROM
62-
off_chain_vote_fetch_error
63-
)
64-
GROUP BY
65-
fetch_error,
66-
voting_anchor_id
52+
SELECT
53+
fetch_error as message,
54+
voting_anchor_id
55+
FROM
56+
off_chain_vote_fetch_error
57+
WHERE
58+
fetch_time = (
59+
SELECT
60+
MAX(fetch_time)
61+
FROM
62+
off_chain_vote_fetch_error
63+
)
64+
GROUP BY
65+
fetch_error,
66+
voting_anchor_id
6767
),
68-
DRepData AS (
68+
LatestExistingVotingAnchor AS (
6969
SELECT
70+
dr.id AS drep_registration_id,
71+
dr.drep_hash_id,
72+
va.id AS voting_anchor_id,
73+
va.url,
74+
encode(va.data_hash, 'hex') AS tx_hash
75+
FROM
76+
drep_registration dr
77+
JOIN voting_anchor va ON dr.voting_anchor_id = va.id
78+
JOIN off_chain_vote_data ocvd ON va.id = ocvd.voting_anchor_id
79+
WHERE
80+
ocvd.voting_anchor_id IS NOT NULL
81+
ORDER BY
82+
dr.tx_id DESC
83+
),
84+
DRepData AS (
85+
SELECT DISTINCT ON (dh.raw)
7086
RankedDRepRegistration.id,
87+
dh.id,
7188
-- encode(dh.raw, 'hex') drep_hash,
7289
dh.view,
7390
-- dh.has_script,
74-
dr_voting_anchor.voting_anchor_id ,
75-
dr_voting_anchor.url,
76-
encode(dr_voting_anchor.data_hash, 'hex') metadata_hash,
91+
leva.voting_anchor_id ,
92+
leva.url,
93+
ocvd.voting_anchor_id,
94+
leva.tx_hash metadata_hash,
7795
COALESCE(RankedDRepRegistration.deposit, 0),
7896
DRepDistr.amount,
7997
(DRepActivity.epoch_no - GREATEST(COALESCE(voting_procedure_block.epoch_no, block_first_register.epoch_no), lve.epoch_no, newestRegister.epoch_no)) <= DRepActivity.drep_activity AS active,
@@ -90,27 +108,12 @@ DRepData AS (
90108
FROM
91109
drep_hash dh
92110
JOIN RankedDRepRegistration ON RankedDRepRegistration.drep_hash_id = dh.id AND RankedDRepRegistration.rn = 1
93-
LEFT JOIN (
94-
SELECT
95-
dr.id,
96-
dr.drep_hash_id,
97-
dr.voting_anchor_id,
98-
voting_anchor.url,
99-
voting_anchor.data_hash,
100-
ROW_NUMBER() OVER (PARTITION BY dr.drep_hash_id ORDER BY dr.tx_id DESC) AS rn,
101-
tx.hash AS tx_hash
102-
FROM
103-
drep_registration dr
104-
JOIN tx ON tx.id = dr.tx_id
105-
JOIN voting_anchor ON voting_anchor.id = dr.voting_anchor_id
106-
WHERE
107-
COALESCE(dr.deposit, 0) >= 0
108-
) AS dr_voting_anchor ON dr_voting_anchor.drep_hash_id = dh.id AND dr_voting_anchor.rn = 1
109111
LEFT JOIN RankedDRepRegistration second_to_newest_drep_registration ON second_to_newest_drep_registration.drep_hash_id = dh.id AND second_to_newest_drep_registration.rn = 2
112+
LEFT JOIN LatestExistingVotingAnchor leva ON leva.drep_hash_id = dh.id
110113
LEFT JOIN DRepDistr ON DRepDistr.hash_id = dh.id AND DRepDistr.rn = 1
111-
LEFT JOIN FetchError fetch_error ON fetch_error.voting_anchor_id = dr_voting_anchor.voting_anchor_id
112-
LEFT JOIN off_chain_vote_data ON off_chain_vote_data.voting_anchor_id = dr_voting_anchor.voting_anchor_id
113-
LEFT JOIN off_chain_vote_drep_data ON off_chain_vote_drep_data.off_chain_vote_data_id = off_chain_vote_data.id
114+
LEFT JOIN FetchError fetch_error ON fetch_error.voting_anchor_id = leva.voting_anchor_id
115+
LEFT JOIN off_chain_vote_data ocvd ON ocvd.voting_anchor_id = leva.voting_anchor_id
116+
LEFT JOIN off_chain_vote_drep_data ON off_chain_vote_drep_data.off_chain_vote_data_id = ocvd.id
114117
LEFT JOIN voting_procedure ON voting_procedure.drep_voter = dh.id
115118
LEFT JOIN tx voting_procedure_transaction ON voting_procedure_transaction.id = voting_procedure.tx_id
116119
LEFT JOIN block voting_procedure_block ON voting_procedure_block.id = voting_procedure_transaction.block_id
@@ -143,21 +146,22 @@ DRepData AS (
143146
dh.view ILIKE 'drep1km69g7ksf8t5g0h9d9tkrcd2tezxelx0wtr76rv2mrkl5nzd6v3'
144147
GROUP BY
145148
RankedDRepRegistration.id,
149+
dh.id,
146150
voting_procedure_block.epoch_no,
147151
voting_procedure_block.epoch_no,
148152
dh.raw,
149153
second_to_newest_drep_registration.voting_anchor_id,
150154
dh.view,
151155
dh.has_script,
152156
RankedDRepRegistration.deposit,
153-
dr_voting_anchor.voting_anchor_id,
154-
dr_voting_anchor.url,
155-
dr_voting_anchor.data_hash,
157+
leva.voting_anchor_id,
158+
leva.url,
159+
ocvd.voting_anchor_id,
156160
DRepDistr.amount,
157161
DRepActivity.epoch_no,
158162
DRepActivity.drep_activity,
159163
lve.epoch_no,
160-
dr_voting_anchor.tx_hash,
164+
leva.tx_hash,
161165
newestRegister.time,
162166
newestRegister.epoch_no,
163167
fetch_error.message,

0 commit comments

Comments
 (0)