Skip to content

Commit

Permalink
Remove: Dependecy from proposal trigger (#766)
Browse files Browse the repository at this point in the history
  • Loading branch information
rmeena840 authored Aug 18, 2022
1 parent a684191 commit fef80b6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 53 deletions.
60 changes: 8 additions & 52 deletions examples/dao/setup_sigma_dao.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,22 @@
-- Indexer schema:
-- https://github.com/algorand/indexer/blob/develop/idb/postgres/internal/schema/setup_postgres.sql

-- create a sigma_dao_proposal table if does not exit already.
CREATE TABLE IF NOT EXISTS sigma_dao_proposals (
id SERIAL PRIMARY KEY, -- auto increment id
addr BYTEA, -- account address
app_id BIGINT, -- SigmaDAO app id
localstate JSONB, -- localstate of addr
voting_start BIGINT, -- voting start
voting_end BIGINT -- voting end
);

-- create an index to do efficient text search using `%<text>%`, it requires
CREATE INDEX sigma_daos_dao_name_idx ON app USING gin (CAST(dao_name AS TEXT) gin_trgm_ops);

-- create a procedure to handle the trigger (sigmadao_proposals_trigger_fn) action
CREATE OR REPLACE FUNCTION sigmadao_proposals_trigger_fn()
RETURNS TRIGGER
AS $$
DECLARE
voting_start_key CHAR(255) := 'dm90aW5nX3N0YXJ0'; -- Byte code of 'voting_end'
voting_end_key CHAR(255) := 'dm90aW5nX2VuZA=='; -- Byte code of 'voting_end'
voting_start_value BIGINT;
voting_end_value BIGINT;
BEGIN
-- Iterate json object to tkv object and fetch the voting start key
IF (SELECT NEW.localstate::jsonb -> 'tkv' -> voting_start_key) IS NOT NULL THEN
-- Iterate json object and get voting start value
SELECT NEW.localstate::jsonb -> 'tkv' -> voting_start_key -> 'ui' INTO voting_start_value;
-- Iterate json object and get voting end value
SELECT NEW.localstate::jsonb -> 'tkv' -> voting_end_key -> 'ui' INTO voting_end_value;
-- insert values in sigma_dao_proposals table
INSERT INTO public.sigma_dao_proposals (
addr, app_id, localstate, voting_start, voting_end)
VALUES (NEW.addr, NEW.app, NEW.localstate::jsonb, voting_start_value, voting_end_value);
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

-- create a trigger (sigmadao_proposals_trigger)
-- Event: UPDATE on public.account_app relation
CREATE TRIGGER sigmadao_proposals_trigger
AFTER UPDATE ON public.account_app FOR EACH ROW
EXECUTE FUNCTION sigmadao_proposals_trigger_fn();

-- grant privileges
GRANT ALL PRIVILEGES ON TABLE sigma_dao_proposals TO algorand;

-- Below are objects needed for sigma dao app. Extend it if more sigma dao objects needed
-- Here, sigma_dao_user should be same as https://github.com/scale-it/algo-builder/blob/cbc2123622a10fbf96f9b99b254abc86a79ac1fb/examples/dao/Makefile#L1
GRANT SELECT ON TABLE sigma_dao_proposals, app, asset, account_asset, account_app, account TO sigma_dao_user;
GRANT SELECT ON TABLE app, asset, account_asset, account_app, account TO sigma_dao_user;

-- Function to search dao name in sigma_daos relation
-- Function to search dao in app relation by app id and filter type
CREATE OR REPLACE FUNCTION search_sigma_daos(daoToBeSearched TEXT)
RETURNS SETOF app AS $$
SELECT * FROM app WHERE dao_name ilike ('%' || daoToBeSearched || '%');
$$ LANGUAGE SQL STABLE;

-- Function to search proposal in sigma_dao_proposals relation by app id and filter type
-- Function to search proposal in account_app relation by app id and filter type
CREATE OR REPLACE FUNCTION sigma_daos_proposal_filter(appId BIGINT, filterType INT)
RETURNS SETOF sigma_dao_proposals AS $$
RETURNS SETOF account_app AS $$
DECLARE
voting_end CHAR(255) := 'dm90aW5nX2VuZA=='; -- Byte code of 'voting_end'
timestamp BIGINT := extract(EPOCH FROM NOW()); -- epoch in seconds
Expand All @@ -71,13 +27,13 @@ DECLARE
filter_past INT := 4; -- filter type -> past
BEGIN
IF (filterType = filter_ongoing) THEN
RETURN QUERY SELECT * FROM sigma_dao_proposals WHERE app_id = appId AND timestamp BETWEEN sigma_dao_proposals.voting_start AND sigma_dao_proposals.voting_end ORDER BY voting_start;
RETURN QUERY SELECT * FROM account_app WHERE app = appId AND timestamp BETWEEN account_app.voting_start AND account_app.voting_end ORDER BY voting_start;
ELSIF (filterType = filter_active) THEN
RETURN QUERY SELECT * FROM sigma_dao_proposals WHERE app_id = appId AND timestamp < sigma_dao_proposals.voting_end ORDER BY voting_start;
RETURN QUERY SELECT * FROM account_app WHERE app = appId AND timestamp < account_app.voting_end ORDER BY voting_start;
ELSIF (filterType = filter_past) THEN
RETURN QUERY SELECT * FROM sigma_dao_proposals WHERE app_id = appId AND sigma_dao_proposals.voting_end < timestamp ORDER BY voting_end DESC;
RETURN QUERY SELECT * FROM account_app WHERE app = appId AND account_app.voting_end < timestamp ORDER BY voting_end DESC;
ELSE
RETURN QUERY SELECT * FROM sigma_dao_proposals WHERE app_id = appId;
RETURN QUERY SELECT * FROM account_app WHERE app = appId;
END IF;
END;
$$ LANGUAGE plpgsql STABLE;
2 changes: 1 addition & 1 deletion infrastructure/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export REACH_PATH = ~/.algorand-reach
export INDEXER_REMOTE_REPO = https://github.com/algorand/indexer.git
export INDEXER_PATH = ~/.algorand-indexer-download
# path to indexer .tar file (https://github.com/algorand/indexer/releases) for local setup
export INDEXER_LATEST_VERSION = 2.12.4
export INDEXER_LATEST_VERSION = 2.13.0
export INDEXER_TAR_NAME = algorand-indexer_$(OS)_amd64_$(INDEXER_LATEST_VERSION).tar.bz2
export INDEXER_TAR_URL = https://github.com/algorand/indexer/releases/download/$(INDEXER_LATEST_VERSION)/$(INDEXER_TAR_NAME)
# default postgres config vars
Expand Down

0 comments on commit fef80b6

Please sign in to comment.