diff --git a/functions/src/place-bet.ts b/functions/src/place-bet.ts index 6a1e737a..7999325d 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, newPot, newBalance } = getNewBetInfo( + const { newBet, newPot, newDpmWeights, newBalance } = getNewBetInfo( user, outcome, amount, @@ -52,7 +52,7 @@ export const placeBet = functions.runWith({ minInstances: 1 }).https.onCall( ) transaction.create(newBetDoc, newBet) - transaction.update(contractDoc, { pot: newPot }) + transaction.update(contractDoc, { pot: newPot, dpmWeights: newDpmWeights }) transaction.update(userDoc, { balance: newBalance }) return { status: 'success' } @@ -71,6 +71,23 @@ const getNewBetInfo = ( ) => { const { YES: yesPot, NO: noPot } = contract.pot + const newPot = + outcome === 'YES' + ? { YES: yesPot + amount, NO: noPot } + : { YES: yesPot, NO: noPot + amount } + + const dpmWeight = + outcome === 'YES' + ? (amount * noPot ** 2) / (yesPot ** 2 + amount * yesPot) + : (amount * yesPot ** 2) / (noPot ** 2 + amount * noPot) + + const { YES: yesWeight, NO: noWeight } = contract.dpmWeights + + const newDpmWeights = + outcome === 'YES' + ? { YES: yesWeight + dpmWeight, NO: noWeight } + : { YES: yesWeight, NO: noWeight + dpmWeight } + const probBefore = yesPot ** 2 / (yesPot ** 2 + noPot ** 2) const probAverage = @@ -79,16 +96,6 @@ const getNewBetInfo = ( noPot * Math.atan((amount + yesPot) / noPot)) / amount - const dpmWeight = - outcome === 'YES' - ? (amount * noPot ** 2) / (yesPot ** 2 + amount * yesPot) - : (amount * yesPot ** 2) / (noPot ** 2 + amount * noPot) - - const newPot = - outcome === 'YES' - ? { YES: yesPot + amount, NO: noPot } - : { YES: yesPot, NO: noPot + amount } - const probAfter = newPot.YES ** 2 / (newPot.YES ** 2 + newPot.NO ** 2) const newBet: Bet = { @@ -106,5 +113,5 @@ const getNewBetInfo = ( const newBalance = user.balance - amount - return { newBet, newPot, newBalance } + return { newBet, newPot, newDpmWeights, newBalance } } diff --git a/functions/src/types/contract.ts b/functions/src/types/contract.ts index 15f256fd..f20da2b7 100644 --- a/functions/src/types/contract.ts +++ b/functions/src/types/contract.ts @@ -11,6 +11,7 @@ export type Contract = { // outcomes: ['YES', 'NO'] seedAmounts: { YES: number; NO: number } pot: { YES: number; NO: number } + dpmWeights: { YES: number; NO: number } createdTime: number // Milliseconds since epoch lastUpdatedTime: number // If the question or description was changed diff --git a/web/lib/firebase/contracts.ts b/web/lib/firebase/contracts.ts index 338de519..0bcca1a7 100644 --- a/web/lib/firebase/contracts.ts +++ b/web/lib/firebase/contracts.ts @@ -26,6 +26,7 @@ export type Contract = { // outcomes: ['YES', 'NO'] seedAmounts: { YES: number; NO: number } // seedBets: [number, number] pot: { YES: number; NO: number } + dpmWeights: { YES: number; NO: number } createdTime: number // Milliseconds since epoch lastUpdatedTime: number // If the question or description was changed diff --git a/web/lib/service/create-contract.ts b/web/lib/service/create-contract.ts index 3c46592d..56ee335f 100644 --- a/web/lib/service/create-contract.ts +++ b/web/lib/service/create-contract.ts @@ -30,6 +30,8 @@ export async function createContract( seedAmounts: { YES: seedYes, NO: seedNo }, pot: { YES: seedYes, NO: seedNo }, + dpmWeights: { YES: 0, NO: 0 }, + isResolved: false, // TODO: Set create time to Firestore timestamp