2022-01-11 03:41:42 +00:00
|
|
|
import clsx from 'clsx'
|
2022-03-02 03:31:48 +00:00
|
|
|
import _ from 'lodash'
|
2022-05-09 13:04:36 +00:00
|
|
|
import { useUser } from 'web/hooks/use-user'
|
|
|
|
import { formatMoney, formatWithCommas } from 'common/util/format'
|
2022-01-11 03:41:42 +00:00
|
|
|
import { Col } from './layout/col'
|
|
|
|
import { Row } from './layout/row'
|
2022-05-09 13:04:36 +00:00
|
|
|
import { Bet } from 'common/bet'
|
2022-03-02 03:31:48 +00:00
|
|
|
import { Spacer } from './layout/spacer'
|
2022-05-09 13:04:36 +00:00
|
|
|
import { calculateCpmmSale } from 'common/calculate-cpmm'
|
|
|
|
import { Binary, CPMM, FullContract } from 'common/contract'
|
2022-05-03 20:36:54 +00:00
|
|
|
import { SiteLink } from './site-link'
|
2022-01-11 03:41:42 +00:00
|
|
|
|
|
|
|
export function AmountInput(props: {
|
|
|
|
amount: number | undefined
|
|
|
|
onChange: (newAmount: number | undefined) => void
|
|
|
|
error: string | undefined
|
2022-03-29 19:56:56 +00:00
|
|
|
label: string
|
2022-01-11 03:41:42 +00:00
|
|
|
disabled?: boolean
|
|
|
|
className?: string
|
|
|
|
inputClassName?: string
|
2022-01-26 20:08:03 +00:00
|
|
|
// Needed to focus the amount input
|
|
|
|
inputRef?: React.MutableRefObject<any>
|
2022-03-29 19:56:56 +00:00
|
|
|
children?: any
|
2022-01-11 03:41:42 +00:00
|
|
|
}) {
|
|
|
|
const {
|
|
|
|
amount,
|
|
|
|
onChange,
|
|
|
|
error,
|
2022-03-29 19:56:56 +00:00
|
|
|
label,
|
2022-01-11 03:41:42 +00:00
|
|
|
disabled,
|
|
|
|
className,
|
|
|
|
inputClassName,
|
2022-01-26 20:08:03 +00:00
|
|
|
inputRef,
|
2022-03-29 19:56:56 +00:00
|
|
|
children,
|
2022-01-11 03:41:42 +00:00
|
|
|
} = props
|
|
|
|
|
|
|
|
const onAmountChange = (str: string) => {
|
2022-04-07 03:48:06 +00:00
|
|
|
const amount = parseInt(str.replace(/\D/g, ''))
|
|
|
|
const isInvalid = !str || isNaN(amount)
|
|
|
|
onChange(isInvalid ? undefined : amount)
|
2022-01-11 03:41:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Col className={className}>
|
|
|
|
<label className="input-group">
|
2022-03-29 19:56:56 +00:00
|
|
|
<span className="bg-gray-200 text-sm">{label}</span>
|
2022-01-11 03:41:42 +00:00
|
|
|
<input
|
|
|
|
className={clsx(
|
2022-05-02 16:15:00 +00:00
|
|
|
'input input-bordered max-w-[200px] text-lg',
|
2022-01-11 03:41:42 +00:00
|
|
|
error && 'input-error',
|
|
|
|
inputClassName
|
|
|
|
)}
|
2022-01-26 20:08:03 +00:00
|
|
|
ref={inputRef}
|
2022-04-07 03:48:06 +00:00
|
|
|
type="text"
|
|
|
|
pattern="[0-9]*"
|
|
|
|
inputMode="numeric"
|
2022-01-11 03:41:42 +00:00
|
|
|
placeholder="0"
|
2022-04-07 03:48:06 +00:00
|
|
|
maxLength={6}
|
2022-01-11 03:41:42 +00:00
|
|
|
value={amount ?? ''}
|
|
|
|
disabled={disabled}
|
|
|
|
onChange={(e) => onAmountChange(e.target.value)}
|
|
|
|
/>
|
|
|
|
</label>
|
2022-03-02 03:31:48 +00:00
|
|
|
|
|
|
|
<Spacer h={4} />
|
|
|
|
|
2022-01-14 23:39:17 +00:00
|
|
|
{error && (
|
2022-03-02 03:31:48 +00:00
|
|
|
<div className="mb-2 mr-auto self-center whitespace-nowrap text-xs font-medium tracking-wide text-red-500">
|
2022-05-03 20:36:54 +00:00
|
|
|
{error === 'Insufficient balance' ? (
|
|
|
|
<>
|
|
|
|
Not enough funds.
|
|
|
|
<span className="ml-1 text-indigo-500">
|
|
|
|
<SiteLink href="/add-funds">Buy more?</SiteLink>
|
|
|
|
</span>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
error
|
|
|
|
)}
|
2022-01-14 23:39:17 +00:00
|
|
|
</div>
|
|
|
|
)}
|
2022-03-29 19:56:56 +00:00
|
|
|
|
|
|
|
{children}
|
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function BuyAmountInput(props: {
|
|
|
|
amount: number | undefined
|
|
|
|
onChange: (newAmount: number | undefined) => void
|
|
|
|
error: string | undefined
|
|
|
|
setError: (error: string | undefined) => void
|
|
|
|
minimumAmount?: number
|
|
|
|
disabled?: boolean
|
|
|
|
className?: string
|
|
|
|
inputClassName?: string
|
|
|
|
// Needed to focus the amount input
|
|
|
|
inputRef?: React.MutableRefObject<any>
|
|
|
|
}) {
|
|
|
|
const {
|
|
|
|
amount,
|
|
|
|
onChange,
|
|
|
|
error,
|
|
|
|
setError,
|
|
|
|
disabled,
|
|
|
|
className,
|
|
|
|
inputClassName,
|
|
|
|
minimumAmount,
|
|
|
|
inputRef,
|
|
|
|
} = props
|
|
|
|
|
|
|
|
const user = useUser()
|
|
|
|
|
|
|
|
const onAmountChange = (amount: number | undefined) => {
|
|
|
|
onChange(amount)
|
|
|
|
|
|
|
|
// Check for errors.
|
|
|
|
if (amount !== undefined) {
|
2022-04-13 17:52:12 +00:00
|
|
|
if (user && user.balance < amount) {
|
2022-03-29 19:56:56 +00:00
|
|
|
setError('Insufficient balance')
|
|
|
|
} else if (minimumAmount && amount < minimumAmount) {
|
|
|
|
setError('Minimum amount: ' + formatMoney(minimumAmount))
|
|
|
|
} else {
|
|
|
|
setError(undefined)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<AmountInput
|
|
|
|
amount={amount}
|
|
|
|
onChange={onAmountChange}
|
|
|
|
label="M$"
|
|
|
|
error={error}
|
|
|
|
disabled={disabled}
|
|
|
|
className={className}
|
|
|
|
inputClassName={inputClassName}
|
|
|
|
inputRef={inputRef}
|
2022-04-13 17:52:12 +00:00
|
|
|
/>
|
2022-03-29 19:56:56 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function SellAmountInput(props: {
|
|
|
|
contract: FullContract<CPMM, Binary>
|
|
|
|
amount: number | undefined
|
|
|
|
onChange: (newAmount: number | undefined) => void
|
|
|
|
userBets: Bet[]
|
|
|
|
error: string | undefined
|
|
|
|
setError: (error: string | undefined) => void
|
|
|
|
disabled?: boolean
|
|
|
|
className?: string
|
|
|
|
inputClassName?: string
|
|
|
|
// Needed to focus the amount input
|
|
|
|
inputRef?: React.MutableRefObject<any>
|
|
|
|
}) {
|
|
|
|
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
|
2022-05-04 15:47:45 +00:00
|
|
|
const shares = Math.round(yesShares) || Math.round(noShares)
|
|
|
|
|
|
|
|
const sharesSold = Math.min(amount ?? 0, shares)
|
2022-03-29 19:56:56 +00:00
|
|
|
|
2022-03-30 02:30:04 +00:00
|
|
|
const { saleValue } = calculateCpmmSale(
|
|
|
|
contract,
|
|
|
|
sharesSold,
|
|
|
|
sellOutcome as 'YES' | 'NO'
|
|
|
|
)
|
2022-03-29 19:56:56 +00:00
|
|
|
|
|
|
|
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 (
|
|
|
|
<AmountInput
|
|
|
|
amount={amount}
|
|
|
|
onChange={onAmountChange}
|
|
|
|
label="Qty"
|
|
|
|
error={error}
|
|
|
|
disabled={disabled}
|
|
|
|
className={className}
|
|
|
|
inputClassName={inputClassName}
|
|
|
|
inputRef={inputRef}
|
|
|
|
>
|
|
|
|
{user && (
|
|
|
|
<Col className="gap-3 text-sm">
|
2022-03-02 03:31:48 +00:00
|
|
|
<Row className="items-center justify-between gap-2 text-gray-500">
|
2022-03-29 19:56:56 +00:00
|
|
|
Sale proceeds{' '}
|
|
|
|
<span className="text-neutral">{formatMoney(saleValue)}</span>
|
2022-01-14 23:39:17 +00:00
|
|
|
</Row>
|
|
|
|
</Col>
|
|
|
|
)}
|
2022-03-29 19:56:56 +00:00
|
|
|
</AmountInput>
|
2022-01-11 03:41:42 +00:00
|
|
|
)
|
|
|
|
}
|