diff --git a/.firebaserc b/.firebaserc index 7594f4c6..0e191214 100644 --- a/.firebaserc +++ b/.firebaserc @@ -1,5 +1,7 @@ { "projects": { - "default": "mantic-markets" + "default": "mantic-markets", + "prod": "mantic-markets", + "dev": "dev-mantic-markets" } -} +} \ No newline at end of file diff --git a/functions/package.json b/functions/package.json index baaac0b4..62dd0c58 100644 --- a/functions/package.json +++ b/functions/package.json @@ -2,6 +2,7 @@ "name": "functions", "scripts": { "build": "tsc", + "watch": "tsc -w", "serve": "yarn build && firebase emulators:start --only functions", "shell": "yarn build && firebase functions:shell", "start": "yarn shell", diff --git a/functions/src/index.ts b/functions/src/index.ts index 5f6cfc99..ee8da84b 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -2,6 +2,7 @@ import * as admin from 'firebase-admin' admin.initializeApp() -export * from './keep-awake' +// export * from './keep-awake' export * from './place-bet' export * from './resolve-market' +export * from './sell-bet' diff --git a/functions/src/place-bet.ts b/functions/src/place-bet.ts index 9e773848..e6e4d25a 100644 --- a/functions/src/place-bet.ts +++ b/functions/src/place-bet.ts @@ -43,7 +43,7 @@ export const placeBet = functions.runWith({ minInstances: 1 }).https.onCall( .collection(`contracts/${contractId}/bets`) .doc() - const { newBet, newPool, newDpmWeights, newBalance } = getNewBetInfo( + const { newBet, newPool, newTotalShares, newBalance } = getNewBetInfo( user, outcome, amount, @@ -54,7 +54,7 @@ export const placeBet = functions.runWith({ minInstances: 1 }).https.onCall( transaction.create(newBetDoc, newBet) transaction.update(contractDoc, { pool: newPool, - dpmWeights: newDpmWeights, + totalShares: newTotalShares, }) transaction.update(userDoc, { balance: newBalance }) @@ -79,29 +79,19 @@ const getNewBetInfo = ( ? { YES: yesPool + amount, NO: noPool } : { YES: yesPool, NO: noPool + amount } - const dpmWeight = + const shares = outcome === 'YES' - ? (amount * noPool ** 2) / (yesPool ** 2 + amount * yesPool) - : (amount * yesPool ** 2) / (noPool ** 2 + amount * noPool) + ? amount + (amount * noPool ** 2) / (yesPool ** 2 + amount * yesPool) + : amount + (amount * yesPool ** 2) / (noPool ** 2 + amount * noPool) - const { YES: yesWeight, NO: noWeight } = contract.dpmWeights || { - YES: 0, - NO: 0, - } // only nesc for old contracts + const { YES: yesShares, NO: noShares } = contract.totalShares - const newDpmWeights = + const newTotalShares = outcome === 'YES' - ? { YES: yesWeight + dpmWeight, NO: noWeight } - : { YES: yesWeight, NO: noWeight + dpmWeight } + ? { YES: yesShares + shares, NO: noShares } + : { YES: yesShares, NO: noShares + shares } const probBefore = yesPool ** 2 / (yesPool ** 2 + noPool ** 2) - - const probAverage = - (amount + - noPool * Math.atan(yesPool / noPool) - - noPool * Math.atan((amount + yesPool) / noPool)) / - amount - const probAfter = newPool.YES ** 2 / (newPool.YES ** 2 + newPool.NO ** 2) const newBet: Bet = { @@ -109,15 +99,14 @@ const getNewBetInfo = ( userId: user.id, contractId: contract.id, amount, - dpmWeight, + shares, outcome, probBefore, - probAverage, probAfter, createdTime: Date.now(), } const newBalance = user.balance - amount - return { newBet, newPool, newDpmWeights, newBalance } + return { newBet, newPool, newTotalShares, newBalance } } diff --git a/functions/src/resolve-market.ts b/functions/src/resolve-market.ts index e20ca354..280af022 100644 --- a/functions/src/resolve-market.ts +++ b/functions/src/resolve-market.ts @@ -78,22 +78,27 @@ export const resolveMarket = functions const firestore = admin.firestore() const getPayouts = (outcome: string, contract: Contract, bets: Bet[]) => { - const [yesBets, noBets] = _.partition(bets, (bet) => bet.outcome === 'YES') + const openBets = bets.filter((b) => !b.isSold && !b.sale) + const [yesBets, noBets] = _.partition( + openBets, + (bet) => bet.outcome === 'YES' + ) - const [pool, winningBets] = + const startPool = contract.startPool.YES + contract.startPool.NO + const truePool = contract.pool.YES + contract.pool.NO - startPool + + const [totalShares, winningBets] = outcome === 'YES' - ? [contract.pool.NO - contract.startPool.NO, yesBets] - : [contract.pool.YES - contract.startPool.YES, noBets] + ? [contract.totalShares.YES, yesBets] + : [contract.totalShares.NO, noBets] - const finalPool = (1 - PLATFORM_FEE - CREATOR_FEE) * pool - const creatorPayout = CREATOR_FEE * pool + const finalPool = (1 - PLATFORM_FEE - CREATOR_FEE) * truePool + const creatorPayout = CREATOR_FEE * truePool console.log('final pool:', finalPool, 'creator fee:', creatorPayout) - const sumWeights = _.sumBy(winningBets, (bet) => bet.dpmWeight) - const winnerPayouts = winningBets.map((bet) => ({ userId: bet.userId, - payout: bet.amount + (bet.dpmWeight / sumWeights) * finalPool, + payout: (bet.shares / totalShares) * finalPool, })) return winnerPayouts.concat([ diff --git a/functions/src/scripts/migrate-contract.ts b/functions/src/scripts/migrate-contract.ts new file mode 100644 index 00000000..eb55783e --- /dev/null +++ b/functions/src/scripts/migrate-contract.ts @@ -0,0 +1,58 @@ +import * as admin from 'firebase-admin' +import * as _ from 'lodash' +import { Bet } from '../types/bet' +import { Contract } from '../types/contract' + +type DocRef = admin.firestore.DocumentReference + +// Generate your own private key, and set the path below: +// https://console.firebase.google.com/u/0/project/mantic-markets/settings/serviceaccounts/adminsdk +const serviceAccount = require('../../../../Downloads/mantic-markets-firebase-adminsdk-1ep46-820891bb87.json') + +admin.initializeApp({ + credential: admin.credential.cert(serviceAccount), +}) +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 }) +} + +async function migrateContract(contractRef: DocRef, contract: Contract) { + const bets = await contractRef + .collection('bets') + .get() + .then((snap) => snap.docs.map((bet) => bet.data() as Bet)) + + const totalShares = { + YES: _.sumBy(bets, (bet) => (bet.outcome === 'YES' ? bet.shares : 0)), + NO: _.sumBy(bets, (bet) => (bet.outcome === 'NO' ? bet.shares : 0)), + } + + 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) + await migrateContract(contractRef, contract) + } +} + +if (require.main === module) migrateContracts().then(() => process.exit()) diff --git a/functions/src/sell-bet.ts b/functions/src/sell-bet.ts new file mode 100644 index 00000000..ba60a5e3 --- /dev/null +++ b/functions/src/sell-bet.ts @@ -0,0 +1,162 @@ +import * as admin from 'firebase-admin' +import * as functions from 'firebase-functions' + +import { CREATOR_FEE, PLATFORM_FEE } from './resolve-market' +import { Bet } from './types/bet' +import { Contract } from './types/contract' +import { User } from './types/user' + +export const sellBet = functions.runWith({ minInstances: 1 }).https.onCall( + async ( + data: { + contractId: string + betId: string + }, + context + ) => { + const userId = context?.auth?.uid + if (!userId) return { status: 'error', message: 'Not authorized' } + + const { contractId, betId } = data + + // run as transaction to prevent race conditions + return await firestore.runTransaction(async (transaction) => { + const userDoc = firestore.doc(`users/${userId}`) + const userSnap = await transaction.get(userDoc) + if (!userSnap.exists) + return { status: 'error', message: 'User not found' } + const user = userSnap.data() as User + + const contractDoc = firestore.doc(`contracts/${contractId}`) + const contractSnap = await transaction.get(contractDoc) + if (!contractSnap.exists) + return { status: 'error', message: 'Invalid contract' } + const contract = contractSnap.data() as Contract + + const betDoc = firestore.doc(`contracts/${contractId}/bets/${betId}`) + const betSnap = await transaction.get(betDoc) + if (!betSnap.exists) return { status: 'error', message: 'Invalid bet' } + const bet = betSnap.data() as Bet + + if (bet.isSold) return { status: 'error', message: 'Bet already sold' } + + const newBetDoc = firestore + .collection(`contracts/${contractId}/bets`) + .doc() + + const { newBet, newPool, newTotalShares, newBalance, creatorFee } = + getSellBetInfo(user, bet, contract, newBetDoc.id) + + const creatorDoc = firestore.doc(`users/${contract.creatorId}`) + const creatorSnap = await transaction.get(creatorDoc) + if (creatorSnap.exists) { + const creator = creatorSnap.data() as User + const creatorNewBalance = creator.balance + creatorFee + transaction.update(creatorDoc, { balance: creatorNewBalance }) + } + + transaction.update(betDoc, { isSold: true }) + transaction.create(newBetDoc, newBet) + transaction.update(contractDoc, { + pool: newPool, + totalShares: newTotalShares, + }) + transaction.update(userDoc, { balance: newBalance }) + + return { status: 'success' } + }) + } +) + +const firestore = admin.firestore() + +const getSellBetInfo = ( + user: User, + bet: Bet, + contract: Contract, + newBetId: string +) => { + const { id: betId, amount, shares, outcome } = bet + + const { YES: yesPool, NO: noPool } = contract.pool + const { YES: yesStart, NO: noStart } = contract.startPool + const { YES: yesShares, NO: noShares } = contract.totalShares + + const [y, n, s] = [yesPool, noPool, shares] + + const shareValue = + outcome === 'YES' + ? // https://www.wolframalpha.com/input/?i=b+%2B+%28b+n%5E2%29%2F%28y+%28-b+%2B+y%29%29+%3D+c+solve+b + (n ** 2 + + s * y + + y ** 2 - + Math.sqrt( + n ** 4 + (s - y) ** 2 * y ** 2 + 2 * n ** 2 * y * (s + y) + )) / + (2 * y) + : (y ** 2 + + s * n + + n ** 2 - + Math.sqrt( + y ** 4 + (s - n) ** 2 * n ** 2 + 2 * y ** 2 * n * (s + n) + )) / + (2 * n) + + const startPool = yesStart + noStart + const pool = yesPool + noPool - startPool + + const probBefore = yesPool ** 2 / (yesPool ** 2 + noPool ** 2) + + const f = pool / (probBefore * yesShares + (1 - probBefore) * noShares) + + const myPool = outcome === 'YES' ? yesPool - yesStart : noPool - noStart + + const adjShareValue = Math.min(Math.min(1, f) * shareValue, myPool) + + const newPool = + outcome === 'YES' + ? { YES: yesPool - adjShareValue, NO: noPool } + : { YES: yesPool, NO: noPool - adjShareValue } + + const newTotalShares = + outcome === 'YES' + ? { YES: yesShares - shares, NO: noShares } + : { YES: yesShares, NO: noShares - shares } + + const probAfter = newPool.YES ** 2 / (newPool.YES ** 2 + newPool.NO ** 2) + + const creatorFee = CREATOR_FEE * adjShareValue + const saleAmount = (1 - CREATOR_FEE - PLATFORM_FEE) * adjShareValue + + console.log( + 'SELL M$', + amount, + outcome, + 'for M$', + saleAmount, + 'M$/share:', + f, + 'creator fee: M$', + creatorFee + ) + + const newBet: Bet = { + id: newBetId, + userId: user.id, + contractId: contract.id, + amount: -adjShareValue, + shares: -shares, + outcome, + probBefore, + probAfter, + createdTime: Date.now(), + sale: { + amount: saleAmount, + betId, + }, + } + + const newBalance = user.balance + saleAmount + + return { newBet, newPool, newTotalShares, newBalance, creatorFee } +} diff --git a/functions/src/types/bet.ts b/functions/src/types/bet.ts index 2113f812..8b540165 100644 --- a/functions/src/types/bet.ts +++ b/functions/src/types/bet.ts @@ -2,11 +2,20 @@ export type Bet = { id: string userId: string contractId: string - amount: number // Amount of bet - outcome: 'YES' | 'NO' // Chosen outcome - createdTime: number + + amount: number // bet size; negative if SELL bet + outcome: 'YES' | 'NO' + shares: number // dynamic parimutuel pool weight; negative if SELL bet + probBefore: number - probAverage: number probAfter: number - dpmWeight: number // Dynamic Parimutuel weight -} \ No newline at end of file + + sale?: { + amount: number // amount user makes from sale + betId: string // id of bet being sold + } + + isSold?: boolean // true if this BUY bet has been sold + + createdTime: number +} diff --git a/functions/src/types/contract.ts b/functions/src/types/contract.ts index 8278206b..5f354654 100644 --- a/functions/src/types/contract.ts +++ b/functions/src/types/contract.ts @@ -12,7 +12,7 @@ export type Contract = { startPool: { YES: number; NO: number } pool: { YES: number; NO: number } - dpmWeights: { YES: number; NO: number } + totalShares: { YES: number; NO: number } createdTime: number // Milliseconds since epoch lastUpdatedTime: number // If the question or description was changed diff --git a/web/components/bet-panel.tsx b/web/components/bet-panel.tsx index f0f34410..42f3abae 100644 --- a/web/components/bet-panel.tsx +++ b/web/components/bet-panel.tsx @@ -12,9 +12,9 @@ import { formatMoney, formatPercent } from '../lib/util/format' import { Title } from './title' import { getProbability, - getDpmWeight, + calculateShares, getProbabilityAfterBet, -} from '../lib/calculation/contract' +} from '../lib/calculate' import { firebaseLogin } from '../lib/firebase/users' export function BetPanel(props: { contract: Contract; className?: string }) { @@ -84,9 +84,9 @@ export function BetPanel(props: { contract: Contract; className?: string }) { betChoice, betAmount ?? 0 ) - const dpmWeight = getDpmWeight(contract.pool, betAmount ?? 0, betChoice) + const shares = calculateShares(contract.pool, betAmount ?? 0, betChoice) - const estimatedWinnings = Math.floor((betAmount ?? 0) + dpmWeight) + const estimatedWinnings = Math.floor(shares) const estimatedReturn = betAmount ? (estimatedWinnings - betAmount) / betAmount : 0 @@ -98,7 +98,7 @@ export function BetPanel(props: { contract: Contract; className?: string }) {