Rename vars and add comments
This commit is contained in:
parent
03689add7e
commit
5d5c91399f
|
@ -101,27 +101,51 @@ func HandleMarket(sc context.Context) echo.HandlerFunc {
|
||||||
var (
|
var (
|
||||||
db = sc.Db
|
db = sc.Db
|
||||||
ctx = c.Request().Context()
|
ctx = c.Request().Context()
|
||||||
|
|
||||||
|
// session user
|
||||||
u = types.User{}
|
u = types.User{}
|
||||||
|
|
||||||
|
// market id
|
||||||
id = c.Param("id")
|
id = c.Param("id")
|
||||||
|
|
||||||
|
// quantity of shares user entered into form
|
||||||
quantity = c.QueryParam("q")
|
quantity = c.QueryParam("q")
|
||||||
|
|
||||||
|
// quantity as number
|
||||||
q int64
|
q int64
|
||||||
|
|
||||||
|
// current market
|
||||||
m = types.Market{}
|
m = types.Market{}
|
||||||
|
|
||||||
|
// market founder
|
||||||
mU = types.User{}
|
mU = types.User{}
|
||||||
|
|
||||||
|
// market LMSR data
|
||||||
l = types.LMSR{}
|
l = types.LMSR{}
|
||||||
|
|
||||||
|
// total price for current quantity of shares in sats
|
||||||
total float64
|
total float64
|
||||||
quote0 = types.MarketQuote{}
|
|
||||||
quote1 = types.MarketQuote{}
|
// market quotes
|
||||||
uQ0 int
|
quoteNo = types.MarketQuote{}
|
||||||
uQ1 int
|
quoteYes = types.MarketQuote{}
|
||||||
|
|
||||||
|
// how many shares the user already holds
|
||||||
|
uQuantityNo int
|
||||||
|
uQuantityYes int
|
||||||
rows *sql.Rows
|
rows *sql.Rows
|
||||||
p0 []types.Point
|
|
||||||
p1 []types.Point
|
// chart data
|
||||||
|
lineNo []types.Point
|
||||||
|
lineYes []types.Point
|
||||||
|
|
||||||
err error
|
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,9 +249,9 @@ 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -239,24 +263,48 @@ func HandleOrder(sc context.Context) echo.HandlerFunc {
|
||||||
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)
|
||||||
|
|
||||||
|
// market id
|
||||||
id = c.Param("id")
|
id = c.Param("id")
|
||||||
|
|
||||||
|
// how many shares user wants to buy
|
||||||
quantity = c.FormValue("q")
|
quantity = c.FormValue("q")
|
||||||
outcome = c.FormValue("o")
|
// quantity as number
|
||||||
q int64
|
q int64
|
||||||
|
|
||||||
|
// on which outcome user wants to bet
|
||||||
|
outcome = c.FormValue("o")
|
||||||
|
// outcome as id
|
||||||
o int64
|
o int64
|
||||||
|
|
||||||
|
// selected market
|
||||||
m = types.Market{}
|
m = types.Market{}
|
||||||
|
|
||||||
|
// market founder
|
||||||
mU = types.User{}
|
mU = types.User{}
|
||||||
|
|
||||||
|
// market LMSR data
|
||||||
l = types.LMSR{}
|
l = types.LMSR{}
|
||||||
|
|
||||||
|
// total price as returned by LMSR for given quantity
|
||||||
totalF float64
|
totalF float64
|
||||||
|
// total rounded to msats
|
||||||
total int
|
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
|
||||||
|
|
||||||
|
// id of created order
|
||||||
orderId int
|
orderId int
|
||||||
|
|
||||||
|
// QR component during render
|
||||||
qr templ.Component
|
qr templ.Component
|
||||||
|
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -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>
|
||||||
|
|
Loading…
Reference in New Issue