Working tree is now the canonical repo. Off-chain play-money + trustless on-chain (FPMM AMM + CLOB via UMA settlement), AI market factory, creator/social/distribution, mobile shell + push, one-command demo + deploy-kit. Secrets excluded (see ENV-AND-KEYS.md). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
246 lines
7.9 KiB
PL/PgSQL
246 lines
7.9 KiB
PL/PgSQL
-- This file is autogenerated from regen-schema.ts
|
|
create table if not exists
|
|
contracts (
|
|
close_time timestamp with time zone,
|
|
conversion_score numeric default 0 not null,
|
|
created_time timestamp with time zone,
|
|
creator_id text,
|
|
daily_score numeric default 0 not null,
|
|
data jsonb not null,
|
|
deleted boolean default false,
|
|
description_fts tsvector generated always as (
|
|
to_tsvector(
|
|
'english_extended'::regconfig,
|
|
add_creator_name_to_description (data)
|
|
)
|
|
) stored,
|
|
freshness_score numeric default 0 not null,
|
|
group_slugs text[],
|
|
home_page_score_adjustment numeric,
|
|
home_page_score_adjustment_expires_at timestamp with time zone,
|
|
id text primary key not null,
|
|
importance_score numeric default 0 not null,
|
|
is_spice_payout boolean default false,
|
|
last_bet_time timestamp with time zone,
|
|
last_comment_time timestamp with time zone,
|
|
last_updated_time timestamp with time zone,
|
|
mechanism text,
|
|
outcome_type text,
|
|
popularity_score numeric default 0 not null,
|
|
question text,
|
|
question_fts tsvector generated always as (
|
|
to_tsvector('english_extended'::regconfig, question)
|
|
) stored,
|
|
question_nostop_fts tsvector generated always as (
|
|
to_tsvector('english_nostop_with_prefix'::regconfig, question)
|
|
) stored,
|
|
resolution text,
|
|
resolution_probability numeric,
|
|
resolution_time timestamp with time zone,
|
|
slug text,
|
|
token text default 'MANA'::character varying not null constraint contracts_token_check check (
|
|
(
|
|
token = any (
|
|
array[
|
|
('MANA'::character varying)::text,
|
|
('CASH'::character varying)::text
|
|
]
|
|
)
|
|
)
|
|
),
|
|
unique_bettor_count bigint default 0 not null,
|
|
view_count bigint default 0 not null,
|
|
visibility text,
|
|
boosted boolean default false not null
|
|
);
|
|
|
|
-- Triggers
|
|
create trigger contract_populate before insert
|
|
or
|
|
update on public.contracts for each row
|
|
execute function contract_populate_cols ();
|
|
|
|
create trigger sync_sibling_contract_trigger
|
|
after
|
|
update on public.contracts for each row
|
|
execute function sync_sibling_contract ();
|
|
|
|
-- Functions
|
|
create
|
|
or replace function public.contract_populate_cols () returns trigger language plpgsql as $function$
|
|
begin
|
|
if new.data is not null then
|
|
new.slug := (new.data) ->> 'slug';
|
|
new.question := (new.data) ->> 'question';
|
|
new.creator_id := (new.data) ->> 'creatorId';
|
|
new.visibility := (new.data) ->> 'visibility';
|
|
new.mechanism := (new.data) ->> 'mechanism';
|
|
new.outcome_type := (new.data) ->> 'outcomeType';
|
|
new.unique_bettor_count := ((new.data) -> 'uniqueBettorCount')::bigint;
|
|
new.created_time := case
|
|
when new.data ? 'createdTime' then millis_to_ts(((new.data) ->> 'createdTime')::bigint)
|
|
else null
|
|
end;
|
|
new.close_time := case
|
|
when new.data ? 'closeTime' then millis_to_ts(((new.data) ->> 'closeTime')::bigint)
|
|
else null
|
|
end;
|
|
new.resolution_time := case
|
|
when new.data ? 'resolutionTime' then millis_to_ts(((new.data) ->> 'resolutionTime')::bigint)
|
|
else null
|
|
end;
|
|
new.resolution_probability := ((new.data) ->> 'resolutionProbability')::numeric;
|
|
new.resolution := (new.data) ->> 'resolution';
|
|
new.is_spice_payout := coalesce(((new.data) ->> 'isSpicePayout')::boolean, false);
|
|
new.deleted := coalesce(((new.data) ->> 'deleted')::boolean, false);
|
|
new.group_slugs := case
|
|
when new.data ? 'groupSlugs' then jsonb_array_to_text_array((new.data) -> 'groupSlugs')
|
|
else null
|
|
end;
|
|
new.last_updated_time := case
|
|
when new.data ? 'lastUpdatedTime' then millis_to_ts(((new.data) ->> 'lastUpdatedTime')::bigint)
|
|
else null
|
|
end;
|
|
new.last_bet_time := case
|
|
when new.data ? 'lastBetTime' then millis_to_ts(((new.data) ->> 'lastBetTime')::bigint)
|
|
else null
|
|
end;
|
|
new.last_comment_time := case
|
|
when new.data ? 'lastCommentTime' then millis_to_ts(((new.data) ->> 'lastCommentTime')::bigint)
|
|
else null
|
|
end;
|
|
end if;
|
|
return new;
|
|
end
|
|
$function$;
|
|
|
|
create
|
|
or replace function public.sync_sibling_contract () returns trigger language plpgsql as $function$
|
|
begin
|
|
if new.token = 'MANA' and (new.data->>'siblingContractId') is not null
|
|
and (
|
|
old.data->>'closeTime' != new.data->>'closeTime'
|
|
or old.data->>'deleted' != new.data->>'deleted'
|
|
or old.data->>'question' != new.data->>'question'
|
|
) then
|
|
update contracts
|
|
set data = data || jsonb_build_object(
|
|
'closeTime', new.data->'closeTime',
|
|
'deleted', new.data->'deleted',
|
|
'question', new.data->'question'
|
|
)
|
|
where id = (new.data->>'siblingContractId')::text;
|
|
end if;
|
|
return new;
|
|
end;
|
|
$function$;
|
|
|
|
-- Row Level Security
|
|
alter table contracts enable row level security;
|
|
|
|
-- Policies
|
|
drop policy if exists "public read" on contracts;
|
|
|
|
create policy "public read" on contracts for
|
|
select
|
|
using (true);
|
|
|
|
-- Indexes
|
|
drop index if exists contracts_close_time;
|
|
|
|
create index contracts_close_time on public.contracts using btree (close_time desc);
|
|
|
|
drop index if exists contracts_created_time;
|
|
|
|
create index contracts_created_time on public.contracts using btree (created_time desc);
|
|
|
|
drop index if exists contracts_creator_id;
|
|
|
|
create index contracts_creator_id on public.contracts using btree (creator_id, created_time);
|
|
|
|
drop index if exists contracts_feed_created;
|
|
|
|
create index contracts_feed_created on public.contracts using btree (token, created_time desc)
|
|
where
|
|
(
|
|
resolution_time is null
|
|
and deleted = false
|
|
and visibility = 'public'::text
|
|
);
|
|
|
|
drop index if exists contracts_elasticity;
|
|
|
|
create index contracts_elasticity on public.contracts using btree ((((data ->> 'elasticity'::text))::numeric) desc);
|
|
|
|
drop index if exists contracts_freshness_score;
|
|
|
|
create index contracts_freshness_score on public.contracts using btree (freshness_score desc);
|
|
|
|
drop index if exists contracts_group_slugs;
|
|
|
|
create index contracts_group_slugs on public.contracts using gin (group_slugs);
|
|
|
|
drop index if exists contracts_importance_score;
|
|
|
|
create index contracts_importance_score on public.contracts using btree (importance_score desc);
|
|
|
|
drop index if exists contracts_last_bet_time;
|
|
|
|
create index contracts_last_bet_time on public.contracts using btree (last_bet_time desc nulls last);
|
|
|
|
drop index if exists contracts_last_comment_time;
|
|
|
|
create index contracts_last_comment_time on public.contracts using btree (last_comment_time desc nulls last);
|
|
|
|
drop index if exists contracts_last_updated_time;
|
|
|
|
create index contracts_last_updated_time on public.contracts using btree (last_updated_time desc nulls last);
|
|
|
|
drop index if exists contracts_outcome_type_binary;
|
|
|
|
create index contracts_outcome_type_binary on public.contracts using btree (outcome_type)
|
|
where
|
|
(outcome_type = 'BINARY'::text);
|
|
|
|
drop index if exists contracts_outcome_type_not_binary;
|
|
|
|
create index contracts_outcome_type_not_binary on public.contracts using btree (outcome_type)
|
|
where
|
|
(outcome_type <> 'BINARY'::text);
|
|
|
|
drop index if exists contracts_pkey;
|
|
|
|
create unique index contracts_pkey on public.contracts using btree (id);
|
|
|
|
drop index if exists contracts_resolution_time;
|
|
|
|
create index contracts_resolution_time on public.contracts using btree (resolution_time desc);
|
|
|
|
drop index if exists contracts_slug;
|
|
|
|
create index contracts_slug on public.contracts using btree (slug);
|
|
|
|
drop index if exists contracts_visibility_public;
|
|
|
|
create index contracts_visibility_public on public.contracts using btree (id)
|
|
where
|
|
(visibility = 'public'::text);
|
|
|
|
drop index if exists contracts_volume_24_hours;
|
|
|
|
create index contracts_volume_24_hours on public.contracts using btree (
|
|
(((data ->> 'volume24Hours'::text))::numeric) desc
|
|
);
|
|
|
|
drop index if exists description_fts;
|
|
|
|
create index description_fts on public.contracts using gin (description_fts);
|
|
|
|
drop index if exists question_fts;
|
|
|
|
create index question_fts on public.contracts using gin (question_fts);
|
|
|
|
drop index if exists question_nostop_fts;
|
|
|
|
create index question_nostop_fts on public.contracts using gin (question_nostop_fts);
|