Sell bets. Various fixes

This commit is contained in:
James Grugett 2022-02-15 23:37:16 -06:00
parent 6f5ac2d1d1
commit fad1c8aaea
6 changed files with 28 additions and 32 deletions

View File

@ -68,8 +68,8 @@ export function getFreeAnswerAnte(
anteBetId: string anteBetId: string
) { ) {
const { totalBets, totalShares } = contract const { totalBets, totalShares } = contract
const amount = totalBets.NONE const amount = totalBets['0']
const shares = totalShares.NONE const shares = totalShares['0']
const { createdTime } = contract const { createdTime } = contract

View File

@ -1,7 +1,7 @@
import { Bet } from './bet' import { Bet } from './bet'
import { calculateShareValue, deductFees, getProbability } from './calculate' import { calculateShareValue, deductFees, getProbability } from './calculate'
import { Contract } from './contract' import { Contract } from './contract'
import { CREATOR_FEE, FEES } from './fees' import { CREATOR_FEE } from './fees'
import { User } from './user' import { User } from './user'
export const getSellBetInfo = ( export const getSellBetInfo = (
@ -10,30 +10,21 @@ export const getSellBetInfo = (
contract: Contract, contract: Contract,
newBetId: string newBetId: string
) => { ) => {
const { pool, totalShares, totalBets } = contract
const { id: betId, amount, shares, outcome } = bet const { id: betId, amount, shares, outcome } = bet
const { YES: yesPool, NO: noPool } = contract.pool
const { YES: yesShares, NO: noShares } = contract.totalShares
const { YES: yesBets, NO: noBets } = contract.totalBets
const adjShareValue = calculateShareValue(contract, bet) const adjShareValue = calculateShareValue(contract, bet)
const newPool = const newPool = { ...pool, [outcome]: pool[outcome] - adjShareValue }
outcome === 'YES'
? { YES: yesPool - adjShareValue, NO: noPool }
: { YES: yesPool, NO: noPool - adjShareValue }
const newTotalShares = const newTotalShares = {
outcome === 'YES' ...totalShares,
? { YES: yesShares - shares, NO: noShares } [outcome]: totalShares[outcome] - shares,
: { YES: yesShares, NO: noShares - shares } }
const newTotalBets = const newTotalBets = { ...totalBets, [outcome]: totalBets[outcome] - amount }
outcome === 'YES'
? { YES: yesBets - amount, NO: noBets }
: { YES: yesBets, NO: noBets - amount }
const probBefore = getProbability(contract.totalShares) const probBefore = getProbability(totalShares)
const probAfter = getProbability(newTotalShares) const probAfter = getProbability(newTotalShares)
const profit = adjShareValue - amount const profit = adjShareValue - amount

View File

@ -36,7 +36,7 @@ export const resolveMarket = functions
if (!['YES', 'NO', 'MKT', 'CANCEL'].includes(outcome)) if (!['YES', 'NO', 'MKT', 'CANCEL'].includes(outcome))
return { status: 'error', message: 'Invalid outcome' } return { status: 'error', message: 'Invalid outcome' }
} else if (outcomeType === 'MULTI') { } else if (outcomeType === 'MULTI') {
if (isNaN(+outcome)) if (outcome !== 'CANCEL' && isNaN(+outcome))
return { status: 'error', message: 'Invalid outcome' } return { status: 'error', message: 'Invalid outcome' }
} else { } else {
return { status: 'error', message: 'Invalid contract outcomeType' } return { status: 'error', message: 'Invalid contract outcomeType' }

View File

@ -401,8 +401,9 @@ function CreateAnswerInput(props: { contract: Contract<'MULTI'> }) {
)} )}
<button <button
className={clsx( className={clsx(
'btn btn-sm self-end mt-2', 'btn self-end mt-2',
canSubmit ? 'btn-outline' : 'btn-disabled' canSubmit ? 'btn-outline' : 'btn-disabled',
isSubmitting && 'loading'
)} )}
disabled={!canSubmit} disabled={!canSubmit}
onClick={submitAnswer} onClick={submitAnswer}
@ -434,12 +435,12 @@ function AnswerResolvePanel(props: {
const [error, setError] = useState<string | undefined>(undefined) const [error, setError] = useState<string | undefined>(undefined)
const onResolve = async () => { const onResolve = async () => {
if (answer === undefined) return if (resolveOption === 'CHOOSE' && answer === undefined) return
setIsSubmitting(true) setIsSubmitting(true)
const result = await resolveMarket({ const result = await resolveMarket({
outcome: answer, outcome: resolveOption === 'CHOOSE' ? (answer as string) : 'CANCEL',
contractId: contract.id, contractId: contract.id,
}).then((r) => r.data as any) }).then((r) => r.data as any)

View File

@ -302,8 +302,10 @@ function TruncatedComment(props: {
function FeedQuestion(props: { contract: Contract }) { function FeedQuestion(props: { contract: Contract }) {
const { contract } = props const { contract } = props
const { creatorName, creatorUsername, question, resolution } = contract const { creatorName, creatorUsername, question, resolution, outcomeType } =
contract
const { truePool } = contractMetrics(contract) const { truePool } = contractMetrics(contract)
const isBinary = outcomeType === 'BINARY'
// Currently hidden on mobile; ideally we'd fit this in somewhere. // Currently hidden on mobile; ideally we'd fit this in somewhere.
const closeMessage = const closeMessage =
@ -339,11 +341,13 @@ function FeedQuestion(props: { contract: Contract }) {
> >
{question} {question}
</SiteLink> </SiteLink>
<ResolutionOrChance {(isBinary || resolution) && (
className="items-center" <ResolutionOrChance
resolution={resolution} className="items-center"
probPercent={getBinaryProbPercent(contract)} resolution={resolution}
/> probPercent={getBinaryProbPercent(contract)}
/>
)}
</Col> </Col>
<TruncatedComment <TruncatedComment
comment={contract.description} comment={contract.description}

View File

@ -111,7 +111,7 @@ export const ContractOverview = (props: {
)} )}
{/* Show a delete button for contracts without any trading */} {/* Show a delete button for contracts without any trading */}
{isCreator && (isBinary ? bets.length <= 2 : bets.length <= 1) && ( {isCreator && bets.length === 0 && (
<> <>
<button <button
className="btn btn-xs btn-error btn-outline mt-1 max-w-fit self-end" className="btn btn-xs btn-error btn-outline mt-1 max-w-fit self-end"