Consolidate logic
This commit is contained in:
parent
b10e0b31e2
commit
2edf31e6e4
|
@ -190,3 +190,29 @@ export function getTopAnswer(contract: FreeResponseContract) {
|
||||||
)
|
)
|
||||||
return top?.answer
|
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 { getContract, getValues } from './utils'
|
||||||
import { sendNewAnswerEmail } from './emails'
|
import { sendNewAnswerEmail } from './emails'
|
||||||
import { Bet } from '../../common/bet'
|
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(
|
export const createAnswer = functions.runWith({ minInstances: 1 }).https.onCall(
|
||||||
async (
|
async (
|
||||||
|
@ -58,7 +61,7 @@ export const createAnswer = functions.runWith({ minInstances: 1 }).https.onCall(
|
||||||
message: 'Requires a free response contract',
|
message: 'Requires a free response contract',
|
||||||
}
|
}
|
||||||
|
|
||||||
const { closeTime, volume, manaLimitPerUser } = contract
|
const { closeTime, volume } = 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' }
|
||||||
|
|
||||||
|
@ -67,18 +70,13 @@ export const createAnswer = functions.runWith({ minInstances: 1 }).https.onCall(
|
||||||
)
|
)
|
||||||
const yourBets = yourBetsSnap.docs.map((doc) => doc.data() as Bet)
|
const yourBets = yourBetsSnap.docs.map((doc) => doc.data() as Bet)
|
||||||
|
|
||||||
const contractMetrics = getContractBetMetrics(contract, yourBets)
|
const { status, message } = hasUserHitManaLimit(
|
||||||
const currentInvested = contractMetrics.currentInvested
|
contract,
|
||||||
console.log('user current invested amount', currentInvested)
|
yourBets,
|
||||||
console.log('mana limit:', manaLimitPerUser)
|
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>(
|
const [lastAnswer] = await getValues<Answer>(
|
||||||
firestore
|
firestore
|
||||||
.collection(`contracts/${contractId}/answers`)
|
.collection(`contracts/${contractId}/answers`)
|
||||||
|
|
|
@ -13,7 +13,10 @@ 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'
|
import {
|
||||||
|
getContractBetMetrics,
|
||||||
|
hasUserHitManaLimit,
|
||||||
|
} from '../../common/calculate'
|
||||||
|
|
||||||
export const placeBet = functions.runWith({ minInstances: 1 }).https.onCall(
|
export const placeBet = functions.runWith({ minInstances: 1 }).https.onCall(
|
||||||
async (
|
async (
|
||||||
|
@ -77,20 +80,12 @@ 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' }
|
||||||
|
|
||||||
if (manaLimitPerUser) {
|
const { status, message } = hasUserHitManaLimit(
|
||||||
const contractMetrics = getContractBetMetrics(contract, yourBets)
|
contract,
|
||||||
const currentInvested = contractMetrics.currentInvested
|
yourBets,
|
||||||
console.log('user current invested amount', currentInvested)
|
amount
|
||||||
console.log('mana limit:', manaLimitPerUser)
|
)
|
||||||
|
if (status === 'error') return { status, message: message }
|
||||||
if (currentInvested + amount > manaLimitPerUser) {
|
|
||||||
const manaAllowed = manaLimitPerUser - currentInvested
|
|
||||||
return {
|
|
||||||
status: 'error',
|
|
||||||
message: `Market bet cap is M$${manaLimitPerUser}, you've M$${manaAllowed} left`,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const newBetDoc = firestore
|
const newBetDoc = firestore
|
||||||
|
|
Loading…
Reference in New Issue
Block a user