From 2edf31e6e4d0bda85a7c1e250ac824487f159462 Mon Sep 17 00:00:00 2001 From: Ian Philips Date: Mon, 9 May 2022 11:03:12 -0400 Subject: [PATCH] Consolidate logic --- common/calculate.ts | 26 ++++++++++++++++++++++++++ functions/src/create-answer.ts | 24 +++++++++++------------- functions/src/place-bet.ts | 25 ++++++++++--------------- 3 files changed, 47 insertions(+), 28 deletions(-) diff --git a/common/calculate.ts b/common/calculate.ts index c42eaaa7..4c11a660 100644 --- a/common/calculate.ts +++ b/common/calculate.ts @@ -190,3 +190,29 @@ export function getTopAnswer(contract: FreeResponseContract) { ) return top?.answer } + +export function hasUserHitManaLimit( + contract: FreeResponseContract, + bets: Bet[], + amount: number +) { + const { manaLimitPerUser } = contract + if (manaLimitPerUser) { + const contractMetrics = getContractBetMetrics(contract, bets) + const currentInvested = contractMetrics.currentInvested + console.log('user current invested amount', currentInvested) + console.log('mana limit:', manaLimitPerUser) + + if (currentInvested + amount > manaLimitPerUser) { + const manaAllowed = manaLimitPerUser - currentInvested + return { + status: 'error', + message: `Market bet cap is M$${manaLimitPerUser}, you've M$${manaAllowed} left`, + } + } + } + return { + status: 'success', + message: '', + } +} diff --git a/functions/src/create-answer.ts b/functions/src/create-answer.ts index b628f56e..148e9b7f 100644 --- a/functions/src/create-answer.ts +++ b/functions/src/create-answer.ts @@ -13,7 +13,10 @@ import { Answer, MAX_ANSWER_LENGTH } from '../../common/answer' import { getContract, getValues } from './utils' import { sendNewAnswerEmail } from './emails' import { Bet } from '../../common/bet' -import { getContractBetMetrics } from '../../common/calculate' +import { + getContractBetMetrics, + hasUserHitManaLimit, +} from '../../common/calculate' export const createAnswer = functions.runWith({ minInstances: 1 }).https.onCall( async ( @@ -58,7 +61,7 @@ export const createAnswer = functions.runWith({ minInstances: 1 }).https.onCall( message: 'Requires a free response contract', } - const { closeTime, volume, manaLimitPerUser } = contract + const { closeTime, volume } = contract if (closeTime && Date.now() > closeTime) return { status: 'error', message: 'Trading is closed' } @@ -67,18 +70,13 @@ export const createAnswer = functions.runWith({ minInstances: 1 }).https.onCall( ) const yourBets = yourBetsSnap.docs.map((doc) => doc.data() as Bet) - const contractMetrics = getContractBetMetrics(contract, yourBets) - const currentInvested = contractMetrics.currentInvested - console.log('user current invested amount', currentInvested) - console.log('mana limit:', manaLimitPerUser) + const { status, message } = hasUserHitManaLimit( + contract, + yourBets, + amount + ) + if (status === 'error') return { status, message: message } - if (manaLimitPerUser && currentInvested + amount > manaLimitPerUser) { - const manaAllowed = manaLimitPerUser - currentInvested - return { - status: 'error', - message: `Market bet cap is M$${manaLimitPerUser}, you've M$${manaAllowed} left`, - } - } const [lastAnswer] = await getValues( firestore .collection(`contracts/${contractId}/answers`) diff --git a/functions/src/place-bet.ts b/functions/src/place-bet.ts index 8fff361e..0e493dca 100644 --- a/functions/src/place-bet.ts +++ b/functions/src/place-bet.ts @@ -13,7 +13,10 @@ import { addObjects, removeUndefinedProps } from '../../common/util/object' import { Bet } from '../../common/bet' import { redeemShares } from './redeem-shares' import { Fees } from '../../common/fees' -import { getContractBetMetrics } from '../../common/calculate' +import { + getContractBetMetrics, + hasUserHitManaLimit, +} from '../../common/calculate' export const placeBet = functions.runWith({ minInstances: 1 }).https.onCall( async ( @@ -77,20 +80,12 @@ export const placeBet = functions.runWith({ minInstances: 1 }).https.onCall( if (!answerSnap.exists) return { status: 'error', message: 'Invalid contract' } - if (manaLimitPerUser) { - const contractMetrics = getContractBetMetrics(contract, yourBets) - const currentInvested = contractMetrics.currentInvested - console.log('user current invested amount', currentInvested) - console.log('mana limit:', manaLimitPerUser) - - if (currentInvested + amount > manaLimitPerUser) { - const manaAllowed = manaLimitPerUser - currentInvested - return { - status: 'error', - message: `Market bet cap is M$${manaLimitPerUser}, you've M$${manaAllowed} left`, - } - } - } + const { status, message } = hasUserHitManaLimit( + contract, + yourBets, + amount + ) + if (status === 'error') return { status, message: message } } const newBetDoc = firestore