Small backend cleanups (#643)

* Reuse DAY_MS in update-metrics job

* More concise transaction in cancelbet

* Remove some meaningless awaits

* Do less work in onCreateLiquidityProvision

* Do less work in onCreateAnswer
This commit is contained in:
Marshall Polaris 2022-07-24 00:45:45 -07:00 committed by GitHub
parent a1d5d161dd
commit 312b244e2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 14 additions and 18 deletions

View File

@ -10,7 +10,7 @@ const bodySchema = z.object({
export const cancelbet = newEndpoint({}, async (req, auth) => { export const cancelbet = newEndpoint({}, async (req, auth) => {
const { betId } = validate(bodySchema, req.body) const { betId } = validate(bodySchema, req.body)
const result = await firestore.runTransaction(async (trans) => { return await firestore.runTransaction(async (trans) => {
const snap = await trans.get( const snap = await trans.get(
firestore.collectionGroup('bets').where('id', '==', betId) firestore.collectionGroup('bets').where('id', '==', betId)
) )
@ -28,8 +28,6 @@ export const cancelbet = newEndpoint({}, async (req, auth) => {
return { ...bet, isCancelled: true } return { ...bet, isCancelled: true }
}) })
return result
}) })
const firestore = admin.firestore() const firestore = admin.firestore()

View File

@ -120,7 +120,7 @@ export const createmarket = newEndpoint({}, async (req, auth) => {
let group = null let group = null
if (groupId) { if (groupId) {
const groupDocRef = await firestore.collection('groups').doc(groupId) const groupDocRef = firestore.collection('groups').doc(groupId)
const groupDoc = await groupDocRef.get() const groupDoc = await groupDocRef.get()
if (!groupDoc.exists) { if (!groupDoc.exists) {
throw new APIError(400, 'No group exists with the given group ID.') throw new APIError(400, 'No group exists with the given group ID.')

View File

@ -10,14 +10,14 @@ export const onCreateAnswer = functions.firestore
contractId: string contractId: string
} }
const { eventId } = context const { eventId } = context
const contract = await getContract(contractId)
if (!contract)
throw new Error('Could not find contract corresponding with answer')
const answer = change.data() as Answer const answer = change.data() as Answer
// Ignore ante answer. // Ignore ante answer.
if (answer.number === 0) return if (answer.number === 0) return
const contract = await getContract(contractId)
if (!contract)
throw new Error('Could not find contract corresponding with answer')
const answerCreator = await getUser(answer.userId) const answerCreator = await getUser(answer.userId)
if (!answerCreator) throw new Error('Could not find answer creator') if (!answerCreator) throw new Error('Could not find answer creator')

View File

@ -8,14 +8,14 @@ export const onCreateLiquidityProvision = functions.firestore
.onCreate(async (change, context) => { .onCreate(async (change, context) => {
const liquidity = change.data() as LiquidityProvision const liquidity = change.data() as LiquidityProvision
const { eventId } = context const { eventId } = context
const contract = await getContract(liquidity.contractId)
if (!contract)
throw new Error('Could not find contract corresponding with liquidity')
// Ignore Manifold Markets liquidity for now - users see a notification for free market liquidity provision // Ignore Manifold Markets liquidity for now - users see a notification for free market liquidity provision
if (liquidity.userId === 'IPTOzEqrpkWmEzh6hwvAyY9PqFb2') return if (liquidity.userId === 'IPTOzEqrpkWmEzh6hwvAyY9PqFb2') return
const contract = await getContract(liquidity.contractId)
if (!contract)
throw new Error('Could not find contract corresponding with liquidity')
const liquidityProvider = await getUser(liquidity.userId) const liquidityProvider = await getUser(liquidity.userId)
if (!liquidityProvider) throw new Error('Could not find liquidity provider') if (!liquidityProvider) throw new Error('Could not find liquidity provider')

View File

@ -103,8 +103,8 @@ async function handleUserUpdatedReferral(user: User, eventId: string) {
description: `Referred new user id: ${user.id} for ${REFERRAL_AMOUNT}`, description: `Referred new user id: ${user.id} for ${REFERRAL_AMOUNT}`,
} }
const txnDoc = await firestore.collection(`txns/`).doc(txn.id) const txnDoc = firestore.collection(`txns/`).doc(txn.id)
await transaction.set(txnDoc, txn) transaction.set(txnDoc, txn)
console.log('created referral with txn id:', txn.id) console.log('created referral with txn id:', txn.id)
// We're currently not subtracting M$ from the house, not sure if we want to for accounting purposes. // We're currently not subtracting M$ from the house, not sure if we want to for accounting purposes.
transaction.update(referredByUserDoc, { transaction.update(referredByUserDoc, {

View File

@ -11,8 +11,6 @@ import { last } from 'lodash'
const firestore = admin.firestore() const firestore = admin.firestore()
const oneDay = 1000 * 60 * 60 * 24
const computeInvestmentValue = ( const computeInvestmentValue = (
bets: Bet[], bets: Bet[],
contractsDict: { [k: string]: Contract } contractsDict: { [k: string]: Contract }
@ -59,8 +57,8 @@ export const updateMetricsCore = async () => {
return { return {
doc: firestore.collection('contracts').doc(contract.id), doc: firestore.collection('contracts').doc(contract.id),
fields: { fields: {
volume24Hours: computeVolume(contractBets, now - oneDay), volume24Hours: computeVolume(contractBets, now - DAY_MS),
volume7Days: computeVolume(contractBets, now - oneDay * 7), volume7Days: computeVolume(contractBets, now - DAY_MS * 7),
}, },
} }
}) })