caelum0x 2b68bde5d5 Predikt: full Polymarket-style prediction market — web + mobile + on-chain + bots
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>
2026-07-06 23:29:50 +03:00

162 lines
4.1 KiB
TypeScript

import { run } from 'common/supabase/utils'
import { type User } from 'common/user'
import { APIError, api } from '../api/api'
import { DAY_MS, WEEK_MS } from 'common/util/time'
import { HIDE_FROM_LEADERBOARD_USER_IDS } from 'common/envs/constants'
import type { DisplayUser } from 'common/api/user-types'
import { convertEntitlement } from 'common/shop/types'
import { db } from './db'
export type { DisplayUser }
export async function getUserById(id: string) {
return api('user/by-id/:id/lite', { id })
}
export async function getUserByUsername(username: string) {
return api('user/:username/lite', { username })
}
export async function getFullUserByUsername(username: string) {
return api('user/:username', { username })
}
export async function getFullUserById(id: string) {
return api('user/by-id/:id', { id })
}
export async function getUserSafe(userId: string) {
try {
return await getFullUserById(userId)
} catch (e) {
if (e instanceof APIError && e.code === 404) {
return null
}
throw e
}
}
export async function getPrivateUserSafe() {
try {
return await api('me/private')
} catch (e) {
return null
}
}
export async function searchUsers(prompt: string, limit: number) {
return api('search-users', { term: prompt, limit: limit })
}
export async function getDisplayUsers(userIds: string[]) {
if (userIds.length === 0) return []
// Fetch users and entitlements in parallel
const [usersResult, entitlementsResult] = await Promise.all([
run(
(
db
.from('users')
.select(
`id, name, username, is_bot, data->avatarUrl, data->isBannedFromPosting`
) as any
).in('id', userIds)
),
run(
db
.from('user_entitlements')
.select('*')
.in('user_id', userIds)
.or('expires_time.is.null,expires_time.gt.now()')
),
])
const { data: users } = usersResult
const { data: entitlements } = entitlementsResult
// Group entitlements by user_id
const entitlementsByUser = new Map<string, typeof entitlements>()
for (const e of entitlements ?? []) {
const userId = e.user_id
if (!entitlementsByUser.has(userId)) {
entitlementsByUser.set(userId, [])
}
entitlementsByUser.get(userId)!.push(e)
}
// Merge entitlements into users
return (users ?? []).map((user: any) => ({
...user,
isBot: user.is_bot ?? undefined,
entitlements: (entitlementsByUser.get(user.id) ?? []).map(
convertEntitlement
),
})) as DisplayUser[]
}
// leaderboards
export async function getProfitRank(userId: string) {
const { data } = await run(
db.rpc('profit_rank', {
uid: userId,
excluded_ids: HIDE_FROM_LEADERBOARD_USER_IDS,
})
)
return data
}
export async function getCreatorRank(userId: string) {
const { data } = await run(db.rpc('creator_rank', { uid: userId }))
return data
}
export const getTotalPublicContractsCreated = async (userId: string) => {
const { count } = await run(
db
.from('contracts')
.select('*', { head: true, count: 'exact' })
.eq('visibility', 'public')
.eq('creator_id', userId)
)
return count
}
export const getContractsCreatedProgress = async (
userId: string,
minTraders = 0
) => {
const currentDate = new Date()
const startDate = new Date()
startDate.setMonth(startDate.getMonth() - 2)
const startIsoString = startDate.toISOString()
const endIsoString = currentDate.toISOString()
const { count } = await run(
db
.from('contracts')
.select('*', { head: true, count: 'exact' })
.eq('visibility', 'public')
.eq('creator_id', userId)
.gte('created_time', startIsoString)
.lt('created_time', endIsoString)
.not('mechanism', 'eq', 'none')
.gte('data->uniqueBettorCount', minTraders)
)
return count
}
export async function getRecentlyActiveUsers(limit: number) {
const { data } = await run(
db
.from('users')
.select('data')
.gt('data->>lastBetTime', Date.now() - DAY_MS)
.lt('data->>createdTime', Date.now() - WEEK_MS)
.limit(limit)
)
console.log('getRecentlyActiveUsers', data)
return data.map((d) => d.data as User)
}