From 50346d0dd534b31cf6c03169c3c323ffd8996474 Mon Sep 17 00:00:00 2001 From: Marshall Polaris Date: Tue, 26 Jul 2022 01:20:26 -0700 Subject: [PATCH] Add API route for selling shares, document --- docs/docs/api.md | 20 ++++++++++++++++++++ web/pages/api/v0/market/[id]/sell.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 web/pages/api/v0/market/[id]/sell.ts diff --git a/docs/docs/api.md b/docs/docs/api.md index 667c68b8..8b7dce30 100644 --- a/docs/docs/api.md +++ b/docs/docs/api.md @@ -579,6 +579,26 @@ $ curl https://manifold.markets/api/v0/market/{marketId}/resolve -X POST \ ]}' ``` +### `POST /v0/market/[marketId]/sell` + +Sells some quantity of shares in a market on behalf of the authorized user. + +Parameters: + +- `outcome`: Required. One of `YES`, `NO`, or a `number` indicating the numeric + bucket ID, depending on the market type. +- `shares`: Optional. The amount of shares to sell of the outcome given + above. If not provided, all the shares you own will be sold. + +Example request: + +``` +$ curl https://manifold.markets/api/v0/market/{marketId}/sell -X POST \ + -H 'Content-Type: application/json' \ + -H 'Authorization: Key {...}' \ + --data-raw '{"outcome": "YES", "shares": 10}' +``` + ### `GET /v0/bets` Gets a list of bets, ordered by creation date descending. diff --git a/web/pages/api/v0/market/[id]/sell.ts b/web/pages/api/v0/market/[id]/sell.ts new file mode 100644 index 00000000..431121f2 --- /dev/null +++ b/web/pages/api/v0/market/[id]/sell.ts @@ -0,0 +1,28 @@ +import { NextApiRequest, NextApiResponse } from 'next' +import { + CORS_ORIGIN_MANIFOLD, + CORS_ORIGIN_LOCALHOST, +} from 'common/envs/constants' +import { applyCorsHeaders } from 'web/lib/api/cors' +import { fetchBackend, forwardResponse } from 'web/lib/api/proxy' + +export const config = { api: { bodyParser: true } } + +export default async function route(req: NextApiRequest, res: NextApiResponse) { + await applyCorsHeaders(req, res, { + origin: [CORS_ORIGIN_MANIFOLD, CORS_ORIGIN_LOCALHOST], + methods: 'POST', + }) + + const { id } = req.query + const contractId = id as string + + if (req.body) req.body.contractId = contractId + try { + const backendRes = await fetchBackend(req, 'sellshares') + await forwardResponse(res, backendRes) + } catch (err) { + console.error('Error talking to cloud function: ', err) + res.status(500).json({ message: 'Error communicating with backend.' }) + } +}