Sell bet UI

This commit is contained in:
jahooma 2021-12-24 12:36:22 -05:00
parent 96d585c327
commit 9245fb218c
3 changed files with 111 additions and 32 deletions

View File

@ -14,7 +14,7 @@ import {
getProbability,
calculateShares,
getProbabilityAfterBet,
} from '../lib/calculation/contract'
} from '../lib/calculate'
import { firebaseLogin } from '../lib/firebase/users'
export function BetPanel(props: { contract: Contract; className?: string }) {

View File

@ -13,11 +13,13 @@ import { Row } from './layout/row'
import { UserLink } from './user-page'
import {
calculatePayout,
calculateSaleAmount,
currentValue,
resolvedPayout,
} from '../lib/calculation/contract'
} from '../lib/calculate'
import clsx from 'clsx'
import { cloudFunction } from '../lib/firebase/api-call'
import { ConfirmationButton } from './confirmation-button'
export function BetsList(props: { user: User }) {
const { user } = props
@ -229,6 +231,11 @@ export function ContractBetsTable(props: {
}) {
const { contract, bets, className } = props
const [sales, buys] = _.partition(bets, (bet) => bet.sale)
const salesDict = _.fromPairs(
sales.map((sale) => [sale.sale?.betId ?? '', sale])
)
const { isResolved } = contract
return (
@ -238,16 +245,21 @@ export function ContractBetsTable(props: {
<tr className="p-2">
<th>Date</th>
<th>Outcome</th>
<th>Bet</th>
<th>Amount</th>
<th>Probability</th>
{!isResolved && <th>Est. max payout</th>}
<th>{isResolved ? <>Payout</> : <>Current value</>}</th>
{!isResolved && <th></th>}
<th></th>
</tr>
</thead>
<tbody>
{bets.map((bet) => (
<BetRow key={bet.id} bet={bet} contract={contract} />
{buys.map((bet) => (
<BetRow
key={bet.id}
bet={bet}
sale={salesDict[bet.id]}
contract={contract}
/>
))}
</tbody>
</table>
@ -255,8 +267,8 @@ export function ContractBetsTable(props: {
)
}
function BetRow(props: { bet: Bet; contract: Contract }) {
const { bet, contract } = props
function BetRow(props: { bet: Bet; contract: Contract; sale?: Bet }) {
const { bet, sale, contract } = props
const {
amount,
outcome,
@ -264,14 +276,13 @@ function BetRow(props: { bet: Bet; contract: Contract }) {
probBefore,
probAfter,
shares,
sale,
isSold,
} = bet
const { isResolved } = contract
return (
<tr>
<td>{dayjs(createdTime).format('MMM D, H:mma')}</td>
<td>{dayjs(createdTime).format('MMM D, h:mma')}</td>
<td>
<OutcomeLabel outcome={outcome} />
</td>
@ -281,25 +292,39 @@ function BetRow(props: { bet: Bet; contract: Contract }) {
</td>
{!isResolved && <td>{formatMoney(shares)}</td>}
<td>
{formatMoney(
isResolved
? resolvedPayout(contract, bet)
: currentValue(contract, bet)
)}
{bet.isSold
? 'N/A'
: formatMoney(
isResolved
? resolvedPayout(contract, bet)
: bet.sale
? bet.sale.amount ?? 0
: currentValue(contract, bet)
)}
</td>
{!isResolved && !sale && !isSold && (
<td>
<button
className="btn"
onClick={async (e) => {
e.preventDefault()
await sellBet({ contractId: contract.id, betId: bet.id })
}}
>
Sell
</button>
</td>
{sale ? (
<td>SOLD for {formatMoney(Math.abs(sale.amount))}</td>
) : (
!isResolved &&
!isSold && (
<td className="text-neutral">
<ConfirmationButton
id={`sell-${bet.id}`}
openModelBtn={{ className: 'btn-sm', label: 'Sell' }}
submitBtn={{ className: 'btn-primary' }}
onSubmit={async () => {
await sellBet({ contractId: contract.id, betId: bet.id })
}}
>
<div className="text-2xl mb-4">Sell</div>
<div>
Do you want to sell your {formatMoney(bet.amount)} bet for{' '}
{formatMoney(calculateSaleAmount(contract, bet))}?
</div>
</ConfirmationButton>
</td>
)
)}
</tr>
)

View File

@ -1,5 +1,5 @@
import { Bet } from '../firebase/bets'
import { Contract } from '../firebase/contracts'
import { Bet } from './firebase/bets'
import { Contract } from './firebase/contracts'
const fees = 0.02
@ -44,16 +44,24 @@ export function calculatePayout(
if (outcome === 'CANCEL') return amount
if (betOutcome !== outcome) return 0
let { totalShares } = contract
const { totalShares } = contract
// Fake data if not set.
// if (!totalShares) totalShares = { YES: 100, NO: 100 }
if (totalShares[outcome] === 0) return 0
const startPool = contract.startPool.YES + contract.startPool.NO
const pool = contract.pool.YES + contract.pool.NO - startPool
console.log(
'calcPayout',
'shares',
shares,
'totalShares',
totalShares,
'pool'
)
return (1 - fees) * (shares / totalShares[outcome]) * pool
}
export function resolvedPayout(contract: Contract, bet: Bet) {
if (contract.resolution)
return calculatePayout(contract, bet, contract.resolution)
@ -65,5 +73,51 @@ export function currentValue(contract: Contract, bet: Bet) {
const yesPayout = calculatePayout(contract, bet, 'YES')
const noPayout = calculatePayout(contract, bet, 'NO')
console.log('calculate value', {
prob,
yesPayout,
noPayout,
amount: bet.amount,
})
return prob * yesPayout + (1 - prob) * noPayout
}
export function calculateSaleAmount(contract: Contract, bet: Bet) {
const { shares, outcome } = bet
const { YES: yesPool, NO: noPool } = contract.pool
const { YES: yesStart, NO: noStart } = contract.startPool
const { YES: yesShares, NO: noShares } = contract.totalShares
const [y, n, s] = [yesPool, noPool, shares]
const shareValue =
outcome === 'YES'
? // https://www.wolframalpha.com/input/?i=b+%2B+%28b+n%5E2%29%2F%28y+%28-b+%2B+y%29%29+%3D+c+solve+b
(n ** 2 +
s * y +
y ** 2 -
Math.sqrt(
n ** 4 + (s - y) ** 2 * y ** 2 + 2 * n ** 2 * y * (s + y)
)) /
(2 * y)
: (y ** 2 +
s * n +
n ** 2 -
Math.sqrt(
y ** 4 + (s - n) ** 2 * n ** 2 + 2 * y ** 2 * n * (s + n)
)) /
(2 * n)
const startPool = yesStart + noStart
const pool = yesPool + noPool - startPool
const f = outcome === 'YES' ? pool / yesShares : pool / noShares
const myPool = outcome === 'YES' ? yesPool - yesStart : noPool - noStart
const adjShareValue = Math.min(Math.min(1, f) * shareValue, myPool)
const saleAmount = (1 - fees) * adjShareValue
return saleAmount
}