2022-05-19 22:04:34 +00:00
|
|
|
import { httpsCallable } from 'firebase/functions'
|
|
|
|
import { Txn } from 'common/txn'
|
|
|
|
import { User } from 'common/user'
|
|
|
|
import { randomString } from 'common/util/random'
|
|
|
|
import './init'
|
|
|
|
import { functions } from './init'
|
2022-05-26 05:30:03 +00:00
|
|
|
import { safeLocalStorage } from '../util/local'
|
2022-05-19 22:04:34 +00:00
|
|
|
|
|
|
|
export const cloudFunction = <RequestData, ResponseData>(name: string) =>
|
|
|
|
httpsCallable<RequestData, ResponseData>(functions, name)
|
|
|
|
|
2022-06-08 18:00:49 +00:00
|
|
|
export const withdrawLiquidity = cloudFunction<
|
|
|
|
{ contractId: string },
|
|
|
|
{ status: 'error' | 'success'; userShares: { [outcome: string]: number } }
|
|
|
|
>('withdrawLiquidity')
|
|
|
|
|
2022-05-19 22:04:34 +00:00
|
|
|
export const transact = cloudFunction<
|
|
|
|
Omit<Txn, 'id' | 'createdTime'>,
|
|
|
|
{ status: 'error' | 'success'; message?: string; txn?: Txn }
|
|
|
|
>('transact')
|
|
|
|
|
|
|
|
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
|
|
|
|
value?: number
|
|
|
|
contractId: string
|
|
|
|
probabilityInt?: number
|
|
|
|
resolutions?: { [outcome: string]: number }
|
|
|
|
},
|
|
|
|
{ status: 'error' | 'success'; message?: string }
|
|
|
|
>('resolveMarket')
|
|
|
|
|
|
|
|
export const createUser: () => Promise<User | null> = () => {
|
2022-05-26 05:30:03 +00:00
|
|
|
const local = safeLocalStorage()
|
|
|
|
let deviceToken = local?.getItem('device-token')
|
2022-05-19 22:04:34 +00:00
|
|
|
if (!deviceToken) {
|
|
|
|
deviceToken = randomString()
|
2022-05-26 05:30:03 +00:00
|
|
|
local?.setItem('device-token', deviceToken)
|
2022-05-19 22:04:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cloudFunction('createUser')({ deviceToken })
|
|
|
|
.then((r) => (r.data as any)?.user || null)
|
|
|
|
.catch(() => null)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 }))
|
|
|
|
}
|
|
|
|
|
|
|
|
export const addLiquidity = (data: { amount: number; contractId: string }) => {
|
|
|
|
return cloudFunction('addLiquidity')(data)
|
|
|
|
.then((r) => r.data as { status: string })
|
|
|
|
.catch((e) => ({ status: 'error', message: e.message }))
|
|
|
|
}
|
2022-06-23 08:07:52 +00:00
|
|
|
|
|
|
|
export const claimManalink = cloudFunction<
|
|
|
|
string,
|
|
|
|
{ status: 'error' | 'success'; message?: string }
|
|
|
|
>('claimManalink')
|