From 813ec0bf032a98f8709af2d9647aaf89e7ab7024 Mon Sep 17 00:00:00 2001 From: mantikoros Date: Sun, 17 Apr 2022 22:28:59 -0500 Subject: [PATCH] cfmm migration script --- functions/src/scripts/migrate-to-cfmm.ts | 124 +++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 functions/src/scripts/migrate-to-cfmm.ts diff --git a/functions/src/scripts/migrate-to-cfmm.ts b/functions/src/scripts/migrate-to-cfmm.ts new file mode 100644 index 00000000..2e119bf6 --- /dev/null +++ b/functions/src/scripts/migrate-to-cfmm.ts @@ -0,0 +1,124 @@ +import * as admin from 'firebase-admin' +import * as _ from 'lodash' + +import { initAdmin } from './script-init' +initAdmin('stephenDev') + +import { + Binary, + Contract, + CPMM, + DPM, + FullContract, +} from '../../../common/contract' +import { Bet } from '../../../common/bet' +import { + calculateStandardDpmPayout, + getDpmProbability, +} from '../../../common/calculate-dpm' +import { User } from '../../../common/user' +import { getCpmmInitialLiquidity } from '../../../common/antes' +import { noFees } from '../../../common/fees' +import { addObjects } from '../../../common/util/object' + +type DocRef = admin.firestore.DocumentReference + +const firestore = admin.firestore() + +async function recalculateContract(contractRef: DocRef, isCommit = false) { + await firestore.runTransaction(async (transaction) => { + const contractDoc = await transaction.get(contractRef) + const contract = contractDoc.data() as FullContract + console.log('recalculating', contract.slug) + + if ( + contract.mechanism !== 'dpm-2' || + contract.outcomeType !== 'BINARY' || + !!contract.resolution + ) { + console.log('invalid candidate to port to cfmm') + return + } + + const betsRef = contractRef.collection('bets') + const betDocs = await transaction.get(betsRef) + const bets = _.sortBy( + betDocs.docs.map((d) => d.data() as Bet), + (b) => b.createdTime + ) + + for (let bet of bets) { + const shares = + bet.isSold || bet.sale + ? 0 + : calculateStandardDpmPayout(contract, bet, bet.outcome) + + console.log( + 'converting', + bet.shares, + bet.outcome, + bet.isSold ? '(sold)' : '', + 'shares to', + shares + ) + + if (isCommit) transaction.update(betsRef.doc(bet.id), { shares }) + } + + const prob = getDpmProbability(contract.totalShares) + const ante = 100 + const newPool = { YES: ante, NO: ante } + console.log('creating liquidity pool at p=', prob, 'for M$', ante) + + const contractUpdate: Partial = { + pool: newPool, + p: prob, + mechanism: 'cpmm-1', + totalLiquidity: ante, + collectedFees: addObjects(contract.collectedFees ?? noFees, noFees), + } + + const liquidityDocRef = contractRef.collection('liquidity').doc() + + const lp = getCpmmInitialLiquidity( + { id: 'IPTOzEqrpkWmEzh6hwvAyY9PqFb2' } as User, // use @ManifoldMarkets' id + { + ...contract, + ...contractUpdate, + } as FullContract, + liquidityDocRef.id, + ante + ) + + if (isCommit) { + transaction.update(contractRef, contractUpdate) + transaction.set(liquidityDocRef, lp) + + console.log('updated', contract.slug) + } + }) +} + +async function main() { + const slug = process.argv[2] + const isCommit = process.argv[3] === 'commit' + + const contractRefs = + slug === 'all' + ? await firestore.collection('contracts').listDocuments() + : await firestore + .collection('contracts') + .where('slug', '==', slug) + .get() + .then((snap) => + !snap.empty ? [firestore.doc(`contracts/${snap.docs[0].id}`)] : [] + ) + + for (let contractRef of contractRefs) { + await recalculateContract(contractRef, isCommit) + console.log() + console.log() + } +} + +if (require.main === module) main().then(() => process.exit())