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 user = useUser()
const userBets = useUserContractBets(user?.id, contract.id) 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( const { yesFloorShares, noFloorShares } = useSaveShares(
contract as FullContract<CPMM | DPM, Binary>, contract as FullContract<DPM | CPMM, Binary | FreeResponseContract>,
userBets userBets,
topAnswer?.number.toString() || undefined
) )
// TODO: This relies on a hack in useSaveShares, where noFloorShares includes const hasUpShares = yesFloorShares || contract.outcomeType === 'NUMERIC'
// all non-YES shares. Ideally, useSaveShares should group by all outcomes
const hasUpShares =
contract.outcomeType === 'BINARY' ? yesFloorShares : noFloorShares
const hasDownShares = const hasDownShares =
contract.outcomeType === 'BINARY' ? noFloorShares : yesFloorShares noFloorShares && yesFloorShares <= 0 && contract.outcomeType !== 'NUMERIC'
const [upHover, setUpHover] = useState(false) const [upHover, setUpHover] = useState(false)
const [downHover, setDownHover] = 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 { Bet } from 'common/bet'
import { useEffect, useState } from 'react' import { useEffect, useState } from 'react'
import { partition, sumBy } from 'lodash' import { partition, sumBy } from 'lodash'
export const useSaveShares = ( export const useSaveShares = (
contract: FullContract<CPMM | DPM, Binary>, contract: FullContract<CPMM | DPM, Binary | FreeResponseContract>,
userBets: Bet[] | undefined userBets: Bet[] | undefined,
freeResponseAnswerOutcome?: string
) => { ) => {
const [savedShares, setSavedShares] = useState< const [savedShares, setSavedShares] = useState<
| { | {
@ -17,9 +24,11 @@ export const useSaveShares = (
| undefined | undefined
>() >()
const [yesBets, noBets] = partition( // TODO: How do we handle numeric yes / no bets? - maybe bet amounts above vs below the highest peak
userBets ?? [], const [yesBets, noBets] = partition(userBets ?? [], (bet) =>
(bet) => bet.outcome === 'YES' freeResponseAnswerOutcome
? bet.outcome === freeResponseAnswerOutcome
: bet.outcome === 'YES'
) )
const [yesShares, noShares] = [ const [yesShares, noShares] = [
sumBy(yesBets, (bet) => bet.shares), sumBy(yesBets, (bet) => bet.shares),