Skip to content

Commit b518a0c

Browse files
authored
Deduplicate Registered Voters View (#122)
* fix: deduplicate registered voters using ROW_NUMBER * fix(views): deduplicate vplu by registered_voter_id to handle relayer events * refactor(views): remove ROW_NUMBER deduplication per performance feedback, keeping vplu fix * fix(views): apply Elaine's feedback - remove signer fallback in vplu, deduplicate delegation_events join
1 parent 37932ff commit b518a0c

File tree

1 file changed

+42
-29
lines changed

1 file changed

+42
-29
lines changed

sql_files/views/registered_voters.sql

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -101,35 +101,39 @@ receipt_actions_prep AS (
101101
)
102102
/* Sourcing Latest Voting Power from Locks + Unlocks */
103103
, voting_power_from_locks_unlocks AS (
104-
SELECT DISTINCT ON (rap.signer_account_id)
105-
rap.block_timestamp
106-
, rap.receipt_id
107-
-- same semantics as before (prefer account_id from logs, fall back to signer)
108-
, COALESCE(
109-
CASE
110-
WHEN (safe_json_parse(REPLACE(rap.action_logs[1], 'EVENT_JSON:', '')) ->> 'error') IS NULL
111-
THEN (safe_json_parse(REPLACE(rap.action_logs[1], 'EVENT_JSON:', '')) -> 'data' -> 0 ->> 'account_id')
112-
ELSE NULL
113-
END,
114-
rap.signer_account_id
115-
) AS registered_voter_id
116-
, CASE
117-
WHEN (safe_json_parse(REPLACE(rap.action_logs[1], 'EVENT_JSON:', '')) ->> 'error') IS NULL
118-
THEN ((safe_json_parse(REPLACE(rap.action_logs[1], 'EVENT_JSON:', '')) -> 'data' -> 0 ->> 'locked_near_balance'))::NUMERIC
119-
ELSE NULL
120-
END AS voting_power_from_locks_unlocks
121-
, CASE
122-
WHEN (safe_json_parse(REPLACE(rap.action_logs[1], 'EVENT_JSON:', '')) ->> 'error') IS NULL
123-
THEN ((safe_json_parse(REPLACE(rap.action_logs[1], 'EVENT_JSON:', '')) -> 'data' -> 0 ->> 'timestamp'))::NUMERIC
124-
ELSE NULL
125-
END AS lockup_update_at_ns
126-
FROM receipt_actions_prep AS rap
127-
WHERE
128-
rap.method_name = 'on_lockup_update'
104+
SELECT DISTINCT ON (vplu_prep.registered_voter_id)
105+
vplu_prep.block_timestamp
106+
, vplu_prep.receipt_id
107+
, vplu_prep.registered_voter_id
108+
, vplu_prep.voting_power_from_locks_unlocks
109+
, vplu_prep.lockup_update_at_ns
110+
FROM (
111+
SELECT
112+
rap.block_timestamp
113+
, rap.receipt_id
114+
, CASE
115+
WHEN (safe_json_parse(REPLACE(rap.action_logs[1], 'EVENT_JSON:', '')) ->> 'error') IS NULL
116+
THEN (safe_json_parse(REPLACE(rap.action_logs[1], 'EVENT_JSON:', '')) -> 'data' -> 0 ->> 'account_id')
117+
ELSE NULL
118+
END AS registered_voter_id
119+
, CASE
120+
WHEN (safe_json_parse(REPLACE(rap.action_logs[1], 'EVENT_JSON:', '')) ->> 'error') IS NULL
121+
THEN ((safe_json_parse(REPLACE(rap.action_logs[1], 'EVENT_JSON:', '')) -> 'data' -> 0 ->> 'locked_near_balance'))::NUMERIC
122+
ELSE NULL
123+
END AS voting_power_from_locks_unlocks
124+
, CASE
125+
WHEN (safe_json_parse(REPLACE(rap.action_logs[1], 'EVENT_JSON:', '')) ->> 'error') IS NULL
126+
THEN ((safe_json_parse(REPLACE(rap.action_logs[1], 'EVENT_JSON:', '')) -> 'data' -> 0 ->> 'timestamp'))::NUMERIC
127+
ELSE NULL
128+
END AS lockup_update_at_ns
129+
FROM receipt_actions_prep AS rap
130+
WHERE
131+
rap.method_name = 'on_lockup_update'
132+
) AS vplu_prep
129133
ORDER BY
130-
rap.signer_account_id -- DISTINCT ON key
131-
, rap.block_timestamp DESC -- "latest" first
132-
, rap.receipt_id DESC -- deterministic tie-breaker
134+
vplu_prep.registered_voter_id
135+
, vplu_prep.block_timestamp DESC
136+
, vplu_prep.receipt_id DESC
133137
)
134138
/* Sourcing Registered Voters from Deploy Lockup Event (Excluding dupes due to account already being registered) */
135139
--Whenever a user registers to vote, there should always be a non-null storage deposit, aka, initial voting power amount.
@@ -236,7 +240,16 @@ receipt_actions_prep AS (
236240
ON ra.signer_account_id = vplu.registered_voter_id
237241
LEFT JOIN proposal_participation AS pp
238242
ON ra.signer_account_id = pp.registered_voter_id
239-
LEFT JOIN {SCHEMA_NAME}.delegation_events AS de
243+
LEFT JOIN
244+
(
245+
SELECT DISTINCT ON (delegator_id)
246+
d.*
247+
FROM {SCHEMA_NAME}.delegation_events AS d
248+
ORDER BY
249+
d.delegator_id
250+
, d.block_timestamp DESC
251+
, d.receipt_id DESC
252+
) AS de
240253
ON ra.signer_account_id = de.delegator_id
241254
AND de.is_latest_delegator_event = TRUE
242255
AND de.delegate_method = 'delegate_all'

0 commit comments

Comments
 (0)