diff --git a/common/challenge.ts b/common/challenge.ts index 2cafe019..5900743a 100644 --- a/common/challenge.ts +++ b/common/challenge.ts @@ -11,7 +11,7 @@ export type Challenge = { message: string // How much to put up - amount: number + creatorAmount: number // YES or NO for now creatorsOutcome: string @@ -49,6 +49,9 @@ export type Acceptance = { userName: string userAvatarUrl: string + // The amount acceptor put up + amount: number + // The ID of the successful bet that tracks the money moved betId: string diff --git a/functions/src/accept-challenge.ts b/functions/src/accept-challenge.ts index a43afa9a..0720e71d 100644 --- a/functions/src/accept-challenge.ts +++ b/functions/src/accept-challenge.ts @@ -49,9 +49,13 @@ export const acceptchallenge = newEndpoint({}, async (req, auth) => { if (!creatorSnap.exists) throw new APIError(400, 'User not found.') const creator = creatorSnap.data() as User - const { amount, yourOutcome, creatorsOutcome, creatorsOutcomeProb } = + const { creatorAmount, yourOutcome, creatorsOutcome, creatorsOutcomeProb } = challenge - if (user.balance < amount) throw new APIError(400, 'Insufficient balance.') + const yourCost = + ((1 - creatorsOutcomeProb) / creatorsOutcomeProb) * creatorAmount + + if (user.balance < yourCost) + throw new APIError(400, 'Insufficient balance.') const { closeTime, outcomeType } = anyContract if (closeTime && Date.now() > closeTime) @@ -61,22 +65,19 @@ export const acceptchallenge = newEndpoint({}, async (req, auth) => { const contract = anyContract as CPMMBinaryContract const { YES: y, NO: n } = contract.pool - const yourShares = (1 / (1 - creatorsOutcomeProb)) * amount - const creatorShares = (1 / creatorsOutcomeProb) * amount + const shares = (1 / creatorsOutcomeProb) * creatorAmount - const newYesShares = creatorsOutcome === 'YES' ? creatorShares : yourShares - const newNoShares = creatorsOutcome === 'NO' ? creatorShares : yourShares const newPool = { - YES: y + newYesShares, - NO: n + newNoShares, + YES: y + shares, + NO: n + shares, } const probBefore = getCpmmProbability(contract.pool, contract.p) const probAfter = getCpmmProbability(newPool, contract.p) const yourNewBet: CandidateBet = removeUndefinedProps({ - orderAmount: amount, - amount: amount, - shares: yourShares, + orderAmount: yourCost, + amount: yourCost, + shares: shares, isCancelled: false, contractId: contract.id, outcome: yourOutcome, @@ -98,9 +99,9 @@ export const acceptchallenge = newEndpoint({}, async (req, auth) => { log('Updated user balance.') const creatorNewBet: CandidateBet = removeUndefinedProps({ - orderAmount: amount, - amount: amount, - shares: creatorShares, + orderAmount: creatorAmount, + amount: creatorAmount, + shares: shares, isCancelled: false, contractId: contract.id, outcome: creatorsOutcome, @@ -141,6 +142,7 @@ export const acceptchallenge = newEndpoint({}, async (req, auth) => { userId: user.id, betId: yourNewBetDoc.id, createdTime: Date.now(), + amount: yourCost, userUsername: user.username, userName: user.name, userAvatarUrl: user.avatarUrl, @@ -154,6 +156,7 @@ export const acceptchallenge = newEndpoint({}, async (req, auth) => { user, creator, challenge, + yourCost, contract ) log('Created notification.') diff --git a/functions/src/create-notification.ts b/functions/src/create-notification.ts index 480d72b1..6cc7631e 100644 --- a/functions/src/create-notification.ts +++ b/functions/src/create-notification.ts @@ -474,6 +474,7 @@ export const createChallengeAcceptedNotification = async ( challenger: User, challengeCreator: User, challenge: Challenge, + acceptedAmount: number, contract: Contract ) => { const notificationRef = firestore @@ -491,7 +492,7 @@ export const createChallengeAcceptedNotification = async ( sourceUserName: challenger.name, sourceUserUsername: challenger.username, sourceUserAvatarUrl: challenger.avatarUrl, - sourceText: challenge.amount.toString(), + sourceText: acceptedAmount.toString(), sourceContractCreatorUsername: contract.creatorUsername, sourceContractTitle: contract.question, sourceContractSlug: contract.slug, diff --git a/web/components/challenges/accept-challenge-button.tsx b/web/components/challenges/accept-challenge-button.tsx index 27bb710e..a8490079 100644 --- a/web/components/challenges/accept-challenge-button.tsx +++ b/web/components/challenges/accept-challenge-button.tsx @@ -21,8 +21,9 @@ export function AcceptChallengeButton(props: { const [open, setOpen] = useState(false) const [errorText, setErrorText] = useState('') const [loading, setLoading] = useState(false) - const yourProb = 1 - challenge.creatorsOutcomeProb - + const { creatorsOutcomeProb, creatorAmount } = challenge + const yourCost = + ((1 - creatorsOutcomeProb) / creatorsOutcomeProb) * creatorAmount useEffect(() => { setErrorText('') }, [open]) @@ -66,9 +67,7 @@ export function AcceptChallengeButton(props: {