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.

Playground

errata.gno

// Package errata renders errata entries supplied by another package or realm.
package errata

import (
	"strconv"
	"strings"

	"gno.land/p/nt/markdown/sanitize/v0"
	"gno.land/p/nt/ufmt/v0"
)

// Entry describes an accepted correction. Link is the optional finder profile URL.
type Entry struct {
	Location string
	Text     string
	Finder   string
	FoundAt  int64
	Paid     int64
	Link     string
}

// RenderSection renders plain-text fields as a Markdown errata table.
func RenderSection(title string, entries []Entry) string {
	if strings.TrimSpace(title) == "" {
		title = "Errata"
	}
	var out strings.Builder
	out.WriteString("## " + sanitize.InlineText(title) + "\n\n")
	if len(entries) == 0 {
		out.WriteString("No errata yet.\n")
		return out.String()
	}

	out.WriteString("| Location | Correction | Finder | Height found | Paid |\n")
	out.WriteString("| --- | --- | --- | ---: | ---: |\n")
	for _, entry := range entries {
		finder := entry.Finder
		if sanitize.BechString(finder, "g") == finder && address(finder).IsValid() {
			finder = ShortAddr(finder)
		}
		finder = sanitize.TableCell(finder)
		if link := sanitize.URL(entry.Link); link != "" && finder != "" {
			finder = "[" + finder + "](" + link + ")"
		}
		out.WriteString(ufmt.Sprintf("| %s | %s | %s | %d | %s |\n",
			sanitize.TableCell(entry.Location), sanitize.TableCell(entry.Text),
			finder, entry.FoundAt, FormatUgnot(entry.Paid)))
	}
	return out.String()
}

// FormatUgnot displays both denominations using exact integer arithmetic.
func FormatUgnot(amount int64) string {
	const units = 1000000
	whole := strconv.FormatInt(amount/units, 10)
	fraction := amount % units
	if fraction < 0 {
		fraction = -fraction
		if amount > -units {
			whole = "-0"
		}
	}
	if fraction != 0 {
		digits := strconv.FormatInt(fraction, 10)
		digits = strings.Repeat("0", 6-len(digits)) + digits
		whole += "." + strings.TrimRight(digits, "0")
	}
	return ufmt.Sprintf("%s GNOT (%d ugnot)", whole, amount)
}

// ShortAddr abbreviates a display address, preserving its prefix and suffix.
func ShortAddr(addr string) string {
	runes := []rune(addr)
	if len(runes) <= 10 {
		return addr
	}
	return string(runes[:6]) + "…" + string(runes[len(runes)-4:])
}

errata_test.gno

package errata

import (
	"strings"
	"testing"
)

func TestFormatUgnot(t *testing.T) {
	tests := []struct {
		amount int64
		want   string
	}{
		{0, "0 GNOT (0 ugnot)"},
		{1, "0.000001 GNOT (1 ugnot)"},
		{2560000, "2.56 GNOT (2560000 ugnot)"},
		{2000000, "2 GNOT (2000000 ugnot)"},
		{-1, "-0.000001 GNOT (-1 ugnot)"},
		{-9223372036854775808, "-9223372036854.775808 GNOT (-9223372036854775808 ugnot)"},
	}
	for _, test := range tests {
		if got := FormatUgnot(test.amount); got != test.want {
			t.Errorf("FormatUgnot(%d) = %q, want %q", test.amount, got, test.want)
		}
	}
}

func TestShortAddr(t *testing.T) {
	if got := ShortAddr("g1abcdefghijklmnopqrstuvwxy"); got != "g1abcd…vwxy" {
		t.Errorf("unexpected abbreviated address: %q", got)
	}
	if got := ShortAddr("g1abcd"); got != "g1abcd" {
		t.Errorf("short address changed: %q", got)
	}
}

func TestRenderSection(t *testing.T) {
	if got := RenderSection("", nil); got != "## Errata\n\nNo errata yet.\n" {
		t.Errorf("unexpected empty section: %q", got)
	}
	entries := []Entry{
		{
			Location: "p. 42 | 43", Text: "*fix*\n[x](javascript:boom) <img>",
			Finder: "[reader]|test", FoundAt: 12, Paid: 2560000, Link: "javascript:boom",
		},
		{Location: "p. 50", Text: "Fix the example.", Finder: "reader",
			FoundAt: 13, Link: "/r/reader/errata/v0:finder/1"},
	}
	got := RenderSection("Errata\n# <unsafe>", entries)
	for _, want := range []string{"p\\. 42 \\| 43", "\\*fix\\*", "\\<img\\>",
		"2.56 GNOT (2560000 ugnot)", "[reader](/r/reader/errata/v0:finder/1)"} {
		if !strings.Contains(got, want) {
			t.Errorf("table missing %q in %q", want, got)
		}
	}
	for _, unwanted := range []string{"\n# <unsafe>", "[x](javascript:", "](javascript:", "<img>"} {
		if strings.Contains(got, unwanted) {
			t.Errorf("unsafe content %q in %q", unwanted, got)
		}
	}
	if lines := strings.Count(got, "\n"); lines != 6 {
		t.Errorf("expected one line per table row, got %d lines in %q", lines, got)
	}
}

gnomod.toml

module = "gno.land/p/g132vz03ycpaagtef4jmxjakstkppgyc5wn3r807/errata/v0"
gno = "0.9"

[addpkg]
  creator = "g132vz03ycpaagtef4jmxjakstkppgyc5wn3r807"
  height = 1351621
  max_deposit = "10000000ugnot"