Add and use floor shares

This commit is contained in:
Ian Philips 2022-04-19 15:11:27 -06:00
parent 8905bfbfc8
commit b249d729ca

View File

@ -42,8 +42,12 @@ export function BetPanel(props: {
const { contract, className } = props const { contract, className } = props
const user = useUser() const user = useUser()
const userBets = useUserContractBets(user?.id, contract.id) const userBets = useUserContractBets(user?.id, contract.id)
const { yesShares, noShares } = useSaveShares(contract, userBets) const { yesFloorShares, noFloorShares } = useSaveShares(contract, userBets)
const sharesOutcome = yesShares ? 'YES' : noShares ? 'NO' : undefined const sharesOutcome = yesFloorShares
? 'YES'
: noFloorShares
? 'NO'
: undefined
return ( return (
<Col className={className}> <Col className={className}>
@ -93,10 +97,14 @@ export function BetPanelSwitcher(props: {
const [tradeType, setTradeType] = useState<'BUY' | 'SELL'>('BUY') const [tradeType, setTradeType] = useState<'BUY' | 'SELL'>('BUY')
const { yesShares, noShares } = useSaveShares(contract, userBets) const { yesFloorShares, noFloorShares } = useSaveShares(contract, userBets)
const shares = yesShares || noShares const shares = yesFloorShares || noFloorShares
const sharesOutcome = yesShares ? 'YES' : noShares ? 'NO' : undefined const sharesOutcome = yesFloorShares
? 'YES'
: noFloorShares
? 'NO'
: undefined
useEffect(() => { useEffect(() => {
// Switch back to BUY if the user has sold all their shares. // Switch back to BUY if the user has sold all their shares.
@ -150,7 +158,7 @@ export function BetPanelSwitcher(props: {
{tradeType === 'SELL' && user && sharesOutcome && ( {tradeType === 'SELL' && user && sharesOutcome && (
<SellPanel <SellPanel
contract={contract as FullContract<CPMM, Binary>} contract={contract as FullContract<CPMM, Binary>}
shares={yesShares || noShares} shares={yesFloorShares || noFloorShares}
sharesOutcome={sharesOutcome} sharesOutcome={sharesOutcome}
user={user} user={user}
userBets={userBets ?? []} userBets={userBets ?? []}
@ -464,7 +472,13 @@ export const useSaveShares = (
userBets: Bet[] | undefined userBets: Bet[] | undefined
) => { ) => {
const [savedShares, setSavedShares] = useState< const [savedShares, setSavedShares] = useState<
{ yesShares: number; noShares: number } | undefined | {
yesShares: number
noShares: number
yesFloorShares: number
noFloorShares: number
}
| undefined
>() >()
const [yesBets, noBets] = _.partition( const [yesBets, noBets] = _.partition(
@ -476,6 +490,11 @@ export const useSaveShares = (
_.sumBy(noBets, (bet) => bet.shares), _.sumBy(noBets, (bet) => bet.shares),
] ]
const [yesFloorShares, noFloorShares] = [
Math.floor(yesShares),
Math.floor(noShares),
]
useEffect(() => { useEffect(() => {
// Save yes and no shares to local storage. // Save yes and no shares to local storage.
const savedShares = localStorage.getItem(`${contract.id}-shares`) const savedShares = localStorage.getItem(`${contract.id}-shares`)
@ -492,6 +511,13 @@ export const useSaveShares = (
} }
}, [contract.id, userBets, noShares, yesShares]) }, [contract.id, userBets, noShares, yesShares])
if (userBets) return { yesShares, noShares } if (userBets) return { yesShares, noShares, yesFloorShares, noFloorShares }
return savedShares ?? { yesShares: 0, noShares: 0 } return (
savedShares ?? {
yesShares: 0,
noShares: 0,
yesFloorShares: 0,
noFloorShares: 0,
}
)
} }