manifold/functions/src/scripts/correct-bet-probability.ts
Marshall Polaris 7c4ec2a8e3
Refactor contract typing to be more concise and more correct (#355)
* Refactor contract types slightly

* Refactor contract types greatly

* Kill dead binary DPM contract creation code

* Use BinaryContract, DPMContract, etc. type aliases
2022-05-31 19:42:35 -07:00

51 lines
1.4 KiB
TypeScript

import * as admin from 'firebase-admin'
import { sortBy } from 'lodash'
import { initAdmin } from './script-init'
initAdmin()
import { Bet } from '../../../common/bet'
import { getDpmProbability } from '../../../common/calculate-dpm'
import { DPMBinaryContract } from '../../../common/contract'
type DocRef = admin.firestore.DocumentReference
const firestore = admin.firestore()
async function migrateContract(
contractRef: DocRef,
contract: DPMBinaryContract
) {
const bets = await contractRef
.collection('bets')
.get()
.then((snap) => snap.docs.map((bet) => bet.data() as Bet))
const lastBet = sortBy(bets, (bet) => -bet.createdTime)[0]
if (lastBet) {
const probAfter = getDpmProbability(contract.totalShares)
await firestore
.doc(`contracts/${contract.id}/bets/${lastBet.id}`)
.update({ probAfter })
console.log('updating last bet from', lastBet.probAfter, 'to', probAfter)
}
}
async function migrateContracts() {
const snapshot = await firestore.collection('contracts').get()
const contracts = snapshot.docs.map((doc) => doc.data() as DPMBinaryContract)
console.log('Loaded contracts', contracts.length)
for (const contract of contracts) {
const contractRef = firestore.doc(`contracts/${contract.id}`)
console.log('contract', contract.question)
await migrateContract(contractRef, contract)
}
}
if (require.main === module) migrateContracts().then(() => process.exit())