2021-12-24 21:06:01 +00:00
|
|
|
import * as admin from 'firebase-admin'
|
2022-05-22 08:36:05 +00:00
|
|
|
import { sumBy } from 'lodash'
|
2022-01-10 21:07:57 +00:00
|
|
|
|
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-15 17:39:42 +00:00
|
|
|
import { Bet } from '../../../common/bet'
|
|
|
|
import { Contract } from '../../../common/contract'
|
2021-12-24 21:06:01 +00:00
|
|
|
|
|
|
|
type DocRef = admin.firestore.DocumentReference
|
|
|
|
|
|
|
|
const firestore = admin.firestore()
|
|
|
|
|
|
|
|
async function migrateBet(contractRef: DocRef, bet: Bet) {
|
|
|
|
const { dpmWeight, amount, id } = bet as Bet & { dpmWeight: number }
|
|
|
|
const shares = dpmWeight + amount
|
|
|
|
|
|
|
|
await contractRef.collection('bets').doc(id).update({ shares })
|
|
|
|
}
|
|
|
|
|
2022-06-06 05:50:27 +00:00
|
|
|
async function migrateContract(contractRef: DocRef) {
|
2021-12-24 21:06:01 +00:00
|
|
|
const bets = await contractRef
|
|
|
|
.collection('bets')
|
|
|
|
.get()
|
|
|
|
.then((snap) => snap.docs.map((bet) => bet.data() as Bet))
|
|
|
|
|
|
|
|
const totalShares = {
|
2022-05-22 08:36:05 +00:00
|
|
|
YES: sumBy(bets, (bet) => (bet.outcome === 'YES' ? bet.shares : 0)),
|
|
|
|
NO: sumBy(bets, (bet) => (bet.outcome === 'NO' ? bet.shares : 0)),
|
2021-12-24 21:06:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await contractRef.update({ totalShares })
|
|
|
|
}
|
|
|
|
|
|
|
|
async function migrateContracts() {
|
|
|
|
console.log('Migrating contracts')
|
|
|
|
|
|
|
|
const snapshot = await firestore.collection('contracts').get()
|
|
|
|
const contracts = snapshot.docs.map((doc) => doc.data() as Contract)
|
|
|
|
|
|
|
|
console.log('Loaded contracts', contracts.length)
|
|
|
|
|
|
|
|
for (const contract of contracts) {
|
|
|
|
const contractRef = firestore.doc(`contracts/${contract.id}`)
|
|
|
|
const betsSnapshot = await contractRef.collection('bets').get()
|
|
|
|
const bets = betsSnapshot.docs.map((bet) => bet.data() as Bet)
|
|
|
|
|
|
|
|
console.log('contract', contract.question, 'bets', bets.length)
|
|
|
|
|
|
|
|
for (const bet of bets) await migrateBet(contractRef, bet)
|
2022-06-06 05:50:27 +00:00
|
|
|
await migrateContract(contractRef)
|
2021-12-24 21:06:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (require.main === module) migrateContracts().then(() => process.exit())
|