2021-12-14 07:02:50 +00:00
|
|
|
import * as functions from 'firebase-functions'
|
|
|
|
import * as admin from 'firebase-admin'
|
|
|
|
import * as _ from 'lodash'
|
|
|
|
|
2022-01-10 21:07:57 +00:00
|
|
|
import { Contract } from '../../common/contract'
|
|
|
|
import { User } from '../../common/user'
|
|
|
|
import { Bet } from '../../common/bet'
|
2022-01-10 22:48:48 +00:00
|
|
|
import { getUser, payUser } from './utils'
|
2022-01-02 00:08:52 +00:00
|
|
|
import { sendMarketResolutionEmail } from './emails'
|
2022-02-20 07:26:26 +00:00
|
|
|
import { getPayouts, getPayoutsMultiOutcome } from '../../common/payouts'
|
2021-12-14 07:02:50 +00:00
|
|
|
|
|
|
|
export const resolveMarket = functions
|
|
|
|
.runWith({ minInstances: 1 })
|
2021-12-17 22:15:09 +00:00
|
|
|
.https.onCall(
|
|
|
|
async (
|
|
|
|
data: {
|
2022-02-20 07:26:26 +00:00
|
|
|
outcome: string
|
2021-12-17 22:15:09 +00:00
|
|
|
contractId: string
|
2022-01-30 21:51:30 +00:00
|
|
|
probabilityInt?: number
|
2022-02-20 07:26:26 +00:00
|
|
|
resolutions?: { [outcome: string]: number }
|
2021-12-17 22:15:09 +00:00
|
|
|
},
|
|
|
|
context
|
|
|
|
) => {
|
|
|
|
const userId = context?.auth?.uid
|
|
|
|
if (!userId) return { status: 'error', message: 'Not authorized' }
|
|
|
|
|
2022-02-20 07:26:26 +00:00
|
|
|
const { outcome, contractId, probabilityInt, resolutions } = data
|
2021-12-17 22:15:09 +00:00
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
const contractDoc = firestore.doc(`contracts/${contractId}`)
|
|
|
|
const contractSnap = await contractDoc.get()
|
|
|
|
if (!contractSnap.exists)
|
|
|
|
return { status: 'error', message: 'Invalid contract' }
|
|
|
|
const contract = contractSnap.data() as Contract
|
|
|
|
const { creatorId, outcomeType } = contract
|
|
|
|
|
|
|
|
if (outcomeType === 'BINARY') {
|
|
|
|
if (!['YES', 'NO', 'MKT', 'CANCEL'].includes(outcome))
|
|
|
|
return { status: 'error', message: 'Invalid outcome' }
|
|
|
|
} else if (outcomeType === 'FREE_RESPONSE') {
|
2022-02-20 07:26:26 +00:00
|
|
|
if (
|
|
|
|
isNaN(+outcome) &&
|
|
|
|
!(outcome === 'MKT' && resolutions) &&
|
|
|
|
outcome !== 'CANCEL'
|
|
|
|
)
|
2022-02-17 23:00:19 +00:00
|
|
|
return { status: 'error', message: 'Invalid outcome' }
|
|
|
|
} else {
|
|
|
|
return { status: 'error', message: 'Invalid contract outcomeType' }
|
|
|
|
}
|
2021-12-17 22:15:09 +00:00
|
|
|
|
2022-01-30 21:51:30 +00:00
|
|
|
if (
|
2022-02-17 23:00:19 +00:00
|
|
|
outcomeType === 'BINARY' &&
|
2022-01-30 21:51:30 +00:00
|
|
|
probabilityInt !== undefined &&
|
2022-02-12 06:19:45 +00:00
|
|
|
(probabilityInt < 0 ||
|
|
|
|
probabilityInt > 100 ||
|
|
|
|
!isFinite(probabilityInt))
|
2022-01-30 21:51:30 +00:00
|
|
|
)
|
|
|
|
return { status: 'error', message: 'Invalid probability' }
|
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
if (creatorId !== userId)
|
2021-12-17 22:15:09 +00:00
|
|
|
return { status: 'error', message: 'User not creator of contract' }
|
|
|
|
|
|
|
|
if (contract.resolution)
|
|
|
|
return { status: 'error', message: 'Contract already resolved' }
|
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
const creator = await getUser(creatorId)
|
2022-01-02 00:08:52 +00:00
|
|
|
if (!creator) return { status: 'error', message: 'Creator not found' }
|
|
|
|
|
2022-01-30 21:51:30 +00:00
|
|
|
const resolutionProbability =
|
|
|
|
probabilityInt !== undefined ? probabilityInt / 100 : undefined
|
|
|
|
|
2021-12-17 22:15:09 +00:00
|
|
|
await contractDoc.update({
|
|
|
|
isResolved: true,
|
|
|
|
resolution: outcome,
|
|
|
|
resolutionTime: Date.now(),
|
2022-01-30 21:51:30 +00:00
|
|
|
...(resolutionProbability === undefined
|
|
|
|
? {}
|
|
|
|
: { resolutionProbability }),
|
2022-02-20 07:26:26 +00:00
|
|
|
...(resolutions === undefined ? {} : { resolutions }),
|
2021-12-17 22:15:09 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
console.log('contract ', contractId, 'resolved to:', outcome)
|
|
|
|
|
|
|
|
const betsSnap = await firestore
|
|
|
|
.collection(`contracts/${contractId}/bets`)
|
|
|
|
.get()
|
2022-01-02 01:03:18 +00:00
|
|
|
|
2021-12-17 22:15:09 +00:00
|
|
|
const bets = betsSnap.docs.map((doc) => doc.data() as Bet)
|
2022-01-02 01:03:18 +00:00
|
|
|
const openBets = bets.filter((b) => !b.isSold && !b.sale)
|
|
|
|
|
2022-02-20 07:26:26 +00:00
|
|
|
const payouts =
|
|
|
|
outcomeType === 'FREE_RESPONSE' && resolutions
|
|
|
|
? getPayoutsMultiOutcome(resolutions, contract, openBets)
|
|
|
|
: getPayouts(outcome, contract, openBets, resolutionProbability)
|
2021-12-17 22:15:09 +00:00
|
|
|
|
|
|
|
console.log('payouts:', payouts)
|
|
|
|
|
|
|
|
const groups = _.groupBy(payouts, (payout) => payout.userId)
|
|
|
|
const userPayouts = _.mapValues(groups, (group) =>
|
|
|
|
_.sumBy(group, (g) => g.payout)
|
|
|
|
)
|
|
|
|
|
2022-01-10 22:48:48 +00:00
|
|
|
const payoutPromises = Object.entries(userPayouts).map(
|
|
|
|
([userId, payout]) => payUser(userId, payout)
|
|
|
|
)
|
2021-12-17 22:15:09 +00:00
|
|
|
|
2022-01-02 00:08:52 +00:00
|
|
|
const result = await Promise.all(payoutPromises)
|
2021-12-17 22:15:09 +00:00
|
|
|
.catch((e) => ({ status: 'error', message: e }))
|
|
|
|
.then(() => ({ status: 'success' }))
|
2022-01-02 00:08:52 +00:00
|
|
|
|
2022-01-02 01:13:30 +00:00
|
|
|
await sendResolutionEmails(
|
|
|
|
openBets,
|
|
|
|
userPayouts,
|
|
|
|
creator,
|
|
|
|
contract,
|
2022-01-30 21:51:30 +00:00
|
|
|
outcome,
|
2022-02-20 07:26:26 +00:00
|
|
|
resolutionProbability,
|
|
|
|
resolutions
|
2022-01-02 00:08:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return result
|
2021-12-17 22:15:09 +00:00
|
|
|
}
|
|
|
|
)
|
2021-12-14 07:02:50 +00:00
|
|
|
|
2022-01-02 01:13:30 +00:00
|
|
|
const sendResolutionEmails = async (
|
|
|
|
openBets: Bet[],
|
|
|
|
userPayouts: { [userId: string]: number },
|
|
|
|
creator: User,
|
|
|
|
contract: Contract,
|
2022-02-20 07:26:26 +00:00
|
|
|
outcome: string,
|
|
|
|
resolutionProbability?: number,
|
|
|
|
resolutions?: { [outcome: string]: number }
|
2022-01-02 01:13:30 +00:00
|
|
|
) => {
|
|
|
|
const nonWinners = _.difference(
|
|
|
|
_.uniq(openBets.map(({ userId }) => userId)),
|
|
|
|
Object.keys(userPayouts)
|
|
|
|
)
|
2022-02-24 07:04:24 +00:00
|
|
|
const investedByUser = _.mapValues(
|
|
|
|
_.groupBy(openBets, (bet) => bet.userId),
|
|
|
|
(bets) => _.sumBy(bets, (bet) => bet.amount)
|
|
|
|
)
|
2022-01-02 01:13:30 +00:00
|
|
|
const emailPayouts = [
|
|
|
|
...Object.entries(userPayouts),
|
|
|
|
...nonWinners.map((userId) => [userId, 0] as const),
|
2022-02-24 07:04:24 +00:00
|
|
|
].map(([userId, payout]) => ({
|
|
|
|
userId,
|
|
|
|
investment: investedByUser[userId],
|
|
|
|
payout,
|
|
|
|
}))
|
|
|
|
|
2022-01-02 01:13:30 +00:00
|
|
|
await Promise.all(
|
2022-02-24 07:04:24 +00:00
|
|
|
emailPayouts.map(({ userId, investment, payout }) =>
|
2022-01-30 21:51:30 +00:00
|
|
|
sendMarketResolutionEmail(
|
|
|
|
userId,
|
2022-02-24 07:04:24 +00:00
|
|
|
investment,
|
2022-01-30 21:51:30 +00:00
|
|
|
payout,
|
|
|
|
creator,
|
|
|
|
contract,
|
|
|
|
outcome,
|
2022-02-20 07:26:26 +00:00
|
|
|
resolutionProbability,
|
|
|
|
resolutions
|
2022-01-30 21:51:30 +00:00
|
|
|
)
|
2022-01-02 01:13:30 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-14 07:02:50 +00:00
|
|
|
const firestore = admin.firestore()
|