Add docs fo /close, allow to pass closeTime
This commit is contained in:
parent
08202c3ede
commit
91f89ccb3d
|
@ -582,6 +582,14 @@ $ curl https://manifold.markets/api/v0/market -X POST -H 'Content-Type: applicat
|
||||||
"initialProb":25}'
|
"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`
|
### `POST /v0/market/[marketId]/resolve`
|
||||||
|
|
||||||
Resolves a market on behalf of the authorized user.
|
Resolves a market on behalf of the authorized user.
|
||||||
|
|
|
@ -9,16 +9,17 @@ import { APIError, newEndpoint, validate } from './api'
|
||||||
|
|
||||||
const bodySchema = z.object({
|
const bodySchema = z.object({
|
||||||
contractId: z.string(),
|
contractId: z.string(),
|
||||||
|
closeTime: z.number().int().nonnegative().optional(),
|
||||||
})
|
})
|
||||||
|
|
||||||
export const closemarket = newEndpoint({}, async (req, auth) => {
|
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 contractDoc = firestore.doc(`contracts/${contractId}`)
|
||||||
const contractSnap = await contractDoc.get()
|
const contractSnap = await contractDoc.get()
|
||||||
if (!contractSnap.exists)
|
if (!contractSnap.exists)
|
||||||
throw new APIError(404, 'No contract exists with the provided ID')
|
throw new APIError(404, 'No contract exists with the provided ID')
|
||||||
const contract = contractSnap.data() as Contract
|
const contract = contractSnap.data() as Contract
|
||||||
const { creatorId, closeTime } = contract
|
const { creatorId } = contract
|
||||||
const firebaseUser = await admin.auth().getUser(auth.uid)
|
const firebaseUser = await admin.auth().getUser(auth.uid)
|
||||||
|
|
||||||
if (
|
if (
|
||||||
|
@ -29,15 +30,22 @@ export const closemarket = newEndpoint({}, async (req, auth) => {
|
||||||
throw new APIError(403, 'User is not creator of contract')
|
throw new APIError(403, 'User is not creator of contract')
|
||||||
|
|
||||||
const now = Date.now()
|
const now = Date.now()
|
||||||
if (closeTime && closeTime < now)
|
if (!closeTime && contract.closeTime && contract.closeTime < now)
|
||||||
throw new APIError(400, 'Contract already closed')
|
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)
|
const creator = await getUser(creatorId)
|
||||||
if (!creator) throw new APIError(500, 'Creator not found')
|
if (!creator) throw new APIError(500, 'Creator not found')
|
||||||
|
|
||||||
const updatedContract = {
|
const updatedContract = {
|
||||||
...contract,
|
...contract,
|
||||||
closeTime: now,
|
closeTime: closeTime ? closeTime : now,
|
||||||
}
|
}
|
||||||
|
|
||||||
await contractDoc.update(updatedContract)
|
await contractDoc.update(updatedContract)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user