@@ -35,133 +35,171 @@ LatestVoteEpoch AS (
3535 JOIN block ON block .id = tx .block_id
3636 WHERE
3737 lvp .rn = 1
38- )
39- SELECT DISTINCT ON (dh .raw )
40- encode(dh .raw , ' hex' ),
41- dh .view ,
42- dh .has_script ,
43- va .url ,
44- encode(va .data_hash , ' hex' ),
45- dr_deposit .deposit ,
46- DRepDistr .amount ,
47- (DRepActivity .epoch_no - GREATEST(COALESCE(block .epoch_no , block_first_register .epoch_no ), lve .epoch_no , newestRegister .epoch_no )) <= DRepActivity .drep_activity AS active,
48- encode(dr_voting_anchor .tx_hash , ' hex' ) AS tx_hash,
49- newestRegister .time AS last_register_time,
50- COALESCE(latestDeposit .deposit , 0 ),
51- non_deregister_voting_anchor .url IS NOT NULL AS has_non_deregister_voting_anchor,
52- fetch_error .message ,
53- off_chain_vote_drep_data .payment_address ,
54- off_chain_vote_drep_data .given_name ,
55- off_chain_vote_drep_data .objectives ,
56- off_chain_vote_drep_data .motivations ,
57- off_chain_vote_drep_data .qualifications ,
58- off_chain_vote_drep_data .image_url ,
59- off_chain_vote_drep_data .image_hash
60- FROM
61- drep_hash dh
62- JOIN (
63- SELECT
64- dr .id ,
65- dr .drep_hash_id ,
66- dr .deposit ,
67- ROW_NUMBER() OVER (PARTITION BY dr .drep_hash_id ORDER BY dr .tx_id DESC ) AS rn
68- FROM
69- drep_registration dr
70- WHERE
71- dr .deposit IS NOT NULL
72- ) AS dr_deposit ON dr_deposit .drep_hash_id = dh .id AND dr_deposit .rn = 1
73- JOIN (
74- SELECT
38+ ),
39+ RankedDRepRegistration AS (
40+ SELECT
7541 dr .id ,
7642 dr .drep_hash_id ,
7743 dr .deposit ,
78- ROW_NUMBER() OVER (PARTITION BY dr .drep_hash_id ORDER BY dr .tx_id DESC ) AS rn
79- FROM
80- drep_registration dr
81- ) AS latestDeposit ON latestDeposit .drep_hash_id = dh .id AND latestDeposit .rn = 1
82- LEFT JOIN (
83- SELECT
84- dr .id ,
85- dr .drep_hash_id ,
8644 dr .voting_anchor_id ,
8745 ROW_NUMBER() OVER (PARTITION BY dr .drep_hash_id ORDER BY dr .tx_id DESC ) AS rn,
88- tx .hash AS tx_hash
46+ encode( tx .hash , ' hex ' ) AS tx_hash
8947 FROM
9048 drep_registration dr
91- JOIN tx ON tx .id = dr .tx_id
92- WHERE
93- COALESCE(dr .deposit , 0 ) >= 0
94- ) AS dr_voting_anchor ON dr_voting_anchor .drep_hash_id = dh .id AND dr_voting_anchor .rn = 1
95- LEFT JOIN (
96- SELECT
97- dr .id ,
98- dr .drep_hash_id ,
99- dr .voting_anchor_id ,
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- ) AS dr_non_deregister_voting_anchor ON dr_non_deregister_voting_anchor .drep_hash_id = dh .id AND dr_non_deregister_voting_anchor .rn = 1
106- LEFT JOIN (
107- SELECT
108- dr .id ,
109- dr .drep_hash_id ,
110- dr .voting_anchor_id ,
111- ROW_NUMBER() OVER (PARTITION BY dr .drep_hash_id ORDER BY dr .tx_id DESC ) AS rn
112- FROM
113- drep_registration dr
114- ) AS second_to_newest_drep_registration ON second_to_newest_drep_registration .drep_hash_id = dh .id AND second_to_newest_drep_registration .rn = 2
115- LEFT JOIN DRepDistr ON DRepDistr .hash_id = dh .id AND DRepDistr .rn = 1
116- LEFT JOIN voting_anchor va ON va .id = dr_voting_anchor .voting_anchor_id
117- LEFT JOIN voting_anchor non_deregister_voting_anchor ON non_deregister_voting_anchor .id = dr_non_deregister_voting_anchor .voting_anchor_id
118- LEFT JOIN (
119- SELECT
120- fetch_error as message,
121- voting_anchor_id
122- FROM
49+ JOIN tx ON tx .id = dr .tx_id
50+ ),
51+ 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
12362 off_chain_vote_fetch_error
124- WHERE
125- fetch_time = (
126- SELECT
127- max (fetch_time)
128- FROM
129- off_chain_vote_fetch_error
130- )
131- GROUP BY
132- fetch_error,
133- voting_anchor_id
134- ) AS fetch_error ON fetch_error .voting_anchor_id = va .id
135- LEFT JOIN off_chain_vote_data ON off_chain_vote_data .voting_anchor_id = va .id
136- LEFT JOIN off_chain_vote_drep_data ON off_chain_vote_drep_data .off_chain_vote_data_id = off_chain_vote_data .id
137- CROSS JOIN DRepActivity
138- LEFT JOIN voting_procedure ON voting_procedure .drep_voter = dh .id
139- LEFT JOIN tx ON tx .id = voting_procedure .tx_id
140- LEFT JOIN block ON block .id = tx .block_id
141- LEFT JOIN (
142- SELECT
143- block .epoch_no ,
144- block .time ,
145- dr .drep_hash_id ,
146- ROW_NUMBER() OVER (PARTITION BY dr .drep_hash_id ORDER BY dr .tx_id DESC ) AS rn
147- FROM
63+ )
64+ GROUP BY
65+ fetch_error,
66+ voting_anchor_id
67+ ),
68+ LatestExistingVotingAnchor AS (
69+ 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 metadata_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+ HasNonDeregisterVotingAnchor AS (
85+ SELECT
86+ dr .drep_hash_id ,
87+ EXISTS (
88+ SELECT 1
89+ FROM drep_registration dr_sub
90+ WHERE
91+ dr_sub .drep_hash_id = dr .drep_hash_id
92+ AND dr_sub .voting_anchor_id IS NULL
93+ AND COALESCE(dr_sub .deposit , 0 ) >= 0
94+ ) AS value
95+ FROM
96+ drep_registration dr
97+ GROUP BY
98+ dr .drep_hash_id
99+ ),
100+ DRepData AS (
101+ SELECT DISTINCT ON (dh .raw )
102+ encode(dh .raw , ' hex' ) drep_hash,
103+ dh .view ,
104+ dh .has_script ,
105+ leva .url ,
106+ leva .metadata_hash ,
107+ dr_deposit .deposit ,
108+ DRepDistr .amount ,
109+ (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,
110+ RankedDRepRegistration .tx_hash ,
111+ newestRegister .time AS last_register_time,
112+ COALESCE(RankedDRepRegistration .deposit , 0 ) as latest_deposit,
113+ hndva .value AS has_non_deregister_voting_anchor,
114+ off_chain_vote_drep_data .given_name ,
115+ off_chain_vote_drep_data .objectives ,
116+ off_chain_vote_drep_data .motivations ,
117+ off_chain_vote_drep_data .qualifications ,
118+ off_chain_vote_drep_data .image_url ,
119+ off_chain_vote_drep_data .image_hash
120+ FROM
121+ drep_hash dh
122+ JOIN RankedDRepRegistration ON RankedDRepRegistration .drep_hash_id = dh .id AND RankedDRepRegistration .rn = 1
123+ 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
124+ JOIN (
125+ SELECT
126+ dr .id ,
127+ dr .drep_hash_id ,
128+ dr .deposit ,
129+ ROW_NUMBER() OVER (PARTITION BY dr .drep_hash_id ORDER BY dr .tx_id DESC ) AS rn
130+ FROM
131+ drep_registration dr
132+ WHERE
133+ dr .deposit IS NOT NULL
134+ ) AS dr_deposit ON dr_deposit .drep_hash_id = dh .id AND dr_deposit .rn = 1
135+ LEFT JOIN (
136+ SELECT
137+ dr .id ,
138+ dr .drep_hash_id ,
139+ dr .deposit ,
140+ ROW_NUMBER() OVER (PARTITION BY dr .drep_hash_id ORDER BY dr .tx_id DESC ) AS rn
141+ FROM
142+ drep_registration dr
143+ ) AS latestDeposit ON latestDeposit .drep_hash_id = dh .id AND latestDeposit .rn = 1
144+ LEFT JOIN LatestExistingVotingAnchor leva ON leva .drep_hash_id = dh .id
145+ LEFT JOIN DRepDistr ON DRepDistr .hash_id = dh .id AND DRepDistr .rn = 1
146+ LEFT JOIN FetchError fetch_error ON fetch_error .voting_anchor_id = leva .voting_anchor_id
147+ LEFT JOIN HasNonDeregisterVotingAnchor hndva ON hndva .drep_hash_id = dh .id
148+ LEFT JOIN off_chain_vote_data ocvd ON ocvd .voting_anchor_id = leva .voting_anchor_id
149+ LEFT JOIN off_chain_vote_drep_data ON off_chain_vote_drep_data .off_chain_vote_data_id = ocvd .id
150+ LEFT JOIN voting_procedure ON voting_procedure .drep_voter = dh .id
151+ LEFT JOIN tx voting_procedure_transaction ON voting_procedure_transaction .id = voting_procedure .tx_id
152+ LEFT JOIN block voting_procedure_block ON voting_procedure_block .id = voting_procedure_transaction .block_id
153+ LEFT JOIN (
154+ SELECT
155+ block .epoch_no ,
156+ block .time ,
157+ dr .drep_hash_id ,
158+ ROW_NUMBER() OVER (PARTITION BY dr .drep_hash_id ORDER BY dr .tx_id DESC ) AS rn
159+ FROM
148160 drep_registration dr
149161 JOIN tx ON tx .id = dr .tx_id
150162 JOIN block ON block .id = tx .block_id
151- WHERE
152- COALESCE(dr .deposit , 0 ) >= 0
153- ) AS newestRegister ON newestRegister .drep_hash_id = dh .id AND newestRegister .rn = 1
154- LEFT JOIN (
155- SELECT
156- dr .tx_id ,
157- dr .drep_hash_id ,
158- ROW_NUMBER() OVER (PARTITION BY dr .drep_hash_id ORDER BY dr .tx_id ASC ) AS rn
159- FROM
160- drep_registration dr
161- ) AS dr_first_register ON dr_first_register .drep_hash_id = dh .id AND dr_first_register .rn = 1
162- LEFT JOIN tx AS tx_first_register ON tx_first_register .id = dr_first_register .tx_id
163- LEFT JOIN block AS block_first_register ON block_first_register .id = tx_first_register .block_id
164- LEFT JOIN LatestVoteEpoch lve ON lve .drep_id = dh .id
163+ WHERE
164+ COALESCE(dr .deposit , 0 ) >= 0
165+ ) AS newestRegister ON newestRegister .drep_hash_id = dh .id AND newestRegister .rn = 1
166+ LEFT JOIN (
167+ SELECT
168+ dr .tx_id ,
169+ dr .drep_hash_id ,
170+ ROW_NUMBER() OVER (PARTITION BY dr .drep_hash_id ORDER BY dr .tx_id ASC ) AS rn
171+ FROM
172+ drep_registration dr
173+ ) AS dr_first_register ON dr_first_register .drep_hash_id = dh .id AND dr_first_register .rn = 1
174+ LEFT JOIN tx AS tx_first_register ON tx_first_register .id = dr_first_register .tx_id
175+ LEFT JOIN block AS block_first_register ON block_first_register .id = tx_first_register .block_id
176+ LEFT JOIN LatestVoteEpoch lve ON lve .drep_id = dh .id
177+ CROSS JOIN DRepActivity
178+ GROUP BY
179+ dh .raw ,
180+ dh .view ,
181+ dh .has_script ,
182+ leva .url ,
183+ leva .metadata_hash ,
184+ dr_deposit .deposit ,
185+ DRepDistr .amount ,
186+ DRepActivity .epoch_no ,
187+ voting_procedure_block .epoch_no ,
188+ block_first_register .epoch_no ,
189+ lve .epoch_no , newestRegister .epoch_no ,
190+ DRepActivity .drep_activity ,
191+ RankedDRepRegistration .tx_hash ,
192+ newestRegister .time ,
193+ RankedDRepRegistration .deposit ,
194+ hndva .value ,
195+ off_chain_vote_drep_data .given_name ,
196+ off_chain_vote_drep_data .objectives ,
197+ off_chain_vote_drep_data .motivations ,
198+ off_chain_vote_drep_data .qualifications ,
199+ off_chain_vote_drep_data .image_url ,
200+ off_chain_vote_drep_data .image_hash
201+ )
202+ SELECT * FROM DRepData
165203WHERE
166204 (
167205 COALESCE(?, ' ' ) = ' ' OR
@@ -172,31 +210,4 @@ WHERE
172210 off_chain_vote_drep_data .objectives ILIKE ? OR
173211 off_chain_vote_drep_data .motivations ILIKE ? OR
174212 off_chain_vote_drep_data .qualifications ILIKE ?
175- )
176- GROUP BY
177- block_first_register .epoch_no ,
178- block .epoch_no ,
179- dh .raw ,
180- second_to_newest_drep_registration .voting_anchor_id ,
181- dh .view ,
182- dh .has_script ,
183- va .url ,
184- va .data_hash ,
185- dr_deposit .deposit ,
186- DRepDistr .amount ,
187- DRepActivity .epoch_no ,
188- DRepActivity .drep_activity ,
189- lve .epoch_no ,
190- dr_voting_anchor .tx_hash ,
191- newestRegister .time ,
192- newestRegister .epoch_no ,
193- latestDeposit .deposit ,
194- non_deregister_voting_anchor .url ,
195- fetch_error .message ,
196- off_chain_vote_drep_data .payment_address ,
197- off_chain_vote_drep_data .given_name ,
198- off_chain_vote_drep_data .objectives ,
199- off_chain_vote_drep_data .motivations ,
200- off_chain_vote_drep_data .qualifications ,
201- off_chain_vote_drep_data .image_url ,
202- off_chain_vote_drep_data .image_hash ;
213+ )
0 commit comments