From 7fe599c8f9013dd7e17814796ada283df4819065 Mon Sep 17 00:00:00 2001 From: Ian Philips Date: Fri, 6 May 2022 16:25:40 -0400 Subject: [PATCH] Apply M$ limit to FR answer bets --- functions/src/create-answer.ts | 13 ++++++++++++- functions/src/place-bet.ts | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/functions/src/create-answer.ts b/functions/src/create-answer.ts index 1da8f350..82c19bf1 100644 --- a/functions/src/create-answer.ts +++ b/functions/src/create-answer.ts @@ -13,6 +13,7 @@ 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' export const createAnswer = functions.runWith({ minInstances: 1 }).https.onCall( async ( @@ -57,7 +58,7 @@ export const createAnswer = functions.runWith({ minInstances: 1 }).https.onCall( message: 'Requires a free response contract', } - const { closeTime, volume } = contract + const { closeTime, volume, manaLimitPerUser } = contract if (closeTime && Date.now() > closeTime) return { status: 'error', message: 'Trading is closed' } @@ -66,6 +67,16 @@ 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) + + if (manaLimitPerUser && currentInvested + amount > manaLimitPerUser) + return { + status: 'error', + message: `Market investment limit is M$${manaLimitPerUser}, you've M$${currentInvested} already`, + } const [lastAnswer] = await getValues( firestore .collection(`contracts/${contractId}/answers`) diff --git a/functions/src/place-bet.ts b/functions/src/place-bet.ts index debc6f5b..e7bfe528 100644 --- a/functions/src/place-bet.ts +++ b/functions/src/place-bet.ts @@ -79,7 +79,7 @@ export const placeBet = functions.runWith({ minInstances: 1 }).https.onCall( const contractMetrics = getContractBetMetrics(contract, yourBets) const currentInvested = contractMetrics.currentInvested - console.log('yourSharesAmount', contractMetrics.currentInvested) + console.log('user current invested amount', currentInvested) console.log('mana limit:', manaLimitPerUser) if (manaLimitPerUser && currentInvested + amount > manaLimitPerUser)