diff --git a/.snaplet/snaplet-client.d.ts b/.snaplet/snaplet-client.d.ts index 062c0ca39..d826352b7 100644 --- a/.snaplet/snaplet-client.d.ts +++ b/.snaplet/snaplet-client.d.ts @@ -139,35 +139,6 @@ type Override = { request_id?: string; }; } - job?: { - name?: string; - fields?: { - jobid?: string; - schedule?: string; - command?: string; - nodename?: string; - nodeport?: string; - database?: string; - username?: string; - active?: string; - jobname?: string; - }; - } - job_run_details?: { - name?: string; - fields?: { - jobid?: string; - runid?: string; - job_pid?: string; - database?: string; - username?: string; - command?: string; - status?: string; - return_message?: string; - start_time?: string; - end_time?: string; - }; - } storage_migrations?: { name?: string; fields?: { @@ -642,17 +613,6 @@ export interface Fingerprint { createdAt?: FingerprintDateField; requestId?: FingerprintNumberField; } - jobs?: { - jobid?: FingerprintNumberField; - nodeport?: FingerprintNumberField; - } - jobRunDetails?: { - jobid?: FingerprintNumberField; - runid?: FingerprintNumberField; - jobPid?: FingerprintNumberField; - startTime?: FingerprintDateField; - endTime?: FingerprintDateField; - } storageMigrations?: { id?: FingerprintNumberField; executedAt?: FingerprintDateField; diff --git a/.snaplet/snaplet.d.ts b/.snaplet/snaplet.d.ts index 5cd84d74d..836867b76 100644 --- a/.snaplet/snaplet.d.ts +++ b/.snaplet/snaplet.d.ts @@ -175,39 +175,6 @@ interface Table_shovel_integrations { name: string | null; conf: Json | null; } -interface Table_cron_job { - jobid: number; - schedule: string; - command: string; - nodename: string; - nodeport: number; - database: string; - username: string; - active: boolean; - /** - * We couldn't determine the type of this column. The type might be coming from an unknown extension - * or be specific to your database. Please if it's a common used type report this issue so we can fix it! - * Otherwise, please manually type this column by casting it to the correct type. - * @example - * Here is a cast example for copycat use: - * ``` - * copycat.scramble(row.unknownColumn as string) - * ``` - */ - jobname: unknown | null; -} -interface Table_cron_job_run_details { - jobid: number | null; - runid: number; - job_pid: number | null; - database: string | null; - username: string | null; - command: string | null; - status: string | null; - return_message: string | null; - start_time: string | null; - end_time: string | null; -} interface Table_pgsodium_key { id: string; status: Enum_pgsodium_key_status | null; @@ -623,10 +590,6 @@ interface Schema_auth { sso_providers: Table_auth_sso_providers; users: Table_auth_users; } -interface Schema_cron { - job: Table_cron_job; - job_run_details: Table_cron_job_run_details; -} interface Schema_dbdev { } @@ -710,7 +673,6 @@ interface Database { _analytics: Schema__analytics; _realtime: Schema__realtime; auth: Schema_auth; - cron: Schema_cron; dbdev: Schema_dbdev; extensions: Schema_extensions; graphql: Schema_graphql; @@ -729,7 +691,7 @@ interface Database { vault: Schema_vault; } interface Extension { - extensions: "http" | "pg_cron" | "pg_net" | "pg_stat_statements" | "pg_trgm" | "pgcrypto" | "pgjwt" | "uuid-ossp"; + extensions: "http" | "pg_net" | "pg_stat_statements" | "pg_trgm" | "pgcrypto" | "pgjwt" | "uuid-ossp"; graphql: "pg_graphql"; pgsodium: "pgsodium"; pgtle: "pg_tle"; diff --git a/supabase/migrations/20240728180825_enable_pg_cron_extension.sql b/supabase/migrations/20240728180825_enable_pg_cron_extension.sql deleted file mode 100644 index 840c3a88f..000000000 --- a/supabase/migrations/20240728180825_enable_pg_cron_extension.sql +++ /dev/null @@ -1 +0,0 @@ -create extension if not exists "pg_cron" with schema "extensions"; diff --git a/supabase/migrations/202407281808278_send_account_transfers_filter_non_send_accounts.sql b/supabase/migrations/202407281808278_send_account_transfers_filter_non_send_accounts.sql new file mode 100644 index 000000000..62b084898 --- /dev/null +++ b/supabase/migrations/202407281808278_send_account_transfers_filter_non_send_accounts.sql @@ -0,0 +1,29 @@ +set check_function_bodies = off; + +-- create trigger function for filtering send_account_transfers with no send_account_created +create or replace function private.filter_send_account_transfers_with_no_send_account_created() + returns trigger + language plpgsql + security definer + as $$ +begin +-- Deletes send_account_transfers with no send_account_created. +-- This is due to performance issues in our shovel indexer and using filter_ref to limit indexing to only +-- send_account_transfers with send_account_created. +-- For now, we index all USDC and SEND token transfers, and use this function cron job to delete the rest. +-- See https://github.com/orgs/indexsupply/discussions/268 + if exists ( select 1 from send_account_created where account = new.f ) + or exists ( select 1 from send_account_created where account = new.t ) + then + return new; + else + return null; + end if; +end; +$$; + +-- create trigger on send_account_transfers table +create trigger filter_send_account_transfers_with_no_send_account_created +before insert on public.send_account_transfers +for each row +execute function private.filter_send_account_transfers_with_no_send_account_created(); diff --git a/supabase/migrations/20240728180827_cron_delete_send_account_transfers_with_no_send_account_created.sql b/supabase/migrations/20240728180827_cron_delete_send_account_transfers_with_no_send_account_created.sql deleted file mode 100644 index 4ec16d454..000000000 --- a/supabase/migrations/20240728180827_cron_delete_send_account_transfers_with_no_send_account_created.sql +++ /dev/null @@ -1,55 +0,0 @@ -set check_function_bodies = off; - -CREATE OR REPLACE FUNCTION private.delete_send_account_transfers_with_no_send_account_created() - RETURNS void - LANGUAGE plpgsql - SECURITY DEFINER -AS $function$ --- Deletes send_account_transfers with no send_account_created. --- This is due to performance issues in our shovel indexer and using filter_ref to limit indexing to only --- send_account_transfers with send_account_created. --- For now, we index all USDC and SEND token transfers, and use this function cron job to delete the rest. --- See https://github.com/orgs/indexsupply/discussions/268 -declare - batch_size INT := 10000; -- Number of records to delete in each batch - affected INT; - total_deleted INT := 0; - start_time TIMESTAMP; - end_time TIMESTAMP; -begin - start_time := clock_timestamp(); - raise notice 'Starting delete_send_account_transfers_with_no_send_account_created %s', start_time; - loop - -- Delete a batch of records - with batch as ( select sat.id - from send_account_transfers sat - where not exists ( select 1 from send_account_created where account = sat.f ) - and not exists ( select 1 from send_account_created where account = sat.t ) - order by sat.id - limit batch_size for update skip locked ) - delete - from send_account_transfers - where id in ( select id from batch ); - - get diagnostics affected = row_count; - total_deleted := total_deleted + affected; - - raise notice 'Deleted % records. Total deleted so far: %', affected, total_deleted; - - exit when affected = 0; -- Exit loop when no more records to delete - - commit; - -- Commit each batch - - -- Optional: Add a small delay to reduce database load - perform pg_sleep(0.1); -- Sleep for 100 milliseconds - end loop; - - end_time := clock_timestamp(); - raise notice 'Total records deleted: %. Time taken: % seconds', total_deleted, extract(epoch from (end_time - start_time)); -end -$function$ -; - --- schedule the function to run every 5 minutes -select cron.schedule('delete-send-account-transfers-with-no-send-account-created', '*/5 * * * *', 'SELECT private.delete_send_account_transfers_with_no_send_account_created()'); diff --git a/supabase/tests/send_account_transfers_test.sql b/supabase/tests/send_account_transfers_test.sql index 964a39364..03f90ba15 100644 --- a/supabase/tests/send_account_transfers_test.sql +++ b/supabase/tests/send_account_transfers_test.sql @@ -1,5 +1,5 @@ BEGIN; -SELECT plan(3); +SELECT plan(5); -- Create the necessary extensions CREATE EXTENSION "basejump-supabase_test_helpers"; -- noqa: RF05 @@ -8,6 +8,10 @@ CREATE EXTENSION "basejump-supabase_test_helpers"; -- noqa: RF05 SELECT tests.create_supabase_user('test_user_from'); SELECT tests.create_supabase_user('test_user_to'); +insert into send_account_created (chain_id, log_addr, block_time, user_op_hash, tx_hash, account, ig_name, src_name, block_num, tx_idx, log_idx) +values (8453, '\xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', floor(extract(EPOCH FROM timestamptz '2013-07-01 12:00:00')), '\x1234', '\x1234', '\x1234567890ABCDEF1234567890ABCDEF12345678', 'send_account_created', 'send_account_created', 1, 0, 0), + (8453, '\xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', floor(extract(EPOCH FROM timestamptz '2013-07-01 12:00:00')), '\x1234', '\x1234', '\xB0B7D5E8A4B6D534B3F608E9D27871F85A4E98DA', 'send_account_transfers', 'send_account_transfers', 1, 0, 0); + INSERT INTO send_accounts (user_id, address, chain_id, init_code) VALUES ( tests.get_supabase_uid('test_user_from'), @@ -57,7 +61,7 @@ VALUES ( -- Test if the trigger function populated the additional columns correctly SELECT results_eq( $$ - select + select (data->>'log_addr')::citext as log_addr, (data->>'f')::citext as f, (data->>'t')::citext as t, @@ -102,8 +106,8 @@ WHERE id = ( -- Test if the delete was successful SELECT is_empty( $$ - SELECT id - FROM send_account_transfers + SELECT id + FROM send_account_transfers WHERE f = '\x1234567890ABCDEF1234567890ABCDEF12345678'::bytea AND t = '\xB0B7D5E8A4B6D534B3F608E9D27871F85A4E98DA'::bytea $$, 'Test if the trigger function removes the activity row' @@ -120,5 +124,55 @@ SELECT is_empty( 'Test if the trigger function removes the activity row' ); +-- Test filter_non_send_account_transfers_before_insert removes transactions when the sender is not a send account +delete from send_account_transfers; +INSERT INTO send_account_transfers ( + f, + t, + v, + block_time, + ig_name, + src_name, + tx_hash, + block_num, + tx_idx, + log_idx, + abi_idx, + chain_id, + log_addr +) +VALUES ( + '\xa71CE00000000000000000000000000000000000', + '\xb055000000000000000000000000000000000000', + 100, + floor(extract(EPOCH FROM timestamptz '2013-07-01 12:00:00')), + 'send_account_transfers', + 'send_account_transfers', + '\x1234', + 1, + 0, + 0, + 0, + 8453, + '\xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266' +); + +SELECT is_empty( + $$ + select 1 + from send_account_transfers + where f = '\xa71CE00000000000000000000000000000000000' + $$, + 'Test if the trigger function filters send_account_transfers with no send_account_created' +); + +select is_empty( + $$ + select 1 + from send_account_transfers + $$, + 'Test if the trigger function filters send_account_transfers with no send_account_created' +); + SELECT * FROM finish(); ROLLBACK;