diff --git a/common/envs/dev.ts b/common/envs/dev.ts index ec8f40b5..6be7b1b2 100644 --- a/common/envs/dev.ts +++ b/common/envs/dev.ts @@ -12,4 +12,8 @@ export const DEV_CONFIG: EnvConfig = { appId: '1:134303100058:web:27f9ea8b83347251f80323', measurementId: 'G-YJC9E37P37', }, + functionEndpoints: { + placebet: 'https://placebet-w3txbmd3ba-uc.a.run.app', + createmarket: 'https://createmarket-w3txbmd3ba-uc.a.run.app', + }, } diff --git a/common/envs/prod.ts b/common/envs/prod.ts index 07218167..909afec1 100644 --- a/common/envs/prod.ts +++ b/common/envs/prod.ts @@ -1,6 +1,9 @@ +export type V2CloudFunction = 'placebet' | 'createmarket' + export type EnvConfig = { domain: string firebaseConfig: FirebaseConfig + functionEndpoints: Record // Access controls adminEmails: string[] @@ -37,6 +40,10 @@ export const PROD_CONFIG: EnvConfig = { appId: '1:128925704902:web:f61f86944d8ffa2a642dc7', measurementId: 'G-SSFK1Q138D', }, + functionEndpoints: { + placebet: 'https://placebet-nggbo3neva-uc.a.run.app', + createmarket: 'https://createmarket-nggbo3neva-uc.a.run.app', + }, adminEmails: [ 'akrolsmir@gmail.com', // Austin 'jahooma@gmail.com', // James diff --git a/common/envs/theoremone.ts b/common/envs/theoremone.ts index 9a5f4ee6..19c64eef 100644 --- a/common/envs/theoremone.ts +++ b/common/envs/theoremone.ts @@ -12,6 +12,11 @@ export const THEOREMONE_CONFIG: EnvConfig = { appId: '1:698012149198:web:b342af75662831aa84b79f', measurementId: 'G-Y3EZ1WNT6E', }, + // TODO: fill in real endpoints for T1 + functionEndpoints: { + placebet: 'https://placebet-nggbo3neva-uc.a.run.app', + createmarket: 'https://createmarket-nggbo3neva-uc.a.run.app', + }, adminEmails: [...PROD_CONFIG.adminEmails, 'david.glidden@theoremone.co'], whitelistEmail: '@theoremone.co', moneyMoniker: 'T$', diff --git a/web/components/bet-panel.tsx b/web/components/bet-panel.tsx index 536a2898..129356a1 100644 --- a/web/components/bet-panel.tsx +++ b/web/components/bet-panel.tsx @@ -202,11 +202,6 @@ function BuyPanel(props: { const [inputRef, focusAmountInput] = useFocus() - useEffect(() => { - // warm up cloud function - placeBet({}).catch(() => {}) - }, []) - useEffect(() => { if (selected) focusAmountInput() }, [selected, focusAmountInput]) diff --git a/web/lib/api/proxy.ts b/web/lib/api/proxy.ts index cc491860..0e70f546 100644 --- a/web/lib/api/proxy.ts +++ b/web/lib/api/proxy.ts @@ -2,6 +2,7 @@ import { NextApiRequest, NextApiResponse } from 'next' import { promisify } from 'util' import { pipeline } from 'stream' import { getFunctionUrl } from 'web/lib/firebase/api-call' +import { V2CloudFunction } from 'common/envs/prod' import fetch, { Headers, Response } from 'node-fetch' function getProxiedRequestHeaders(req: NextApiRequest, whitelist: string[]) { @@ -32,8 +33,8 @@ function getProxiedResponseHeaders(res: Response, whitelist: string[]) { return result } -export const fetchBackend = (req: NextApiRequest, endpoint: string) => { - const url = getFunctionUrl(endpoint) +export const fetchBackend = (req: NextApiRequest, name: V2CloudFunction) => { + const url = getFunctionUrl(name) const headers = getProxiedRequestHeaders(req, [ 'Authorization', 'Content-Length', diff --git a/web/lib/firebase/api-call.ts b/web/lib/firebase/api-call.ts index c0073837..0e833d1c 100644 --- a/web/lib/firebase/api-call.ts +++ b/web/lib/firebase/api-call.ts @@ -1,5 +1,6 @@ import { auth } from './users' -import { FIREBASE_CONFIG } from 'common/envs/constants' +import { ENV_CONFIG } from 'common/envs/constants' +import { V2CloudFunction } from 'common/envs/prod' export class APIError extends Error { code: number @@ -38,17 +39,14 @@ export async function call(url: string, method: string, params: any) { // app just hit the cloud functions directly -- there's no difference and it's // one less hop -export function getFunctionUrl(name: string) { - const { projectId, region } = FIREBASE_CONFIG - return process.env.NEXT_PUBLIC_FIREBASE_EMULATE - ? `http://localhost:5001/${projectId}/${region}/${name}` - : `https://${region}-${projectId}.cloudfunctions.net/${name}` +export function getFunctionUrl(name: V2CloudFunction) { + return ENV_CONFIG.functionEndpoints[name] } -export function createContract(params: any) { - return call(getFunctionUrl('createContract'), 'POST', params) +export function createMarket(params: any) { + return call(getFunctionUrl('createmarket'), 'POST', params) } export function placeBet(params: any) { - return call(getFunctionUrl('placeBet'), 'POST', params) + return call(getFunctionUrl('placebet'), 'POST', params) } diff --git a/web/pages/api/v0/bets/index.ts b/web/pages/api/v0/bets/index.ts index a7154824..ab7d78aa 100644 --- a/web/pages/api/v0/bets/index.ts +++ b/web/pages/api/v0/bets/index.ts @@ -14,7 +14,7 @@ export default async function route(req: NextApiRequest, res: NextApiResponse) { methods: 'POST', }) try { - const backendRes = await fetchBackend(req, 'placeBet') + const backendRes = await fetchBackend(req, 'placebet') await forwardResponse(res, backendRes) } catch (err) { console.error('Error talking to cloud function: ', err) diff --git a/web/pages/api/v0/market/index.ts b/web/pages/api/v0/market/index.ts index 6053247a..c9e82800 100644 --- a/web/pages/api/v0/market/index.ts +++ b/web/pages/api/v0/market/index.ts @@ -14,7 +14,7 @@ export default async function route(req: NextApiRequest, res: NextApiResponse) { methods: 'POST', }) try { - const backendRes = await fetchBackend(req, 'createContract') + const backendRes = await fetchBackend(req, 'createmarket') await forwardResponse(res, backendRes) } catch (err) { console.error('Error talking to cloud function: ', err) diff --git a/web/pages/create.tsx b/web/pages/create.tsx index 78abfa10..c5f01b47 100644 --- a/web/pages/create.tsx +++ b/web/pages/create.tsx @@ -6,7 +6,7 @@ import Textarea from 'react-expanding-textarea' import { Spacer } from 'web/components/layout/spacer' import { useUser } from 'web/hooks/use-user' import { Contract, contractPath } from 'web/lib/firebase/contracts' -import { createContract } from 'web/lib/firebase/api-call' +import { createMarket } from 'web/lib/firebase/api-call' import { FIXED_ANTE, MINIMUM_ANTE } from 'common/antes' import { InfoTooltip } from 'web/components/info-tooltip' import { Page } from 'web/components/page' @@ -64,10 +64,6 @@ export function NewContract(props: { question: string; tag?: string }) { if (creator === null) router.push('/') }, [creator]) - useEffect(() => { - createContract({}).catch(() => {}) // warm up function - }, []) - const [outcomeType, setOutcomeType] = useState('BINARY') const [initialProb, setInitialProb] = useState(50) const [minString, setMinString] = useState('') @@ -141,7 +137,7 @@ export function NewContract(props: { question: string; tag?: string }) { setIsSubmitting(true) try { - const result = await createContract( + const result = await createMarket( removeUndefinedProps({ question, outcomeType, diff --git a/web/pages/make-predictions.tsx b/web/pages/make-predictions.tsx index dccef955..8b35dd16 100644 --- a/web/pages/make-predictions.tsx +++ b/web/pages/make-predictions.tsx @@ -16,7 +16,7 @@ import { Linkify } from 'web/components/linkify' import { Page } from 'web/components/page' import { Title } from 'web/components/title' import { useUser } from 'web/hooks/use-user' -import { createContract } from 'web/lib/firebase/api-call' +import { createMarket } from 'web/lib/firebase/api-call' import { contractPath } from 'web/lib/firebase/contracts' type Prediction = { @@ -138,7 +138,7 @@ ${TEST_VALUE} }) } - async function createContracts() { + async function createMarkets() { if (!user) { // TODO: Convey error with snackbar/toast console.error('You need to be signed in!') @@ -146,7 +146,7 @@ ${TEST_VALUE} } setIsSubmitting(true) for (const prediction of predictions) { - const contract = (await createContract({ + const contract = (await createMarket({ question: prediction.question, description: prediction.description, initialProb: prediction.initialProb, @@ -270,7 +270,7 @@ ${TEST_VALUE} disabled={predictions.length === 0 || isSubmitting} onClick={(e) => { e.preventDefault() - createContracts() + createMarkets() }} > Create all