use correct contract type in scripts / functions

This commit is contained in:
mantikoros 2022-03-03 11:03:20 -05:00
parent 6852314feb
commit 9b10be065e
5 changed files with 68 additions and 43 deletions

View File

@ -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<DPM, FreeResponse>,
loanAmount,
contract,
newBetDoc.id
)

View File

@ -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,

View File

@ -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<Comment>(
firestore.collection('contracts').doc(contractId).collection('comments')

View File

@ -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<DPM, Binary>
) {
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<DPM, Binary>
)
console.log('Loaded contracts', contracts.length)

View File

@ -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<DPM, Binary>
const betDocs = await transaction.get(contractRef.collection('bets'))
const bets = _.sortBy(