contract now has dpmWeights

This commit is contained in:
mantikoros 2021-12-15 16:44:22 -06:00
parent 4b123d47ee
commit e281233924
4 changed files with 24 additions and 13 deletions

View File

@ -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 }
}

View File

@ -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

View File

@ -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

View File

@ -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