diff --git a/functions/src/create-answer.ts b/functions/src/create-answer.ts index f192fc7e..0df8c0df 100644 --- a/functions/src/create-answer.ts +++ b/functions/src/create-answer.ts @@ -1,7 +1,12 @@ import * as functions from 'firebase-functions' import * as admin from 'firebase-admin' -import { Contract } from '../../common/contract' +import { + Contract, + DPM, + FreeResponse, + FullContract, +} from '../../common/contract' import { User } from '../../common/user' import { getLoanAmount, getNewMultiBetInfo } from '../../common/new-bet' import { Answer } from '../../common/answer' @@ -105,8 +110,8 @@ export const createAnswer = functions.runWith({ minInstances: 1 }).https.onCall( user, answerId, amount, + contract as FullContract, loanAmount, - contract, newBetDoc.id ) diff --git a/functions/src/emails.ts b/functions/src/emails.ts index c239ab0f..12dda2b5 100644 --- a/functions/src/emails.ts +++ b/functions/src/emails.ts @@ -1,7 +1,8 @@ -import _ = require('lodash') +import * as _ from 'lodash' + import { Answer } from '../../common/answer' import { Bet } from '../../common/bet' -import { getDpmProbability } from '../../common/calculate-dpm' +import { getProbability } from '../../common/calculate' import { Comment } from '../../common/comment' import { Contract } from '../../common/contract' import { CREATOR_FEE } from '../../common/fees' @@ -10,34 +11,6 @@ import { formatMoney, formatPercent } from '../../common/util/format' import { sendTemplateEmail, sendTextEmail } from './send-email' import { getPrivateUser, getUser, isProd } from './utils' -type market_resolved_template = { - userId: string - name: string - creatorName: string - question: string - outcome: string - investment: string - payout: string - url: string -} - -const toDisplayResolution = ( - outcome: string, - prob?: number, - resolutions?: { [outcome: string]: number } -) => { - if (outcome === 'MKT' && resolutions) return 'MULTI' - - const display = { - YES: 'YES', - NO: 'NO', - CANCEL: 'N/A', - MKT: formatPercent(prob ?? 0), - }[outcome] - - return display === undefined ? `#${outcome}` : display -} - export const sendMarketResolutionEmail = async ( userId: string, investment: number, @@ -59,9 +32,12 @@ export const sendMarketResolutionEmail = async ( const user = await getUser(userId) if (!user) return - const prob = resolutionProbability ?? getDpmProbability(contract.totalShares) - - const outcome = toDisplayResolution(resolution, prob, resolutions) + const outcome = toDisplayResolution( + contract, + resolution, + resolutionProbability, + resolutions + ) const subject = `Resolved ${outcome}: ${contract.question}` @@ -88,6 +64,41 @@ export const sendMarketResolutionEmail = async ( ) } +type market_resolved_template = { + userId: string + name: string + creatorName: string + question: string + outcome: string + investment: string + payout: string + url: string +} + +const toDisplayResolution = ( + contract: Contract, + resolution: string, + resolutionProbability?: number, + resolutions?: { [outcome: string]: number } +) => { + if (contract.outcomeType === 'BINARY') { + const prob = resolutionProbability ?? getProbability(contract) + + const display = { + YES: 'YES', + NO: 'NO', + CANCEL: 'N/A', + MKT: formatPercent(prob ?? 0), + }[resolution] + + return display || resolution + } + + if (resolution === 'MKT' && resolutions) return 'MULTI' + + return `#${resolution}` +} + export const sendWelcomeEmail = async ( user: User, privateUser: PrivateUser @@ -198,7 +209,10 @@ export const sendNewCommentEmail = async ( { from } ) } else { - betDescription = `${betDescription} of ${toDisplayResolution(outcome)}` + betDescription = `${betDescription} of ${toDisplayResolution( + contract, + outcome + )}` await sendTemplateEmail( privateUser.email, diff --git a/functions/src/on-create-comment.ts b/functions/src/on-create-comment.ts index 85af37ee..e48d6039 100644 --- a/functions/src/on-create-comment.ts +++ b/functions/src/on-create-comment.ts @@ -33,8 +33,9 @@ export const onCreateComment = functions.firestore const bet = betSnapshot.data() as Bet const answer = - contract.answers && - contract.answers.find((answer) => answer.id === bet.outcome) + contract.outcomeType === 'FREE_RESPONSE' && contract.answers + ? contract.answers.find((answer) => answer.id === bet.outcome) + : undefined const comments = await getValues( firestore.collection('contracts').doc(contractId).collection('comments') diff --git a/functions/src/scripts/correct-bet-probability.ts b/functions/src/scripts/correct-bet-probability.ts index e6f1939b..30be7a1c 100644 --- a/functions/src/scripts/correct-bet-probability.ts +++ b/functions/src/scripts/correct-bet-probability.ts @@ -6,12 +6,15 @@ initAdmin('stephen') import { Bet } from '../../../common/bet' import { getDpmProbability } from '../../../common/calculate-dpm' -import { Contract } from '../../../common/contract' +import { Binary, Contract, DPM, FullContract } from '../../../common/contract' type DocRef = admin.firestore.DocumentReference const firestore = admin.firestore() -async function migrateContract(contractRef: DocRef, contract: Contract) { +async function migrateContract( + contractRef: DocRef, + contract: FullContract +) { const bets = await contractRef .collection('bets') .get() @@ -31,7 +34,9 @@ async function migrateContract(contractRef: DocRef, contract: Contract) { async function migrateContracts() { const snapshot = await firestore.collection('contracts').get() - const contracts = snapshot.docs.map((doc) => doc.data() as Contract) + const contracts = snapshot.docs.map( + (doc) => doc.data() as FullContract + ) console.log('Loaded contracts', contracts.length) diff --git a/functions/src/scripts/migrate-to-dpm-2.ts b/functions/src/scripts/migrate-to-dpm-2.ts index a2235f10..a7e611b6 100644 --- a/functions/src/scripts/migrate-to-dpm-2.ts +++ b/functions/src/scripts/migrate-to-dpm-2.ts @@ -4,7 +4,7 @@ import * as _ from 'lodash' import { initAdmin } from './script-init' initAdmin('stephenDev') -import { Contract } from '../../../common/contract' +import { Binary, Contract, DPM, FullContract } from '../../../common/contract' import { Bet } from '../../../common/bet' import { calculateDpmShares, @@ -32,7 +32,7 @@ async function recalculateContract( await firestore.runTransaction(async (transaction) => { const contractDoc = await transaction.get(contractRef) - const contract = contractDoc.data() as Contract + const contract = contractDoc.data() as FullContract const betDocs = await transaction.get(contractRef.collection('bets')) const bets = _.sortBy(