From ac4964f05817bab79d731818fabef3c3999cb445 Mon Sep 17 00:00:00 2001 From: Ian Philips Date: Thu, 21 Jul 2022 12:01:33 -0600 Subject: [PATCH] Fix before and after probs --- common/challenge.ts | 3 ++ functions/src/accept-challenge.ts | 50 ++++++++----------- web/components/bet-row.tsx | 5 -- web/components/copy-link-button.tsx | 2 +- web/pages/[username]/[contractSlug].tsx | 5 -- .../[contractSlug]/[challengeSlug].tsx | 30 +++++++---- 6 files changed, 44 insertions(+), 51 deletions(-) diff --git a/common/challenge.ts b/common/challenge.ts index b49bd836..2cafe019 100644 --- a/common/challenge.ts +++ b/common/challenge.ts @@ -37,15 +37,18 @@ export type Challenge = { // Successful redemptions of the link acceptances: Acceptance[] + // TODO: will have to fill this on resolve contract isResolved: boolean resolutionOutcome?: string } export type Acceptance = { + // User that accepted the challenge userId: string userUsername: string userName: string userAvatarUrl: string + // 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 eb6c0350..a43afa9a 100644 --- a/functions/src/accept-challenge.ts +++ b/functions/src/accept-challenge.ts @@ -8,10 +8,7 @@ import { FieldValue } from 'firebase-admin/firestore' import { removeUndefinedProps } from '../../common/util/object' import { Acceptance, Challenge } from '../../common/challenge' import { CandidateBet } from '../../common/new-bet' -import { - calculateCpmmPurchase, - getCpmmProbability, -} from '../../common/calculate-cpmm' +import { getCpmmProbability } from '../../common/calculate-cpmm' import { createChallengeAcceptedNotification } from './create-notification' const bodySchema = z.object({ @@ -63,11 +60,19 @@ export const acceptchallenge = newEndpoint({}, async (req, auth) => { throw new APIError(400, 'Challenges only accepted for binary markets.') const contract = anyContract as CPMMBinaryContract - log('contract stats:', contract.pool, contract.p) - const probs = getCpmmProbability(contract.pool, contract.p) - log('probs:', probs) - + const { YES: y, NO: n } = contract.pool const yourShares = (1 / (1 - creatorsOutcomeProb)) * amount + const creatorShares = (1 / creatorsOutcomeProb) * amount + + const newYesShares = creatorsOutcome === 'YES' ? creatorShares : yourShares + const newNoShares = creatorsOutcome === 'NO' ? creatorShares : yourShares + const newPool = { + YES: y + newYesShares, + NO: n + newNoShares, + } + const probBefore = getCpmmProbability(contract.pool, contract.p) + const probAfter = getCpmmProbability(newPool, contract.p) + const yourNewBet: CandidateBet = removeUndefinedProps({ orderAmount: amount, amount: amount, @@ -75,8 +80,8 @@ export const acceptchallenge = newEndpoint({}, async (req, auth) => { isCancelled: false, contractId: contract.id, outcome: yourOutcome, - probBefore: probs, - probAfter: probs, + probBefore, + probAfter, loanAmount: 0, createdTime: Date.now(), fees: { creatorFee: 0, platformFee: 0, liquidityFee: 0 }, @@ -92,15 +97,6 @@ export const acceptchallenge = newEndpoint({}, async (req, auth) => { trans.update(userDoc, { balance: FieldValue.increment(-yourNewBet.amount) }) log('Updated user balance.') - let cpmmState = { pool: contract.pool, p: contract.p } - const { newPool, newP } = calculateCpmmPurchase( - cpmmState, - yourNewBet.amount, - yourNewBet.outcome - ) - cpmmState = { pool: newPool, p: newP } - - const creatorShares = (1 / creatorsOutcomeProb) * amount const creatorNewBet: CandidateBet = removeUndefinedProps({ orderAmount: amount, amount: amount, @@ -108,8 +104,8 @@ export const acceptchallenge = newEndpoint({}, async (req, auth) => { isCancelled: false, contractId: contract.id, outcome: creatorsOutcome, - probBefore: probs, - probAfter: probs, + probBefore, + probAfter, loanAmount: 0, createdTime: Date.now(), fees: { creatorFee: 0, platformFee: 0, liquidityFee: 0 }, @@ -126,19 +122,11 @@ export const acceptchallenge = newEndpoint({}, async (req, auth) => { balance: FieldValue.increment(-creatorNewBet.amount), }) log('Updated user balance.') - const newPurchaseStats = calculateCpmmPurchase( - cpmmState, - creatorNewBet.amount, - creatorNewBet.outcome - ) - cpmmState = { pool: newPurchaseStats.newPool, p: newPurchaseStats.newP } trans.update( contractDoc, removeUndefinedProps({ - pool: cpmmState.pool, - // p shouldn't have changed - p: contract.p, + pool: newPool, volume: contract.volume + yourNewBet.amount + creatorNewBet.amount, }) ) @@ -160,6 +148,7 @@ export const acceptchallenge = newEndpoint({}, async (req, auth) => { ], }) ) + log('Updated challenge properties with new acceptance.') await createChallengeAcceptedNotification( user, @@ -167,6 +156,7 @@ export const acceptchallenge = newEndpoint({}, async (req, auth) => { challenge, contract ) + log('Created notification.') return yourNewBetDoc }) diff --git a/web/components/bet-row.tsx b/web/components/bet-row.tsx index 933eb02f..28dabb86 100644 --- a/web/components/bet-row.tsx +++ b/web/components/bet-row.tsx @@ -8,7 +8,6 @@ import { useUser } from 'web/hooks/use-user' import { useUserContractBets } from 'web/hooks/use-user-bets' import { useSaveBinaryShares } from './use-save-binary-shares' import { Col } from './layout/col' -import { CreateChallengeButton } from 'web/components/challenges/create-challenge-button' // Inline version of a bet panel. Opens BetPanel in a new modal. export default function BetRow(props: { @@ -49,10 +48,6 @@ export default function BetRow(props: { : ''} - - - - ) : (
- ) } @@ -282,7 +283,7 @@ function ChallengeContract(props: { contract: Contract; bets: Bet[] }) { const isBinary = contract.outcomeType === 'BINARY' const isPseudoNumeric = contract.outcomeType === 'PSEUDO_NUMERIC' return ( - +
{question} @@ -292,13 +293,9 @@ function ChallengeContract(props: { contract: Contract; bets: Bet[] }) { )} - - -
- {(isBinary || isPseudoNumeric) && ( - - )} -
+ {(isBinary || isPseudoNumeric) && ( + + )}
) @@ -309,8 +306,9 @@ function OpenChallengeContent(props: { challenge: Challenge creator: User user: User | null | undefined + bets: Bet[] }) { - const { contract, challenge, creator, user } = props + const { contract, challenge, creator, user, bets } = props const { question } = contract const [creatorPortfolioHistory, setUsersCreatorPortfolioHistory] = useState< PortfolioMetrics[] @@ -332,6 +330,9 @@ function OpenChallengeContent(props: { (containerRef?.offsetTop ?? 0) - bottomBarHeight + const isBinary = contract.outcomeType === 'BINARY' + const isPseudoNumeric = contract.outcomeType === 'PSEUDO_NUMERIC' + const userColumn = ( challenger: User | null | undefined, portfolioHistory: PortfolioMetrics[], @@ -389,8 +390,17 @@ function OpenChallengeContent(props: { + {(isBinary || isPseudoNumeric) && ( +
+ +
+ )} {question}