initial commit
This commit is contained in:
parent
b5761baeaf
commit
0b1d59a2ac
16
functions/src/api-call.ts
Normal file
16
functions/src/api-call.ts
Normal 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)
|
||||
})
|
|
@ -4,3 +4,4 @@ admin.initializeApp()
|
|||
|
||||
export * from './place-bet'
|
||||
export * from './resolve-market'
|
||||
export * from './api-call'
|
||||
|
|
|
@ -5,19 +5,22 @@ import { Contract } from './types/contract'
|
|||
import { User } from './types/user'
|
||||
import { Bet } from './types/bet'
|
||||
|
||||
export const placeBet = functions.runWith({ minInstances: 1 }).https.onCall(
|
||||
async (
|
||||
data: {
|
||||
export const placeBet = functions
|
||||
.runWith({ minInstances: 1 })
|
||||
.https.onCall((data, context) => {
|
||||
placeBetLogic({ ...data, userId: context?.auth?.uid })
|
||||
})
|
||||
|
||||
export const placeBetLogic = async (data: {
|
||||
amount: number
|
||||
outcome: string
|
||||
contractId: string
|
||||
},
|
||||
context
|
||||
) => {
|
||||
const userId = context?.auth?.uid
|
||||
if (!userId) return { status: 'error', message: 'Not authorized' }
|
||||
userId: string
|
||||
}) => {
|
||||
console.log('placeBet called')
|
||||
// if (!userId) return { status: 'error', message: 'Not authorized' }
|
||||
|
||||
const { amount, outcome, contractId } = data
|
||||
const { amount, outcome, contractId, userId } = data
|
||||
|
||||
if (outcome !== 'YES' && outcome !== 'NO')
|
||||
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) => {
|
||||
const userDoc = firestore.doc(`users/${userId}`)
|
||||
const userSnap = await transaction.get(userDoc)
|
||||
if (!userSnap.exists)
|
||||
return { status: 'error', message: 'User not found' }
|
||||
if (!userSnap.exists) return { status: 'error', message: 'User not found' }
|
||||
const user = userSnap.data() as User
|
||||
|
||||
if (user.balance < amount)
|
||||
|
@ -39,9 +41,7 @@ export const placeBet = functions.runWith({ minInstances: 1 }).https.onCall(
|
|||
return { status: 'error', message: 'Invalid contract' }
|
||||
const contract = contractSnap.data() as Contract
|
||||
|
||||
const newBetDoc = firestore
|
||||
.collection(`contracts/${contractId}/bets`)
|
||||
.doc()
|
||||
const newBetDoc = firestore.collection(`contracts/${contractId}/bets`).doc()
|
||||
|
||||
const { newBet, newPool, newDpmWeights, newBalance } = getNewBetInfo(
|
||||
user,
|
||||
|
@ -60,8 +60,7 @@ export const placeBet = functions.runWith({ minInstances: 1 }).https.onCall(
|
|||
|
||||
return { status: 'success' }
|
||||
})
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
const firestore = admin.firestore()
|
||||
|
||||
|
|
|
@ -15,6 +15,7 @@ import {
|
|||
getDpmWeight,
|
||||
getProbabilityAfterBet,
|
||||
} from '../lib/calculation/contract'
|
||||
import { apiCall } from '../lib/firebase/api-call'
|
||||
|
||||
export function BetPanel(props: { contract: Contract; className?: string }) {
|
||||
const { contract, className } = props
|
||||
|
@ -61,6 +62,7 @@ export function BetPanel(props: { contract: Contract; className?: string }) {
|
|||
amount: betAmount,
|
||||
outcome: betChoice,
|
||||
contractId: contract.id,
|
||||
userId: user.id,
|
||||
}).then((r) => r.data as any)
|
||||
|
||||
console.log('placed bet. Result:', result)
|
||||
|
@ -171,5 +173,9 @@ export function BetPanel(props: { contract: Contract; className?: string }) {
|
|||
)
|
||||
}
|
||||
|
||||
const functions = getFunctions()
|
||||
export const placeBet = httpsCallable(functions, 'placeBet')
|
||||
// const functions = getFunctions()
|
||||
// export const placeBet = httpsCallable(functions, 'placeBet')
|
||||
export const placeBet = async (bet: any) => {
|
||||
await apiCall({ ...bet, name: 'place-bet' })
|
||||
return { data: { success: true } }
|
||||
}
|
||||
|
|
9
web/lib/firebase/api-call.ts
Normal file
9
web/lib/firebase/api-call.ts
Normal 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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user