Sell bet UI
This commit is contained in:
parent
96d585c327
commit
9245fb218c
|
@ -14,7 +14,7 @@ import {
|
||||||
getProbability,
|
getProbability,
|
||||||
calculateShares,
|
calculateShares,
|
||||||
getProbabilityAfterBet,
|
getProbabilityAfterBet,
|
||||||
} from '../lib/calculation/contract'
|
} from '../lib/calculate'
|
||||||
import { firebaseLogin } from '../lib/firebase/users'
|
import { firebaseLogin } from '../lib/firebase/users'
|
||||||
|
|
||||||
export function BetPanel(props: { contract: Contract; className?: string }) {
|
export function BetPanel(props: { contract: Contract; className?: string }) {
|
||||||
|
|
|
@ -13,11 +13,13 @@ import { Row } from './layout/row'
|
||||||
import { UserLink } from './user-page'
|
import { UserLink } from './user-page'
|
||||||
import {
|
import {
|
||||||
calculatePayout,
|
calculatePayout,
|
||||||
|
calculateSaleAmount,
|
||||||
currentValue,
|
currentValue,
|
||||||
resolvedPayout,
|
resolvedPayout,
|
||||||
} from '../lib/calculation/contract'
|
} from '../lib/calculate'
|
||||||
import clsx from 'clsx'
|
import clsx from 'clsx'
|
||||||
import { cloudFunction } from '../lib/firebase/api-call'
|
import { cloudFunction } from '../lib/firebase/api-call'
|
||||||
|
import { ConfirmationButton } from './confirmation-button'
|
||||||
|
|
||||||
export function BetsList(props: { user: User }) {
|
export function BetsList(props: { user: User }) {
|
||||||
const { user } = props
|
const { user } = props
|
||||||
|
@ -229,6 +231,11 @@ export function ContractBetsTable(props: {
|
||||||
}) {
|
}) {
|
||||||
const { contract, bets, className } = 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
|
const { isResolved } = contract
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -238,16 +245,21 @@ export function ContractBetsTable(props: {
|
||||||
<tr className="p-2">
|
<tr className="p-2">
|
||||||
<th>Date</th>
|
<th>Date</th>
|
||||||
<th>Outcome</th>
|
<th>Outcome</th>
|
||||||
<th>Bet</th>
|
<th>Amount</th>
|
||||||
<th>Probability</th>
|
<th>Probability</th>
|
||||||
{!isResolved && <th>Est. max payout</th>}
|
{!isResolved && <th>Est. max payout</th>}
|
||||||
<th>{isResolved ? <>Payout</> : <>Current value</>}</th>
|
<th>{isResolved ? <>Payout</> : <>Current value</>}</th>
|
||||||
{!isResolved && <th></th>}
|
<th></th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{bets.map((bet) => (
|
{buys.map((bet) => (
|
||||||
<BetRow key={bet.id} bet={bet} contract={contract} />
|
<BetRow
|
||||||
|
key={bet.id}
|
||||||
|
bet={bet}
|
||||||
|
sale={salesDict[bet.id]}
|
||||||
|
contract={contract}
|
||||||
|
/>
|
||||||
))}
|
))}
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
@ -255,8 +267,8 @@ export function ContractBetsTable(props: {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function BetRow(props: { bet: Bet; contract: Contract }) {
|
function BetRow(props: { bet: Bet; contract: Contract; sale?: Bet }) {
|
||||||
const { bet, contract } = props
|
const { bet, sale, contract } = props
|
||||||
const {
|
const {
|
||||||
amount,
|
amount,
|
||||||
outcome,
|
outcome,
|
||||||
|
@ -264,14 +276,13 @@ function BetRow(props: { bet: Bet; contract: Contract }) {
|
||||||
probBefore,
|
probBefore,
|
||||||
probAfter,
|
probAfter,
|
||||||
shares,
|
shares,
|
||||||
sale,
|
|
||||||
isSold,
|
isSold,
|
||||||
} = bet
|
} = bet
|
||||||
const { isResolved } = contract
|
const { isResolved } = contract
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<tr>
|
<tr>
|
||||||
<td>{dayjs(createdTime).format('MMM D, H:mma')}</td>
|
<td>{dayjs(createdTime).format('MMM D, h:mma')}</td>
|
||||||
<td>
|
<td>
|
||||||
<OutcomeLabel outcome={outcome} />
|
<OutcomeLabel outcome={outcome} />
|
||||||
</td>
|
</td>
|
||||||
|
@ -281,25 +292,39 @@ function BetRow(props: { bet: Bet; contract: Contract }) {
|
||||||
</td>
|
</td>
|
||||||
{!isResolved && <td>{formatMoney(shares)}</td>}
|
{!isResolved && <td>{formatMoney(shares)}</td>}
|
||||||
<td>
|
<td>
|
||||||
{formatMoney(
|
{bet.isSold
|
||||||
isResolved
|
? 'N/A'
|
||||||
? resolvedPayout(contract, bet)
|
: formatMoney(
|
||||||
: currentValue(contract, bet)
|
isResolved
|
||||||
)}
|
? resolvedPayout(contract, bet)
|
||||||
|
: bet.sale
|
||||||
|
? bet.sale.amount ?? 0
|
||||||
|
: currentValue(contract, bet)
|
||||||
|
)}
|
||||||
</td>
|
</td>
|
||||||
|
|
||||||
{!isResolved && !sale && !isSold && (
|
{sale ? (
|
||||||
<td>
|
<td>SOLD for {formatMoney(Math.abs(sale.amount))}</td>
|
||||||
<button
|
) : (
|
||||||
className="btn"
|
!isResolved &&
|
||||||
onClick={async (e) => {
|
!isSold && (
|
||||||
e.preventDefault()
|
<td className="text-neutral">
|
||||||
await sellBet({ contractId: contract.id, betId: bet.id })
|
<ConfirmationButton
|
||||||
}}
|
id={`sell-${bet.id}`}
|
||||||
>
|
openModelBtn={{ className: 'btn-sm', label: 'Sell' }}
|
||||||
Sell
|
submitBtn={{ className: 'btn-primary' }}
|
||||||
</button>
|
onSubmit={async () => {
|
||||||
</td>
|
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>
|
</tr>
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { Bet } from '../firebase/bets'
|
import { Bet } from './firebase/bets'
|
||||||
import { Contract } from '../firebase/contracts'
|
import { Contract } from './firebase/contracts'
|
||||||
|
|
||||||
const fees = 0.02
|
const fees = 0.02
|
||||||
|
|
||||||
|
@ -44,16 +44,24 @@ export function calculatePayout(
|
||||||
if (outcome === 'CANCEL') return amount
|
if (outcome === 'CANCEL') return amount
|
||||||
if (betOutcome !== outcome) return 0
|
if (betOutcome !== outcome) return 0
|
||||||
|
|
||||||
let { totalShares } = contract
|
const { totalShares } = contract
|
||||||
|
|
||||||
// Fake data if not set.
|
if (totalShares[outcome] === 0) return 0
|
||||||
// if (!totalShares) totalShares = { YES: 100, NO: 100 }
|
|
||||||
|
|
||||||
const startPool = contract.startPool.YES + contract.startPool.NO
|
const startPool = contract.startPool.YES + contract.startPool.NO
|
||||||
const pool = contract.pool.YES + contract.pool.NO - startPool
|
const pool = contract.pool.YES + contract.pool.NO - startPool
|
||||||
|
|
||||||
|
console.log(
|
||||||
|
'calcPayout',
|
||||||
|
'shares',
|
||||||
|
shares,
|
||||||
|
'totalShares',
|
||||||
|
totalShares,
|
||||||
|
'pool'
|
||||||
|
)
|
||||||
return (1 - fees) * (shares / totalShares[outcome]) * pool
|
return (1 - fees) * (shares / totalShares[outcome]) * pool
|
||||||
}
|
}
|
||||||
|
|
||||||
export function resolvedPayout(contract: Contract, bet: Bet) {
|
export function resolvedPayout(contract: Contract, bet: Bet) {
|
||||||
if (contract.resolution)
|
if (contract.resolution)
|
||||||
return calculatePayout(contract, bet, 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 yesPayout = calculatePayout(contract, bet, 'YES')
|
||||||
const noPayout = calculatePayout(contract, bet, 'NO')
|
const noPayout = calculatePayout(contract, bet, 'NO')
|
||||||
|
|
||||||
|
console.log('calculate value', {
|
||||||
|
prob,
|
||||||
|
yesPayout,
|
||||||
|
noPayout,
|
||||||
|
amount: bet.amount,
|
||||||
|
})
|
||||||
|
|
||||||
return prob * yesPayout + (1 - prob) * noPayout
|
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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user