Skip to content
This repository has been archived by the owner on Nov 21, 2024. It is now read-only.

Commit

Permalink
Undo format to see diff better
Browse files Browse the repository at this point in the history
  • Loading branch information
matt committed Jun 10, 2024
1 parent 8f1f870 commit ba7ed99
Showing 1 changed file with 15 additions and 29 deletions.
44 changes: 15 additions & 29 deletions sql_extensions/migrations/00001_special_queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -184,18 +184,18 @@ WHERE tmp.market_id = inbox_volume.market_id;
CREATE OR REPLACE FUNCTION UPDATE_VOLUME()
RETURNS TRIGGER AS $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM inbox_volume WHERE market_id = (NEW.data->'market_metadata'->>'market_id')::numeric) THEN
INSERT INTO inbox_volume (market_id, all_time_volume, volume_events)
VALUES ((NEW.data->'market_metadata'->>'market_id')::numeric, 0::numeric, '[]'::jsonb);
END IF;
INSERT INTO inbox_volume (market_id, all_time_volume, volume_events)
VALUES (
(NEW.data->'market_metadata'->>'market_id')::numeric,
0::numeric,
'[]'::jsonb
)
ON CONFLICT (market_id) DO NOTHING;
UPDATE inbox_volume
SET
volume_events = (
SELECT COALESCE(
jsonb_agg(jsonb_build_object(
'time', (e.data->'periodic_state_metadata'->>'start_time')::numeric,
'volume_quote', (e.data->>'volume_quote')::numeric
)),
jsonb_agg(jsonb_build_object('time', (e.data->'periodic_state_metadata'->>'start_time')::numeric, 'volume_quote', (e.data->>'volume_quote')::numeric)),
'[]'::jsonb
)
FROM inbox_events e
Expand Down Expand Up @@ -233,41 +233,27 @@ CREATE INDEX inbox_latest_state_by_daily_volume ON inbox_volume (
CREATE VIEW market_data AS
SELECT
-- General data
(
registration.data -> 'market_metadata' ->> 'market_id'
)::NUMERIC AS market_id,
(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,
(registration.data -> 'market_metadata' ->> 'market_address') AS market_address,
-- Latest state data
state.transaction_version,
(
state.data -> 'instantaneous_stats' ->> 'market_cap'
)::NUMERIC AS market_cap,
(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 -> '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,
-- Volume data
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'
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
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
Expand Down

0 comments on commit ba7ed99

Please sign in to comment.