From 8331e7afc532dde6a8b36967566dbd66182ec9ae Mon Sep 17 00:00:00 2001 From: mantikoros Date: Sun, 16 Jan 2022 15:53:40 -0600 Subject: [PATCH] migration script now re-simulates individual markets specified in command line --- functions/src/scripts/migrate-to-dpm-2.ts | 48 +++++++++++++---------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/functions/src/scripts/migrate-to-dpm-2.ts b/functions/src/scripts/migrate-to-dpm-2.ts index 1f914b00..a9787950 100644 --- a/functions/src/scripts/migrate-to-dpm-2.ts +++ b/functions/src/scripts/migrate-to-dpm-2.ts @@ -17,9 +17,14 @@ const serviceAccount = require('../../../../../../Downloads/mantic-markets-fireb admin.initializeApp({ credential: admin.credential.cert(serviceAccount), }) + const firestore = admin.firestore() -async function recalculateContract(contractRef: DocRef, contract: Contract) { +async function recalculateContract( + contractRef: DocRef, + contract: Contract, + isCommit = false +) { const startPool = (contract as any).startPool as | undefined | { YES: number; NO: number } @@ -113,8 +118,8 @@ async function recalculateContract(contractRef: DocRef, contract: Contract) { getSellBetInfo(fakeUser, soldBet, fakeContract, bet.id) newBet.createdTime = bet.createdTime - // console.log('sale bet', newBet) - transaction.update(betsRef.doc(bet.id), newBet) + console.log('sale bet', newBet) + if (isCommit) transaction.update(betsRef.doc(bet.id), newBet) pool = newPool totalShares = newTotalShares @@ -152,10 +157,10 @@ async function recalculateContract(contractRef: DocRef, contract: Contract) { probAfter, } - // console.log('bet', betUpdate) - // console.log('update', { pool, totalBets, totalShares }) + console.log('bet', betUpdate) + console.log('update', { pool, totalBets, totalShares }) - transaction.update(betsRef.doc(bet.id), betUpdate) + if (isCommit) transaction.update(betsRef.doc(bet.id), betUpdate) } const contractUpdate: Partial = { @@ -166,7 +171,7 @@ async function recalculateContract(contractRef: DocRef, contract: Contract) { } console.log('final', contractUpdate) - transaction.update(contractRef, contractUpdate) + if (isCommit) transaction.update(contractRef, contractUpdate) }) console.log('updated', contract.slug) @@ -174,21 +179,24 @@ async function recalculateContract(contractRef: DocRef, contract: Contract) { console.log() } -async function recalculateContractTotals() { - console.log('Migrating ante calculations to DPM-2') +async function main() { + const slug = process.argv[2] + const isCommit = process.argv[3] === 'commit' - const snapshot = await firestore.collection('contracts').get() - const contracts = snapshot.docs.map((doc) => doc.data() as Contract) + const snap = await firestore + .collection('contracts') + .where('slug', '==', slug) + .get() - console.log('Loaded', contracts.length, 'contracts') - - for (const contract of contracts) { - // if (contract.slug !== 'will-polymarket-list-any-new-market') continue - const contractRef = firestore.doc(`contracts/${contract.id}`) - - await recalculateContract(contractRef, contract) + const contract = snap.docs[0]?.data() as Contract + if (!contract) { + console.log('No contract found for', slug) + return } + + const contractRef = firestore.doc(`contracts/${contract.id}`) + + await recalculateContract(contractRef, contract, isCommit) } -if (require.main === module) - recalculateContractTotals().then(() => process.exit()) +if (require.main === module) main().then(() => process.exit())