migration script

This commit is contained in:
mantikoros 2022-01-13 12:49:47 -06:00
parent 411a739004
commit 58fbc40276

View File

@ -1,9 +1,9 @@
import * as admin from 'firebase-admin' import * as admin from 'firebase-admin'
import * as _ from 'lodash' import * as _ from 'lodash'
import { Contract } from '../../../common/contract'
import { Bet } from '../../../common/bet' import { Bet } from '../../../common/bet'
import { calculateShares, getProbability } from '../../../common/calculate' import { calculateShares, getProbability } from '../../../common/calculate'
import { Contract } from '../../../common/contract'
import { getSellBetInfo } from '../../../common/sell-bet' import { getSellBetInfo } from '../../../common/sell-bet'
import { User } from '../../../common/user' import { User } from '../../../common/user'
@ -11,8 +11,8 @@ type DocRef = admin.firestore.DocumentReference
// Generate your own private key, and set the path below: // Generate your own private key, and set the path below:
// https://console.firebase.google.com/u/0/project/mantic-markets/settings/serviceaccounts/adminsdk // https://console.firebase.google.com/u/0/project/mantic-markets/settings/serviceaccounts/adminsdk
const serviceAccount = require('../../../../../../Downloads/dev-mantic-markets-firebase-adminsdk-sir5m-b2d27f8970.json') // const serviceAccount = require('../../../../../../Downloads/dev-mantic-markets-firebase-adminsdk-sir5m-b2d27f8970.json')
// const serviceAccount = require('../../../../../../Downloads/mantic-markets-firebase-adminsdk-1ep46-351a65eca3.json') const serviceAccount = require('../../../../../../Downloads/mantic-markets-firebase-adminsdk-1ep46-351a65eca3.json')
admin.initializeApp({ admin.initializeApp({
credential: admin.credential.cert(serviceAccount), credential: admin.credential.cert(serviceAccount),
@ -26,6 +26,8 @@ async function recalculateContract(contractRef: DocRef, contract: Contract) {
if (!startPool) return if (!startPool) return
console.log('recalculating', contract.slug)
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 Contract
@ -38,28 +40,39 @@ async function recalculateContract(contractRef: DocRef, contract: Contract) {
const phantomAnte = startPool.YES + startPool.NO const phantomAnte = startPool.YES + startPool.NO
// const s = ({ YES, NO }: any) => YES + NO const leftovers =
// const leftovers = _.sumBy(bets, (b) => b.amount) - s((contract as any).oldTotalBets || contract.totalBets) _.sumBy(bets, (b) => b.amount) -
// const poolTotal = s((contract as any).oldPool || contract.pool) _.sumBy(bets, (b) => {
if (!b.sale) return b.amount
const realAnte = 0 // poolTotal - leftovers const soldBet = bets.find((bet) => bet.id === b.sale?.betId)
return soldBet?.amount || 0
// console.log(
// 'pool',
// poolTotal,
// 'phantomAnte',
// phantomAnte,
// 'realAnte',
// realAnte,
// 'leftovers',
// leftovers
// )
if (!(contract as any).oldTotalBets)
transaction.update(contractRef, {
oldTotalBets: contract.totalBets,
oldPool: contract.pool,
}) })
const poolTotal = contract.pool.YES + contract.pool.NO
const prevTotalBets = contract.totalBets.YES + contract.totalBets.NO
const calculatedrealAnte = poolTotal - prevTotalBets - leftovers
const realAnte = Math.max(
0,
(contract as any).realAnte || calculatedrealAnte
)
if (!(contract as any).realAnte)
transaction.update(contractRef, {
realAnte,
})
console.log(
'pool',
poolTotal,
'phantomAnte',
phantomAnte,
'realAnte',
realAnte,
'calculatedRealAnte',
calculatedrealAnte,
'leftovers',
leftovers
)
let p = startPool.YES ** 2 / (startPool.YES ** 2 + startPool.NO ** 2) let p = startPool.YES ** 2 / (startPool.YES ** 2 + startPool.NO ** 2)
@ -77,41 +90,6 @@ async function recalculateContract(contractRef: DocRef, contract: Contract) {
let totalBets = { YES: p * realAnte, NO: (1 - p) * realAnte } let totalBets = { YES: p * realAnte, NO: (1 - p) * realAnte }
// const yesBetRef = firestore
// .collection(`contracts/${contract.id}/bets`)
// .doc('auto-yes-ante-' + contract.id)
// const noBetRef = firestore
// .collection(`contracts/${contract.id}/bets`)
// .doc('auto-no-ante-' + contract.id)
// const yesBet: Bet = {
// id: yesBetRef.id,
// userId: contract.creatorId,
// contractId: contract.id,
// amount: p * realAnte,
// shares: Math.sqrt(p) * realAnte,
// outcome: 'YES',
// probBefore: p,
// probAfter: p,
// createdTime: contract.createdTime,
// }
// const noBet: Bet = {
// id: noBetRef.id,
// userId: contract.creatorId,
// contractId: contract.id,
// amount: (1 - p) * realAnte,
// shares: Math.sqrt(1 - p) * realAnte,
// outcome: 'NO',
// probBefore: p,
// probAfter: p,
// createdTime: contract.createdTime,
// }
// transaction.set(yesBetRef, yesBet)
// transaction.set(noBetRef, noBet)
const betsRef = contractRef.collection('bets') const betsRef = contractRef.collection('bets')
console.log('start', { pool, totalBets, totalShares }) console.log('start', { pool, totalBets, totalShares })
@ -135,6 +113,7 @@ async function recalculateContract(contractRef: DocRef, contract: Contract) {
getSellBetInfo(fakeUser, soldBet, fakeContract, bet.id) getSellBetInfo(fakeUser, soldBet, fakeContract, bet.id)
newBet.createdTime = bet.createdTime newBet.createdTime = bet.createdTime
// console.log('sale bet', newBet)
transaction.update(betsRef.doc(bet.id), newBet) transaction.update(betsRef.doc(bet.id), newBet)
pool = newPool pool = newPool
@ -173,7 +152,8 @@ async function recalculateContract(contractRef: DocRef, contract: Contract) {
probAfter, probAfter,
} }
console.log('update', { pool, totalBets, totalShares }) // console.log('bet', betUpdate)
// console.log('update', { pool, totalBets, totalShares })
transaction.update(betsRef.doc(bet.id), betUpdate) transaction.update(betsRef.doc(bet.id), betUpdate)
} }
@ -185,11 +165,13 @@ async function recalculateContract(contractRef: DocRef, contract: Contract) {
phantomShares, phantomShares,
} }
console.log('final', contractUpdate)
transaction.update(contractRef, contractUpdate) transaction.update(contractRef, contractUpdate)
}) })
console.log('updated', contract.slug) console.log('updated', contract.slug)
console.log() console.log()
console.log()
} }
async function recalculateContractTotals() { async function recalculateContractTotals() {
@ -201,7 +183,7 @@ async function recalculateContractTotals() {
console.log('Loaded', contracts.length, 'contracts') console.log('Loaded', contracts.length, 'contracts')
for (const contract of contracts) { for (const contract of contracts) {
if (contract.slug !== 'another-test') continue // if (contract.slug !== 'will-polymarket-list-any-new-market') continue
const contractRef = firestore.doc(`contracts/${contract.id}`) const contractRef = firestore.doc(`contracts/${contract.id}`)
await recalculateContract(contractRef, contract) await recalculateContract(contractRef, contract)