Change sellshares
to be able to sell all shares
This commit is contained in:
parent
ad46a60c4f
commit
db14a63c08
|
@ -16,7 +16,7 @@ import { redeemShares } from './redeem-shares'
|
||||||
|
|
||||||
const bodySchema = z.object({
|
const bodySchema = z.object({
|
||||||
contractId: z.string(),
|
contractId: z.string(),
|
||||||
shares: z.number(),
|
shares: z.number().optional(), // leave it out to sell all shares
|
||||||
outcome: z.enum(['YES', 'NO']),
|
outcome: z.enum(['YES', 'NO']),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -49,11 +49,12 @@ export const sellshares = newEndpoint({}, async (req, auth) => {
|
||||||
|
|
||||||
const outcomeBets = userBets.filter((bet) => bet.outcome == outcome)
|
const outcomeBets = userBets.filter((bet) => bet.outcome == outcome)
|
||||||
const maxShares = sumBy(outcomeBets, (bet) => bet.shares)
|
const maxShares = sumBy(outcomeBets, (bet) => bet.shares)
|
||||||
|
const sharesToSell = shares ?? maxShares
|
||||||
|
|
||||||
if (!floatingLesserEqual(shares, maxShares))
|
if (!floatingLesserEqual(sharesToSell, maxShares))
|
||||||
throw new APIError(400, `You can only sell up to ${maxShares} shares.`)
|
throw new APIError(400, `You can only sell up to ${maxShares} shares.`)
|
||||||
|
|
||||||
const soldShares = Math.min(shares, maxShares)
|
const soldShares = Math.min(sharesToSell, maxShares)
|
||||||
|
|
||||||
const unfilledBetsSnap = await transaction.get(
|
const unfilledBetsSnap = await transaction.get(
|
||||||
getUnfilledBetsQuery(contractDoc)
|
getUnfilledBetsQuery(contractDoc)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user