-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
594 additions
and
545 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
-- migrate:up | ||
CREATE EXTENSION IF NOT EXISTS moddatetime; | ||
CREATE TABLE public.event( | ||
"blockHash" character varying(65), | ||
"blockIndex" integer, | ||
"transactionHash" character varying(65) NOT NULL, | ||
"transactionIndex" integer NOT NULL, | ||
"eventIndex" integer NOT NULL, | ||
"eventName" character varying NOT NULL, | ||
"eventData" jsonb NOT NULL, | ||
"createdAt" timestamp without time zone DEFAULT now() NOT NULL, | ||
"updatedAt" timestamp without time zone DEFAULT now() NOT NULL | ||
); | ||
ALTER TABLE public.event | ||
ADD CONSTRAINT "event_pkey" PRIMARY KEY ("transactionHash", "eventIndex"); | ||
CREATE TRIGGER mdt_event BEFORE | ||
UPDATE ON event FOR EACH ROW EXECUTE PROCEDURE moddatetime ("updatedAt"); | ||
-- migrate:down | ||
DROP TABLE public.event; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
-- migrate:up | ||
CREATE VIEW public.transfer_events AS ( | ||
SELECT event."eventData"->>'from' AS "transferFrom", | ||
event."eventData"->>'to' AS "transferTo", | ||
(event."eventData"->>'value')::numeric AS "transferAmount", | ||
event."blockHash", | ||
event."blockIndex", | ||
event."transactionHash", | ||
event."transactionIndex", | ||
event."eventIndex", | ||
event."createdAt", | ||
event."updatedAt" | ||
FROM public.event | ||
WHERE event."eventName" = 'Transfer' | ||
ORDER BY "blockIndex" DESC, | ||
"transactionIndex" DESC, | ||
"eventIndex" DESC | ||
); | ||
CREATE VIEW public.balance_events AS ( | ||
SELECT transfer_events."transferTo" AS "userId", | ||
transfer_events."transferAmount" AS "balance", | ||
transfer_events."blockHash", | ||
transfer_events."blockIndex", | ||
transfer_events."transactionHash", | ||
transfer_events."transactionIndex", | ||
transfer_events."eventIndex", | ||
transfer_events."createdAt", | ||
transfer_events."updatedAt" | ||
FROM public.transfer_events | ||
UNION ALL | ||
SELECT transfer_events."transferFrom" AS "userId", | ||
0 - transfer_events."transferAmount" AS "balance", | ||
transfer_events."blockHash", | ||
transfer_events."blockIndex", | ||
transfer_events."transactionHash", | ||
transfer_events."transactionIndex", | ||
transfer_events."eventIndex", | ||
transfer_events."createdAt", | ||
transfer_events."updatedAt" | ||
FROM public.transfer_events | ||
); | ||
CREATE VIEW public.balance AS ( | ||
SELECT "userId", | ||
sum(balance) AS balance | ||
from balance_events | ||
GROUP BY "userId" | ||
); | ||
-- migrate:down | ||
drop VIEW public.balance; | ||
drop VIEW public.balance_events; | ||
drop VIEW public.transfer_events; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
-- migrate:up | ||
CREATE VIEW public.game_events AS ( | ||
SELECT event."blockHash", | ||
event."blockIndex", | ||
event."transactionHash", | ||
event."transactionIndex", | ||
event."eventData"->>'user_id' AS "transactionOwner", | ||
CASE | ||
event."blockIndex" | ||
WHEN NULL THEN 'PENDING' | ||
ELSE 'ACCEPTED_ON_L2' | ||
END as "transactionStatus", | ||
event."eventIndex", | ||
event."eventName", | ||
event."eventData", | ||
event."createdAt", | ||
event."updatedAt", | ||
CASE | ||
WHEN event."eventData" ? 'game_id' THEN event."eventData"->>'game_id' | ||
ELSE '0x7300100008000000000000000000000000' | ||
END as "gameId", | ||
CASE | ||
WHEN event."eventData" ? 'generation' THEN (event."eventData"->>'generation')::numeric | ||
ELSE 1 | ||
END as "gameGeneration", | ||
(event."eventData"->>'state')::numeric AS "gameState", | ||
(event."eventData"->>'cell_index')::numeric AS "revivedCellIndex", | ||
(event."eventData"->>'state')::numeric = 0 as "gameOver" | ||
FROM public.event | ||
WHERE event."eventName" in ( | ||
'GameCreated', | ||
'GameEvolved', | ||
'CellRevived' | ||
) | ||
order by event."blockIndex" desc, | ||
event."transactionIndex" desc, | ||
event."eventIndex" desc | ||
); | ||
CREATE VIEW public.game_state AS ( | ||
SELECT e."gameId", | ||
e."gameGeneration", | ||
e."gameState", | ||
e."gameState" = 0 as "gameOver", | ||
( | ||
SELECT jsonb_agg( | ||
jsonb_build_object( | ||
'cell_index', | ||
rci."revivedCellIndex", | ||
'user_id', | ||
rci."transactionOwner" | ||
) | ||
) | ||
from public.game_events rci | ||
WHERE rci."gameId" = e."gameId" | ||
AND rci."gameGeneration" = e."gameGeneration" | ||
AND rci."eventName" = 'CellRevived' | ||
) as "revivedCells" | ||
FROM public.game_events e | ||
WHERE "eventName" in ('GameCreated', 'GameEvolved') | ||
ORDER BY "blockIndex" DESC, | ||
"transactionIndex" DESC, | ||
"eventIndex" DESC | ||
); | ||
CREATE VIEW public.game AS ( | ||
SELECT distinct on ("gameId") * | ||
FROM public.game_state | ||
ORDER BY "gameId", | ||
"gameGeneration" DESC | ||
); | ||
-- migrate:down | ||
DROP VIEW public.game; | ||
DROP VIEW public.game_state; | ||
DROP VIEW public.game_events; |
Oops, something went wrong.