From e0bb92124096182c3aa9a9ed37623755cc1f8f95 Mon Sep 17 00:00:00 2001 From: Marshall Polaris Date: Sun, 15 May 2022 14:43:24 -0700 Subject: [PATCH] Hoist SellAmountInput logic into SellPanel --- web/components/amount-input.tsx | 59 ++------------------------------- web/components/bet-panel.tsx | 46 +++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 59 deletions(-) diff --git a/web/components/amount-input.tsx b/web/components/amount-input.tsx index ccd668d9..78605b55 100644 --- a/web/components/amount-input.tsx +++ b/web/components/amount-input.tsx @@ -1,12 +1,9 @@ import clsx from 'clsx' import _ from 'lodash' import { useUser } from 'web/hooks/use-user' -import { formatMoney, formatWithCommas } from 'common/util/format' +import { formatMoney } from 'common/util/format' import { Col } from './layout/col' -import { Row } from './layout/row' -import { Bet } from 'common/bet' import { Spacer } from './layout/spacer' -import { calculateCpmmSale } from 'common/calculate-cpmm' import { Binary, CPMM, FullContract } from 'common/contract' import { SiteLink } from './site-link' @@ -143,9 +140,7 @@ export function SellAmountInput(props: { contract: FullContract amount: number | undefined onChange: (newAmount: number | undefined) => void - userBets: Bet[] error: string | undefined - setError: (error: string | undefined) => void disabled?: boolean className?: string inputClassName?: string @@ -153,73 +148,25 @@ export function SellAmountInput(props: { inputRef?: React.MutableRefObject }) { const { - contract, amount, onChange, - userBets, error, - setError, disabled, className, inputClassName, inputRef, } = props - const user = useUser() - - const openUserBets = userBets.filter((bet) => !bet.isSold && !bet.sale) - const [yesBets, noBets] = _.partition( - openUserBets, - (bet) => bet.outcome === 'YES' - ) - const [yesShares, noShares] = [ - _.sumBy(yesBets, (bet) => bet.shares), - _.sumBy(noBets, (bet) => bet.shares), - ] - - const sellOutcome = yesShares ? 'YES' : noShares ? 'NO' : undefined - const shares = Math.round(yesShares) || Math.round(noShares) - - const sharesSold = Math.min(amount ?? 0, shares) - - const { saleValue } = calculateCpmmSale( - contract, - sharesSold, - sellOutcome as 'YES' | 'NO' - ) - - const onAmountChange = (amount: number | undefined) => { - onChange(amount) - - // Check for errors. - if (amount !== undefined) { - if (amount > shares) { - setError(`Maximum ${formatWithCommas(Math.floor(shares))} shares`) - } else { - setError(undefined) - } - } - } - return ( - {user && ( - - - Sale proceeds{' '} - {formatMoney(saleValue)} - - - )} - + > ) } diff --git a/web/components/bet-panel.tsx b/web/components/bet-panel.tsx index e02e14d0..978821f5 100644 --- a/web/components/bet-panel.tsx +++ b/web/components/bet-panel.tsx @@ -1,4 +1,5 @@ import clsx from 'clsx' +import _ from 'lodash' import React, { useEffect, useState } from 'react' import { useUser } from 'web/hooks/use-user' @@ -437,6 +438,40 @@ export function SellPanel(props: { ) const resultProb = getCpmmProbability(newPool, contract.p) + const openUserBets = userBets.filter((bet) => !bet.isSold && !bet.sale) + const [yesBets, noBets] = _.partition( + openUserBets, + (bet) => bet.outcome === 'YES' + ) + const [yesShares, noShares] = [ + _.sumBy(yesBets, (bet) => bet.shares), + _.sumBy(noBets, (bet) => bet.shares), + ] + + const sellOutcome = yesShares ? 'YES' : noShares ? 'NO' : undefined + const ownedShares = Math.round(yesShares) || Math.round(noShares) + + const sharesSold = Math.min(amount ?? 0, ownedShares) + + const { saleValue } = calculateCpmmSale( + contract, + sharesSold, + sellOutcome as 'YES' | 'NO' + ) + + const onAmountChange = (amount: number | undefined) => { + setAmount(amount) + + // Check for errors. + if (amount !== undefined) { + if (amount > ownedShares) { + setError(`Maximum ${formatWithCommas(Math.floor(ownedShares))} shares`) + } else { + setError(undefined) + } + } + } + return ( <> + + + Sale proceeds{' '} + {formatMoney(saleValue)} + + +
Probability