Implement POST /market/:id/order
This commit is contained in:
parent
9b40d9f031
commit
f09beb1a4a
1
init.sql
1
init.sql
@ -34,3 +34,4 @@ CREATE TABLE orders(
|
|||||||
price BIGINT NOT NULL,
|
price BIGINT NOT NULL,
|
||||||
order_id UUID REFERENCES orders(id)
|
order_id UUID REFERENCES orders(id)
|
||||||
);
|
);
|
||||||
|
ALTER TABLE orders ADD CONSTRAINT order_price CHECK(price > 0 AND price < 100);
|
||||||
|
@ -80,10 +80,10 @@
|
|||||||
{{ else }}
|
{{ else }}
|
||||||
<td></td>
|
<td></td>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
{{ if and (eq .ShareId $.YesShare.Id) (eq .Side "BUY") }}
|
{{ if and (eq .ShareId $.NoShare.Id) (eq .Side "BUY") }}
|
||||||
<td>
|
<td>
|
||||||
<div class="flex yes">
|
<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>
|
<span style="width: 100%" class="align-right">{{.Quantity}} @ {{.Price}}</span>
|
||||||
</div>
|
</div>
|
||||||
</td>
|
</td>
|
||||||
@ -97,7 +97,7 @@
|
|||||||
<hr />
|
<hr />
|
||||||
<details open class="align-left">
|
<details open class="align-left">
|
||||||
<summary><span class="font-mono mb-1"><strong>Order Form</strong></span></summary>
|
<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="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>
|
<button id="sell" type="button" class="order-button no w-100p">SELL</button>
|
||||||
<input id="market-id" hidden name="market_id" value="{{$.Id}}" />
|
<input id="market-id" hidden name="market_id" value="{{$.Id}}" />
|
||||||
|
37
public/400.html
Normal file
37
public/400.html
Normal 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>
|
@ -84,3 +84,12 @@ func (db *DB) FetchOrders(marketId int, orders *[]Order) error {
|
|||||||
}
|
}
|
||||||
return nil
|
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
|
||||||
|
}
|
||||||
|
@ -2,10 +2,10 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"log"
|
|
||||||
"math"
|
"math"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
)
|
)
|
||||||
@ -25,10 +25,10 @@ type Share struct {
|
|||||||
type Order struct {
|
type Order struct {
|
||||||
Session
|
Session
|
||||||
Id string
|
Id string
|
||||||
ShareId string
|
ShareId string `form:"share_id"`
|
||||||
Side string
|
Side string `form:"side"`
|
||||||
Price int
|
Price int `form:"price"`
|
||||||
Quantity int
|
Quantity int `form:"quantity"`
|
||||||
OrderId string
|
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 {
|
func order(c echo.Context) error {
|
||||||
// TODO: implement POST /market/:id/order
|
// (TBD) Step 0: If SELL order, check share balance of users
|
||||||
return echo.NewHTTPError(http.StatusMethodNotAllowed, "Method Not Allowed")
|
// (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 {
|
func market(c echo.Context) error {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user