2021-12-24 21:06:01 +00:00
|
|
|
import { getFunctions, httpsCallable } from 'firebase/functions'
|
2022-01-21 23:21:46 +00:00
|
|
|
import { Fold } from '../../../common/fold'
|
2022-01-18 07:18:38 +00:00
|
|
|
import { User } from '../../../common/user'
|
2022-01-19 03:36:46 +00:00
|
|
|
import { randomString } from '../../../common/util/random'
|
2022-01-25 23:02:02 +00:00
|
|
|
import './init'
|
2021-12-24 21:06:01 +00:00
|
|
|
|
|
|
|
const functions = getFunctions()
|
|
|
|
|
2022-01-21 23:21:46 +00:00
|
|
|
export const cloudFunction = <RequestData, ResponseData>(name: string) =>
|
|
|
|
httpsCallable<RequestData, ResponseData>(functions, name)
|
2021-12-24 21:06:01 +00:00
|
|
|
|
2022-01-05 18:23:58 +00:00
|
|
|
export const createContract = cloudFunction('createContract')
|
|
|
|
|
2022-01-21 23:21:46 +00:00
|
|
|
export const createFold = cloudFunction<
|
2022-01-25 20:47:25 +00:00
|
|
|
{ name: string; about: string; tags: string[] },
|
2022-01-21 23:21:46 +00:00
|
|
|
{ status: 'error' | 'success'; message?: string; fold?: Fold }
|
|
|
|
>('createFold')
|
|
|
|
|
2022-01-05 18:23:58 +00:00
|
|
|
export const placeBet = cloudFunction('placeBet')
|
|
|
|
|
2022-03-29 19:56:56 +00:00
|
|
|
export const sellBet = cloudFunction('sellBet')
|
|
|
|
|
|
|
|
export const sellShares = cloudFunction<
|
|
|
|
{ contractId: string; shares: number; outcome: 'YES' | 'NO' },
|
|
|
|
{ status: 'error' | 'success'; message?: string }
|
|
|
|
>('sellShares')
|
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
export const createAnswer = cloudFunction<
|
|
|
|
{ contractId: string; text: string; amount: number },
|
|
|
|
{
|
|
|
|
status: 'error' | 'success'
|
|
|
|
message?: string
|
|
|
|
answerId?: string
|
|
|
|
betId?: string
|
|
|
|
}
|
|
|
|
>('createAnswer')
|
|
|
|
|
|
|
|
export const resolveMarket = cloudFunction<
|
|
|
|
{
|
|
|
|
outcome: string
|
|
|
|
contractId: string
|
|
|
|
probabilityInt?: number
|
2022-02-20 07:26:26 +00:00
|
|
|
resolutions?: { [outcome: string]: number }
|
2022-02-17 23:00:19 +00:00
|
|
|
},
|
|
|
|
{ status: 'error' | 'success'; message?: string }
|
|
|
|
>('resolveMarket')
|
2022-01-05 18:23:58 +00:00
|
|
|
|
2022-01-19 03:36:46 +00:00
|
|
|
export const createUser: () => Promise<User | null> = () => {
|
|
|
|
let deviceToken = window.localStorage.getItem('device-token')
|
|
|
|
if (!deviceToken) {
|
|
|
|
deviceToken = randomString()
|
|
|
|
window.localStorage.setItem('device-token', deviceToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cloudFunction('createUser')({ deviceToken })
|
2022-01-18 07:18:38 +00:00
|
|
|
.then((r) => (r.data as any)?.user || null)
|
|
|
|
.catch(() => null)
|
2022-01-19 03:36:46 +00:00
|
|
|
}
|
2022-02-04 03:04:56 +00:00
|
|
|
|
|
|
|
export const changeUserInfo = (data: {
|
|
|
|
username?: string
|
|
|
|
name?: string
|
|
|
|
avatarUrl?: string
|
|
|
|
}) => {
|
|
|
|
return cloudFunction('changeUserInfo')(data)
|
|
|
|
.then((r) => r.data as { status: string; message?: string })
|
|
|
|
.catch((e) => ({ status: 'error', message: e.message }))
|
|
|
|
}
|