Skip to content

Commit e128ce2

Browse files
filter send token transfers by send account
1 parent aabd3d6 commit e128ce2

File tree

3 files changed

+154
-6
lines changed

3 files changed

+154
-6
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
set check_function_bodies = off;
2+
3+
CREATE OR REPLACE FUNCTION private.filter_send_token_transfers_with_no_send_account_created()
4+
RETURNS trigger
5+
LANGUAGE plpgsql
6+
SECURITY DEFINER
7+
AS $function$
8+
begin
9+
if exists ( select 1 from send_account_created where account = new.f )
10+
or exists ( select 1 from send_account_created where account = new.t )
11+
then
12+
return new;
13+
else
14+
return null;
15+
end if;
16+
end;
17+
$function$
18+
;
19+
20+
CREATE OR REPLACE FUNCTION private.filter_send_token_v0_transfers_with_no_send_account_created()
21+
RETURNS trigger
22+
LANGUAGE plpgsql
23+
SECURITY DEFINER
24+
AS $function$
25+
begin
26+
if exists ( select 1 from send_account_created where account = new.f )
27+
or exists ( select 1 from send_account_created where account = new.t )
28+
then
29+
return new;
30+
else
31+
return null;
32+
end if;
33+
end;
34+
$function$
35+
;
36+
37+
38+
drop policy "Users can see their own token transfers" on "public"."send_token_transfers";
39+
40+
drop policy "Users can see their own token transfers" on "public"."send_token_v0_transfers";
41+
42+
CREATE INDEX idx_send_token_transfers_f_t_block_time ON public.send_token_transfers USING btree (f, t, block_time);
43+
44+
create policy "users can see their own transfers"
45+
on "public"."send_token_transfers"
46+
as permissive
47+
for select
48+
to public
49+
using ((EXISTS ( SELECT 1
50+
FROM send_accounts
51+
WHERE ((send_accounts.user_id = ( SELECT auth.uid() AS uid)) AND ((send_accounts.address = (lower(concat('0x', encode(send_token_transfers.f, 'hex'::text))))::citext) OR (send_accounts.address = (lower(concat('0x', encode(send_token_transfers.t, 'hex'::text))))::citext))))));
52+
53+
54+
create policy "users can see their own transfers"
55+
on "public"."send_token_v0_transfers"
56+
as permissive
57+
for select
58+
to public
59+
using ((EXISTS ( SELECT 1
60+
FROM send_accounts
61+
WHERE ((send_accounts.user_id = ( SELECT auth.uid() AS uid)) AND ((send_accounts.address = (lower(concat('0x', encode(send_token_v0_transfers.f, 'hex'::text))))::citext) OR (send_accounts.address = (lower(concat('0x', encode(send_token_v0_transfers.t, 'hex'::text))))::citext))))));
62+
63+
64+
65+
CREATE TRIGGER filter_send_token_transfers_with_no_send_account_created BEFORE INSERT ON public.send_token_transfers FOR EACH ROW EXECUTE FUNCTION private.filter_send_token_transfers_with_no_send_account_created();
66+
67+
CREATE TRIGGER filter_send_token_v0_transfers_with_no_send_account_created BEFORE INSERT ON public.send_token_v0_transfers FOR EACH ROW EXECUTE FUNCTION private.filter_send_token_v0_transfers_with_no_send_account_created();
68+
69+
70+
-- Delete from send_token_transfers where neither account exists
71+
DELETE FROM send_token_transfers
72+
WHERE NOT EXISTS (SELECT 1 FROM send_account_created WHERE account = f)
73+
AND NOT EXISTS (SELECT 1 FROM send_account_created WHERE account = t);
74+
75+
-- Delete from send_token_v0_transfers where neither account exists
76+
DELETE FROM send_token_v0_transfers
77+
WHERE NOT EXISTS (SELECT 1 FROM send_account_created WHERE account = f)
78+
AND NOT EXISTS (SELECT 1 FROM send_account_created WHERE account = t);

