2022-01-12 19:01:04 +00:00
|
|
|
import * as admin from 'firebase-admin'
|
2022-05-22 08:36:05 +00:00
|
|
|
import { sortBy, sumBy } from 'lodash'
|
2022-01-12 19:01:04 +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-06-01 02:42:35 +00:00
|
|
|
import { Contract, DPMBinaryContract } from '../../../common/contract'
|
2022-05-15 17:39:42 +00:00
|
|
|
import { Bet } from '../../../common/bet'
|
|
|
|
import {
|
|
|
|
calculateDpmShares,
|
|
|
|
getDpmProbability,
|
|
|
|
} from '../../../common/calculate-dpm'
|
|
|
|
import { getSellBetInfo } from '../../../common/sell-bet'
|
2022-01-12 19:01:04 +00:00
|
|
|
|
|
|
|
type DocRef = admin.firestore.DocumentReference
|
|
|
|
|
|
|
|
const firestore = admin.firestore()
|
|
|
|
|
2022-01-16 21:53:40 +00:00
|
|
|
async function recalculateContract(
|
|
|
|
contractRef: DocRef,
|
|
|
|
contract: Contract,
|
|
|
|
isCommit = false
|
|
|
|
) {
|
2022-01-12 19:01:04 +00:00
|
|
|
const startPool = (contract as any).startPool as
|
|
|
|
| undefined
|
|
|
|
| { YES: number; NO: number }
|
|
|
|
|
2022-01-13 05:43:41 +00:00
|
|
|
if (!startPool) return
|
|
|
|
|
2022-01-13 18:49:47 +00:00
|
|
|
console.log('recalculating', contract.slug)
|
|
|
|
|
2022-01-13 05:43:41 +00:00
|
|
|
await firestore.runTransaction(async (transaction) => {
|
|
|
|
const contractDoc = await transaction.get(contractRef)
|
2022-06-01 02:42:35 +00:00
|
|
|
const contract = contractDoc.data() as DPMBinaryContract
|
2022-01-13 05:43:41 +00:00
|
|
|
|
|
|
|
const betDocs = await transaction.get(contractRef.collection('bets'))
|
2022-05-22 08:36:05 +00:00
|
|
|
const bets = sortBy(
|
2022-01-13 05:43:41 +00:00
|
|
|
betDocs.docs.map((d) => d.data() as Bet),
|
|
|
|
(b) => b.createdTime
|
|
|
|
)
|
|
|
|
|
|
|
|
const phantomAnte = startPool.YES + startPool.NO
|
|
|
|
|
2022-01-13 18:49:47 +00:00
|
|
|
const leftovers =
|
2022-05-22 08:36:05 +00:00
|
|
|
sumBy(bets, (b) => b.amount) -
|
|
|
|
sumBy(bets, (b) => {
|
2022-01-13 18:49:47 +00:00
|
|
|
if (!b.sale) return b.amount
|
|
|
|
const soldBet = bets.find((bet) => bet.id === b.sale?.betId)
|
|
|
|
return soldBet?.amount || 0
|
|
|
|
})
|
|
|
|
const poolTotal = contract.pool.YES + contract.pool.NO
|
|
|
|
const prevTotalBets = contract.totalBets.YES + contract.totalBets.NO
|
|
|
|
const calculatedrealAnte = poolTotal - prevTotalBets - leftovers
|
2022-01-13 05:43:41 +00:00
|
|
|
|
2022-01-13 18:49:47 +00:00
|
|
|
const realAnte = Math.max(
|
|
|
|
0,
|
|
|
|
(contract as any).realAnte || calculatedrealAnte
|
|
|
|
)
|
2022-01-13 05:43:41 +00:00
|
|
|
|
2022-01-13 18:49:47 +00:00
|
|
|
if (!(contract as any).realAnte)
|
2022-01-13 05:43:41 +00:00
|
|
|
transaction.update(contractRef, {
|
2022-01-13 18:49:47 +00:00
|
|
|
realAnte,
|
2022-01-13 05:43:41 +00:00
|
|
|
})
|
|
|
|
|
2022-01-13 18:49:47 +00:00
|
|
|
console.log(
|
|
|
|
'pool',
|
|
|
|
poolTotal,
|
|
|
|
'phantomAnte',
|
|
|
|
phantomAnte,
|
|
|
|
'realAnte',
|
|
|
|
realAnte,
|
|
|
|
'calculatedRealAnte',
|
|
|
|
calculatedrealAnte,
|
|
|
|
'leftovers',
|
|
|
|
leftovers
|
|
|
|
)
|
|
|
|
|
2022-01-13 05:43:41 +00:00
|
|
|
let p = startPool.YES ** 2 / (startPool.YES ** 2 + startPool.NO ** 2)
|
|
|
|
|
|
|
|
const phantomShares = {
|
|
|
|
YES: Math.sqrt(p) * phantomAnte,
|
|
|
|
NO: Math.sqrt(1 - p) * phantomAnte,
|
|
|
|
}
|
|
|
|
|
|
|
|
let totalShares = {
|
|
|
|
YES: Math.sqrt(p) * (phantomAnte + realAnte),
|
|
|
|
NO: Math.sqrt(1 - p) * (phantomAnte + realAnte),
|
2022-02-17 23:00:19 +00:00
|
|
|
} as { [outcome: string]: number }
|
2022-01-13 05:43:41 +00:00
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
let pool = { YES: p * realAnte, NO: (1 - p) * realAnte } as {
|
|
|
|
[outcome: string]: number
|
|
|
|
}
|
2022-01-13 05:43:41 +00:00
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
let totalBets = { YES: p * realAnte, NO: (1 - p) * realAnte } as {
|
|
|
|
[outcome: string]: number
|
|
|
|
}
|
2022-01-13 05:43:41 +00:00
|
|
|
|
|
|
|
const betsRef = contractRef.collection('bets')
|
|
|
|
|
|
|
|
console.log('start', { pool, totalBets, totalShares })
|
|
|
|
|
2022-06-06 05:50:27 +00:00
|
|
|
for (const bet of bets) {
|
2022-01-13 05:43:41 +00:00
|
|
|
if (bet.sale) {
|
|
|
|
const soldBet = bets.find((b) => b.id === bet.sale?.betId)
|
|
|
|
if (!soldBet) throw new Error('invalid sold bet' + bet.sale.betId)
|
|
|
|
|
|
|
|
const fakeContract: Contract = {
|
|
|
|
...contract,
|
|
|
|
totalBets,
|
|
|
|
totalShares,
|
|
|
|
pool,
|
|
|
|
phantomShares,
|
|
|
|
}
|
|
|
|
|
|
|
|
const { newBet, newPool, newTotalShares, newTotalBets } =
|
2022-06-07 21:08:56 +00:00
|
|
|
getSellBetInfo(soldBet, fakeContract)
|
2022-01-13 05:43:41 +00:00
|
|
|
|
2022-06-07 21:08:56 +00:00
|
|
|
const betDoc = betsRef.doc(bet.id)
|
|
|
|
const userId = soldBet.userId
|
2022-01-13 05:43:41 +00:00
|
|
|
newBet.createdTime = bet.createdTime
|
2022-01-16 21:53:40 +00:00
|
|
|
console.log('sale bet', newBet)
|
2022-06-07 21:08:56 +00:00
|
|
|
if (isCommit)
|
|
|
|
transaction.update(betDoc, { id: bet.id, userId, ...newBet })
|
2022-01-13 05:43:41 +00:00
|
|
|
|
|
|
|
pool = newPool
|
|
|
|
totalShares = newTotalShares
|
|
|
|
totalBets = newTotalBets
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
const shares = calculateDpmShares(totalShares, bet.amount, bet.outcome)
|
2022-01-13 05:43:41 +00:00
|
|
|
const probBefore = p
|
|
|
|
const ind = bet.outcome === 'YES' ? 1 : 0
|
|
|
|
|
|
|
|
totalShares = {
|
|
|
|
YES: totalShares.YES + ind * shares,
|
|
|
|
NO: totalShares.NO + (1 - ind) * shares,
|
|
|
|
}
|
|
|
|
|
|
|
|
pool = {
|
|
|
|
YES: pool.YES + ind * bet.amount,
|
|
|
|
NO: pool.NO + (1 - ind) * bet.amount,
|
|
|
|
}
|
|
|
|
|
|
|
|
totalBets = {
|
|
|
|
YES: totalBets.YES + ind * bet.amount,
|
|
|
|
NO: totalBets.NO + (1 - ind) * bet.amount,
|
|
|
|
}
|
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
p = getDpmProbability(totalShares)
|
2022-01-13 05:43:41 +00:00
|
|
|
|
|
|
|
const probAfter = p
|
|
|
|
|
|
|
|
const betUpdate: Partial<Bet> = {
|
|
|
|
shares,
|
|
|
|
probBefore,
|
|
|
|
probAfter,
|
|
|
|
}
|
|
|
|
|
2022-01-16 21:53:40 +00:00
|
|
|
console.log('bet', betUpdate)
|
|
|
|
console.log('update', { pool, totalBets, totalShares })
|
2022-01-13 05:43:41 +00:00
|
|
|
|
2022-01-16 21:53:40 +00:00
|
|
|
if (isCommit) transaction.update(betsRef.doc(bet.id), betUpdate)
|
2022-01-13 05:43:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const contractUpdate: Partial<Contract> = {
|
|
|
|
pool,
|
|
|
|
totalBets,
|
|
|
|
totalShares,
|
|
|
|
phantomShares,
|
|
|
|
}
|
|
|
|
|
2022-01-13 18:49:47 +00:00
|
|
|
console.log('final', contractUpdate)
|
2022-01-16 21:53:40 +00:00
|
|
|
if (isCommit) transaction.update(contractRef, contractUpdate)
|
2022-01-13 05:43:41 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
console.log('updated', contract.slug)
|
2022-01-12 19:01:04 +00:00
|
|
|
console.log()
|
2022-01-13 18:49:47 +00:00
|
|
|
console.log()
|
2022-01-12 19:01:04 +00:00
|
|
|
}
|
|
|
|
|
2022-01-16 21:53:40 +00:00
|
|
|
async function main() {
|
|
|
|
const slug = process.argv[2]
|
|
|
|
const isCommit = process.argv[3] === 'commit'
|
2022-01-12 19:01:04 +00:00
|
|
|
|
2022-01-16 21:53:40 +00:00
|
|
|
const snap = await firestore
|
|
|
|
.collection('contracts')
|
|
|
|
.where('slug', '==', slug)
|
|
|
|
.get()
|
2022-01-12 19:01:04 +00:00
|
|
|
|
2022-01-16 21:53:40 +00:00
|
|
|
const contract = snap.docs[0]?.data() as Contract
|
|
|
|
if (!contract) {
|
|
|
|
console.log('No contract found for', slug)
|
|
|
|
return
|
|
|
|
}
|
2022-01-12 19:01:04 +00:00
|
|
|
|
2022-01-16 21:53:40 +00:00
|
|
|
const contractRef = firestore.doc(`contracts/${contract.id}`)
|
2022-01-12 19:01:04 +00:00
|
|
|
|
2022-01-16 21:53:40 +00:00
|
|
|
await recalculateContract(contractRef, contract, isCommit)
|
2022-01-12 19:01:04 +00:00
|
|
|
}
|
|
|
|
|
2022-01-16 21:53:40 +00:00
|
|
|
if (require.main === module) main().then(() => process.exit())
|