Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

render.gno

4.14 Kb · 113 lines
  1package vs
  2
  3import (
  4	"strconv"
  5	"strings"
  6	"time"
  7
  8	mimir "gno.land/p/g12dcsmwccgdxasqde2eumua88tlrkw83kqf6tcz/mimir"
  9	"gno.land/p/nt/ufmt/v0"
 10)
 11
 12var stateNames = []string{"Open", "Active", "Resolved", "Cancelled", "Proposed", "Disputed"}
 13
 14func sideName(c *Claim, s int) string {
 15	switch s {
 16	case SideCreator:
 17		return "Creator: " + mimir.Escape(c.CreatorPosition)
 18	case SideChallengers:
 19		return "Challengers: " + mimir.Escape(c.CounterPosition)
 20	case SideDraw:
 21		return "Draw (refund)"
 22	case SideUnresolvable:
 23		return "Unresolvable (refund)"
 24	}
 25	return "-"
 26}
 27
 28func when(unix int64) string { return time.Unix(unix, 0).UTC().Format("2006-01-02 15:04 UTC") }
 29
 30func Render(path string) string {
 31	if path == "" {
 32		return renderHome()
 33	}
 34	id, err := strconv.ParseInt(path, 10, 64)
 35	if err != nil || id < 1 || id > claimCount {
 36		return "# Not found\n\n[All duels](:)\n"
 37	}
 38	return renderClaim(getClaim(id))
 39}
 40
 41func renderHome() string {
 42	var b strings.Builder
 43	b.WriteString("# Mimir VS\n\n")
 44	b.WriteString("Personal duels. A creator stakes mUSD on a position; challengers take the other side. ")
 45	b.WriteString("An AI oracle proposes the result after the deadline and pays everyone in one transaction.\n\n")
 46	b.WriteString(ufmt.Sprintf("%d duels · %d resolved · minimum stake %s mUSD · dispute bond %s mUSD\n\n",
 47		claimCount, totalResolved, mimir.FormatUSD(MinStake), mimir.FormatUSD(DisputeBond)))
 48	if claimCount == 0 {
 49		b.WriteString("No duels yet.\n")
 50		return b.String()
 51	}
 52	b.WriteString("| # | Question | Creator stake | Challengers | Odds | State | Closes |\n|---|---|---|---|---|---|---|\n")
 53	claims.ReverseIterateByOffset(0, 50, func(_ string, v any) bool {
 54		c := v.(*Claim)
 55		b.WriteString(ufmt.Sprintf("| [%d](:%d) | %s | %s | %d · %s | %s | %s | %s |\n",
 56			c.ID, c.ID, mimir.Escape(c.Question), mimir.FormatUSD(c.CreatorStake),
 57			len(c.challengers), mimir.FormatUSD(c.TotalChallengerStake), c.OddsMode,
 58			stateNames[c.State], when(c.Deadline)))
 59		return false
 60	})
 61	return b.String()
 62}
 63
 64func renderClaim(c *Claim) string {
 65	var b strings.Builder
 66	b.WriteString(ufmt.Sprintf("# %s\n\n", mimir.Escape(c.Question)))
 67	b.WriteString(ufmt.Sprintf("Duel #%d · %s · %s · %s odds", c.ID, mimir.Escape(c.Category), stateNames[c.State], c.OddsMode))
 68	if c.OddsMode == "fixed" {
 69		b.WriteString(ufmt.Sprintf(" (pays %s)", bpsX(c.ChallengerPayoutBps)))
 70	}
 71	if c.IsPrivate {
 72		b.WriteString(" · private")
 73	}
 74	b.WriteString("\n\n")
 75	b.WriteString(ufmt.Sprintf("| Side | Position | Stake (mUSD) |\n|---|---|---|\n| Creator | %s | %s |\n| Challengers (%d/%d) | %s | %s |\n\n",
 76		mimir.Escape(c.CreatorPosition), mimir.FormatUSD(c.CreatorStake),
 77		len(c.challengers), c.MaxChallengers, mimir.Escape(c.CounterPosition), mimir.FormatUSD(c.TotalChallengerStake)))
 78	b.WriteString(ufmt.Sprintf("- Closes: %s\n- Source: %s\n- Creator: %s\n",
 79		when(c.Deadline), mimir.Escape(c.ResolutionURL), c.Creator.String()))
 80	if c.SettlementRule != "" {
 81		b.WriteString("- Rule: " + mimir.Escape(c.SettlementRule) + "\n")
 82	}
 83	p := c.Proposal
 84	switch c.State {
 85	case StProposed:
 86		b.WriteString(ufmt.Sprintf("\n## Proposed: %s (%d%% confidence)\n\nDisputable until %s.\n\n> %s\n\nEvidence: %s\n",
 87			sideName(c, p.WinnerSide), p.Confidence, when(p.ProposedAt+disputeWindow), mimir.Escape(p.Summary), hashOrNone(p.EvidenceHash)))
 88	case StDisputed:
 89		b.WriteString(ufmt.Sprintf("\n## Disputed\n\nThe oracle proposed %s; the arbiter rules.\n", sideName(c, p.WinnerSide)))
 90	case StResolved:
 91		b.WriteString(ufmt.Sprintf("\n## Result: %s (%d%% confidence)\n\n> %s\n\nEvidence: %s\n",
 92			sideName(c, c.WinnerSide), c.Confidence, mimir.Escape(c.ResolutionSummary), hashOrNone(c.EvidenceHash)))
 93	}
 94	b.WriteString(ufmt.Sprintf("\n## Act\n\nApprove this realm on mUSD first, then: [Challenge]($help&func=ChallengeClaim&claimID=%d) · [Finalize]($help&func=FinalizeResolution&claimID=%d) · [Dispute]($help&func=DisputeResolution&claimID=%d)\n\n[All duels](:)\n",
 95		c.ID, c.ID, c.ID))
 96	return b.String()
 97}
 98
 99func bpsX(bps int64) string {
100	cents := (bps % 10_000) / 100
101	pad := ""
102	if cents < 10 {
103		pad = "0"
104	}
105	return ufmt.Sprintf("%d.%s%dx", bps/10_000, pad, cents)
106}
107
108func hashOrNone(h string) string {
109	if h == "" {
110		return "none"
111	}
112	return "`" + h + "`"
113}