2022-05-19 22:04:34 +00:00
|
|
|
import { auth } from './users'
|
2022-06-06 19:46:06 +00:00
|
|
|
import { ENV_CONFIG } from 'common/envs/constants'
|
2022-05-19 22:04:34 +00:00
|
|
|
|
|
|
|
export class APIError extends Error {
|
|
|
|
code: number
|
2022-06-22 16:35:50 +00:00
|
|
|
details?: string
|
|
|
|
constructor(code: number, message: string, details?: string) {
|
2022-05-19 22:04:34 +00:00
|
|
|
super(message)
|
|
|
|
this.code = code
|
|
|
|
this.name = 'APIError'
|
2022-06-22 16:35:50 +00:00
|
|
|
this.details = details
|
2022-02-17 23:00:19 +00:00
|
|
|
}
|
2022-07-08 22:28:04 +00:00
|
|
|
toString() {
|
|
|
|
return this.name
|
|
|
|
}
|
2022-05-19 22:04:34 +00:00
|
|
|
}
|
2022-01-05 18:23:58 +00:00
|
|
|
|
2022-05-23 21:16:56 +00:00
|
|
|
export async function call(url: string, method: string, params: any) {
|
2022-05-19 22:04:34 +00:00
|
|
|
const user = auth.currentUser
|
|
|
|
if (user == null) {
|
|
|
|
throw new Error('Must be signed in to make API calls.')
|
2022-01-19 03:36:46 +00:00
|
|
|
}
|
2022-05-19 22:04:34 +00:00
|
|
|
const token = await user.getIdToken()
|
|
|
|
const req = new Request(url, {
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bearer ${token}`,
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
method: method,
|
2022-05-20 21:58:14 +00:00
|
|
|
body: JSON.stringify(params),
|
2022-05-19 22:04:34 +00:00
|
|
|
})
|
|
|
|
return await fetch(req).then(async (resp) => {
|
|
|
|
const json = (await resp.json()) as { [k: string]: any }
|
2022-05-20 21:58:14 +00:00
|
|
|
if (!resp.ok) {
|
2022-06-22 16:35:50 +00:00
|
|
|
throw new APIError(resp.status, json?.message, json?.details)
|
2022-05-19 22:04:34 +00:00
|
|
|
}
|
2022-05-20 21:58:14 +00:00
|
|
|
return json
|
2022-05-19 22:04:34 +00:00
|
|
|
})
|
2022-01-19 03:36:46 +00:00
|
|
|
}
|
2022-02-04 03:04:56 +00:00
|
|
|
|
2022-06-03 07:50:24 +00:00
|
|
|
// Our users access the API through the Vercel proxy routes at /api/v0/blah,
|
|
|
|
// but right now at least until we get performance under control let's have the
|
|
|
|
// app just hit the cloud functions directly -- there's no difference and it's
|
|
|
|
// one less hop
|
|
|
|
|
2022-06-23 23:46:49 +00:00
|
|
|
export function getFunctionUrl(name: string) {
|
2022-06-29 23:31:53 +00:00
|
|
|
if (process.env.NEXT_PUBLIC_FIREBASE_EMULATE) {
|
|
|
|
const { projectId, region } = ENV_CONFIG.firebaseConfig
|
|
|
|
return `http://localhost:5001/${projectId}/${region}/${name}`
|
|
|
|
} else {
|
|
|
|
const { cloudRunId, cloudRunRegion } = ENV_CONFIG
|
|
|
|
return `https://${name}-${cloudRunId}-${cloudRunRegion}.a.run.app`
|
|
|
|
}
|
2022-06-03 07:50:24 +00:00
|
|
|
}
|
|
|
|
|
2022-07-08 22:00:03 +00:00
|
|
|
export function changeUserInfo(params: any) {
|
|
|
|
return call(getFunctionUrl('changeuserinfo'), 'POST', params)
|
|
|
|
}
|
|
|
|
|
2022-07-08 22:08:17 +00:00
|
|
|
export function addLiquidity(params: any) {
|
|
|
|
return call(getFunctionUrl('addliquidity'), 'POST', params)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function withdrawLiquidity(params: any) {
|
|
|
|
return call(getFunctionUrl('withdrawliquidity'), 'POST', params)
|
|
|
|
}
|
|
|
|
|
2022-06-06 19:46:06 +00:00
|
|
|
export function createMarket(params: any) {
|
|
|
|
return call(getFunctionUrl('createmarket'), 'POST', params)
|
2022-02-04 03:04:56 +00:00
|
|
|
}
|
2022-04-21 17:58:12 +00:00
|
|
|
|
2022-06-29 23:47:06 +00:00
|
|
|
export function resolveMarket(params: any) {
|
|
|
|
return call(getFunctionUrl('resolvemarket'), 'POST', params)
|
|
|
|
}
|
|
|
|
|
2022-05-19 22:04:34 +00:00
|
|
|
export function placeBet(params: any) {
|
2022-06-06 19:46:06 +00:00
|
|
|
return call(getFunctionUrl('placebet'), 'POST', params)
|
2022-04-21 17:58:12 +00:00
|
|
|
}
|
2022-06-07 20:54:58 +00:00
|
|
|
|
|
|
|
export function sellShares(params: any) {
|
|
|
|
return call(getFunctionUrl('sellshares'), 'POST', params)
|
|
|
|
}
|
2022-06-07 21:08:56 +00:00
|
|
|
|
|
|
|
export function sellBet(params: any) {
|
|
|
|
return call(getFunctionUrl('sellbet'), 'POST', params)
|
|
|
|
}
|
2022-06-22 16:35:50 +00:00
|
|
|
|
2022-07-08 22:28:04 +00:00
|
|
|
export function claimManalink(params: any) {
|
|
|
|
return call(getFunctionUrl('claimmanalink'), 'POST', params)
|
|
|
|
}
|
|
|
|
|
2022-06-22 16:35:50 +00:00
|
|
|
export function createGroup(params: any) {
|
|
|
|
return call(getFunctionUrl('creategroup'), 'POST', params)
|
|
|
|
}
|
2022-07-05 17:29:26 +00:00
|
|
|
|
|
|
|
export function requestBonuses(params: any) {
|
|
|
|
return call(getFunctionUrl('getdailybonuses'), 'POST', params)
|
|
|
|
}
|