Skip to content

Commit

Permalink
/send checks create/claim shovel integration migrations + triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
nicky-LV committed Aug 9, 2024
1 parent 0289b59 commit cfacb7b
Show file tree
Hide file tree
Showing 3 changed files with 329 additions and 0 deletions.
156 changes: 156 additions & 0 deletions supabase/migrations/20240809182925_add_send_check_tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
create table "public"."send_check_claimed" (
"chain_id" numeric,
"block_time" numeric,
"tx_hash" bytea,
"log_addr" bytea,
"token" bytea,
"amount" numeric,
"from" bytea,
"redeemer" bytea,
"ig_name" text,
"src_name" text,
"block_num" numeric,
"tx_idx" integer,
"log_idx" integer,
"abi_idx" smallint
);


create table "public"."send_check_created" (
"chain_id" numeric,
"block_time" numeric,
"tx_hash" bytea,
"log_addr" bytea,
"token" bytea,
"amount" numeric,
"from" bytea,
"ig_name" text,
"src_name" text,
"block_num" numeric,
"tx_idx" integer,
"log_idx" integer,
"abi_idx" smallint
);


CREATE UNIQUE INDEX u_send_check_claimed ON public.send_check_claimed USING btree (ig_name, src_name, block_num, tx_idx, log_idx, abi_idx);

CREATE UNIQUE INDEX u_send_check_created ON public.send_check_created USING btree (ig_name, src_name, block_num, tx_idx, log_idx, abi_idx);

grant delete on table "public"."send_check_claimed" to "anon";

grant insert on table "public"."send_check_claimed" to "anon";

grant references on table "public"."send_check_claimed" to "anon";

grant select on table "public"."send_check_claimed" to "anon";

grant trigger on table "public"."send_check_claimed" to "anon";

grant truncate on table "public"."send_check_claimed" to "anon";

grant update on table "public"."send_check_claimed" to "anon";

grant delete on table "public"."send_check_claimed" to "authenticated";

grant insert on table "public"."send_check_claimed" to "authenticated";

grant references on table "public"."send_check_claimed" to "authenticated";

grant select on table "public"."send_check_claimed" to "authenticated";

grant trigger on table "public"."send_check_claimed" to "authenticated";

grant truncate on table "public"."send_check_claimed" to "authenticated";

grant update on table "public"."send_check_claimed" to "authenticated";

grant delete on table "public"."send_check_claimed" to "service_role";

grant insert on table "public"."send_check_claimed" to "service_role";

grant references on table "public"."send_check_claimed" to "service_role";

grant select on table "public"."send_check_claimed" to "service_role";

grant trigger on table "public"."send_check_claimed" to "service_role";

grant truncate on table "public"."send_check_claimed" to "service_role";

grant update on table "public"."send_check_claimed" to "service_role";

grant delete on table "public"."send_check_created" to "anon";

grant insert on table "public"."send_check_created" to "anon";

grant references on table "public"."send_check_created" to "anon";

grant select on table "public"."send_check_created" to "anon";

grant trigger on table "public"."send_check_created" to "anon";

grant truncate on table "public"."send_check_created" to "anon";

grant update on table "public"."send_check_created" to "anon";

grant delete on table "public"."send_check_created" to "authenticated";

grant insert on table "public"."send_check_created" to "authenticated";

grant references on table "public"."send_check_created" to "authenticated";

grant select on table "public"."send_check_created" to "authenticated";

grant trigger on table "public"."send_check_created" to "authenticated";

grant truncate on table "public"."send_check_created" to "authenticated";

grant update on table "public"."send_check_created" to "authenticated";

grant delete on table "public"."send_check_created" to "service_role";

grant insert on table "public"."send_check_created" to "service_role";

grant references on table "public"."send_check_created" to "service_role";

grant select on table "public"."send_check_created" to "service_role";

grant trigger on table "public"."send_check_created" to "service_role";

grant truncate on table "public"."send_check_created" to "service_role";

grant update on table "public"."send_check_created" to "service_role";


ALTER TABLE "public"."send_check_created" ENABLE ROW LEVEL SECURITY;

-- Policy for viewing created checks: Users can only select their own created checks
CREATE POLICY "Users can see their own created checks" ON "public"."send_check_created" FOR
SELECT USING (
lower(concat('0x', encode(send_check_created.from, 'hex')))::citext in (
SELECT send_accounts.address FROM send_accounts
WHERE (
send_accounts.user_id = (
SELECT
auth.uid () AS uid
)
)
)
);

ALTER TABLE "public"."send_check_claimed" ENABLE ROW LEVEL SECURITY;

-- Policy for viewing claimed checks: Users can only select their own claimed checks
CREATE POLICY "Users can see their own claimed checks" ON "public"."send_check_claimed" FOR
SELECT USING (
lower(concat('0x', encode(send_check_claimed.redeemer, 'hex')))::citext in (
SELECT send_accounts.address FROM send_accounts
WHERE (
send_accounts.user_id = (
SELECT
auth.uid () AS uid
)
)
)
);

Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
-- add `event_id` column to send check created shovel integration table
alter table "public"."send_check_created" add column "event_id" text GENERATED ALWAYS AS (((((((((ig_name || '/'::text) || src_name) || '/'::text) || (block_num)::text) || '/'::text) || (tx_idx)::text) || '/'::text) || (log_idx)::text)) STORED NOT NULL;
alter table "public"."send_check_created" add column id integer NOT NULL;

