Consolidate logic
This commit is contained in:
parent
b10e0b31e2
commit
2edf31e6e4
|
@ -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: '',
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<Answer>(
|
||||
firestore
|
||||
.collection(`contracts/${contractId}/answers`)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user