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

Commit

Permalink
Fix user balance migration
Browse files Browse the repository at this point in the history
  • Loading branch information
CRBl69 committed Jul 29, 2024
1 parent 53a9271 commit 9767415
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions sql_extensions/migrations/00006_user_rank.sql
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
CREATE TABLE inbox_user_balance (
user_address TEXT NOT NULL PRIMARY KEY,
balance_as_fraction_of_circulating_supply NUMERIC
user_address TEXT NOT NULL,
market_id NUMERIC,
balance_as_fraction_of_circulating_supply NUMERIC,
PRIMARY KEY (user_address, market_id)
);

INSERT INTO inbox_user_balance
SELECT DISTINCT ON (data ->> 'user', data -> 'market_metadata' ->> 'market_id')
data ->> 'user',
(data -> 'market_metadata' ->> 'market_id')::NUMERIC,
(data ->> 'balance_as_fraction_of_circulating_supply_q64')::NUMERIC / POW(2::NUMERIC, 64::NUMERIC)
FROM
inbox_events
WHERE
event_name = 'emojicoin_dot_fun::Chat'
ORDER BY
data ->> 'user',
data -> 'market_metadata' ->> 'market_id',
transaction_version DESC,
event_index DESC;

CREATE OR REPLACE FUNCTION UPDATE_USER_BALANCE()
RETURNS TRIGGER AS $$
BEGIN
INSERT INTO inbox_user_balance
SELECT
NEW.data ->> 'user',
(NEW.data -> 'market_metadata' ->> 'market_id')::NUMERIC,
(NEW.data ->> 'balance_as_fraction_of_circulating_supply_q64')::NUMERIC / POW(2::NUMERIC, 64::NUMERIC)
ON CONFLICT (user_address) DO UPDATE SET
ON CONFLICT (user_address, market_id) DO UPDATE SET
balance_as_fraction_of_circulating_supply = (NEW.data ->> 'balance_as_fraction_of_circulating_supply_q64')::NUMERIC / POW(2::NUMERIC, 64::NUMERIC);
RETURN NEW;
END;
Expand All @@ -22,7 +40,7 @@ FOR EACH ROW
WHEN (new.event_name = 'emojicoin_dot_fun::Chat')
EXECUTE PROCEDURE UPDATE_USER_BALANCE();

CREATE VIEW inbox_swaps AS
CREATE OR REPLACE VIEW inbox_swaps AS
SELECT
swaps.sequence_number,
swaps.creation_number,
Expand All @@ -35,7 +53,8 @@ SELECT
swaps.event_index,
swaps.indexed_type,
swaps.event_name,
(swaps."data"->>'market_id')::NUMERIC AS market_id,
inbox_user_balance.balance_as_fraction_of_circulating_supply
FROM inbox_events AS swaps
LEFT JOIN inbox_user_balance ON swaps.data ->> 'swapper' = inbox_user_balance.user_address
LEFT JOIN inbox_user_balance ON swaps.data ->> 'swapper' = inbox_user_balance.user_address AND (swaps."data" ->> 'market_id')::NUMERIC = inbox_user_balance.market_id
WHERE swaps.event_name = 'emojicoin_dot_fun::Swap';

0 comments on commit 9767415

Please sign in to comment.