big test rc
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
-- Registration email. Run in Supabase Dashboard → SQL Editor.
|
||||
-- Application stores normalized (trimmed, lower-case) addresses.
|
||||
|
||||
alter table public.users
|
||||
add column if not exists email text null;
|
||||
|
||||
create unique index if not exists users_email_unique
|
||||
on public.users (email)
|
||||
where email is not null;
|
||||
@@ -0,0 +1,8 @@
|
||||
-- `users.rc` as type `real` is IEEE float32: above ~1e7, consecutive values are spaced
|
||||
-- by more than 1 (and by hundreds at ~4e9), so debits like -20 do not change the stored value.
|
||||
-- Run this in Supabase SQL before relying on `purchase_rc` or large RC balances.
|
||||
--
|
||||
-- After this, re-apply public.purchase_rc from schemas/rpc_purchase_rc.sql (no ::real casts).
|
||||
|
||||
alter table public.users
|
||||
alter column rc type bigint using round(coalesce(rc, 0))::bigint;
|
||||
@@ -0,0 +1,45 @@
|
||||
I have introduced a new table called transactions, @schemas/table_transactions.md
|
||||
|
||||
I need all RC related transactions logged in here. This is to keep track of all purchases of RC, spendings of RC and withdrawals of RC in track.
|
||||
|
||||
I have added my first record with remarks of 'supply',to acc id:1. a large sum.
|
||||
|
||||
All purchases of RC (in purchases rc api endpoint) will be deducted from acc id:1 and given to the requested user.
|
||||
|
||||
During reward distribution, Record the bet fee going to the acc id:1 and prize going to the winner seperately. and set match_id on reward distribution .
|
||||
|
||||
Heres how a RC purchase record would look like
|
||||
|
||||
from: 1
|
||||
to: requested_user_id
|
||||
amount: requested_amount
|
||||
remarks: purchase
|
||||
|
||||
|
||||
Heres how a reward distribution would look like.
|
||||
|
||||
from a single players POV (there are 2 players per match).
|
||||
PS: bet fee is usually 10%, stored in settings table
|
||||
entry record 1/2
|
||||
|
||||
from: user_id
|
||||
to: 1
|
||||
amount: entry fee - bet fee
|
||||
remarks: entry_hold
|
||||
match_id: match_id
|
||||
|
||||
entry record 2/2
|
||||
|
||||
from: user_id
|
||||
to: 1
|
||||
amount: bet fee
|
||||
remarks: entry_fee
|
||||
match_id: match_id
|
||||
|
||||
And for giving the reward.
|
||||
|
||||
from: 1
|
||||
to: winner_user_id
|
||||
amount: (already calculated properly)
|
||||
remarks: reward
|
||||
match_id: match_id
|
||||
@@ -0,0 +1,66 @@
|
||||
-- One-shot: run in Supabase Dashboard → SQL Editor **after** schemas/alter_users_rc_bigint.sql
|
||||
-- (requires `users.rc` bigint so large balances support small debits/credits).
|
||||
-- Atomically debits RC from supply user id 1, credits the buyer, inserts `transactions`.
|
||||
-- SECURITY DEFINER avoids RLS/trigger edge cases on direct `users` updates from the API.
|
||||
|
||||
create or replace function public.purchase_rc(p_buyer_id bigint, p_amount bigint)
|
||||
returns jsonb
|
||||
language plpgsql
|
||||
security definer
|
||||
set search_path = public
|
||||
as $$
|
||||
declare
|
||||
v_supply bigint := 1;
|
||||
begin
|
||||
if p_buyer_id is null or p_buyer_id < 1 then
|
||||
raise exception 'invalid_buyer';
|
||||
end if;
|
||||
if p_amount is null or p_amount <= 0 then
|
||||
raise exception 'invalid_amount';
|
||||
end if;
|
||||
|
||||
-- Same account: net balance must stay the same; only log for audit.
|
||||
if p_buyer_id = v_supply then
|
||||
insert into public.transactions ("from", "to", amount, remarks)
|
||||
values (v_supply, v_supply, p_amount, 'purchase');
|
||||
return (
|
||||
select to_jsonb(t)
|
||||
from (
|
||||
select id, username, cc, rc, created_at, last_logged_at
|
||||
from public.users
|
||||
where id = p_buyer_id
|
||||
) t
|
||||
);
|
||||
end if;
|
||||
|
||||
update public.users
|
||||
set rc = coalesce(rc, 0) - p_amount
|
||||
where id = v_supply
|
||||
and coalesce(rc, 0) >= p_amount;
|
||||
if not found then
|
||||
raise exception 'insufficient_supply';
|
||||
end if;
|
||||
|
||||
update public.users
|
||||
set rc = coalesce(rc, 0) + p_amount
|
||||
where id = p_buyer_id;
|
||||
if not found then
|
||||
raise exception 'buyer_not_found';
|
||||
end if;
|
||||
|
||||
insert into public.transactions ("from", "to", amount, remarks)
|
||||
values (v_supply, p_buyer_id, p_amount, 'purchase');
|
||||
|
||||
return (
|
||||
select to_jsonb(t)
|
||||
from (
|
||||
select id, username, cc, rc, created_at, last_logged_at
|
||||
from public.users
|
||||
where id = p_buyer_id
|
||||
) t
|
||||
);
|
||||
end;
|
||||
$$;
|
||||
|
||||
revoke all on function public.purchase_rc(bigint, bigint) from public;
|
||||
grant execute on function public.purchase_rc(bigint, bigint) to service_role;
|
||||
@@ -0,0 +1,13 @@
|
||||
create table public.transactions (
|
||||
id bigint generated by default as identity not null,
|
||||
created_at timestamp with time zone not null default now(),
|
||||
"from" bigint null,
|
||||
"to" bigint null,
|
||||
amount bigint not null default '4'::bigint,
|
||||
remarks character varying null,
|
||||
match_id bigint null,
|
||||
constraint transactions_pkey primary key (id),
|
||||
constraint transactions_from_fkey foreign KEY ("from") references users (id),
|
||||
constraint transactions_match_id_fkey foreign KEY (match_id) references matches (id),
|
||||
constraint transactions_to_fkey foreign KEY ("to") references users (id)
|
||||
) TABLESPACE pg_default;
|
||||
@@ -2,6 +2,7 @@ create table public.users (
|
||||
id bigint generated by default as identity not null,
|
||||
created_at timestamp with time zone not null default now(),
|
||||
username text null,
|
||||
email text null,
|
||||
password text null,
|
||||
cc real null default '0'::real,
|
||||
rc real null default '0'::real,
|
||||
|
||||
Reference in New Issue
Block a user