CREATE SEQUENCE public.send_check_created_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;

ALTER SEQUENCE public.send_check_created_id_seq OWNED BY public.send_check_created.id;

ALTER TABLE ONLY public.send_check_created ALTER COLUMN id SET DEFAULT nextval('public.send_check_created'::regclass);

ALTER TABLE ONLY public.send_check_created ADD CONSTRAINT send_check_created_pkey PRIMARY KEY (id);

-- send check created trigger function
create or replace function send_check_created_trigger_insert_activity() returns trigger
language plpgsql
security definer as
$$
_f_user_id uuid;
_data jsonb;
begin
-- populate f_user_id with sender's user_id
select user_id into _f_user_id from send_accounts where address = concat('0x', encode(NEW.from, 'hex'))::citext;

_data := json_build_object(
'log_addr', NEW.log_addr,
'token', NEW.token,
'amount', NEW.amount::text,
'from', NEW.from,
'tx_hash': NEW.tx_hash,
'block_num': NEW.block_num::text,
'tx_idx': NEW.tx_idx::text,
'log_idx': NEW.log_idx::text,
)

insert into activity (event_name, event_id, from_user_id, data, created_at)
values (
'send_check_created',
NEW.event_id,
_f_user_id,
_data,
to_timestamp(NEW.block_time) at time zone 'UTC'
)
on conflict (event_name, event_id) do update set
from_user_id = _f_user_id,
data = _data,
created_at = to_timestamp(NEW.block_time) at time zone 'UTC';
return NEW;
end;
$$;

-- send check created trigger
create trigger send_check_created_trigger_insert_activity
after insert
on send_check_created
for each row
execute function send_check_created_trigger_insert_activity();

-- send check created delete trigger function
create or replace function send_check_created_trigger_delete_activity() returns trigger
language plpgsql
security definer as
$$
begin
delete
from activity
where event_id = OLD.event_id
and event_name = 'send_check_created';
return OLD;
end;
$$;

create trigger send_check_created_trigger_delete_activity
after delete
on send_check_created
for each row
execute function send_check_created_trigger_delete_activity();
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
-- add `event_id` column to send check clauimed shovel integration table
alter table "public"."send_check_claimed" add column "event_id" text GENERATED ALWAYS AS (((((((((ig_name || '/'::text) || src_name) || '/'::text) || (block_num)::text) || '/'::text) || (tx_idx)::text) || '/'::text) || (log_idx)::text)) STORED NOT NULL;
alter table "public"."send_check_claimed" add column id integer NOT NULL;

CREATE SEQUENCE public.send_check_claimed_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;

ALTER SEQUENCE public.send_check_claimed_id_seq OWNED BY public.send_check_claimed.id;

ALTER TABLE ONLY public.send_check_claimed ALTER COLUMN id SET DEFAULT nextval('public.send_check_claimed'::regclass);

ALTER TABLE ONLY public.send_check_claimed ADD CONSTRAINT send_check_claimed_pkey PRIMARY KEY (id);

-- send check created trigger function
create or replace function send_check_claimed_trigger_insert_activity() returns trigger
language plpgsql
security definer as
$$
_f_user_id uuid;
_t_user_id uuid;
_data jsonb;
begin
-- populate f_user_id with sender's user_id
select user_id into _f_user_id from send_accounts where address = concat('0x', encode(NEW.from, 'hex'))::citext;

-- populate t_user_id with receiver's user_id
select user_id into _t_user_id from send_accounts where address = concat('0x', encode(NEW.reedeemer, 'hex'))::citext;

_data := json_build_object(
'log_addr', NEW.log_addr,
'token', NEW.token,
'amount', NEW.amount::text,
'from', NEW.from,
'reedeemer', NEW.redeemer,
'tx_hash': NEW.tx_hash,
'block_num': NEW.block_num::text,
'tx_idx': NEW.tx_idx::text,
'log_idx': NEW.log_idx::text,
)

insert into activity (event_name, event_id, from_user_id, to_user_id, data, created_at)
values (
'send_check_claimed',
NEW.event_id,
_f_user_id,
_t_user_id,
_data,
to_timestamp(NEW.block_time) at time zone 'UTC'
)
on conflict (event_name, event_id) do update set
from_user_id = _f_user_id,
to_user_id = _t_user_id,
data = _data,
created_at = to_timestamp(NEW.block_time) at time zone 'UTC';
return NEW;
end;
$$;

-- send check claimed trigger
create trigger send_check_claimed_trigger_insert_activity
after insert
on send_check_claimed
for each row
execute function send_check_claimed_trigger_insert_activity();

-- send check claimed delete trigger function
create or replace function send_check_claimed_trigger_delete_activity() returns trigger
language plpgsql
security definer as
$$
begin
delete
from activity
where event_id = OLD.event_id
and event_name = 'send_check_claimed';
return OLD;
end;
$$;

-- send check claimed delete trigger
-- deletes activity record when a send check claimed record is deleted
create trigger send_check_claimed_trigger_delete_activity
after delete
on send_check_claimed
for each row
execute function send_check_claimed_trigger_delete_activity();

0 comments on commit cfacb7b

Please sign in to comment.