Skip to content

Commit ae615d2

Browse files
authored
Add DB trigger to populate SIGMA DAO table (#678)
1 parent dd75d0b commit ae615d2

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

examples/dao/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ We use functional notation to describe use cases we will implement.
6868
- `withdraw_from_proposal()` - Withdraw the assets from the proposalLsig back to the owner. Receiver must be the owner of the proposalLsig. Transaction composition:
6969
- _tx0_: Withdraw _gov_tokens_ from _proposalLsig_ back to _proposer_ (owner) (ASA transfer).
7070

71+
## Setup Sigma Dao
72+
73+
Setup the sigma_daos table. Indexer should be running before executing below script.
74+
75+
psql -U algorand -d pgdb -a -f setup_sigma_dao.sql
76+
7177
## Spec document
7278

7379
Algo Builder DAO [specification](https://paper.dropbox.com/doc/Algo-Builder-DAO--BRlh~FwufNzIzk4wNUuAjLTuAg-ncLdytuFa7EJrRerIASSl).

examples/dao/setup_sigma_dao.sql

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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

Comments
 (0)