From b7a24b48fd87f1eeeecaa5d618fd6d28d86789e7 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Tue, 10 Sep 2024 23:41:52 +0200 Subject: [PATCH] Show pYes and volume in market row --- public/css/_tw-input.css | 8 ++++++++ server/router/handler/index.go | 20 +++++++++++++++++++- server/router/handler/market.go | 5 ++++- server/router/pages/index.templ | 23 +++++++++++++++++++++-- types/types.go | 3 +++ 5 files changed, 55 insertions(+), 4 deletions(-) diff --git a/public/css/_tw-input.css b/public/css/_tw-input.css index 64b332e..b239d52 100644 --- a/public/css/_tw-input.css +++ b/public/css/_tw-input.css @@ -108,6 +108,14 @@ color: var(--color); } + .text-success { + color: var(--fg-success); + } + + .text-error { + color: var(--fg-error); + } + .hitbox { padding: 15px; margin: -15px; diff --git a/server/router/handler/index.go b/server/router/handler/index.go index 289e0cf..2d2862c 100644 --- a/server/router/handler/index.go +++ b/server/router/handler/index.go @@ -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) } diff --git a/server/router/handler/market.go b/server/router/handler/market.go index 3557018..67bdc5a 100644 --- a/server/router/handler/market.go +++ b/server/router/handler/market.go @@ -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 ( "+ diff --git a/server/router/pages/index.templ b/server/router/pages/index.templ index f9f9a47..a316d5d 100644 --- a/server/router/pages/index.templ +++ b/server/router/pages/index.templ @@ -36,8 +36,8 @@ templ Index(markets []types.Market) { { m.Question }
{ m.User.Name } / { humanize.Time(m.CreatedAt) } / { humanize.Time(m.EndDate) }
-
51%
-
0
+
{ fmt.Sprintf("%.2f%%", m.Pyes * 100) }
+
{ fmt.Sprintf("%s", humanizeRound(m.Volume)) }
} } 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 { diff --git a/types/types.go b/types/types.go index 96840a0..e5cbead 100644 --- a/types/types.go +++ b/types/types.go @@ -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 {