global warming: warm up all cloud functions on client

This commit is contained in:
mantikoros 2022-01-05 12:23:58 -06:00
parent 5eaf50612d
commit 0b8ad76b0f
7 changed files with 41 additions and 22 deletions

View File

@ -29,6 +29,9 @@ export const createContract = functions
const { question, description, initialProb, ante, closeTime } = data const { question, description, initialProb, ante, closeTime } = data
if (!question || !initialProb)
return { status: 'error', message: 'Missing contract attributes' }
if (ante !== undefined && (ante < 0 || ante > creator.balance)) if (ante !== undefined && (ante < 0 || ante > creator.balance))
return { status: 'error', message: 'Invalid ante' } return { status: 'error', message: 'Invalid ante' }

View File

@ -1,6 +1,6 @@
import { getFunctions, httpsCallable } from 'firebase/functions' import { getFunctions, httpsCallable } from 'firebase/functions'
import clsx from 'clsx' import clsx from 'clsx'
import React, { useState } from 'react' import React, { useEffect, useState } from 'react'
import { useUser } from '../hooks/use-user' import { useUser } from '../hooks/use-user'
import { Contract } from '../lib/firebase/contracts' import { Contract } from '../lib/firebase/contracts'
@ -24,8 +24,14 @@ import { firebaseLogin } from '../lib/firebase/users'
import { OutcomeLabel } from './outcome-label' import { OutcomeLabel } from './outcome-label'
import { AdvancedPanel } from './advanced-panel' import { AdvancedPanel } from './advanced-panel'
import { Bet } from '../lib/firebase/bets' import { Bet } from '../lib/firebase/bets'
import { placeBet } from '../lib/firebase/api-call'
export function BetPanel(props: { contract: Contract; className?: string }) { export function BetPanel(props: { contract: Contract; className?: string }) {
useEffect(() => {
// warm up cloud function
placeBet({}).catch()
}, [])
const { contract, className } = props const { contract, className } = props
const user = useUser() const user = useUser()
@ -212,6 +218,3 @@ export function BetPanel(props: { contract: Contract; className?: string }) {
</Col> </Col>
) )
} }
const functions = getFunctions()
export const placeBet = httpsCallable(functions, 'placeBet')

View File

