From e1d265f7aa881b388f521252c339469a8a532dff Mon Sep 17 00:00:00 2001 From: James Grugett Date: Tue, 29 Mar 2022 00:28:08 -0500 Subject: [PATCH] Don't allow selling more than max shares in cloud function --- functions/src/sell-shares.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/functions/src/sell-shares.ts b/functions/src/sell-shares.ts index 91d88fef..5c2ae0d0 100644 --- a/functions/src/sell-shares.ts +++ b/functions/src/sell-shares.ts @@ -1,3 +1,4 @@ +import * as _ from 'lodash' import * as admin from 'firebase-admin' import * as functions from 'firebase-functions' @@ -5,6 +6,8 @@ import { Binary, CPMM, FullContract } from '../../common/contract' import { User } from '../../common/user' import { getCpmmSellBetInfo } from '../../common/sell-bet' import { addObjects, removeUndefinedProps } from '../../common/util/object' +import { getValues } from './utils' +import { Bet } from '../../common/bet' export const sellShares = functions.runWith({ minInstances: 1 }).https.onCall( async ( @@ -44,6 +47,27 @@ export const sellShares = functions.runWith({ minInstances: 1 }).https.onCall( if (closeTime && Date.now() > closeTime) return { status: 'error', message: 'Trading is closed' } + const userBets = await getValues( + contractDoc.collection('bets').where('userId', '==', userId) + ) + + const [yesBets, noBets] = _.partition( + userBets ?? [], + (bet) => bet.outcome === 'YES' + ) + const [yesShares, noShares] = [ + _.sumBy(yesBets, (bet) => bet.shares), + _.sumBy(noBets, (bet) => bet.shares), + ] + + const maxShares = outcome === 'YES' ? yesShares : noShares + if (shares > maxShares) { + return { + status: 'error', + message: `You can only sell ${maxShares} shares`, + } + } + const newBetDoc = firestore .collection(`contracts/${contractId}/bets`) .doc()