From 466ffdf921f27d14f4aa45a7ccaa8fba2ce21260 Mon Sep 17 00:00:00 2001 From: Ian Philips Date: Fri, 6 May 2022 16:10:30 -0400 Subject: [PATCH] Allow M$ limit for markets --- functions/src/create-contract.ts | 13 +++++++++++-- functions/src/place-bet.ts | 22 ++++++++++++++++++++-- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/functions/src/create-contract.ts b/functions/src/create-contract.ts index dfc8128d..322579a5 100644 --- a/functions/src/create-contract.ts +++ b/functions/src/create-contract.ts @@ -39,6 +39,7 @@ export const createContract = functions ante: number closeTime: number tags?: string[] + manaLimitPerUser?: number }, context ) => { @@ -48,7 +49,14 @@ export const createContract = functions const creator = await getUser(userId) if (!creator) return { status: 'error', message: 'User not found' } - let { question, description, initialProb, closeTime, tags } = data + let { + question, + description, + initialProb, + closeTime, + tags, + manaLimitPerUser, + } = data if (!question || typeof question != 'string') return { status: 'error', message: 'Missing or invalid question field' } @@ -115,7 +123,8 @@ export const createContract = functions initialProb, ante, closeTime, - tags ?? [] + tags ?? [], + manaLimitPerUser ?? 0 ) if (!isFree && ante) await chargeUser(creator.id, ante) diff --git a/functions/src/place-bet.ts b/functions/src/place-bet.ts index 74487126..debc6f5b 100644 --- a/functions/src/place-bet.ts +++ b/functions/src/place-bet.ts @@ -13,6 +13,7 @@ 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' export const placeBet = functions.runWith({ minInstances: 1 }).https.onCall( async ( @@ -49,8 +50,14 @@ export const placeBet = functions.runWith({ minInstances: 1 }).https.onCall( return { status: 'error', message: 'Invalid contract' } const contract = contractSnap.data() as Contract - const { closeTime, outcomeType, mechanism, collectedFees, volume } = - contract + const { + closeTime, + outcomeType, + mechanism, + collectedFees, + volume, + manaLimitPerUser, + } = contract if (closeTime && Date.now() > closeTime) return { status: 'error', message: 'Trading is closed' } @@ -69,6 +76,17 @@ export const placeBet = functions.runWith({ minInstances: 1 }).https.onCall( ) if (!answerSnap.exists) return { status: 'error', message: 'Invalid contract' } + + const contractMetrics = getContractBetMetrics(contract, yourBets) + const currentInvested = contractMetrics.currentInvested + console.log('yourSharesAmount', contractMetrics.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 newBetDoc = firestore