From db14a63c0879653532ca3b32e63982a53fe0834e Mon Sep 17 00:00:00 2001 From: Marshall Polaris Date: Tue, 26 Jul 2022 01:03:29 -0700 Subject: [PATCH] Change `sellshares` to be able to sell all shares --- functions/src/sell-shares.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/functions/src/sell-shares.ts b/functions/src/sell-shares.ts index 40ea0f4a..b6238434 100644 --- a/functions/src/sell-shares.ts +++ b/functions/src/sell-shares.ts @@ -16,7 +16,7 @@ import { redeemShares } from './redeem-shares' const bodySchema = z.object({ contractId: z.string(), - shares: z.number(), + shares: z.number().optional(), // leave it out to sell all shares 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 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.`) - const soldShares = Math.min(shares, maxShares) + const soldShares = Math.min(sharesToSell, maxShares) const unfilledBetsSnap = await transaction.get( getUnfilledBetsQuery(contractDoc)