Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ECO-2007] Add inbox_swaps endpoint with balance percentage field #49

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions sql_extensions/migrations/00006_user_rank.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
CREATE TABLE inbox_user_balance (
user_address TEXT NOT NULL PRIMARY KEY,
balance_as_fraction_of_circulating_supply NUMERIC
);

CREATE OR REPLACE FUNCTION UPDATE_USER_BALANCE()
RETURNS TRIGGER AS $$
BEGIN
INSERT INTO inbox_user_balance
SELECT
NEW.data ->> 'user',
(NEW.data ->> 'balance_as_fraction_of_circulating_supply_q64')::NUMERIC / POW(2::NUMERIC, 64::NUMERIC)
ON CONFLICT (user_address) 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;
$$ LANGUAGE plpgsql;

CREATE TRIGGER update_user_balance
AFTER INSERT ON inbox_events
FOR EACH ROW
WHEN (new.event_name = 'emojicoin_dot_fun::Chat')
EXECUTE PROCEDURE UPDATE_USER_BALANCE();

CREATE VIEW inbox_swaps AS
SELECT
swaps.sequence_number,
swaps.creation_number,
swaps.account_address,
swaps.transaction_version,
swaps.transaction_block_height,
swaps."type",
swaps."data",
swaps.inserted_at,
swaps.event_index,
swaps.indexed_type,
swaps.event_name,
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
WHERE swaps.event_name = 'emojicoin_dot_fun::Swap';
Loading