Skip to content

Commit 8d99337

Browse files
authored
fixed bug with missing registered voter and added additional optimizations (#117)
1 parent 92fd78e commit 8d99337

File tree

1 file changed

+65
-38
lines changed

1 file changed

+65
-38
lines changed

sql_files/views/registered_voters.sql

Lines changed: 65 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,23 @@ receipt_actions_prep AS (
3333
SELECT
3434
decode(ra.args_base64, 'base64') AS args_decoded
3535
, eo.logs AS action_logs
36-
, ra.*
36+
, ra.id
37+
, ra.receipt_id
38+
, ra.receiver_id
39+
, ra.signer_account_id
40+
, ra.predecessor_id
41+
, ra.method_name
42+
, ra.block_timestamp
43+
, ra.block_height
44+
, ra.block_hash
3745
FROM {SCHEMA_NAME}.receipt_actions AS ra
3846
INNER JOIN {SCHEMA_NAME}.execution_outcomes AS eo
3947
ON ra.receipt_id = eo.receipt_id
4048
AND eo.status IN ('SuccessReceiptId', 'SuccessValue')
4149
WHERE
4250
ra.action_kind = 'FunctionCall'
4351
AND ra.method_name IN ('new', 'storage_deposit', 'on_lockup_update', 'deploy_lockup')
44-
AND ra.receiver_id IN ( --House of Stake contracts
52+
AND ra.receiver_id IN ( ---House of Stake contracts
4553
'{VENEAR_CONTRACT_PREFIX}.{HOS_CONTRACT}' --veNEAR contract
4654
, '{VOTING_CONTRACT_PREFIX}.{HOS_CONTRACT}' --Voting contract
4755
)
@@ -94,55 +102,74 @@ receipt_actions_prep AS (
94102
/* Sourcing Latest Voting Power from Locks + Unlocks */
95103
, voting_power_from_locks_unlocks AS (
96104
SELECT DISTINCT ON (rap.signer_account_id)
97-
rap.block_timestamp,
98-
rap.receipt_id,
105+
rap.block_timestamp
106+
, rap.receipt_id
99107
-- same semantics as before (prefer account_id from logs, fall back to signer)
100-
COALESCE(
101-
CASE
102-
WHEN (fastnear.safe_json_parse(replace(rap.action_logs[1], 'EVENT_JSON:', '')) ->> 'error') IS NULL
103-
THEN (fastnear.safe_json_parse(replace(rap.action_logs[1], 'EVENT_JSON:', '')) -> 'data' -> 0 ->> 'account_id')
104-
ELSE NULL
105-
END,
106-
rap.signer_account_id
107-
) AS registered_voter_id,
108-
CASE
109-
WHEN (fastnear.safe_json_parse(replace(rap.action_logs[1], 'EVENT_JSON:', '')) ->> 'error') IS NULL
110-
THEN ((fastnear.safe_json_parse(replace(rap.action_logs[1], 'EVENT_JSON:', '')) -> 'data' -> 0 ->> 'locked_near_balance'))::numeric
111-
ELSE NULL
112-
END AS voting_power_from_locks_unlocks,
113-
CASE
114-
WHEN (fastnear.safe_json_parse(replace(rap.action_logs[1], 'EVENT_JSON:', '')) ->> 'error') IS NULL
115-
THEN ((fastnear.safe_json_parse(replace(rap.action_logs[1], 'EVENT_JSON:', '')) -> 'data' -> 0 ->> 'timestamp'))::numeric
116-
ELSE NULL
117-
END AS lockup_update_at_ns,
118-
1 AS row_num -- keep compatibility with downstream "WHERE row_num = 1"
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
119126
FROM receipt_actions_prep AS rap
120127
WHERE
121128
rap.method_name = 'on_lockup_update'
122129
ORDER BY
123-
rap.signer_account_id, -- DISTINCT ON key
124-
rap.block_timestamp DESC, -- "latest" first
125-
rap.receipt_id DESC -- deterministic tie-breaker
130+
rap.signer_account_id -- DISTINCT ON key
131+
, rap.block_timestamp DESC -- "latest" first
132+
, rap.receipt_id DESC -- deterministic tie-breaker
126133
)
127134
/* Sourcing Registered Voters from Deploy Lockup Event (Excluding dupes due to account already being registered) */
128135
--Whenever a user registers to vote, there should always be a non-null storage deposit, aka, initial voting power amount.
129-
--Inner join excludes scenarios where a vote registration action (aka deploy_lockup for a given user) is duped bc the user's account was already registered and the subsequent storage_deposit event tracks a null initial_voting_power amount.
136+
--is_remove_dupe flag excludes scenarios where a vote registration action (aka deploy_lockup for a given user) is duped bc the user's account was already registered and the subsequent storage_deposit event tracks a null initial_voting_power amount.
137+
--NOTE: When a user leverages the web app to register to vote, the storage_deposit and deploy_lockup event for their account_id will share the same receipt_id.
138+
------- When a user leverages the NEAR CLI to register to vote, the receipt_ids for these events are different (rare scenario).
130139
, registered_voters_prep AS (
131140
SELECT
132-
ra.*
133-
, (EXTRACT(EPOCH FROM ra.block_timestamp) * 1e9)::BIGINT AS registered_at_ns
134-
FROM receipt_actions_prep AS ra
141+
rv.*
142+
, vpvr.voting_power_from_vote_registration AS voting_power_from_vote_registrations
143+
FROM (
144+
SELECT
145+
ra.*
146+
, (EXTRACT(EPOCH FROM ra.block_timestamp) * 1e9)::BIGINT AS registered_at_ns
147+
, COALESCE(
148+
CASE
149+
WHEN safe_json_parse(REPLACE(ra.action_logs[1], 'EVENT_JSON:', ''))->>'error' IS NULL
150+
THEN safe_json_parse(REPLACE(ra.action_logs[1], 'EVENT_JSON:', ''))->'data'->0->>'owner_id'
151+
ELSE NULL END
152+
, ra.signer_account_id
153+
) AS registered_voter_id
154+
, CASE
155+
WHEN safe_json_parse(ra.action_logs[1])->>'original_text' = 'The account is already registered, refunding the deposit'
156+
THEN 1 ELSE 0
157+
END AS is_remove_dupe
158+
FROM receipt_actions_prep AS ra
159+
WHERE
160+
ra.method_name = 'deploy_lockup'
161+
) AS rv
135162
INNER JOIN voting_power_from_vote_registration AS vpvr
136-
ON ra.receipt_id = vpvr.receipt_id
163+
ON rv.registered_voter_id = vpvr.registered_voter_id
137164
WHERE
138-
ra.method_name = 'deploy_lockup'
165+
rv.is_remove_dupe = 0
139166
)
140167
--------------------------------------
141168
--PROPOSAL PARTICIPATION CALCULATION--
142169
--------------------------------------
143170
/* VI. Sourcing Proposal Participation (from 10 most recently approved proposals) */
144171
, ten_most_recently_approved_proposals AS (
145-
SELECT *
172+
SELECT proposal_id
146173
FROM {SCHEMA_NAME}.approved_proposals
147174
ORDER BY proposal_approved_at DESC
148175
LIMIT 10
@@ -198,17 +225,15 @@ receipt_actions_prep AS (
198225

199226
--Voting Powers
200227
, COALESCE(vplu.voting_power_from_locks_unlocks, 0) AS voting_power_from_locks_unlocks
201-
, COALESCE(vpvr.voting_power_from_vote_registration, 0) AS voting_power_from_vote_registration --aka initial voting power, as a registered voter
228+
, COALESCE(ra.voting_power_from_vote_registrations, 0) AS voting_power_from_vote_registration --aka initial voting power, as a registered voter
202229
, COALESCE(vplu.voting_power_from_locks_unlocks, 0)
203-
+ COALESCE(vpvr.voting_power_from_vote_registration, 0) AS principal_balance
230+
+ COALESCE(ra.voting_power_from_vote_registrations, 0) AS principal_balance
204231

205232
FROM registered_voters_prep AS ra --Sourced from the deploy_lockup event
206233
LEFT JOIN venear_contract_growth_config AS gc --Sourced from method_name = 'new', function call sets up the contract state when it is first deployed
207234
ON ra.receiver_id = gc.hos_contract_address
208235
LEFT JOIN voting_power_from_locks_unlocks AS vplu --Sourced from the voter's most recent on_lockup_update event
209236
ON ra.signer_account_id = vplu.registered_voter_id
210-
LEFT JOIN voting_power_from_vote_registration AS vpvr --Sourced from the voter's storage_deposit event associated with the vote registration action
211-
ON ra.signer_account_id = vpvr.registered_voter_id
212237
LEFT JOIN proposal_participation AS pp
213238
ON ra.signer_account_id = pp.registered_voter_id
214239
LEFT JOIN {SCHEMA_NAME}.delegation_events AS de
@@ -245,6 +270,8 @@ receipt_actions_prep AS (
245270
, SUM(principal_balance) AS delegated_balance
246271
, SUM(extra_venear_on_principal) AS delegated_extra_venear
247272
FROM voting_power_from_rewards
273+
WHERE
274+
delegatee_id IS NOT NULL -- Optimization: Filter NULLs early
248275
GROUP BY 1
249276
)
250277
-------------
@@ -255,7 +282,7 @@ SELECT
255282
, ra.receipt_id
256283
, DATE(ra.block_timestamp) AS registered_date
257284
, ra.block_timestamp AS registered_at
258-
, ra.signer_account_id AS registered_voter_id
285+
, ra.registered_voter_id
259286
, ra.receiver_id AS hos_contract_address
260287
, ra.has_locked_unlocked_near
261288
, ra.is_actively_delegating

0 commit comments

Comments
 (0)