Rename vars and add comments

This commit is contained in:
ekzyis 2024-09-10 22:41:58 +02:00
parent 03689add7e
commit 5d5c91399f
2 changed files with 95 additions and 47 deletions

View File

@ -99,29 +99,53 @@ func HandleCreate(sc context.Context) echo.HandlerFunc {
func HandleMarket(sc context.Context) echo.HandlerFunc { func HandleMarket(sc context.Context) echo.HandlerFunc {
return func(c echo.Context) error { return func(c echo.Context) error {
var ( var (
db = sc.Db db = sc.Db
ctx = c.Request().Context() ctx = c.Request().Context()
u = types.User{}
id = c.Param("id") // session user
u = types.User{}
// market id
id = c.Param("id")
// quantity of shares user entered into form
quantity = c.QueryParam("q") quantity = c.QueryParam("q")
q int64
m = types.Market{} // quantity as number
mU = types.User{} q int64
l = types.LMSR{}
total float64 // current market
quote0 = types.MarketQuote{} m = types.Market{}
quote1 = types.MarketQuote{}
uQ0 int // market founder
uQ1 int mU = types.User{}
rows *sql.Rows
p0 []types.Point // market LMSR data
p1 []types.Point l = types.LMSR{}
err error
// total price for current quantity of shares in sats
total float64
// market quotes
quoteNo = types.MarketQuote{}
quoteYes = types.MarketQuote{}
// how many shares the user already holds
uQuantityNo int
uQuantityYes int
rows *sql.Rows
// chart data
lineNo []types.Point
lineYes []types.Point
err error
) )
if c.Get("session") != nil { if c.Get("session") != nil {
u = c.Get("session").(types.User) u = c.Get("session").(types.User)
} else { } else {
// unauthenticated user
u.Id = -1 u.Id = -1
} }
@ -167,8 +191,8 @@ func HandleMarket(sc context.Context) echo.HandlerFunc {
// but this isn't sybil resistant. // but this isn't sybil resistant.
// //
// For now, we will ignore pending orders. // For now, we will ignore pending orders.
"WHERE o.market_id = $1 AND i.confirmed_at IS NOT NULL", id, u.Id).Scan( "WHERE o.market_id = $1 AND i.confirmed_at IS NOT NULL", id, u.Id).
&l.Q1, &l.Q2, &uQ0, &uQ1); err != nil { Scan(&l.Q1, &l.Q2, &uQuantityNo, &uQuantityYes); err != nil {
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
return echo.NewHTTPError(http.StatusNotFound) return echo.NewHTTPError(http.StatusNotFound)
} }
@ -203,12 +227,12 @@ func HandleMarket(sc context.Context) echo.HandlerFunc {
if err = rows.Scan(&createdAt, &_p0, &_p1); err != nil { if err = rows.Scan(&createdAt, &_p0, &_p1); err != nil {
return err return err
} }
p0 = append(p0, types.Point{X: createdAt, Y: _p0}) lineNo = append(lineNo, types.Point{X: createdAt, Y: _p0})
p1 = append(p1, types.Point{X: createdAt, Y: _p1}) lineYes = append(lineYes, types.Point{X: createdAt, Y: _p1})
} }
total = lmsr.Quote(l.B, l.Q1, l.Q2, int(q)) total = lmsr.Quote(l.B, l.Q1, l.Q2, int(q))
quote0 = types.MarketQuote{ quoteNo = types.MarketQuote{
Outcome: 0, Outcome: 0,
AvgPrice: total / float64(q), AvgPrice: total / float64(q),
TotalPrice: total, TotalPrice: total,
@ -216,7 +240,7 @@ func HandleMarket(sc context.Context) echo.HandlerFunc {
} }
total = lmsr.Quote(l.B, l.Q2, l.Q1, int(q)) total = lmsr.Quote(l.B, l.Q2, l.Q1, int(q))
quote1 = types.MarketQuote{ quoteYes = types.MarketQuote{
Outcome: 1, Outcome: 1,
AvgPrice: total / float64(q), AvgPrice: total / float64(q),
TotalPrice: total, TotalPrice: total,
@ -225,39 +249,63 @@ func HandleMarket(sc context.Context) echo.HandlerFunc {
return pages.Market( return pages.Market(
m, m,
p0, p1, lineNo, lineYes,
quote0, quote1, quoteNo, quoteYes,
uQ0, uQ1).Render(context.RenderContext(sc, c), c.Response().Writer) uQuantityNo, uQuantityYes).Render(context.RenderContext(sc, c), c.Response().Writer)
} }
} }
func HandleOrder(sc context.Context) echo.HandlerFunc { func HandleOrder(sc context.Context) echo.HandlerFunc {
return func(c echo.Context) error { return func(c echo.Context) error {
var ( var (
db = sc.Db db = sc.Db
lnd = sc.Lnd lnd = sc.Lnd
tx *sql.Tx tx *sql.Tx
ctx = c.Request().Context() ctx = c.Request().Context()
u = c.Get("session").(types.User) u = c.Get("session").(types.User)
id = c.Param("id")
quantity = c.FormValue("q") // market id
outcome = c.FormValue("o") id = c.Param("id")
q int64
o int64 // how many shares user wants to buy
m = types.Market{} quantity = c.FormValue("q")
mU = types.User{} // quantity as number
l = types.LMSR{} q int64
totalF float64
total int // on which outcome user wants to bet
outcome = c.FormValue("o")
// outcome as id
o int64
// selected market
m = types.Market{}
// market founder
mU = types.User{}
// market LMSR data
l = types.LMSR{}
// total price as returned by LMSR for given quantity
totalF float64
// total rounded to msats
total int
// invoice data
hash lntypes.Hash hash lntypes.Hash
paymentRequest string paymentRequest string
expiry = int64(60) expiry = int64(60)
expiresAt = time.Now().Add(time.Second * time.Duration(expiry)) expiresAt = time.Now().Add(time.Second * time.Duration(expiry))
invoiceId int invoiceId int
invDescription string invDescription string
orderId int
qr templ.Component // id of created order
err error orderId int
// QR component during render
qr templ.Component
err error
) )
if quantity == "" { if quantity == "" {

View File

@ -7,7 +7,7 @@ import (
) )
// TODO: Add countdown? Use or at least show somewhere precise timestamps? // TODO: Add countdown? Use or at least show somewhere precise timestamps?
templ Market(m types.Market, p0 []types.MarketPoint, p1 []types.MarketPoint, q0 types.MarketQuote, q1 types.MarketQuote, uQ0 int, uQ1 int) { templ Market(m types.Market, p0 []types.Point, p1 []types.Point, quoteNo types.MarketQuote, quoteYes types.MarketQuote, uQuantityNo int, uQuantityYes int) {
<html> <html>
@components.Head() @components.Head()
<body <body
@ -54,10 +54,10 @@ templ Market(m types.Market, p0 []types.MarketPoint, p1 []types.MarketPoint, q0
</button> </button>
</div> </div>
<div class="mx-auto my-5" x-show="outcome === 1"> <div class="mx-auto my-5" x-show="outcome === 1">
@components.MarketForm(m, 1, q1, uQ1) @components.MarketForm(m, 1, quoteYes, uQuantityYes)
</div> </div>
<div class="mx-auto my-5" x-show="outcome === 0"> <div class="mx-auto my-5" x-show="outcome === 0">
@components.MarketForm(m, 0, q0, uQ0) @components.MarketForm(m, 0, quoteNo, uQuantityNo)
</div> </div>
</div> </div>
</div> </div>