2022-01-12 21:21:03 +00:00
|
|
|
import * as admin from 'firebase-admin'
|
|
|
|
import * as _ from 'lodash'
|
|
|
|
|
2022-01-30 21:11:01 +00:00
|
|
|
import { initAdmin } from './script-init'
|
2022-04-20 04:31:46 +00:00
|
|
|
initAdmin()
|
2022-01-30 21:11:01 +00:00
|
|
|
|
2022-05-10 20:59:38 +00:00
|
|
|
import { Bet } from 'common/bet'
|
|
|
|
import { getDpmProbability } from 'common/calculate-dpm'
|
|
|
|
import { Binary, Contract, DPM, FullContract } from 'common/contract'
|
2022-01-12 21:21:03 +00:00
|
|
|
|
|
|
|
type DocRef = admin.firestore.DocumentReference
|
|
|
|
const firestore = admin.firestore()
|
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
async function migrateContract(
|
|
|
|
contractRef: DocRef,
|
|
|
|
contract: FullContract<DPM, Binary>
|
|
|
|
) {
|
2022-01-12 21:21:03 +00:00
|
|
|
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) {
|
2022-03-15 22:27:51 +00:00
|
|
|
const probAfter = getDpmProbability(contract.totalShares)
|
2022-01-12 21:21:03 +00:00
|
|
|
|
|
|
|
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()
|
2022-03-15 22:27:51 +00:00
|
|
|
const contracts = snapshot.docs.map(
|
|
|
|
(doc) => doc.data() as FullContract<DPM, Binary>
|
|
|
|
)
|
2022-01-12 21:21:03 +00:00
|
|
|
|
|
|
|
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())
|