@ -2,6 +2,8 @@ import Link from 'next/link'
import _ from 'lodash' import _ from 'lodash'
import dayjs from 'dayjs' import dayjs from 'dayjs'
import { useEffect, useState } from 'react' import { useEffect, useState } from 'react'
import clsx from 'clsx'
import { useUserBets } from '../hooks/use-user-bets' import { useUserBets } from '../hooks/use-user-bets'
import { Bet } from '../lib/firebase/bets' import { Bet } from '../lib/firebase/bets'
import { User } from '../lib/firebase/users' import { User } from '../lib/firebase/users'
@ -20,8 +22,7 @@ import {
calculateSaleAmount, calculateSaleAmount,
resolvedPayout, resolvedPayout,
} from '../lib/calculate' } from '../lib/calculate'
import clsx from 'clsx' import { sellBet } from '../lib/firebase/api-call'
import { cloudFunction } from '../lib/firebase/api-call'
import { ConfirmationButton } from './confirmation-button' import { ConfirmationButton } from './confirmation-button'
import { OutcomeLabel, YesLabel, NoLabel, MarketLabel } from './outcome-label' import { OutcomeLabel, YesLabel, NoLabel, MarketLabel } from './outcome-label'
@ -341,9 +342,12 @@ function BetRow(props: { bet: Bet; contract: Contract; sale?: Bet }) {
) )
} }
const sellBet = cloudFunction('sellBet')
function SellButton(props: { contract: Contract; bet: Bet }) { function SellButton(props: { contract: Contract; bet: Bet }) {
useEffect(() => {
// warm up cloud function
sellBet({}).catch()
}, [])
const { contract, bet } = props const { contract, bet } = props
const [isSubmitting, setIsSubmitting] = useState(false) const [isSubmitting, setIsSubmitting] = useState(false)

View File

@ -1,6 +1,5 @@
import clsx from 'clsx' import clsx from 'clsx'
import React, { useState } from 'react' import React, { useEffect, useState } from 'react'
import { getFunctions, httpsCallable } from 'firebase/functions'
import { Contract } from '../lib/firebase/contracts' import { Contract } from '../lib/firebase/contracts'
import { Col } from './layout/col' import { Col } from './layout/col'
@ -9,15 +8,18 @@ import { User } from '../lib/firebase/users'
import { YesNoCancelSelector } from './yes-no-selector' import { YesNoCancelSelector } from './yes-no-selector'
import { Spacer } from './layout/spacer' import { Spacer } from './layout/spacer'
import { ConfirmationButton as ConfirmationButton } from './confirmation-button' import { ConfirmationButton as ConfirmationButton } from './confirmation-button'
import { resolveMarket } from '../lib/firebase/api-call'
const functions = getFunctions()
export const resolveMarket = httpsCallable(functions, 'resolveMarket')
export function ResolutionPanel(props: { export function ResolutionPanel(props: {
creator: User creator: User
contract: Contract contract: Contract
className?: string className?: string
}) { }) {
useEffect(() => {
// warm up cloud function
resolveMarket({}).catch()
}, [])
const { contract, className } = props const { contract, className } = props
const [outcome, setOutcome] = useState< const [outcome, setOutcome] = useState<

View File

@ -4,3 +4,10 @@ const functions = getFunctions()
export const cloudFunction = (name: string) => httpsCallable(functions, name) export const cloudFunction = (name: string) => httpsCallable(functions, name)
export const createContract = cloudFunction('createContract')
export const placeBet = cloudFunction('placeBet')
export const resolveMarket = cloudFunction('resolveMarket')
export const sellBet = cloudFunction('sellBet')

View File

@ -2,7 +2,6 @@ import router from 'next/router'
import { useEffect, useState } from 'react' import { useEffect, useState } from 'react'
import clsx from 'clsx' import clsx from 'clsx'
import dayjs from 'dayjs' import dayjs from 'dayjs'
import { getFunctions, httpsCallable } from 'firebase/functions'
import { CreatorContractsList } from '../components/contracts-list' import { CreatorContractsList } from '../components/contracts-list'
import { Spacer } from '../components/layout/spacer' import { Spacer } from '../components/layout/spacer'
@ -12,6 +11,7 @@ import { Contract, path } from '../lib/firebase/contracts'
import { Page } from '../components/page' import { Page } from '../components/page'
import { formatMoney } from '../lib/util/format' import { formatMoney } from '../lib/util/format'
import { AdvancedPanel } from '../components/advanced-panel' import { AdvancedPanel } from '../components/advanced-panel'
import { createContract } from '../lib/firebase/api-call'
// Allow user to create a new contract // Allow user to create a new contract
export default function NewContract() { export default function NewContract() {
@ -19,7 +19,12 @@ export default function NewContract() {
useEffect(() => { useEffect(() => {
if (creator === null) router.push('/') if (creator === null) router.push('/')
}) }, [creator])
useEffect(() => {
// warm up function
createContract({}).catch()
}, [])
const [initialProb, setInitialProb] = useState(50) const [initialProb, setInitialProb] = useState(50)
const [question, setQuestion] = useState('') const [question, setQuestion] = useState('')
@ -225,9 +230,6 @@ export default function NewContract() {
) )
} }
const functions = getFunctions()
export const createContract = httpsCallable(functions, 'createContract')
// Given a date string like '2022-04-02', // Given a date string like '2022-04-02',
// return the time just before midnight on that date (in the user's local time), as millis since epoch // return the time just before midnight on that date (in the user's local time), as millis since epoch
function dateToMillis(date: string) { function dateToMillis(date: string) {

View File

@ -1,7 +1,7 @@
import clsx from 'clsx' import clsx from 'clsx'
import { getFunctions, httpsCallable } from 'firebase/functions'
import Link from 'next/link' import Link from 'next/link'
import { useState } from 'react' import { useState } from 'react'
import { Col } from '../components/layout/col' import { Col } from '../components/layout/col'
import { Row } from '../components/layout/row' import { Row } from '../components/layout/row'
import { Spacer } from '../components/layout/spacer' import { Spacer } from '../components/layout/spacer'
@ -9,11 +9,9 @@ import { Linkify } from '../components/linkify'
import { Page } from '../components/page' import { Page } from '../components/page'
import { Title } from '../components/title' import { Title } from '../components/title'
import { useUser } from '../hooks/use-user' import { useUser } from '../hooks/use-user'
import { createContract } from '../lib/firebase/api-call'
import { compute, Contract, path } from '../lib/firebase/contracts' import { compute, Contract, path } from '../lib/firebase/contracts'
const functions = getFunctions()
export const createContract = httpsCallable(functions, 'createContract')
type Prediction = { type Prediction = {
question: string question: string
description: string description: string