Show pYes and volume in market row

This commit is contained in:
ekzyis 2024-09-10 23:41:52 +02:00
parent 70b5659dc8
commit b7a24b48fd
5 changed files with 55 additions and 4 deletions

View File

@ -108,6 +108,14 @@
color: var(--color);
}
.text-success {
color: var(--fg-success);
}
.text-error {
color: var(--fg-error);
}
.hitbox {
padding: 15px;
margin: -15px;

View File

@ -3,6 +3,7 @@ package handler
import (
"database/sql"
"git.ekzyis.com/ekzyis/delphi.market/lib/lmsr"
"git.ekzyis.com/ekzyis/delphi.market/server/router/context"
"git.ekzyis.com/ekzyis/delphi.market/server/router/pages"
"git.ekzyis.com/ekzyis/delphi.market/types"
@ -15,16 +16,29 @@ func HandleIndex(sc context.Context) echo.HandlerFunc {
db = sc.Db
ctx = c.Request().Context()
rows *sql.Rows
err error
markets []types.Market
err error
)
if rows, err = db.QueryContext(ctx, ""+
"WITH lmsr AS ("+
" SELECT "+
" o.market_id, "+
" COALESCE(SUM(o.quantity) FILTER(WHERE o.outcome = 0), 0) AS q1, "+
" COALESCE(SUM(o.quantity) FILTER(WHERE o.outcome = 1), 0) AS q2, "+
" COALESCE(SUM(i.msats_received), 0) / 1000 AS volume "+
" FROM orders o "+
" JOIN invoices i ON o.invoice_id = i.id "+
" WHERE i.confirmed_at IS NOT NULL "+
" GROUP BY o.market_id"+
")"+
"SELECT m.id, m.question, m.description, m.created_at, m.end_date, "+
"m.lmsr_b, l.q1, l.q2, l.volume, "+
"u.id, u.name, u.created_at, u.ln_pubkey, u.nostr_pubkey, u.msats "+
"FROM markets m "+
"JOIN users u ON m.user_id = u.id "+
"JOIN invoices i ON m.invoice_id = i.id "+
"JOIN lmsr l ON m.id = l.market_id "+
"WHERE i.confirmed_at IS NOT NULL"); err != nil {
return err
}
@ -32,12 +46,16 @@ func HandleIndex(sc context.Context) echo.HandlerFunc {
for rows.Next() {
var m types.Market
var u types.User
var l types.LMSR
if err = rows.Scan(
&m.Id, &m.Question, &m.Description, &m.CreatedAt, &m.EndDate,
&l.B, &l.Q1, &l.Q2, &m.Volume,
&u.Id, &u.Name, &u.CreatedAt, &u.LnPubkey, &u.NostrPubkey, &u.Msats); err != nil {
return err
}
m.User = u
m.Pyes = lmsr.Quote(l.B, l.Q2, l.Q1, 1)
markets = append(markets, m)
}

View File

@ -173,6 +173,7 @@ func HandleMarket(sc context.Context) echo.HandlerFunc {
if err = db.QueryRowContext(ctx, ""+
"SELECT "+
"COALESCE(SUM(i.msats_received), 0) / 1000 AS volume, "+
"COALESCE(SUM(o.quantity) FILTER(WHERE o.outcome = 0), 0) AS q1, "+
"COALESCE(SUM(o.quantity) FILTER(WHERE o.outcome = 1), 0) AS q2, "+
"COALESCE(SUM(o.quantity) FILTER(WHERE o.outcome = 0 AND o.user_id = $2), 0) AS uq1, "+
@ -192,13 +193,15 @@ func HandleMarket(sc context.Context) echo.HandlerFunc {
//
// For now, we will ignore pending orders.
"WHERE o.market_id = $1 AND i.confirmed_at IS NOT NULL", id, u.Id).
Scan(&l.Q1, &l.Q2, &uQuantityNo, &uQuantityYes); err != nil {
Scan(&m.Volume, &l.Q1, &l.Q2, &uQuantityNo, &uQuantityYes); err != nil {
if err == sql.ErrNoRows {
return echo.NewHTTPError(http.StatusNotFound)
}
return err
}
m.Pyes = lmsr.Quote(l.B, l.Q2, l.Q1, 1)
if rows, err = db.QueryContext(ctx, ""+
"SELECT created_at, quote(b, q0, q1, 1) AS p0, quote(b, q1, q0, 1) AS p1 "+
"FROM ( "+

View File

@ -36,8 +36,8 @@ templ Index(markets []types.Market) {
<a href={ templ.SafeURL(fmt.Sprintf("/market/%d", m.Id)) }>{ m.Question }</a>
<div class="text-small text-muted">{ m.User.Name } / { humanize.Time(m.CreatedAt) } / { humanize.Time(m.EndDate) }</div>
</span>
<span class="px-3 border-b border-muted pb-3 mt-3 flex"><div class="self-center">51%</div></span>
<span class="pe-3 border-b border-muted pb-3 mt-3 flex"><div class="self-center">0</div></span>
<span class={ fmt.Sprintf("%s %s", "px-3 border-b border-muted pb-3 mt-3 flex", colorize(m.Pyes)) }><div class="self-center">{ fmt.Sprintf("%.2f%%", m.Pyes * 100) }</div></span>
<span class="pe-3 border-b border-muted pb-3 mt-3 flex"><div class="self-center">{ fmt.Sprintf("%s", humanizeRound(m.Volume)) }</div></span>
}
</div>
} else {
@ -92,6 +92,25 @@ func minDate() string {
return time.Now().Add(24 * time.Hour).Format("2006-01-02")
}
func humanizeRound(f float64) string {
if f > 1_000_000 {
return fmt.Sprintf("%.2fm", f/1_000_000)
} else if f > 1_000 {
return fmt.Sprintf("%.2fk", f/1_000)
} else {
return fmt.Sprintf("%.2f", f)
}
}
func colorize(f float64) string {
if f > 0.5 {
return "text-success"
} else {
return "text-error"
}
}
func tabStyle(path string, tab string) string {
class := "!no-underline"
if path == tab {

View File

@ -36,6 +36,9 @@ type Market struct {
Description string
CreatedAt time.Time
EndDate time.Time
Pyes float64
// market volume in sats
Volume float64
}
type LMSR struct {