supabase/schemas/send_token_transfers.sql

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,29 @@
1+
-- Filter function to ensure transfers only include existing send accounts
2+
-- create trigger function for filtering send_token_transfers with no send_account_created
3+
-- Deletes send_token_transfers with no send_account_created.
4+
-- This is due to performance issues in our shovel indexer and using filter_ref to limit indexing to only
5+
-- send_token_transfers with send_account_created.
6+
-- For now, we index all SEND token transfers, and use this function filter any send_token_transfers with no send_account_created.
7+
-- See https://github.com/orgs/indexsupply/discussions/268
8+
CREATE OR REPLACE FUNCTION private.filter_send_token_transfers_with_no_send_account_created()
9+
RETURNS trigger
10+
LANGUAGE plpgsql
11+
SECURITY DEFINER
12+
AS $function$
13+
begin
14+
if exists ( select 1 from send_account_created where account = new.f )
15+
or exists ( select 1 from send_account_created where account = new.t )
16+
then
17+
return new;
18+
else
19+
return null;
20+
end if;
21+
end;
22+
$function$
23+
;
24+
25+
ALTER FUNCTION "private"."filter_send_token_transfers_with_no_send_account_created"() OWNER TO "postgres";
26+
127
-- Sequences
228
CREATE SEQUENCE IF NOT EXISTS "public"."send_token_transfers_id_seq"
329
AS integer
@@ -43,12 +69,20 @@ CREATE INDEX "send_token_transfers_composite" ON "public"."send_token_transfers"
4369
CREATE INDEX "send_token_transfers_f" ON "public"."send_token_transfers" USING "btree" ("f");
4470
CREATE INDEX "send_token_transfers_t" ON "public"."send_token_transfers" USING "btree" ("t");
4571
CREATE UNIQUE INDEX "u_send_token_transfers" ON "public"."send_token_transfers" USING "btree" ("ig_name", "src_name", "block_num", "tx_idx", "log_idx", "abi_idx");
72+
CREATE INDEX "idx_send_token_transfers_f_t_block_time" ON "public"."send_token_transfers" USING "btree" ("f", "t", "block_time");
4673

4774
-- RLS
4875
ALTER TABLE "public"."send_token_transfers" ENABLE ROW LEVEL SECURITY;
49-
CREATE POLICY "Users can see their own token transfers" ON "public"."send_token_transfers" FOR SELECT USING (("auth"."uid"() IN ( SELECT "chain_addresses"."user_id"
50-
FROM "public"."chain_addresses"
51-
WHERE (("chain_addresses"."address" OPERATOR("public".=) ("lower"("concat"('0x', "encode"("send_token_transfers"."f", 'hex'::"text"))))::"public"."citext") OR ("chain_addresses"."address" OPERATOR("public".=) ("lower"("concat"('0x', "encode"("send_token_transfers"."t", 'hex'::"text"))))::"public"."citext")))));
76+
create policy "users can see their own transfers"
77+
on "public"."send_token_transfers"
78+
as permissive
79+
for select
80+
to public
81+
using ((EXISTS ( SELECT 1
82+
FROM send_accounts
83+
WHERE ((send_accounts.user_id = ( SELECT auth.uid() AS uid)) AND ((send_accounts.address = (lower(concat('0x', encode(send_token_transfers.f, 'hex'::text))))::citext) OR (send_accounts.address = (lower(concat('0x', encode(send_token_transfers.t, 'hex'::text))))::citext))))));
84+
85+
5286

5387
-- Grants
5488
GRANT ALL ON TABLE "public"."send_token_transfers" TO "anon";
@@ -63,3 +97,4 @@ GRANT ALL ON SEQUENCE "public"."send_token_transfers_id_seq" TO "service_role";
6397
CREATE OR REPLACE TRIGGER "insert_verification_send_ceiling_trigger" AFTER INSERT ON "public"."send_token_transfers" FOR EACH ROW EXECUTE FUNCTION "public"."insert_verification_send_ceiling"();
6498
CREATE OR REPLACE TRIGGER "insert_verification_sends" AFTER INSERT ON "public"."send_token_transfers" FOR EACH ROW EXECUTE FUNCTION "public"."insert_verification_sends"();
6599
CREATE OR REPLACE TRIGGER "insert_send_streak_verification" AFTER INSERT ON "public"."send_token_transfers" FOR EACH ROW EXECUTE FUNCTION "public"."insert_send_streak_verification"();
100+
CREATE OR REPLACE TRIGGER "filter_send_token_transfers_with_no_send_account_created" BEFORE INSERT ON "public"."send_token_transfers" FOR EACH ROW EXECUTE FUNCTION "private"."filter_send_token_transfers_with_no_send_account_created"();

