|
| 1 | +-- #Note: Indexer should be running before executing the queries present in this file |
| 2 | +-- Indexer schema: |
| 3 | +-- https://github.com/algorand/indexer/blob/develop/idb/postgres/internal/schema/setup_postgres.sql |
| 4 | + |
| 5 | +-- create a sigma_daos table if does not exit already. |
| 6 | +CREATE TABLE IF NOT EXISTS sigma_daos ( |
| 7 | + app_id bigint PRIMARY KEY, -- application id |
| 8 | + app_params jsonb, -- application params with approval etc. |
| 9 | + asset_id bigint -- token id |
| 10 | +); |
| 11 | + |
| 12 | +-- create a procedure to handle the trigger (sigmadao_trigger_fn) action |
| 13 | +CREATE OR REPLACE FUNCTION sigmadao_trigger_fn() |
| 14 | +RETURNS TRIGGER |
| 15 | +AS $$ |
| 16 | +DECLARE |
| 17 | + asset_id bigint; |
| 18 | + log_sigma text := 'U2lnbWFEQU8gY3JlYXRlZA=='; |
| 19 | + i record; |
| 20 | +BEGIN |
| 21 | + /* txn colmun of txn relation stores both asset and app id. typeenum column |
| 22 | + identifies the asset and app */ |
| 23 | + FOR i IN SELECT txn FROM txn WHERE asset = NEW.asset AND typeenum=6 LOOP |
| 24 | + /* sigma dao contrac check. Here 'dt' and 'lg' is a json object present |
| 25 | + in each record of txn column */ |
| 26 | + IF i.txn::jsonb -> 'dt'->'lg' ?& ARRAY[log_sigma] THEN |
| 27 | + -- TODO: Fetch asset id from global state not from latest entry in asset table |
| 28 | + -- store token id from db to above decalred variable asset_id |
| 29 | + SELECT index INTO asset_id FROM public.asset ORDER BY index DESC LIMIT 1; |
| 30 | + -- insert values in sigma_daos table |
| 31 | + INSERT INTO public.sigma_daos ( |
| 32 | + app_id, app_params, asset_id) |
| 33 | + VALUES (NEW.asset, NEW.txn, asset_id); |
| 34 | + EXIT; |
| 35 | + END IF; |
| 36 | + END LOOP; |
| 37 | + RETURN NEW; |
| 38 | +END; |
| 39 | +$$ LANGUAGE plpgsql; |
| 40 | + |
| 41 | +-- create a trigger (sigmadao_trigger) |
| 42 | +-- Event: INSERT on publoc.txn relation |
| 43 | +CREATE TRIGGER sigmadao_trigger |
| 44 | +AFTER INSERT ON public.txn FOR EACH ROW |
| 45 | +EXECUTE FUNCTION sigmadao_trigger_fn(); |
| 46 | + |
| 47 | +-- grant privileges to relation sigma_daos |
| 48 | +GRANT ALL PRIVILEGES ON TABLE sigma_daos TO algorand; |
0 commit comments