From 4991e50bc971f0223058c826b7b66d7c36781ced Mon Sep 17 00:00:00 2001 From: youngkidwarrior Date: Sat, 9 Nov 2024 18:17:24 -0800 Subject: [PATCH] Calculate send slash function --- supabase/database-generated.types.ts | 13 +++ .../20241110000436_calculate_send_slash.sql | 106 ++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 supabase/migrations/20241110000436_calculate_send_slash.sql diff --git a/supabase/database-generated.types.ts b/supabase/database-generated.types.ts index d573df701..1cd951302 100644 --- a/supabase/database-generated.types.ts +++ b/supabase/database-generated.types.ts @@ -295,6 +295,7 @@ export type Database = { number: number qualification_end: string qualification_start: string + send_slash_divisor: number | null snapshot_block_num: number | null updated_at: string } @@ -313,6 +314,7 @@ export type Database = { number: number qualification_end: string qualification_start: string + send_slash_divisor?: number | null snapshot_block_num?: number | null updated_at?: string } @@ -331,6 +333,7 @@ export type Database = { number?: number qualification_end?: string qualification_start?: string + send_slash_divisor?: number | null snapshot_block_num?: number | null updated_at?: string } @@ -1228,6 +1231,16 @@ export type Database = { Args: Record Returns: string } + get_send_slash_calculations: { + Args: { + distribution_number: number + } + Returns: { + user_id: string + capped_sum: number + slash_percentage: number + }[] + } insert_challenge: { Args: Record Returns: { diff --git a/supabase/migrations/20241110000436_calculate_send_slash.sql b/supabase/migrations/20241110000436_calculate_send_slash.sql new file mode 100644 index 000000000..3c424897b --- /dev/null +++ b/supabase/migrations/20241110000436_calculate_send_slash.sql @@ -0,0 +1,106 @@ +ALTER TABLE "public"."distributions" + ADD COLUMN "send_slash_minimum_sends" smallint DEFAULT '1'::smallint; + +ALTER TABLE "public"."distributions" + ADD COLUMN "send_slash_past_rewards_divisor" smallint DEFAULT '1'::smallint; + +UPDATE + "public"."distributions" +SET + send_slash_minimum_sends = 50 +WHERE + number = 9; + +UPDATE + "public"."distributions" +SET + send_slash_past_rewards_divisor = 3 +WHERE + number = 9; + +CREATE OR REPLACE FUNCTION get_send_slash_calculations(distribution_number integer) + RETURNS TABLE( + user_id uuid, + capped_sum numeric, + slash_percentage numeric) + LANGUAGE plpgsql + AS $$ +BEGIN + RETURN QUERY WITH distribution_info AS( + SELECT + id, + qualification_start, + qualification_end, + send_slash_minimum_sends, + send_slash_past_rewards_divisor, + hodler_min_balance, +( + SELECT + SUM(ds.amount) + FROM + distribution_shares ds + WHERE + ds.distribution_id =( + SELECT + id + FROM + distributions + WHERE + number = distribution_number - 1)) AS prev_shares + FROM + distributions + WHERE + number = distribution_number +), +previous_shares AS( + SELECT + ds.user_id, + ds.amount AS user_prev_shares + FROM + distribution_shares ds + WHERE + ds.distribution_id =( + SELECT + id + FROM + distributions + WHERE + number = distribution_number - 1) +), +user_transfers AS( + SELECT + sa.user_id, + SUM(LEAST(stt.v,(ROUND(COALESCE(ps.user_prev_shares, di.hodler_min_balance) /(di.send_slash_minimum_sends * di.send_slash_past_rewards_divisor)))::numeric)) AS capped_sum +FROM + send_token_transfers stt + JOIN send_accounts sa ON sa.address = concat('0x', encode(stt.f, 'hex'))::citext + CROSS JOIN distribution_info di + LEFT JOIN previous_shares ps ON ps.user_id = sa.user_id + WHERE + stt.block_time >= extract(epoch FROM di.qualification_start) + AND stt.block_time < extract(epoch FROM di.qualification_end) + GROUP BY + sa.user_id +) +SELECT + ut.user_id, + ut.capped_sum, + ROUND( + CASE WHEN ut.capped_sum > 0 THEN + LEAST(1.0, ut.capped_sum / NULLIF(COALESCE(( + SELECT + user_prev_shares + FROM previous_shares ps + WHERE + ps.user_id = ut.user_id), di.hodler_min_balance) / di.send_slash_past_rewards_divisor, 0))::numeric +ELSE + 0 + END, 8) AS slash_percentage +FROM + user_transfers ut + CROSS JOIN distribution_info di +WHERE + ut.capped_sum > 0; +END; +$$; +