Allow M$ limit for markets

This commit is contained in:
Ian Philips 2022-05-06 16:10:30 -04:00
parent d2b4277c43
commit 466ffdf921
2 changed files with 31 additions and 4 deletions

View File

@ -39,6 +39,7 @@ export const createContract = functions
ante: number ante: number
closeTime: number closeTime: number
tags?: string[] tags?: string[]
manaLimitPerUser?: number
}, },
context context
) => { ) => {
@ -48,7 +49,14 @@ export const createContract = functions
const creator = await getUser(userId) const creator = await getUser(userId)
if (!creator) return { status: 'error', message: 'User not found' } 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') if (!question || typeof question != 'string')
return { status: 'error', message: 'Missing or invalid question field' } return { status: 'error', message: 'Missing or invalid question field' }
@ -115,7 +123,8 @@ export const createContract = functions
initialProb, initialProb,
ante, ante,
closeTime, closeTime,
tags ?? [] tags ?? [],
manaLimitPerUser ?? 0
) )
if (!isFree && ante) await chargeUser(creator.id, ante) if (!isFree && ante) await chargeUser(creator.id, ante)

View File

@ -13,6 +13,7 @@ import { addObjects, removeUndefinedProps } from '../../common/util/object'
import { Bet } from '../../common/bet' import { Bet } from '../../common/bet'
import { redeemShares } from './redeem-shares' import { redeemShares } from './redeem-shares'
import { Fees } from '../../common/fees' import { Fees } from '../../common/fees'
import { getContractBetMetrics } from '../../common/calculate'
export const placeBet = functions.runWith({ minInstances: 1 }).https.onCall( export const placeBet = functions.runWith({ minInstances: 1 }).https.onCall(
async ( async (
@ -49,8 +50,14 @@ export const placeBet = functions.runWith({ minInstances: 1 }).https.onCall(
return { status: 'error', message: 'Invalid contract' } return { status: 'error', message: 'Invalid contract' }
const contract = contractSnap.data() as Contract const contract = contractSnap.data() as Contract
const { closeTime, outcomeType, mechanism, collectedFees, volume } = const {
contract closeTime,
outcomeType,
mechanism,
collectedFees,
volume,
manaLimitPerUser,
} = contract
if (closeTime && Date.now() > closeTime) if (closeTime && Date.now() > closeTime)
return { status: 'error', message: 'Trading is closed' } return { status: 'error', message: 'Trading is closed' }
@ -69,6 +76,17 @@ export const placeBet = functions.runWith({ minInstances: 1 }).https.onCall(
) )
if (!answerSnap.exists) if (!answerSnap.exists)
return { status: 'error', message: 'Invalid contract' } 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 const newBetDoc = firestore