Compare commits

...

1 Commits

Author SHA1 Message Date
mantikoros
0b1d59a2ac initial commit 2021-12-17 19:47:46 -06:00
5 changed files with 81 additions and 50 deletions

16
functions/src/api-call.ts Normal file
View File

@ -0,0 +1,16 @@
import * as functions from 'firebase-functions'
import { placeBetLogic } from '.'
const apiFunctions = {
'place-bet': placeBetLogic,
}
export const apiCall = functions.firestore
.document('api-calls/{apiCallId}')
.onCreate(async (snap, context) => {
const data = snap.data() as any
console.log('data:', data)
const apiFunction = apiFunctions[data.name as keyof typeof apiFunctions]
await apiFunction(data)
})

View File

@ -4,3 +4,4 @@ admin.initializeApp()
export * from './place-bet' export * from './place-bet'
export * from './resolve-market' export * from './resolve-market'
export * from './api-call'

View File

@ -5,19 +5,22 @@ import { Contract } from './types/contract'
import { User } from './types/user' import { User } from './types/user'
import { Bet } from './types/bet' import { Bet } from './types/bet'
export const placeBet = functions.runWith({ minInstances: 1 }).https.onCall( export const placeBet = functions
async ( .runWith({ minInstances: 1 })
data: { .https.onCall((data, context) => {
placeBetLogic({ ...data, userId: context?.auth?.uid })
})
export const placeBetLogic = async (data: {
amount: number amount: number
outcome: string outcome: string
contractId: string contractId: string
}, userId: string
context }) => {
) => { console.log('placeBet called')
const userId = context?.auth?.uid // if (!userId) return { status: 'error', message: 'Not authorized' }
if (!userId) return { status: 'error', message: 'Not authorized' }
const { amount, outcome, contractId } = data const { amount, outcome, contractId, userId } = data
if (outcome !== 'YES' && outcome !== 'NO') if (outcome !== 'YES' && outcome !== 'NO')
return { status: 'error', message: 'Invalid outcome' } return { status: 'error', message: 'Invalid outcome' }
@ -26,8 +29,7 @@ export const placeBet = functions.runWith({ minInstances: 1 }).https.onCall(
return await firestore.runTransaction(async (transaction) => { return await firestore.runTransaction(async (transaction) => {
const userDoc = firestore.doc(`users/${userId}`) const userDoc = firestore.doc(`users/${userId}`)
const userSnap = await transaction.get(userDoc) const userSnap = await transaction.get(userDoc)
if (!userSnap.exists) if (!userSnap.exists) return { status: 'error', message: 'User not found' }
return { status: 'error', message: 'User not found' }
const user = userSnap.data() as User const user = userSnap.data() as User
if (user.balance < amount) if (user.balance < amount)
@ -39,9 +41,7 @@ export const placeBet = functions.runWith({ minInstances: 1 }).https.onCall(
return { status: 'error', message: 'Invalid contract' } return { status: 'error', message: 'Invalid contract' }
const contract = contractSnap.data() as Contract const contract = contractSnap.data() as Contract
const newBetDoc = firestore const newBetDoc = firestore.collection(`contracts/${contractId}/bets`).doc()
.collection(`contracts/${contractId}/bets`)
.doc()
const { newBet, newPool, newDpmWeights, newBalance } = getNewBetInfo( const { newBet, newPool, newDpmWeights, newBalance } = getNewBetInfo(
user, user,
@ -60,8 +60,7 @@ export const placeBet = functions.runWith({ minInstances: 1 }).https.onCall(
return { status: 'success' } return { status: 'success' }
}) })
} }
)
const firestore = admin.firestore() const firestore = admin.firestore()

View File

@ -15,6 +15,7 @@ import {
getDpmWeight, getDpmWeight,
getProbabilityAfterBet, getProbabilityAfterBet,
} from '../lib/calculation/contract' } from '../lib/calculation/contract'
import { apiCall } from '../lib/firebase/api-call'
export function BetPanel(props: { contract: Contract; className?: string }) { export function BetPanel(props: { contract: Contract; className?: string }) {
const { contract, className } = props const { contract, className } = props
@ -61,6 +62,7 @@ export function BetPanel(props: { contract: Contract; className?: string }) {
amount: betAmount, amount: betAmount,
outcome: betChoice, outcome: betChoice,
contractId: contract.id, contractId: contract.id,
userId: user.id,
}).then((r) => r.data as any) }).then((r) => r.data as any)
console.log('placed bet. Result:', result) console.log('placed bet. Result:', result)
@ -171,5 +173,9 @@ export function BetPanel(props: { contract: Contract; className?: string }) {
) )
} }
const functions = getFunctions() // const functions = getFunctions()
export const placeBet = httpsCallable(functions, 'placeBet') // export const placeBet = httpsCallable(functions, 'placeBet')
export const placeBet = async (bet: any) => {
await apiCall({ ...bet, name: 'place-bet' })
return { data: { success: true } }
}

View File

@ -0,0 +1,9 @@
import { app } from './init'
import { doc, collection, getFirestore, setDoc } from 'firebase/firestore'
const db = getFirestore(app)
export async function apiCall(apiCallDoc: any) {
const apiCallRef = doc(collection(db, 'api-calls'))
await setDoc(apiCallRef, apiCallDoc)
}