use correct contract type in scripts / functions
This commit is contained in:
		
							parent
							
								
									6852314feb
								
							
						
					
					
						commit
						9b10be065e
					
				|  | @ -1,7 +1,12 @@ | ||||||
| import * as functions from 'firebase-functions' | import * as functions from 'firebase-functions' | ||||||
| import * as admin from 'firebase-admin' | 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 { User } from '../../common/user' | ||||||
| import { getLoanAmount, getNewMultiBetInfo } from '../../common/new-bet' | import { getLoanAmount, getNewMultiBetInfo } from '../../common/new-bet' | ||||||
| import { Answer } from '../../common/answer' | import { Answer } from '../../common/answer' | ||||||
|  | @ -105,8 +110,8 @@ export const createAnswer = functions.runWith({ minInstances: 1 }).https.onCall( | ||||||
|           user, |           user, | ||||||
|           answerId, |           answerId, | ||||||
|           amount, |           amount, | ||||||
|  |           contract as FullContract<DPM, FreeResponse>, | ||||||
|           loanAmount, |           loanAmount, | ||||||
|           contract, |  | ||||||
|           newBetDoc.id |           newBetDoc.id | ||||||
|         ) |         ) | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -1,7 +1,8 @@ | ||||||
| import _ = require('lodash') | import * as _ from 'lodash' | ||||||
|  | 
 | ||||||
| import { Answer } from '../../common/answer' | import { Answer } from '../../common/answer' | ||||||
| import { Bet } from '../../common/bet' | import { Bet } from '../../common/bet' | ||||||
| import { getDpmProbability } from '../../common/calculate-dpm' | import { getProbability } from '../../common/calculate' | ||||||
| import { Comment } from '../../common/comment' | import { Comment } from '../../common/comment' | ||||||
| import { Contract } from '../../common/contract' | import { Contract } from '../../common/contract' | ||||||
| import { CREATOR_FEE } from '../../common/fees' | import { CREATOR_FEE } from '../../common/fees' | ||||||
|  | @ -10,34 +11,6 @@ import { formatMoney, formatPercent } from '../../common/util/format' | ||||||
| import { sendTemplateEmail, sendTextEmail } from './send-email' | import { sendTemplateEmail, sendTextEmail } from './send-email' | ||||||
| import { getPrivateUser, getUser, isProd } from './utils' | 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 ( | export const sendMarketResolutionEmail = async ( | ||||||
|   userId: string, |   userId: string, | ||||||
|   investment: number, |   investment: number, | ||||||
|  | @ -59,9 +32,12 @@ export const sendMarketResolutionEmail = async ( | ||||||
|   const user = await getUser(userId) |   const user = await getUser(userId) | ||||||
|   if (!user) return |   if (!user) return | ||||||
| 
 | 
 | ||||||
|   const prob = resolutionProbability ?? getDpmProbability(contract.totalShares) |   const outcome = toDisplayResolution( | ||||||
| 
 |     contract, | ||||||
|   const outcome = toDisplayResolution(resolution, prob, resolutions) |     resolution, | ||||||
|  |     resolutionProbability, | ||||||
|  |     resolutions | ||||||
|  |   ) | ||||||
| 
 | 
 | ||||||
|   const subject = `Resolved ${outcome}: ${contract.question}` |   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 ( | export const sendWelcomeEmail = async ( | ||||||
|   user: User, |   user: User, | ||||||
|   privateUser: PrivateUser |   privateUser: PrivateUser | ||||||
|  | @ -198,7 +209,10 @@ export const sendNewCommentEmail = async ( | ||||||
|       { from } |       { from } | ||||||
|     ) |     ) | ||||||
|   } else { |   } else { | ||||||
|     betDescription = `${betDescription} of ${toDisplayResolution(outcome)}` |     betDescription = `${betDescription} of ${toDisplayResolution( | ||||||
|  |       contract, | ||||||
|  |       outcome | ||||||
|  |     )}` | ||||||
| 
 | 
 | ||||||
|     await sendTemplateEmail( |     await sendTemplateEmail( | ||||||
|       privateUser.email, |       privateUser.email, | ||||||
|  |  | ||||||
|  | @ -33,8 +33,9 @@ export const onCreateComment = functions.firestore | ||||||
|     const bet = betSnapshot.data() as Bet |     const bet = betSnapshot.data() as Bet | ||||||
| 
 | 
 | ||||||
|     const answer = |     const answer = | ||||||
|       contract.answers && |       contract.outcomeType === 'FREE_RESPONSE' && contract.answers | ||||||
|       contract.answers.find((answer) => answer.id === bet.outcome) |         ? contract.answers.find((answer) => answer.id === bet.outcome) | ||||||
|  |         : undefined | ||||||
| 
 | 
 | ||||||
|     const comments = await getValues<Comment>( |     const comments = await getValues<Comment>( | ||||||
|       firestore.collection('contracts').doc(contractId).collection('comments') |       firestore.collection('contracts').doc(contractId).collection('comments') | ||||||
|  |  | ||||||
|  | @ -6,12 +6,15 @@ initAdmin('stephen') | ||||||
| 
 | 
 | ||||||
| import { Bet } from '../../../common/bet' | import { Bet } from '../../../common/bet' | ||||||
| import { getDpmProbability } from '../../../common/calculate-dpm' | import { getDpmProbability } from '../../../common/calculate-dpm' | ||||||
| import { Contract } from '../../../common/contract' | import { Binary, Contract, DPM, FullContract } from '../../../common/contract' | ||||||
| 
 | 
 | ||||||
| type DocRef = admin.firestore.DocumentReference | type DocRef = admin.firestore.DocumentReference | ||||||
| const firestore = admin.firestore() | const firestore = admin.firestore() | ||||||
| 
 | 
 | ||||||
| async function migrateContract(contractRef: DocRef, contract: Contract) { | async function migrateContract( | ||||||
|  |   contractRef: DocRef, | ||||||
|  |   contract: FullContract<DPM, Binary> | ||||||
|  | ) { | ||||||
|   const bets = await contractRef |   const bets = await contractRef | ||||||
|     .collection('bets') |     .collection('bets') | ||||||
|     .get() |     .get() | ||||||
|  | @ -31,7 +34,9 @@ async function migrateContract(contractRef: DocRef, contract: Contract) { | ||||||
| 
 | 
 | ||||||
| async function migrateContracts() { | async function migrateContracts() { | ||||||
|   const snapshot = await firestore.collection('contracts').get() |   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) |   console.log('Loaded contracts', contracts.length) | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -4,7 +4,7 @@ import * as _ from 'lodash' | ||||||
| import { initAdmin } from './script-init' | import { initAdmin } from './script-init' | ||||||
| initAdmin('stephenDev') | initAdmin('stephenDev') | ||||||
| 
 | 
 | ||||||
| import { Contract } from '../../../common/contract' | import { Binary, Contract, DPM, FullContract } from '../../../common/contract' | ||||||
| import { Bet } from '../../../common/bet' | import { Bet } from '../../../common/bet' | ||||||
| import { | import { | ||||||
|   calculateDpmShares, |   calculateDpmShares, | ||||||
|  | @ -32,7 +32,7 @@ async function recalculateContract( | ||||||
| 
 | 
 | ||||||
|   await firestore.runTransaction(async (transaction) => { |   await firestore.runTransaction(async (transaction) => { | ||||||
|     const contractDoc = await transaction.get(contractRef) |     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 betDocs = await transaction.get(contractRef.collection('bets')) | ||||||
|     const bets = _.sortBy( |     const bets = _.sortBy( | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user