supabase/schemas/send_token_v0_transfers.sql

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,29 @@
1+
-- Filter function to ensure transfers only include existing send accounts
2+
-- create trigger function for filtering send_token_v0_transfers with no send_account_created
3+
-- Deletes send_token_v0_transfers with no send_account_created.
4+
-- This is due to performance issues in our shovel indexer and using filter_ref to limit indexing to only
5+
-- send_token_v0_transfers with send_account_created.
6+
-- For now, we index all SENDV0 token transfers, and use this function filter any send_token_v0_transfers with no send_account_created.
7+
-- See https://github.com/orgs/indexsupply/discussions/268
8+
CREATE OR REPLACE FUNCTION private.filter_send_token_v0_transfers_with_no_send_account_created()
9+
RETURNS trigger
10+
LANGUAGE plpgsql
11+
SECURITY DEFINER
12+
AS $function$
13+
begin
14+
if exists ( select 1 from send_account_created where account = new.f )
15+
or exists ( select 1 from send_account_created where account = new.t )
16+
then
17+
return new;
18+
else
19+
return null;
20+
end if;
21+
end;
22+
$function$
23+
;
24+
25+
ALTER FUNCTION "private"."filter_send_token_v0_transfers_with_no_send_account_created"() OWNER TO "postgres";
26+
127
-- Sequences
228
CREATE SEQUENCE IF NOT EXISTS "public"."send_token_v0_transfers_id_seq"
329
AS integer
@@ -46,9 +72,17 @@ CREATE UNIQUE INDEX "u_send_token_v0_transfers" ON "public"."send_token_v0_trans
4672

4773
-- RLS
4874
ALTER TABLE "public"."send_token_v0_transfers" ENABLE ROW LEVEL SECURITY;
49-
CREATE POLICY "Users can see their own token transfers" ON "public"."send_token_v0_transfers" FOR SELECT USING (("auth"."uid"() IN ( SELECT "chain_addresses"."user_id"
50-
FROM "public"."chain_addresses"
51-
WHERE (("chain_addresses"."address" OPERATOR("public".=) ("lower"("concat"('0x', "encode"("send_token_v0_transfers"."f", 'hex'::"text"))))::"public"."citext") OR ("chain_addresses"."address" OPERATOR("public".=) ("lower"("concat"('0x', "encode"("send_token_v0_transfers"."t", 'hex'::"text"))))::"public"."citext")))));
75+
76+
create policy "users can see their own transfers"
77+
on "public"."send_token_v0_transfers"
78+
as permissive
79+
for select
80+
to public
81+
using ((EXISTS ( SELECT 1
82+
FROM send_accounts
83+
WHERE ((send_accounts.user_id = ( SELECT auth.uid() AS uid)) AND ((send_accounts.address = (lower(concat('0x', encode(send_token_v0_transfers.f, 'hex'::text))))::citext) OR (send_accounts.address = (lower(concat('0x', encode(send_token_v0_transfers.t, 'hex'::text))))::citext))))));
84+
85+
5286

5387
-- Grants
5488
GRANT ALL ON TABLE "public"."send_token_v0_transfers" TO "anon";
@@ -62,3 +96,4 @@ GRANT ALL ON SEQUENCE "public"."send_token_v0_transfers_id_seq" TO "service_role
6296
-- Triggers
6397
CREATE OR REPLACE TRIGGER "insert_verification_sends" AFTER INSERT ON "public"."send_token_v0_transfers" FOR EACH ROW EXECUTE FUNCTION "public"."insert_verification_sends"();
6498
CREATE OR REPLACE TRIGGER "insert_verification_send_ceiling" AFTER INSERT ON "public"."send_token_v0_transfers" FOR EACH ROW EXECUTE FUNCTION "public"."insert_verification_send_ceiling"();
99+
CREATE OR REPLACE TRIGGER "filter_send_token_v0_transfers_with_no_send_account_created" BEFORE INSERT ON "public"."send_token_v0_transfers" FOR EACH ROW EXECUTE FUNCTION "private"."filter_send_token_v0_transfers_with_no_send_account_created"();

0 commit comments

Comments
 (0)