From 4efaa2bb516b1796eb9ce3bed6b18c4f5e442828 Mon Sep 17 00:00:00 2001 From: Bogdan Crisan Date: Thu, 30 May 2024 11:33:53 +0200 Subject: [PATCH 01/12] add mqtt events for emojicoin events --- sql_extensions/migrations/mqtt-events.sql | 67 +++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 sql_extensions/migrations/mqtt-events.sql diff --git a/sql_extensions/migrations/mqtt-events.sql b/sql_extensions/migrations/mqtt-events.sql new file mode 100644 index 0000000..89df63f --- /dev/null +++ b/sql_extensions/migrations/mqtt-events.sql @@ -0,0 +1,67 @@ +CREATE OR REPLACE FUNCTION notify_periodic_event() + RETURNS trigger AS $$ +DECLARE +BEGIN + PERFORM pg_notify( + 'inbox_event', + (SELECT jsonb_build_object('topic', (NEW.type || '/' || (((NEW.data::jsonb)->('market_metadata'::text))::jsonb->>('market_id'::text)) || '/' || ((NEW.data::jsonb->('periodic_state_metadata'::text))::jsonb->>('period'::text))), 'payload', to_jsonb(NEW))::text)); + RETURN NEW; +END; +$$ LANGUAGE plpgsql; + +CREATE TRIGGER notify_periodic_event + AFTER INSERT ON inbox_events + FOR EACH ROW + WHEN (NEW.data ? 'market_metadata' AND NEW.data ? 'periodic_state_metadata') + EXECUTE PROCEDURE notify_periodic_event(); + +CREATE OR REPLACE FUNCTION notify_metadata_event() + RETURNS trigger AS $$ +DECLARE +BEGIN + PERFORM pg_notify( + 'inbox_event', + (SELECT jsonb_build_object('topic', (NEW.type || '/' || (((NEW.data::jsonb)->('market_metadata'::text))::jsonb->>('market_id'::text))), 'payload', to_jsonb(NEW))::text)); + RETURN NEW; +END; +$$ LANGUAGE plpgsql; + +CREATE TRIGGER notify_metadata_event + AFTER INSERT ON inbox_events + FOR EACH ROW + WHEN (NEW.data ? 'market_metadata' AND NOT NEW.data ? 'periodic_state_metadata') + EXECUTE PROCEDURE notify_metadata_event(); + +CREATE OR REPLACE FUNCTION notify_flat_event() + RETURNS trigger AS $$ +DECLARE +BEGIN + PERFORM pg_notify( + 'inbox_event', + (SELECT jsonb_build_object('topic', (NEW.type || '/' || ((NEW.data::jsonb)->>('market_id'::text))), 'payload', to_jsonb(NEW))::text)); + RETURN NEW; +END; +$$ LANGUAGE plpgsql; + +CREATE TRIGGER notify_flat_event + AFTER INSERT ON inbox_events + FOR EACH ROW + WHEN (NEW.data ? 'market_id' AND NOT NEW.data ? 'results_in_state_transition') + EXECUTE PROCEDURE notify_flat_event(); + +CREATE OR REPLACE FUNCTION notify_swap_event() + RETURNS trigger AS $$ +DECLARE +BEGIN + PERFORM pg_notify( + 'inbox_event', + (SELECT jsonb_build_object('topic', (NEW.type || '/' || ((NEW.data::jsonb)->>('market_id'::text)) || '/' || ((NEW.data::jsonb)->>('results_in_state_transition'::text))), 'payload', to_jsonb(NEW))::text)); + RETURN NEW; +END; +$$ LANGUAGE plpgsql; + +CREATE TRIGGER notify_swap_event + AFTER INSERT ON inbox_events + FOR EACH ROW + WHEN (NEW.data ? 'market_id' AND NEW.data ? 'results_in_state_transition') + EXECUTE PROCEDURE notify_swap_event(); From 599d8949dcf47ad2bf64745fc02f90c167c9c4c9 Mon Sep 17 00:00:00 2001 From: Bogdan Crisan Date: Fri, 31 May 2024 18:24:31 +0200 Subject: [PATCH 02/12] add special queries --- sql_extensions/migrations/special-queries.sql | 153 ++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 sql_extensions/migrations/special-queries.sql diff --git a/sql_extensions/migrations/special-queries.sql b/sql_extensions/migrations/special-queries.sql new file mode 100644 index 0000000..42cbee1 --- /dev/null +++ b/sql_extensions/migrations/special-queries.sql @@ -0,0 +1,153 @@ +-- LATEST EVENTS {{{ + +-- Create a table containing the latest state for each market and auto update +-- using triggers. + +CREATE TABLE inbox_latest_state AS +TABLE inbox_events +WITH NO DATA; + +ALTER TABLE inbox_latest_state +ADD COLUMN market_id NUMERIC; + +ALTER TABLE inbox_latest_state +ADD PRIMARY KEY (market_id); + +INSERT INTO inbox_latest_state +SELECT DISTINCT ON ((data->'market_metadata'->>'market_id')::numeric) + *, + (data->'market_metadata'->>'market_id')::numeric AS market_id +FROM + inbox_events +WHERE + REVERSE(indexed_type) LIKE REVERSE('%::emojicoin_dot_fun::State') +ORDER BY + (data->'market_metadata'->>'market_id')::numeric, + (data->>'transaction_version')::numeric DESC, + (data->>'event_index')::numeric DESC; + +CREATE OR REPLACE FUNCTION update_latest_state() + RETURNS trigger AS $$ +BEGIN + DELETE FROM inbox_latest_state + WHERE (inbox_latest_state.data->'market_metadata'->>'market_id')::numeric = (NEW.data->'market_metadata'->>'market_id')::numeric + AND (inbox_latest_state.data->'state_metadata'->>'bump_time')::numeric <= (NEW.data->'state_metadata'->>'bump_time')::numeric; + INSERT INTO inbox_latest_state SELECT NEW.*; + RETURN NEW; +END; +$$ LANGUAGE plpgsql; + +CREATE TRIGGER update_latest_state + AFTER INSERT ON inbox_events + FOR EACH ROW + WHEN (NEW.type LIKE '%::emojicoin_dot_fun::State') + EXECUTE PROCEDURE update_latest_state(); + +CREATE INDEX inbox_latest_state_by_market_cap ON inbox_events ( + ((data->'instantaneous_stats'->>'market_cap')::numeric) DESC +); + +CREATE INDEX inbox_latest_state_by_bump_time ON inbox_events ( + ((data->'state_metadata'->>'bump_time')::numeric) DESC +); + +-- }}} + +-- VOLUME {{{ + +-- Create a table containing the daily and all time volume for each market and +-- auto update using triggers. + +CREATE TABLE inbox_volume ( + market_id NUMERIC PRIMARY KEY, + all_time_volume NUMERIC NOT NULL, + daily_volume NUMERIC NOT NULL +); + +INSERT INTO inbox_volume +SELECT + (data#>>'{market_metadata,market_id}')::numeric AS market_id, + 0::numeric AS all_time_volume, + SUM((data->>'volume_quote')::numeric) AS daily_volume +FROM + inbox_events +WHERE + REVERSE(indexed_type) LIKE REVERSE('%::emojicoin_dot_fun::PeriodicState') +AND + to_timestamp((data->'periodic_state_metadata'->>'start_time')::numeric / 1000) > CURRENT_TIMESTAMP - interval '1 day' +AND + data->'periodic_state_metadata'->>'period' = '60000000' +GROUP BY + data#>>'{market_metadata,market_id}'; + +UPDATE inbox_volume SET all_time_volume = tmp.volume +FROM ( + SELECT + SUM((data->>'volume_quote')::numeric) AS volume, + (data#>>'{market_metadata,market_id}')::numeric AS market_id + FROM + inbox_events + WHERE + REVERSE(indexed_type) LIKE REVERSE('%::emojicoin_dot_fun::PeriodicState') + AND + data->'periodic_state_metadata'->>'period' = '900000000' + GROUP BY + data#>>'{market_metadata,market_id}') AS tmp +WHERE tmp.market_id = inbox_volume.market_id; + +CREATE OR REPLACE FUNCTION update_volume() + RETURNS trigger AS $$ +BEGIN + IF NOT EXISTS (SELECT * FROM inbox_volume WHERE market_id = (NEW.data->'market_metadata'->>'market_id')::numeric) THEN + INSERT INTO inbox_volume VALUES ((NEW.data->'market_metadata'->>'market_id')::numeric, 0::numeric, 0::numeric); + END IF; + UPDATE inbox_volume + SET + daily_volume = ( + SELECT SUM((e.data->>'volume_quote')::numeric) FROM inbox_events e + WHERE REVERSE(e.indexed_type) LIKE REVERSE('%::emojicoin_dot_fun::PeriodicState') + AND to_timestamp((e.data->'periodic_state_metadata'->>'start_time')::numeric / 1000) > CURRENT_TIMESTAMP - interval '1 day' + AND e.data->'periodic_state_metadata'->>'period' = '60000000' + AND (e.data->'market_metadata'->>'market_id')::numeric = (NEW.data->'market_metadata'->>'market_id')::numeric + ), + all_time_volume = inbox_volume.all_time_volume + (NEW.data->>'volume_quote')::numeric + WHERE + inbox_volume.market_id = (NEW.data->'market_metadata'->'market_id')::numeric; + RETURN NEW; +END; +$$ LANGUAGE plpgsql; + +CREATE TRIGGER update_volume + AFTER INSERT ON inbox_events + FOR EACH ROW + WHEN (NEW.type LIKE '%::emojicoin_dot_fun::PeriodicState' AND NEW.data->'periodic_state_metadata'->>'period' = '60000000') + EXECUTE PROCEDURE update_volume(); + +CREATE INDEX inbox_latest_state_by_all_time_volume ON inbox_volume ( + all_time_volume DESC +); +CREATE INDEX inbox_latest_state_by_daily_volume ON inbox_volume ( + daily_volume DESC +); + +-- }}} + +CREATE VIEW market_data AS +SELECT + inbox_latest_state.market_id, + (data->'instantaneous_stats'->>'market_cap')::numeric AS market_cap, + (data->'state_metadata'->>'bump_time')::numeric AS bump_time, + all_time_volume, + daily_volume +FROM inbox_latest_state, inbox_volume +WHERE inbox_latest_state.market_id = inbox_volume.market_id; + +CREATE INDEX inbox_indexed_type ON inbox_events (REVERSE(indexed_type)); + +CREATE INDEX inbox_periodic_state ON inbox_events ( + ((data->'market_metadata'->>'market_id')::numeric), + ((data->'periodic_state_metadata'->>'period')::numeric), + ((data->'periodic_state_metadata'->>'start_time')::numeric) +) WHERE REVERSE(indexed_type) LIKE REVERSE('%::emojicoin_dot_fun::PeriodicState'); + +-- vim: foldmethod=marker foldenable From 46ed2e48438d1091bf8949752543c0445e4e2081 Mon Sep 17 00:00:00 2001 From: Bogdan Crisan Date: Fri, 31 May 2024 18:38:41 +0200 Subject: [PATCH 03/12] fix pre-commit lints --- cfg/cspell-dictionary.txt | 3 + cfg/sqlfluff | 1 + sql_extensions/migrations/mqtt-events.sql | 40 +++--- sql_extensions/migrations/special-queries.sql | 130 ++++++++++-------- 4 files changed, 96 insertions(+), 78 deletions(-) diff --git a/cfg/cspell-dictionary.txt b/cfg/cspell-dictionary.txt index 494698f..f6c23e3 100644 --- a/cfg/cspell-dictionary.txt +++ b/cfg/cspell-dictionary.txt @@ -19,6 +19,8 @@ econialabs emojicoin eventloop eventpoll +foldenable +foldmethod gcloud googleapis grpc @@ -51,6 +53,7 @@ sqlx sslmode subnetwork subnetworks +templater testnet tfstate tfvars diff --git a/cfg/sqlfluff b/cfg/sqlfluff index 46a1838..3837526 100644 --- a/cfg/sqlfluff +++ b/cfg/sqlfluff @@ -1,6 +1,7 @@ [sqlfluff] dialect = postgres +templater = raw max_line_length = 120 diff --git a/sql_extensions/migrations/mqtt-events.sql b/sql_extensions/migrations/mqtt-events.sql index 89df63f..8297ae2 100644 --- a/sql_extensions/migrations/mqtt-events.sql +++ b/sql_extensions/migrations/mqtt-events.sql @@ -1,5 +1,5 @@ CREATE OR REPLACE FUNCTION notify_periodic_event() - RETURNS trigger AS $$ +RETURNS trigger AS $$ DECLARE BEGIN PERFORM pg_notify( @@ -10,13 +10,13 @@ END; $$ LANGUAGE plpgsql; CREATE TRIGGER notify_periodic_event - AFTER INSERT ON inbox_events - FOR EACH ROW - WHEN (NEW.data ? 'market_metadata' AND NEW.data ? 'periodic_state_metadata') - EXECUTE PROCEDURE notify_periodic_event(); +AFTER INSERT ON inbox_events +FOR EACH ROW +WHEN (new.data ? 'market_metadata' AND new.data ? 'periodic_state_metadata') +EXECUTE PROCEDURE notify_periodic_event(); CREATE OR REPLACE FUNCTION notify_metadata_event() - RETURNS trigger AS $$ +RETURNS trigger AS $$ DECLARE BEGIN PERFORM pg_notify( @@ -27,13 +27,13 @@ END; $$ LANGUAGE plpgsql; CREATE TRIGGER notify_metadata_event - AFTER INSERT ON inbox_events - FOR EACH ROW - WHEN (NEW.data ? 'market_metadata' AND NOT NEW.data ? 'periodic_state_metadata') - EXECUTE PROCEDURE notify_metadata_event(); +AFTER INSERT ON inbox_events +FOR EACH ROW +WHEN (new.data ? 'market_metadata' AND NOT new.data ? 'periodic_state_metadata') +EXECUTE PROCEDURE notify_metadata_event(); CREATE OR REPLACE FUNCTION notify_flat_event() - RETURNS trigger AS $$ +RETURNS trigger AS $$ DECLARE BEGIN PERFORM pg_notify( @@ -44,13 +44,13 @@ END; $$ LANGUAGE plpgsql; CREATE TRIGGER notify_flat_event - AFTER INSERT ON inbox_events - FOR EACH ROW - WHEN (NEW.data ? 'market_id' AND NOT NEW.data ? 'results_in_state_transition') - EXECUTE PROCEDURE notify_flat_event(); +AFTER INSERT ON inbox_events +FOR EACH ROW +WHEN (new.data ? 'market_id' AND NOT new.data ? 'results_in_state_transition') +EXECUTE PROCEDURE notify_flat_event(); CREATE OR REPLACE FUNCTION notify_swap_event() - RETURNS trigger AS $$ +RETURNS trigger AS $$ DECLARE BEGIN PERFORM pg_notify( @@ -61,7 +61,7 @@ END; $$ LANGUAGE plpgsql; CREATE TRIGGER notify_swap_event - AFTER INSERT ON inbox_events - FOR EACH ROW - WHEN (NEW.data ? 'market_id' AND NEW.data ? 'results_in_state_transition') - EXECUTE PROCEDURE notify_swap_event(); +AFTER INSERT ON inbox_events +FOR EACH ROW +WHEN (new.data ? 'market_id' AND new.data ? 'results_in_state_transition') +EXECUTE PROCEDURE notify_swap_event(); diff --git a/sql_extensions/migrations/special-queries.sql b/sql_extensions/migrations/special-queries.sql index 42cbee1..4e1bf9f 100644 --- a/sql_extensions/migrations/special-queries.sql +++ b/sql_extensions/migrations/special-queries.sql @@ -14,20 +14,29 @@ ALTER TABLE inbox_latest_state ADD PRIMARY KEY (market_id); INSERT INTO inbox_latest_state -SELECT DISTINCT ON ((data->'market_metadata'->>'market_id')::numeric) - *, - (data->'market_metadata'->>'market_id')::numeric AS market_id +SELECT DISTINCT ON ((data -> 'market_metadata' ->> 'market_id')::NUMERIC) + sequence_number, + creation_number, + account_address, + transaction_version, + transaction_block_height, + type, + data, + inserted_at, + event_index, + indexed_type, + (data -> 'market_metadata' ->> 'market_id')::NUMERIC AS market_id FROM - inbox_events + inbox_events WHERE - REVERSE(indexed_type) LIKE REVERSE('%::emojicoin_dot_fun::State') + REVERSE(indexed_type) LIKE REVERSE('%::emojicoin_dot_fun::State') ORDER BY - (data->'market_metadata'->>'market_id')::numeric, - (data->>'transaction_version')::numeric DESC, - (data->>'event_index')::numeric DESC; + (data -> 'market_metadata' ->> 'market_id')::NUMERIC, + (data ->> 'transaction_version')::NUMERIC DESC, + (data ->> 'event_index')::NUMERIC DESC; -CREATE OR REPLACE FUNCTION update_latest_state() - RETURNS trigger AS $$ +CREATE OR REPLACE FUNCTION UPDATE_LATEST_STATE() +RETURNS TRIGGER AS $$ BEGIN DELETE FROM inbox_latest_state WHERE (inbox_latest_state.data->'market_metadata'->>'market_id')::numeric = (NEW.data->'market_metadata'->>'market_id')::numeric @@ -38,17 +47,17 @@ END; $$ LANGUAGE plpgsql; CREATE TRIGGER update_latest_state - AFTER INSERT ON inbox_events - FOR EACH ROW - WHEN (NEW.type LIKE '%::emojicoin_dot_fun::State') - EXECUTE PROCEDURE update_latest_state(); +AFTER INSERT ON inbox_events +FOR EACH ROW +WHEN (new.type LIKE '%::emojicoin_dot_fun::State') +EXECUTE PROCEDURE UPDATE_LATEST_STATE(); CREATE INDEX inbox_latest_state_by_market_cap ON inbox_events ( - ((data->'instantaneous_stats'->>'market_cap')::numeric) DESC + ((data -> 'instantaneous_stats' ->> 'market_cap')::NUMERIC) DESC ); CREATE INDEX inbox_latest_state_by_bump_time ON inbox_events ( - ((data->'state_metadata'->>'bump_time')::numeric) DESC + ((data -> 'state_metadata' ->> 'bump_time')::NUMERIC) DESC ); -- }}} @@ -59,44 +68,46 @@ CREATE INDEX inbox_latest_state_by_bump_time ON inbox_events ( -- auto update using triggers. CREATE TABLE inbox_volume ( - market_id NUMERIC PRIMARY KEY, - all_time_volume NUMERIC NOT NULL, - daily_volume NUMERIC NOT NULL + market_id NUMERIC PRIMARY KEY, + all_time_volume NUMERIC NOT NULL, + daily_volume NUMERIC NOT NULL ); INSERT INTO inbox_volume SELECT - (data#>>'{market_metadata,market_id}')::numeric AS market_id, - 0::numeric AS all_time_volume, - SUM((data->>'volume_quote')::numeric) AS daily_volume + (data #>> '{market_metadata,market_id}')::NUMERIC AS market_id, + 0::NUMERIC AS all_time_volume, + SUM((data ->> 'volume_quote')::NUMERIC) AS daily_volume FROM - inbox_events + inbox_events WHERE - REVERSE(indexed_type) LIKE REVERSE('%::emojicoin_dot_fun::PeriodicState') -AND - to_timestamp((data->'periodic_state_metadata'->>'start_time')::numeric / 1000) > CURRENT_TIMESTAMP - interval '1 day' -AND - data->'periodic_state_metadata'->>'period' = '60000000' + REVERSE(indexed_type) LIKE REVERSE('%::emojicoin_dot_fun::PeriodicState') + AND + TO_TIMESTAMP((data -> 'periodic_state_metadata' ->> 'start_time')::NUMERIC / 1000) + > CURRENT_TIMESTAMP - INTERVAL '1 day' + AND + data -> 'periodic_state_metadata' ->> 'period' = '60000000' GROUP BY - data#>>'{market_metadata,market_id}'; + data #>> '{market_metadata,market_id}'; UPDATE inbox_volume SET all_time_volume = tmp.volume FROM ( - SELECT - SUM((data->>'volume_quote')::numeric) AS volume, - (data#>>'{market_metadata,market_id}')::numeric AS market_id - FROM - inbox_events - WHERE - REVERSE(indexed_type) LIKE REVERSE('%::emojicoin_dot_fun::PeriodicState') - AND - data->'periodic_state_metadata'->>'period' = '900000000' - GROUP BY - data#>>'{market_metadata,market_id}') AS tmp + SELECT + (data #>> '{market_metadata,market_id}')::NUMERIC AS market_id, + SUM((data ->> 'volume_quote')::NUMERIC) AS volume + FROM + inbox_events + WHERE + REVERSE(indexed_type) LIKE REVERSE('%::emojicoin_dot_fun::PeriodicState') + AND + data -> 'periodic_state_metadata' ->> 'period' = '900000000' + GROUP BY + data #>> '{market_metadata,market_id}' +) AS tmp WHERE tmp.market_id = inbox_volume.market_id; -CREATE OR REPLACE FUNCTION update_volume() - RETURNS trigger AS $$ +CREATE OR REPLACE FUNCTION UPDATE_VOLUME() +RETURNS TRIGGER AS $$ BEGIN IF NOT EXISTS (SELECT * FROM inbox_volume WHERE market_id = (NEW.data->'market_metadata'->>'market_id')::numeric) THEN INSERT INTO inbox_volume VALUES ((NEW.data->'market_metadata'->>'market_id')::numeric, 0::numeric, 0::numeric); @@ -118,36 +129,39 @@ END; $$ LANGUAGE plpgsql; CREATE TRIGGER update_volume - AFTER INSERT ON inbox_events - FOR EACH ROW - WHEN (NEW.type LIKE '%::emojicoin_dot_fun::PeriodicState' AND NEW.data->'periodic_state_metadata'->>'period' = '60000000') - EXECUTE PROCEDURE update_volume(); +AFTER INSERT ON inbox_events +FOR EACH ROW +WHEN ( + new.type LIKE '%::emojicoin_dot_fun::PeriodicState' + AND new.data -> 'periodic_state_metadata' ->> 'period' = '60000000' +) +EXECUTE PROCEDURE UPDATE_VOLUME(); CREATE INDEX inbox_latest_state_by_all_time_volume ON inbox_volume ( - all_time_volume DESC + all_time_volume DESC ); CREATE INDEX inbox_latest_state_by_daily_volume ON inbox_volume ( - daily_volume DESC + daily_volume DESC ); -- }}} CREATE VIEW market_data AS SELECT - inbox_latest_state.market_id, - (data->'instantaneous_stats'->>'market_cap')::numeric AS market_cap, - (data->'state_metadata'->>'bump_time')::numeric AS bump_time, - all_time_volume, - daily_volume -FROM inbox_latest_state, inbox_volume -WHERE inbox_latest_state.market_id = inbox_volume.market_id; + state.market_id, + (state.data -> 'instantaneous_stats' ->> 'market_cap')::NUMERIC AS market_cap, + (state.data -> 'state_metadata' ->> 'bump_time')::NUMERIC AS bump_time, + volume.all_time_volume, + volume.daily_volume +FROM inbox_latest_state AS state, inbox_volume AS volume +WHERE state.market_id = volume.market_id; CREATE INDEX inbox_indexed_type ON inbox_events (REVERSE(indexed_type)); CREATE INDEX inbox_periodic_state ON inbox_events ( - ((data->'market_metadata'->>'market_id')::numeric), - ((data->'periodic_state_metadata'->>'period')::numeric), - ((data->'periodic_state_metadata'->>'start_time')::numeric) + ((data -> 'market_metadata' ->> 'market_id')::NUMERIC), + ((data -> 'periodic_state_metadata' ->> 'period')::NUMERIC), + ((data -> 'periodic_state_metadata' ->> 'start_time')::NUMERIC) ) WHERE REVERSE(indexed_type) LIKE REVERSE('%::emojicoin_dot_fun::PeriodicState'); -- vim: foldmethod=marker foldenable From 135b3af45e3b4cd9e58a4381fdce5d531299e19a Mon Sep 17 00:00:00 2001 From: Bogdan Crisan Date: Fri, 31 May 2024 18:42:14 +0200 Subject: [PATCH 04/12] remove useless declare --- sql_extensions/migrations/mqtt-events.sql | 4 ---- 1 file changed, 4 deletions(-) diff --git a/sql_extensions/migrations/mqtt-events.sql b/sql_extensions/migrations/mqtt-events.sql index 8297ae2..73caa9b 100644 --- a/sql_extensions/migrations/mqtt-events.sql +++ b/sql_extensions/migrations/mqtt-events.sql @@ -1,6 +1,5 @@ CREATE OR REPLACE FUNCTION notify_periodic_event() RETURNS trigger AS $$ -DECLARE BEGIN PERFORM pg_notify( 'inbox_event', @@ -17,7 +16,6 @@ EXECUTE PROCEDURE notify_periodic_event(); CREATE OR REPLACE FUNCTION notify_metadata_event() RETURNS trigger AS $$ -DECLARE BEGIN PERFORM pg_notify( 'inbox_event', @@ -34,7 +32,6 @@ EXECUTE PROCEDURE notify_metadata_event(); CREATE OR REPLACE FUNCTION notify_flat_event() RETURNS trigger AS $$ -DECLARE BEGIN PERFORM pg_notify( 'inbox_event', @@ -51,7 +48,6 @@ EXECUTE PROCEDURE notify_flat_event(); CREATE OR REPLACE FUNCTION notify_swap_event() RETURNS trigger AS $$ -DECLARE BEGIN PERFORM pg_notify( 'inbox_event', From d5e4dbc383ab3c6af9293c8cf6d792134d2c0226 Mon Sep 17 00:00:00 2001 From: Bogdan Crisan Date: Mon, 3 Jun 2024 03:54:56 +0200 Subject: [PATCH 05/12] add more info to market endpoint --- sql_extensions/migrations/special-queries.sql | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/sql_extensions/migrations/special-queries.sql b/sql_extensions/migrations/special-queries.sql index 4e1bf9f..afa05f7 100644 --- a/sql_extensions/migrations/special-queries.sql +++ b/sql_extensions/migrations/special-queries.sql @@ -149,14 +149,20 @@ CREATE INDEX inbox_latest_state_by_daily_volume ON inbox_volume ( CREATE VIEW market_data AS SELECT state.market_id, + state.transaction_version, (state.data -> 'instantaneous_stats' ->> 'market_cap')::NUMERIC AS market_cap, (state.data -> 'state_metadata' ->> 'bump_time')::NUMERIC AS bump_time, + (state.data -> 'cumulative_stats' ->> 'n_swaps')::NUMERIC AS n_swaps, + (state.data -> 'cumulative_stats' ->> 'n_chat_messages')::NUMERIC AS n_chat_messages, + state.data -> 'clamm_virtual_reserves' AS clamm_virtual_reserves, + state.data -> 'cpamm_real_reserves' AS cpamm_real_reserves, volume.all_time_volume, volume.daily_volume FROM inbox_latest_state AS state, inbox_volume AS volume WHERE state.market_id = volume.market_id; CREATE INDEX inbox_indexed_type ON inbox_events (REVERSE(indexed_type)); +CREATE INDEX inbox_indexed_type ON inbox_events (indexed_type); CREATE INDEX inbox_periodic_state ON inbox_events ( ((data -> 'market_metadata' ->> 'market_id')::NUMERIC), From 6cd53f64e7ea3146efeca0e7c494f0d749d8659f Mon Sep 17 00:00:00 2001 From: Bogdan Crisan Date: Mon, 3 Jun 2024 04:31:58 +0200 Subject: [PATCH 06/12] incorporate event_name column --- sql_extensions/migrations/special-queries.sql | 39 ++++++++++++------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/sql_extensions/migrations/special-queries.sql b/sql_extensions/migrations/special-queries.sql index afa05f7..8b36aef 100644 --- a/sql_extensions/migrations/special-queries.sql +++ b/sql_extensions/migrations/special-queries.sql @@ -13,7 +13,20 @@ ADD COLUMN market_id NUMERIC; ALTER TABLE inbox_latest_state ADD PRIMARY KEY (market_id); -INSERT INTO inbox_latest_state +INSERT INTO inbox_latest_state ( + sequence_number, + creation_number, + account_address, + transaction_version, + transaction_block_height, + type, + data, + inserted_at, + event_index, + indexed_type, + market_id, + event_name +) SELECT DISTINCT ON ((data -> 'market_metadata' ->> 'market_id')::NUMERIC) sequence_number, creation_number, @@ -25,11 +38,12 @@ SELECT DISTINCT ON ((data -> 'market_metadata' ->> 'market_id')::NUMERIC) inserted_at, event_index, indexed_type, - (data -> 'market_metadata' ->> 'market_id')::NUMERIC AS market_id + (data -> 'market_metadata' ->> 'market_id')::NUMERIC AS market_id, + event_name FROM inbox_events WHERE - REVERSE(indexed_type) LIKE REVERSE('%::emojicoin_dot_fun::State') + event_name = 'emojicoin_dot_fun::State' ORDER BY (data -> 'market_metadata' ->> 'market_id')::NUMERIC, (data ->> 'transaction_version')::NUMERIC DESC, @@ -81,7 +95,7 @@ SELECT FROM inbox_events WHERE - REVERSE(indexed_type) LIKE REVERSE('%::emojicoin_dot_fun::PeriodicState') + event_name = 'emojicoin_dot_fun::PeriodicState' AND TO_TIMESTAMP((data -> 'periodic_state_metadata' ->> 'start_time')::NUMERIC / 1000) > CURRENT_TIMESTAMP - INTERVAL '1 day' @@ -98,7 +112,7 @@ FROM ( FROM inbox_events WHERE - REVERSE(indexed_type) LIKE REVERSE('%::emojicoin_dot_fun::PeriodicState') + event_name = 'emojicoin_dot_fun::PeriodicState' AND data -> 'periodic_state_metadata' ->> 'period' = '900000000' GROUP BY @@ -116,7 +130,7 @@ BEGIN SET daily_volume = ( SELECT SUM((e.data->>'volume_quote')::numeric) FROM inbox_events e - WHERE REVERSE(e.indexed_type) LIKE REVERSE('%::emojicoin_dot_fun::PeriodicState') + WHERE e.event_name = 'emojicoin_dot_fun::PeriodicState' AND to_timestamp((e.data->'periodic_state_metadata'->>'start_time')::numeric / 1000) > CURRENT_TIMESTAMP - interval '1 day' AND e.data->'periodic_state_metadata'->>'period' = '60000000' AND (e.data->'market_metadata'->>'market_id')::numeric = (NEW.data->'market_metadata'->>'market_id')::numeric @@ -132,7 +146,7 @@ CREATE TRIGGER update_volume AFTER INSERT ON inbox_events FOR EACH ROW WHEN ( - new.type LIKE '%::emojicoin_dot_fun::PeriodicState' + new.event_name = 'emojicoin_dot_fun::PeriodicState' AND new.data -> 'periodic_state_metadata' ->> 'period' = '60000000' ) EXECUTE PROCEDURE UPDATE_VOLUME(); @@ -150,24 +164,21 @@ CREATE VIEW market_data AS SELECT state.market_id, state.transaction_version, + volume.all_time_volume, + volume.daily_volume, (state.data -> 'instantaneous_stats' ->> 'market_cap')::NUMERIC AS market_cap, (state.data -> 'state_metadata' ->> 'bump_time')::NUMERIC AS bump_time, (state.data -> 'cumulative_stats' ->> 'n_swaps')::NUMERIC AS n_swaps, (state.data -> 'cumulative_stats' ->> 'n_chat_messages')::NUMERIC AS n_chat_messages, state.data -> 'clamm_virtual_reserves' AS clamm_virtual_reserves, - state.data -> 'cpamm_real_reserves' AS cpamm_real_reserves, - volume.all_time_volume, - volume.daily_volume + state.data -> 'cpamm_real_reserves' AS cpamm_real_reserves FROM inbox_latest_state AS state, inbox_volume AS volume WHERE state.market_id = volume.market_id; -CREATE INDEX inbox_indexed_type ON inbox_events (REVERSE(indexed_type)); -CREATE INDEX inbox_indexed_type ON inbox_events (indexed_type); - CREATE INDEX inbox_periodic_state ON inbox_events ( ((data -> 'market_metadata' ->> 'market_id')::NUMERIC), ((data -> 'periodic_state_metadata' ->> 'period')::NUMERIC), ((data -> 'periodic_state_metadata' ->> 'start_time')::NUMERIC) -) WHERE REVERSE(indexed_type) LIKE REVERSE('%::emojicoin_dot_fun::PeriodicState'); +) WHERE event_name = 'emojicoin_dot_fun::PeriodicState'; -- vim: foldmethod=marker foldenable From 7498bd38e9bdeb07f37d106eefafec95dac4c6f1 Mon Sep 17 00:00:00 2001 From: Bogdan Crisan Date: Mon, 3 Jun 2024 04:34:36 +0200 Subject: [PATCH 07/12] fix pre-commit errors --- cfg/cspell-dictionary.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cfg/cspell-dictionary.txt b/cfg/cspell-dictionary.txt index f6c23e3..add451d 100644 --- a/cfg/cspell-dictionary.txt +++ b/cfg/cspell-dictionary.txt @@ -10,9 +10,11 @@ bigdecimal capitalisation chdir chrono +clamm clippy cloudbuild cloudresourcemanager +cpamm creds devnet econialabs From ca6f5ecf759bbbf89f6e12039c1cff62f0601429 Mon Sep 17 00:00:00 2001 From: Matt <90358481+xbtmatt@users.noreply.github.com> Date: Wed, 5 Jun 2024 01:39:56 -0700 Subject: [PATCH 08/12] [ECO 1753] Fix `market_id` cast in `market_data` view, use `market_nonce` instead of `bump_time` (#21) * Fix `market_id` issue in psql view * Change `transaction_version` to `version` and add `emoji_bytes` * Sort by market nonce, break early on market nonce, then use ON CONFLICT DO UPDATE SET with WHERE clause * Lint and rebase * Remove schema refresh since I can just do it locally * Remove unnecessary WHERE clause and use `event_name` column * Fix the state metadata update logic * Add `market_address` to query response * [ECO-1768] Update SQL queries for emojicoin-dot-fun (#24) * small tweaks to matt's pr * show markets before first state event --------- Co-authored-by: matt Co-authored-by: Bogdan Crisan --- cfg/cspell-dictionary.txt | 1 + sql_extensions/migrations/special-queries.sql | 56 +++++++++++++------ 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/cfg/cspell-dictionary.txt b/cfg/cspell-dictionary.txt index add451d..b54d6ca 100644 --- a/cfg/cspell-dictionary.txt +++ b/cfg/cspell-dictionary.txt @@ -39,6 +39,7 @@ mdformat mosquitto mqtt mqttoptions +noqa notif pgrep plpgsql diff --git a/sql_extensions/migrations/special-queries.sql b/sql_extensions/migrations/special-queries.sql index 8b36aef..2c91e3c 100644 --- a/sql_extensions/migrations/special-queries.sql +++ b/sql_extensions/migrations/special-queries.sql @@ -1,4 +1,4 @@ --- LATEST EVENTS {{{ +-- LATEST STATE {{{ -- Create a table containing the latest state for each market and auto update -- using triggers. @@ -47,15 +47,25 @@ WHERE ORDER BY (data -> 'market_metadata' ->> 'market_id')::NUMERIC, (data ->> 'transaction_version')::NUMERIC DESC, - (data ->> 'event_index')::NUMERIC DESC; + (data -> 'state_metadata' ->> 'market_nonce')::NUMERIC DESC; CREATE OR REPLACE FUNCTION UPDATE_LATEST_STATE() RETURNS TRIGGER AS $$ BEGIN - DELETE FROM inbox_latest_state - WHERE (inbox_latest_state.data->'market_metadata'->>'market_id')::numeric = (NEW.data->'market_metadata'->>'market_id')::numeric - AND (inbox_latest_state.data->'state_metadata'->>'bump_time')::numeric <= (NEW.data->'state_metadata'->>'bump_time')::numeric; - INSERT INTO inbox_latest_state SELECT NEW.*; + INSERT INTO inbox_latest_state + SELECT + NEW.*, + (NEW.data -> 'market_metadata' ->> 'market_id')::NUMERIC + ON CONFLICT (market_id) DO UPDATE SET + transaction_version = EXCLUDED.transaction_version, + transaction_block_height = EXCLUDED.transaction_block_height, + data = EXCLUDED.data, + inserted_at = EXCLUDED.inserted_at, + event_index = EXCLUDED.event_index + WHERE + (inbox_latest_state.data -> 'market_metadata' ->> 'market_id')::NUMERIC = (EXCLUDED.data -> 'market_metadata' ->> 'market_id')::NUMERIC + AND + (inbox_latest_state.data -> 'state_metadata' ->> 'market_nonce')::NUMERIC < (EXCLUDED.data -> 'state_metadata' ->> 'market_nonce')::NUMERIC; RETURN NEW; END; $$ LANGUAGE plpgsql; @@ -63,15 +73,15 @@ $$ LANGUAGE plpgsql; CREATE TRIGGER update_latest_state AFTER INSERT ON inbox_events FOR EACH ROW -WHEN (new.type LIKE '%::emojicoin_dot_fun::State') +WHEN (new.event_name = 'emojicoin_dot_fun::State') EXECUTE PROCEDURE UPDATE_LATEST_STATE(); CREATE INDEX inbox_latest_state_by_market_cap ON inbox_events ( ((data -> 'instantaneous_stats' ->> 'market_cap')::NUMERIC) DESC ); -CREATE INDEX inbox_latest_state_by_bump_time ON inbox_events ( - ((data -> 'state_metadata' ->> 'bump_time')::NUMERIC) DESC +CREATE INDEX inbox_latest_state_by_nonce ON inbox_events ( + ((data -> 'state_metadata' ->> 'market_nonce')::NUMERIC) DESC ); -- }}} @@ -137,7 +147,7 @@ BEGIN ), all_time_volume = inbox_volume.all_time_volume + (NEW.data->>'volume_quote')::numeric WHERE - inbox_volume.market_id = (NEW.data->'market_metadata'->'market_id')::numeric; + inbox_volume.market_id = (NEW.data->'market_metadata'->>'market_id')::numeric; RETURN NEW; END; $$ LANGUAGE plpgsql; @@ -160,20 +170,34 @@ CREATE INDEX inbox_latest_state_by_daily_volume ON inbox_volume ( -- }}} +-- noqa: disable=ST06 CREATE VIEW market_data AS SELECT - state.market_id, + -- General data + (registration.data -> 'market_metadata' ->> 'market_id')::NUMERIC AS market_id, + (registration.data -> 'market_metadata' ->> 'emoji_bytes') AS emoji_bytes, + (registration.data -> 'market_metadata' ->> 'market_address') AS market_address, + -- Latest state data state.transaction_version, - volume.all_time_volume, - volume.daily_volume, (state.data -> 'instantaneous_stats' ->> 'market_cap')::NUMERIC AS market_cap, (state.data -> 'state_metadata' ->> 'bump_time')::NUMERIC AS bump_time, (state.data -> 'cumulative_stats' ->> 'n_swaps')::NUMERIC AS n_swaps, (state.data -> 'cumulative_stats' ->> 'n_chat_messages')::NUMERIC AS n_chat_messages, + (state.data -> 'last_swap' ->> 'avg_execution_price_q64')::NUMERIC AS avg_execution_price_q64, + (state.data ->> 'lp_coin_supply')::NUMERIC AS lp_coin_supply, state.data -> 'clamm_virtual_reserves' AS clamm_virtual_reserves, - state.data -> 'cpamm_real_reserves' AS cpamm_real_reserves -FROM inbox_latest_state AS state, inbox_volume AS volume -WHERE state.market_id = volume.market_id; + state.data -> 'cpamm_real_reserves' AS cpamm_real_reserves, + -- Volume data + volume.all_time_volume, + volume.daily_volume +FROM ( + SELECT data FROM inbox_events WHERE event_name = 'emojicoin_dot_fun::MarketRegistration' +) AS registration +LEFT JOIN inbox_latest_state AS state + ON (registration.data -> 'market_metadata' ->> 'market_id')::NUMERIC = state.market_id +LEFT JOIN inbox_volume AS volume + ON (registration.data -> 'market_metadata' ->> 'market_id')::NUMERIC = volume.market_id; +-- noqa: disable=ST06 CREATE INDEX inbox_periodic_state ON inbox_events ( ((data -> 'market_metadata' ->> 'market_id')::NUMERIC), From 11f138517f5501062744c2cbef215a8d57048ed1 Mon Sep 17 00:00:00 2001 From: Bogdan Crisan Date: Wed, 5 Jun 2024 20:23:50 +0200 Subject: [PATCH 09/12] add auth for postgrest --- terraform/main.tf | 1 + terraform/modules/postgrest/main.tf | 19 +++++++++++++++++-- terraform/modules/postgrest/variables.tf | 2 ++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/terraform/main.tf b/terraform/main.tf index c9a6a75..690bf05 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -58,6 +58,7 @@ module "postgrest" { migrations_complete = module.migrations.migrations_complete no_auth_policy_data = module.no_auth_policy.policy_data postgrest_max_rows = var.postgrest_max_rows + project_id = var.project_id region = var.region source = "./modules/postgrest" sql_vpc_connector_id = module.db.sql_vpc_connector_id diff --git a/terraform/modules/postgrest/main.tf b/terraform/modules/postgrest/main.tf index 3e30530..f5fff8a 100644 --- a/terraform/modules/postgrest/main.tf +++ b/terraform/modules/postgrest/main.tf @@ -30,11 +30,26 @@ resource "google_cloud_run_v2_service" "postgrest" { egress = "ALL_TRAFFIC" } } + custom_audiences = ["vercel"] } -resource "google_cloud_run_service_iam_policy" "no_auth_postgrest" { +resource "google_cloud_run_service_iam_policy" "auth_postgrest" { location = google_cloud_run_v2_service.postgrest.location project = google_cloud_run_v2_service.postgrest.project service = google_cloud_run_v2_service.postgrest.name - policy_data = var.no_auth_policy_data + policy_data = data.google_iam_policy.auth_postgrest.policy_data +} + +resource "google_service_account" "vercel" { + account_id = "vercel" + display_name = "Vercel" +} + +data "google_iam_policy" "auth_postgrest" { + binding { + role = "roles/run.invoker" + members = [ + join("", ["serviceAccount:vercel@", var.project_id, ".iam.gserviceaccount.com"]), + ] + } } diff --git a/terraform/modules/postgrest/variables.tf b/terraform/modules/postgrest/variables.tf index ba203f0..2fe8d6d 100644 --- a/terraform/modules/postgrest/variables.tf +++ b/terraform/modules/postgrest/variables.tf @@ -6,6 +6,8 @@ variable "no_auth_policy_data" {} variable "postgrest_max_rows" {} +variable "project_id" {} + variable "region" {} variable "sql_vpc_connector_id" {} From 0e4629a596647e8ecf8dfe6d654e8f5d0006c45b Mon Sep 17 00:00:00 2001 From: Bogdan Crisan Date: Wed, 5 Jun 2024 22:35:25 +0200 Subject: [PATCH 10/12] fix migrations --- sql_extensions/migrations/special-queries.sql | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/sql_extensions/migrations/special-queries.sql b/sql_extensions/migrations/special-queries.sql index 2c91e3c..65094f1 100644 --- a/sql_extensions/migrations/special-queries.sql +++ b/sql_extensions/migrations/special-queries.sql @@ -52,9 +52,32 @@ ORDER BY CREATE OR REPLACE FUNCTION UPDATE_LATEST_STATE() RETURNS TRIGGER AS $$ BEGIN - INSERT INTO inbox_latest_state + INSERT INTO inbox_latest_state ( + sequence_number, + creation_number, + account_address, + transaction_version, + transaction_block_height, + type, + data, + inserted_at, + event_index, + indexed_type, + event_name, + market_id + ) SELECT - NEW.*, + NEW.sequence_number, + NEW.creation_number, + NEW.account_address, + NEW.transaction_version, + NEW.transaction_block_height, + NEW.type, + NEW.data, + NEW.inserted_at, + NEW.event_index, + NEW.indexed_type, + NEW.event_name, (NEW.data -> 'market_metadata' ->> 'market_id')::NUMERIC ON CONFLICT (market_id) DO UPDATE SET transaction_version = EXCLUDED.transaction_version, From 0c2252785e1a266bf08137f61f459dabb4902d7b Mon Sep 17 00:00:00 2001 From: Bogdan Crisan Date: Thu, 6 Jun 2024 17:53:31 +0200 Subject: [PATCH 11/12] [ECO-1779] fix daily_volume issues (#26) * fix daily_volume issues * fix pre-commit lints --- cfg/cspell-dictionary.txt | 2 + sql_extensions/migrations/special-queries.sql | 52 ++++++++++++++----- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/cfg/cspell-dictionary.txt b/cfg/cspell-dictionary.txt index b54d6ca..87e4396 100644 --- a/cfg/cspell-dictionary.txt +++ b/cfg/cspell-dictionary.txt @@ -45,6 +45,7 @@ pgrep plpgsql psql readwrite +recordset rumqttc rustls serde @@ -61,5 +62,6 @@ testnet tfstate tfvars trimsuffix +vercel vpcaccess websockets diff --git a/sql_extensions/migrations/special-queries.sql b/sql_extensions/migrations/special-queries.sql index 65094f1..b7f946e 100644 --- a/sql_extensions/migrations/special-queries.sql +++ b/sql_extensions/migrations/special-queries.sql @@ -114,30 +114,58 @@ CREATE INDEX inbox_latest_state_by_nonce ON inbox_events ( -- Create a table containing the daily and all time volume for each market and -- auto update using triggers. +CREATE OR REPLACE FUNCTION GET_DAILY_VOLUME(JSONB) +RETURNS NUMERIC AS $$ +DECLARE + res NUMERIC; +BEGIN + WITH raw AS ( + SELECT * FROM jsonb_to_recordset($1) AS x(time numeric, volume_quote numeric) + ), + less_raw AS ( + SELECT time / 1000000 AS time, volume_quote AS volume_quote FROM raw + ) + SELECT COALESCE(SUM(volume_quote), 0) INTO res FROM less_raw WHERE time > extract(epoch from (now() - interval '1 day')); + RETURN res; +END; +$$ IMMUTABLE LANGUAGE plpgsql; + CREATE TABLE inbox_volume ( market_id NUMERIC PRIMARY KEY, all_time_volume NUMERIC NOT NULL, - daily_volume NUMERIC NOT NULL + volume_events JSONB NOT NULL ); +CREATE FUNCTION DAILY_VOLUME(INBOX_VOLUME) +RETURNS NUMERIC AS $$ + SELECT GET_DAILY_VOLUME($1.volume_events); +$$ IMMUTABLE LANGUAGE sql; + INSERT INTO inbox_volume SELECT (data #>> '{market_metadata,market_id}')::NUMERIC AS market_id, 0::NUMERIC AS all_time_volume, - SUM((data ->> 'volume_quote')::NUMERIC) AS daily_volume + JSON_AGG( + JSON_BUILD_OBJECT( + 'time', + (data -> 'periodic_state_metadata' ->> 'start_time')::NUMERIC, + 'volume_quote', + (data ->> 'volume_quote')::NUMERIC + ) + ) AS volume_events FROM inbox_events WHERE event_name = 'emojicoin_dot_fun::PeriodicState' AND - TO_TIMESTAMP((data -> 'periodic_state_metadata' ->> 'start_time')::NUMERIC / 1000) - > CURRENT_TIMESTAMP - INTERVAL '1 day' + (data -> 'periodic_state_metadata' ->> 'start_time')::NUMERIC / 1000000 + > EXTRACT(EPOCH FROM (NOW() - INTERVAL '1 day')) AND data -> 'periodic_state_metadata' ->> 'period' = '60000000' GROUP BY data #>> '{market_metadata,market_id}'; -UPDATE inbox_volume SET all_time_volume = tmp.volume +UPDATE inbox_volume SET all_time_volume = COALESCE(tmp.volume, 0) FROM ( SELECT (data #>> '{market_metadata,market_id}')::NUMERIC AS market_id, @@ -157,14 +185,14 @@ CREATE OR REPLACE FUNCTION UPDATE_VOLUME() RETURNS TRIGGER AS $$ BEGIN IF NOT EXISTS (SELECT * FROM inbox_volume WHERE market_id = (NEW.data->'market_metadata'->>'market_id')::numeric) THEN - INSERT INTO inbox_volume VALUES ((NEW.data->'market_metadata'->>'market_id')::numeric, 0::numeric, 0::numeric); + INSERT INTO inbox_volume VALUES ((NEW.data->'market_metadata'->>'market_id')::numeric, 0::numeric, '[]'::jsonb); END IF; UPDATE inbox_volume SET - daily_volume = ( - SELECT SUM((e.data->>'volume_quote')::numeric) FROM inbox_events e + volume_events = ( + SELECT json_agg(json_build_object('time', (e.data->'periodic_state_metadata'->>'start_time')::numeric, 'volume_quote', (e.data->>'volume_quote')::numeric)) FROM inbox_events e WHERE e.event_name = 'emojicoin_dot_fun::PeriodicState' - AND to_timestamp((e.data->'periodic_state_metadata'->>'start_time')::numeric / 1000) > CURRENT_TIMESTAMP - interval '1 day' + AND (e.data->'periodic_state_metadata'->>'start_time')::numeric / 1000000 > extract(epoch from (now() - interval '1 day')) AND e.data->'periodic_state_metadata'->>'period' = '60000000' AND (e.data->'market_metadata'->>'market_id')::numeric = (NEW.data->'market_metadata'->>'market_id')::numeric ), @@ -188,7 +216,7 @@ CREATE INDEX inbox_latest_state_by_all_time_volume ON inbox_volume ( all_time_volume DESC ); CREATE INDEX inbox_latest_state_by_daily_volume ON inbox_volume ( - daily_volume DESC + DAILY_VOLUME(inbox_volume) DESC ); -- }}} @@ -211,8 +239,8 @@ SELECT state.data -> 'clamm_virtual_reserves' AS clamm_virtual_reserves, state.data -> 'cpamm_real_reserves' AS cpamm_real_reserves, -- Volume data - volume.all_time_volume, - volume.daily_volume + COALESCE(volume.all_time_volume, 0) AS all_time_volume, + DAILY_VOLUME(volume) AS daily_volume -- noqa: RF02 FROM ( SELECT data FROM inbox_events WHERE event_name = 'emojicoin_dot_fun::MarketRegistration' ) AS registration From 04d3ec29019a1d11b630746579ddc2ddae7d53d7 Mon Sep 17 00:00:00 2001 From: Bogdan Crisan Date: Thu, 6 Jun 2024 18:09:54 +0200 Subject: [PATCH 12/12] rename migrations --- .../migrations/{mqtt-events.sql => 00000_mqtt_events.sql} | 0 .../migrations/{special-queries.sql => 00001_special_queries.sql} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename sql_extensions/migrations/{mqtt-events.sql => 00000_mqtt_events.sql} (100%) rename sql_extensions/migrations/{special-queries.sql => 00001_special_queries.sql} (100%) diff --git a/sql_extensions/migrations/mqtt-events.sql b/sql_extensions/migrations/00000_mqtt_events.sql similarity index 100% rename from sql_extensions/migrations/mqtt-events.sql rename to sql_extensions/migrations/00000_mqtt_events.sql diff --git a/sql_extensions/migrations/special-queries.sql b/sql_extensions/migrations/00001_special_queries.sql similarity index 100% rename from sql_extensions/migrations/special-queries.sql rename to sql_extensions/migrations/00001_special_queries.sql