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.

presentation.gno

5.34 Kb · 176 lines
  1package budget
  2
  3import (
  4	"encoding/hex"
  5	"math/bits"
  6	"strconv"
  7	"strings"
  8
  9	"gno.land/p/nt/svg/v0"
 10)
 11
 12func FormatInt(n int64) string {
 13	digits := strconv.FormatInt(n, 10)
 14	var out strings.Builder
 15	if digits[0] == '-' {
 16		out.WriteByte('-')
 17		digits = digits[1:]
 18	}
 19	for i := 0; i < len(digits); i++ {
 20		if i > 0 && (len(digits)-i)%3 == 0 {
 21			out.WriteByte(',')
 22		}
 23		out.WriteByte(digits[i])
 24	}
 25	return out.String()
 26}
 27
 28// FormatUGNOT shows the exact amount in both ugnot and GNOT.
 29func FormatUGNOT(amount int64) string {
 30	const unit int64 = 1000000
 31	whole := FormatInt(amount / unit)
 32	fraction := amount % unit
 33	if fraction < 0 {
 34		fraction = -fraction
 35	}
 36	if amount < 0 && amount > -unit {
 37		whole = "-" + whole
 38	}
 39	decimal := strconv.FormatInt(unit+fraction, 10)[1:]
 40	for len(decimal) > 2 && decimal[len(decimal)-1] == '0' {
 41		decimal = decimal[:len(decimal)-1]
 42	}
 43	return FormatInt(amount) + " ugnot (" + whole + "." + decimal + " GNOT)"
 44}
 45
 46// FormatAsset formats an amount in base units using the asset's precision.
 47func FormatAsset(amount int64, asset string, decimals int) string {
 48	return formatQuantity(amount, decimals) + " " + asset
 49}
 50
 51func formatQuantity(amount int64, decimals int) string {
 52	if decimals < 0 || decimals > 18 {
 53		panic("asset decimals must be 0 to 18")
 54	}
 55	if decimals == 0 {
 56		return FormatInt(amount)
 57	}
 58	unit := int64(1)
 59	for i := 0; i < decimals; i++ {
 60		unit *= 10
 61	}
 62	whole := FormatInt(amount / unit)
 63	fraction := amount % unit
 64	if fraction == 0 {
 65		return whole
 66	}
 67	if fraction < 0 {
 68		fraction = -fraction
 69		if amount > -unit {
 70			whole = "-" + whole
 71		}
 72	}
 73	digits := strconv.FormatInt(unit+fraction, 10)[1:]
 74	return whole + "." + strings.TrimRight(digits, "0")
 75}
 76
 77// MarkdownText escapes printable text for inline Markdown and table cells.
 78func MarkdownText(s string) string {
 79	return strings.NewReplacer(
 80		"\\", "\\\\", "`", "\\`", "*", "\\*", "_", "\\_", "~", "\\~", "#", "\\#",
 81		"[", "\\[", "]", "\\]", "<", "\\<", ">", "\\>", "|", "\\|", "&", "\\&",
 82		"!", "\\!", "(", "\\(", ")", "\\)",
 83	).Replace(s)
 84}
 85
 86func PolicyLaneURL(path string, programID int64, lane string) string {
 87	return strings.TrimPrefix(path, "gno.land") + ":" + strconv.FormatInt(programID, 10) + "/" + lane
 88}
 89
 90func PolicyLaneLinks(path string, programID int64, lanes []string) string {
 91	var links strings.Builder
 92	for _, lane := range lanes {
 93		links.WriteString("- [" + MarkdownText(lane) + "](" + PolicyLaneURL(path, programID, lane) + ")\n")
 94	}
 95	return links.String()
 96}
 97
 98func AllocationBarSVG(ledger Ledger, reserveFloor int64, asset string) string {
 99	ledger.AssertConserved()
100	protected := min64(ledger.Available, nonnegative(reserveFloor))
101	segments := []struct {
102		amount int64
103		label  string
104		color  string
105		x      int
106	}{
107		{ledger.Available - protected, "Available", "#047857", 12},
108		{protected, "Protected reserve floor", "#b45309", 111},
109		{ledger.Reserved, "Reserved", "#2563eb", 296},
110		{ledger.Claimable, "Claimable", "#6d28d9", 396},
111		{ledger.Paid, "Paid", "#475569", 502},
112	}
113	const barWidth = 552
114	canvas := svg.NewCanvas(576, 65).WithViewBox(0, 0, 576, 65)
115	canvas.Append(svg.NewRectangle(0, 0, 576, 65, "#f8fafc"),
116		svg.NewRectangle(12, 12, barWidth, 20, "#e2e8f0"))
117	position := 0
118	total := int64(0)
119	for _, segment := range segments {
120		total += segment.amount
121		end := scaledWidth(total, ledger.Funded, barWidth)
122		if end > position {
123			canvas.Append(svg.NewRectangle(12+position, 12, end-position, 20, segment.color))
124		}
125		position = end
126		canvas.Append(svg.NewRectangle(segment.x, 43, 10, 10, segment.color),
127			svgLabel(segment.x+16, 52, segment.label))
128	}
129	return canvas.Render("Allocation of " + MarkdownText(asset) +
130		" across available funds, protected reserve floor, reserved funds, claims and payments")
131}
132
133func CapMeterSVG(label string, used, cap int64, colorHex, asset string, decimals int) string {
134	RequireMemo(label)
135	color := strings.TrimPrefix(colorHex, "#")
136	if _, err := hex.DecodeString(color); err != nil || len(color) != 6 {
137		color = "2563eb"
138	}
139	usedText, capText := formatQuantity(used, decimals), formatQuantity(cap, decimals)
140	title := label + " · " + usedText + " / " + capText
141	// Long realm denoms remain in the accessible label and the surrounding page.
142	if len(asset) <= 24 {
143		title += " " + asset
144	}
145	canvasWidth := 436
146	if textWidth := len(title)*8 + 24; textWidth > canvasWidth {
147		canvasWidth = textWidth
148	}
149	barWidth := canvasWidth - 24
150	canvas := svg.NewCanvas(canvasWidth, 56).WithViewBox(0, 0, canvasWidth, 56)
151	width := scaledWidth(nonnegative(min64(used, cap)), cap, barWidth)
152	canvas.Append(svg.NewRectangle(0, 0, canvasWidth, 56, "#f8fafc"),
153		svgLabel(12, 20, title),
154		svg.NewRectangle(12, 31, barWidth, 13, "#e2e8f0"))
155	if width > 0 {
156		canvas.Append(svg.NewRectangle(12, 31, width, 13, "#"+color))
157	}
158	return canvas.Render(MarkdownText(label) + " · " + usedText + " of " + capText + " " + MarkdownText(asset) + " used")
159}
160
161func svgLabel(x, y int, text string) *svg.Text {
162	text = strings.NewReplacer("&", "&amp;", "<", "&lt;", ">", "&gt;").Replace(text)
163	element := svg.NewText(x, y, text, "#0f172a")
164	element.Attr.Style = "font-family:system-ui,sans-serif;font-size:13px"
165	return element
166}
167
168func scaledWidth(amount, total int64, width int) int {
169	if total <= 0 {
170		return 0
171	}
172	// Use a 128-bit product so large ledgers retain exact pixel boundaries.
173	hi, lo := bits.Mul64(uint64(amount), uint64(width))
174	pixels, _ := bits.Div64(hi, lo, uint64(total))
175	return int(pixels)
176}