diff --git a/docs/docs/api.md b/docs/docs/api.md index 64e26de8..986654a4 100644 --- a/docs/docs/api.md +++ b/docs/docs/api.md @@ -582,6 +582,14 @@ $ curl https://manifold.markets/api/v0/market -X POST -H 'Content-Type: applicat "initialProb":25}' ``` + +### `POST /v0/market/[marketId]/close` + +Closes a market on behalf of the authorized user. +- `contractId`: Required. The id of the market to close. +- `closeTime`: Optional. Milliseconds since the epoch to close the market at. If not provided, the market will be closed immediately. Cannot provide close time in past. + + ### `POST /v0/market/[marketId]/resolve` Resolves a market on behalf of the authorized user. diff --git a/functions/src/close-market.ts b/functions/src/close-market.ts index 86d73107..b8b252a7 100644 --- a/functions/src/close-market.ts +++ b/functions/src/close-market.ts @@ -9,16 +9,17 @@ import { APIError, newEndpoint, validate } from './api' const bodySchema = z.object({ contractId: z.string(), + closeTime: z.number().int().nonnegative().optional(), }) export const closemarket = newEndpoint({}, async (req, auth) => { - const { contractId } = validate(bodySchema, req.body) + const { contractId, closeTime } = validate(bodySchema, req.body) const contractDoc = firestore.doc(`contracts/${contractId}`) const contractSnap = await contractDoc.get() if (!contractSnap.exists) throw new APIError(404, 'No contract exists with the provided ID') const contract = contractSnap.data() as Contract - const { creatorId, closeTime } = contract + const { creatorId } = contract const firebaseUser = await admin.auth().getUser(auth.uid) if ( @@ -29,15 +30,22 @@ export const closemarket = newEndpoint({}, async (req, auth) => { throw new APIError(403, 'User is not creator of contract') const now = Date.now() - if (closeTime && closeTime < now) + if (!closeTime && contract.closeTime && contract.closeTime < now) throw new APIError(400, 'Contract already closed') + if (closeTime && closeTime < now) + throw new APIError( + 400, + 'Close time must be in the future. ' + + 'Alternatively, do not provide a close time to close immediately.' + ) + const creator = await getUser(creatorId) if (!creator) throw new APIError(500, 'Creator not found') const updatedContract = { ...contract, - closeTime: now, + closeTime: closeTime ? closeTime : now, } await contractDoc.update(updatedContract)