Implement POST /market/:id/order

This commit is contained in:
ekzyis 2023-09-09 22:52:51 +02:00
parent 9b40d9f031
commit f09beb1a4a
5 changed files with 76 additions and 10 deletions

View File

@ -34,3 +34,4 @@ CREATE TABLE orders(
price BIGINT NOT NULL,
order_id UUID REFERENCES orders(id)
);
ALTER TABLE orders ADD CONSTRAINT order_price CHECK(price > 0 AND price < 100);

View File

@ -80,10 +80,10 @@
{{ else }}
<td></td>
{{ end }}
{{ if and (eq .ShareId $.YesShare.Id) (eq .Side "BUY") }}
{{ if and (eq .ShareId $.NoShare.Id) (eq .Side "BUY") }}
<td>
<div class="flex yes">
<span class="align-left">YES</span>
<span class="align-left">NO</span>
<span style="width: 100%" class="align-right">{{.Quantity}} @ {{.Price}}</span>
</div>
</td>
@ -97,7 +97,7 @@
<hr />
<details open class="align-left">
<summary><span class="font-mono mb-1"><strong>Order Form</strong></span></summary>
<form id="form" class="order-form" hidden action="/api/market/{{$.Id}}/order" method="post">
<form id="form" class="order-form" hidden action="/market/{{$.Id}}/order" method="post">
<button id="buy" type="button" class="order-button yes w-100p selected">BUY</button>
<button id="sell" type="button" class="order-button no w-100p">SELL</button>
<input id="market-id" hidden name="market_id" value="{{$.Id}}" />

37
public/400.html Normal file
View File

@ -0,0 +1,37 @@
<!DOCTYPE html>
<html>
<head>
<title>delphi.market</title>
<link rel="icon" type="image/x-icon" href="/favicon.ico" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png" />
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png" />
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png" />
<link rel="manifest" href="/site.webmanifest" />
<link rel="stylesheet" href="/index.css" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#091833" />
</head>
<body>
<header class="flex flex-row text-center justify-center pt-1">
<nav>
<a href="/">home</a>
</nav>
</header>
<div class="container flex flex-column text-center">
<code>
<strong>
<pre>
_ _ ___ ___
| || | / _ \ / _ \
| || |_| | | | | | |
|__ _| |_| | |_| |
|_| \___/ \___/ </pre>
</strong>
</code>
<div class="font-mono mb-1">Bad Request</div>
</div>
</body>
</html>

View File

@ -84,3 +84,12 @@ func (db *DB) FetchOrders(marketId int, orders *[]Order) error {
}
return nil
}
func (db *DB) CreateOrder(order *Order) error {
if _, err := db.Exec(""+
"INSERT INTO orders(share_id, pubkey, side, quantity, price) "+
"VALUES ($1, $2, $3, $4, $5)", order.ShareId, order.Pubkey, order.Side, order.Quantity, order.Price); err != nil {
return err
}
return nil
}

View File

@ -2,10 +2,10 @@ package main
import (
"database/sql"
"log"
"math"
"net/http"
"strconv"
"strings"
"github.com/labstack/echo/v4"
)
@ -25,10 +25,10 @@ type Share struct {
type Order struct {
Session
Id string
ShareId string
Side string
Price int
Quantity int
ShareId string `form:"share_id"`
Side string `form:"side"`
Price int `form:"price"`
Quantity int `form:"quantity"`
OrderId string
}
@ -48,8 +48,27 @@ func BinaryLMSR(invariant int, funding int, q1 int, q2 int, dq1 int) float64 {
}
func order(c echo.Context) error {
// TODO: implement POST /market/:id/order
return echo.NewHTTPError(http.StatusMethodNotAllowed, "Method Not Allowed")
// (TBD) Step 0: If SELL order, check share balance of users
// (TBD) Step 1: respond with HODL invoice
// ...
// Step 2: After payment, create order if no matching order was found
o := new(Order)
if err := c.Bind(o); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "bad request")
}
session := c.Get("session").(Session)
o.Pubkey = session.Pubkey
if err := db.CreateOrder(o); err != nil {
if strings.Contains(err.Error(), "violates check constraint") {
return echo.NewHTTPError(http.StatusBadRequest, "Bad Request")
}
return err
}
// (TBD) Step 3:
// Settle hodl invoice when matching order was found
// else cancel hodl invoice if expired
// ...
return market(c)
}
func market(c echo.Context) error {