Skip to content

Commit 37932ff

Browse files
authored
Add unstake and withdraw events to user_activities (#119)
* feat(views): add unstake and withdraw events to user_activities * fix: include unstake_all and withdraw_all events in user_activities view * fix(sql): parse amount from logs for unstake_all and withdraw_all * fix(sql): filter failed receipts for unstake and withdraw * chore: add CI workflows from main (hos-dev/stg/prd)
1 parent 0fbd848 commit 37932ff

File tree

1 file changed

+85
-3
lines changed

1 file changed

+85
-3
lines changed

sql_files/views/user_activities.sql

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ WITH receipt_actions_prep AS (
3232
ON ra.receipt_id = eo.receipt_id
3333
WHERE
3434
ra.action_kind = 'FunctionCall'
35-
AND ra.method_name IN ('on_lockup_deployed', 'lock_near', 'on_lockup_update', 'delegate_all', 'undelegate', 'begin_unlock_near', 'lock_pending_near')
35+
AND ra.method_name IN ('on_lockup_deployed', 'lock_near', 'on_lockup_update', 'delegate_all', 'undelegate', 'begin_unlock_near', 'lock_pending_near', 'withdraw_from_staking_pool', 'withdraw_all_from_staking_pool', 'unstake', 'unstake_all')
3636
)
3737
--------------------
3838
--Account Creation--
@@ -191,12 +191,12 @@ WITH receipt_actions_prep AS (
191191
ELSE NULL
192192
END, ra.signer_account_id) AS account_id
193193
, ra.receiver_id AS hos_contract_address
194-
, CASE
194+
, CASE
195195
WHEN safe_json_parse(REPLACE(unnested_logs, 'EVENT_JSON:', ''))->>'error' IS NULL
196196
THEN (safe_json_parse(REPLACE(unnested_logs, 'EVENT_JSON:', ''))->'data'->0->>'amount')::NUMERIC
197197
ELSE NULL
198198
END AS near_amount
199-
, NULL::NUMERIC AS locked_near_balance --This does NOT exist FOR delegate_all AND undelegate events
199+
, NULL::NUMERIC AS locked_near_balance --This does NOT exist FOR delegate_all AND undelegate events
200200
, ra.block_height
201201
, ra.block_hash
202202
FROM receipt_actions_prep AS ra
@@ -267,6 +267,84 @@ WITH receipt_actions_prep AS (
267267
, '{VOTING_CONTRACT_PREFIX}.{HOS_CONTRACT}'
268268
)
269269
)
270+
----------------------------
271+
--Withdraw From Staking Pool--
272+
----------------------------
273+
, withdraw_from_staking_pool AS (
274+
SELECT
275+
ra.receipt_id AS id
276+
, ra.receipt_id
277+
, ra.block_timestamp AS event_timestamp
278+
, method_name AS event_type
279+
, ra.method_name
280+
, ra.event_status
281+
, ra.signer_account_id AS account_id
282+
, SUBSTRING(ra.receiver_id FROM POSITION('.' IN ra.receiver_id) + 1) AS hos_contract_address
283+
, COALESCE(
284+
CASE
285+
WHEN safe_json_parse(CONVERT_FROM(ra.args_decoded, 'UTF8'))->>'error' IS NULL
286+
THEN (safe_json_parse(CONVERT_FROM(ra.args_decoded, 'UTF8'))->>'amount')::NUMERIC
287+
ELSE NULL
288+
END,
289+
CASE
290+
WHEN safe_json_parse(REPLACE(unnested_logs, 'EVENT_JSON:', ''))->>'error' IS NULL
291+
THEN (safe_json_parse(REPLACE(unnested_logs, 'EVENT_JSON:', ''))->'data'->0->>'amount')::NUMERIC
292+
ELSE NULL
293+
END,
294+
0
295+
) AS near_amount
296+
, NULL::NUMERIC AS locked_near_balance
297+
, ra.block_height
298+
, ra.block_hash
299+
FROM receipt_actions_prep AS ra
300+
LEFT JOIN LATERAL UNNEST(ra.logs) AS unnested_logs ON TRUE
301+
WHERE
302+
ra.method_name IN ('withdraw_from_staking_pool', 'withdraw_all_from_staking_pool')
303+
AND ra.event_status = 'succeeded'
304+
AND SUBSTRING(ra.receiver_id FROM POSITION('.' IN ra.receiver_id) + 1) IN (
305+
'{VENEAR_CONTRACT_PREFIX}.{HOS_CONTRACT}'
306+
, '{VOTING_CONTRACT_PREFIX}.{HOS_CONTRACT}'
307+
)
308+
)
309+
-----------
310+
--Unstake--
311+
-----------
312+
, unstake AS (
313+
SELECT
314+
ra.receipt_id AS id
315+
, ra.receipt_id
316+
, ra.block_timestamp AS event_timestamp
317+
, method_name AS event_type
318+
, ra.method_name
319+
, ra.event_status
320+
, ra.signer_account_id AS account_id
321+
, SUBSTRING(ra.receiver_id FROM POSITION('.' IN ra.receiver_id) + 1) AS hos_contract_address
322+
, COALESCE(
323+
CASE
324+
WHEN safe_json_parse(CONVERT_FROM(ra.args_decoded, 'UTF8'))->>'error' IS NULL
325+
THEN (safe_json_parse(CONVERT_FROM(ra.args_decoded, 'UTF8'))->>'amount')::NUMERIC
326+
ELSE NULL
327+
END,
328+
CASE
329+
WHEN safe_json_parse(REPLACE(unnested_logs, 'EVENT_JSON:', ''))->>'error' IS NULL
330+
THEN (safe_json_parse(REPLACE(unnested_logs, 'EVENT_JSON:', ''))->'data'->0->>'amount')::NUMERIC
331+
ELSE NULL
332+
END,
333+
0
334+
) AS near_amount
335+
, NULL::NUMERIC AS locked_near_balance
336+
, ra.block_height
337+
, ra.block_hash
338+
FROM receipt_actions_prep AS ra
339+
LEFT JOIN LATERAL UNNEST(ra.logs) AS unnested_logs ON TRUE
340+
WHERE
341+
ra.method_name IN ('unstake', 'unstake_all')
342+
AND ra.event_status = 'succeeded'
343+
AND SUBSTRING(ra.receiver_id FROM POSITION('.' IN ra.receiver_id) + 1) IN (
344+
'{VENEAR_CONTRACT_PREFIX}.{HOS_CONTRACT}'
345+
, '{VOTING_CONTRACT_PREFIX}.{HOS_CONTRACT}'
346+
)
347+
)
270348
----------
271349
--UNIONS--
272350
----------
@@ -282,6 +360,10 @@ WITH receipt_actions_prep AS (
282360
SELECT * FROM begin_unlock_near
283361
UNION ALL
284362
SELECT * FROM relock_pending_near
363+
UNION ALL
364+
SELECT * FROM withdraw_from_staking_pool
365+
UNION ALL
366+
SELECT * FROM unstake
285367
)
286368
SELECT
287369
id

0 commit comments

Comments
 (0)