Point client at new v2 versions of createmarket and placebet (#433)

* Kill 'warmup spam' for createContract and placeBet

* Point v2 function calls at v2 endpoints

* Add real prod placebet and createmarket endpoints
This commit is contained in:
Marshall Polaris 2022-06-06 12:46:06 -07:00 committed by GitHub
parent 13826b5759
commit d9eb9798e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 34 additions and 28 deletions

View File

@ -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',
},
}

View File

@ -1,6 +1,9 @@
export type V2CloudFunction = 'placebet' | 'createmarket'
export type EnvConfig = {
domain: string
firebaseConfig: FirebaseConfig
functionEndpoints: Record<V2CloudFunction, string>
// 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

View File

@ -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$',

View File

@ -202,11 +202,6 @@ function BuyPanel(props: {
const [inputRef, focusAmountInput] = useFocus()
useEffect(() => {
// warm up cloud function
placeBet({}).catch(() => {})
}, [])
useEffect(() => {
if (selected) focusAmountInput()
}, [selected, focusAmountInput])

View File

@ -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',

View File

@ -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)
}

View File

@ -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)

View File

@ -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)

View File

@ -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<outcomeType>('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,

View File

@ -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