Handle free response outcomes for yes/no shares (#338)

This commit is contained in:
Ian Philips 2022-05-25 16:51:33 -06:00 committed by GitHub
parent 1334840ee0
commit 0c3fa9f065
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 13 deletions

View File

@ -38,16 +38,20 @@ export function QuickBet(props: { contract: Contract }) {
const user = useUser()
const userBets = useUserContractBets(user?.id, contract.id)
const topAnswer =
contract.outcomeType === 'FREE_RESPONSE'
? getTopAnswer(contract as FreeResponseContract)
: undefined
// TODO: yes/no from useSaveShares doesn't work on numeric contracts
const { yesFloorShares, noFloorShares } = useSaveShares(
contract as FullContract<CPMM | DPM, Binary>,
userBets
contract as FullContract<DPM | CPMM, Binary | FreeResponseContract>,
userBets,
topAnswer?.number.toString() || undefined
)
// TODO: This relies on a hack in useSaveShares, where noFloorShares includes
// all non-YES shares. Ideally, useSaveShares should group by all outcomes
const hasUpShares =
contract.outcomeType === 'BINARY' ? yesFloorShares : noFloorShares
const hasUpShares = yesFloorShares || contract.outcomeType === 'NUMERIC'
const hasDownShares =
contract.outcomeType === 'BINARY' ? noFloorShares : yesFloorShares
noFloorShares && yesFloorShares <= 0 && contract.outcomeType !== 'NUMERIC'
const [upHover, setUpHover] = useState(false)
const [downHover, setDownHover] = useState(false)

View File

@ -1,11 +1,18 @@
import { Binary, CPMM, DPM, FullContract } from 'common/contract'
import {
Binary,
CPMM,
DPM,
FreeResponseContract,
FullContract,
} from 'common/contract'
import { Bet } from 'common/bet'
import { useEffect, useState } from 'react'
import { partition, sumBy } from 'lodash'
export const useSaveShares = (
contract: FullContract<CPMM | DPM, Binary>,
userBets: Bet[] | undefined
contract: FullContract<CPMM | DPM, Binary | FreeResponseContract>,
userBets: Bet[] | undefined,
freeResponseAnswerOutcome?: string
) => {
const [savedShares, setSavedShares] = useState<
| {
@ -17,9 +24,11 @@ export const useSaveShares = (
| undefined
>()
const [yesBets, noBets] = partition(
userBets ?? [],
(bet) => bet.outcome === 'YES'
// TODO: How do we handle numeric yes / no bets? - maybe bet amounts above vs below the highest peak
const [yesBets, noBets] = partition(userBets ?? [], (bet) =>
freeResponseAnswerOutcome
? bet.outcome === freeResponseAnswerOutcome
: bet.outcome === 'YES'
)
const [yesShares, noShares] = [
sumBy(yesBets, (bet) => bet.shares),