Playground
across.gno
// Package across reads one address across fifteen other realms that are
// already live, and renders everything they will say about it on one page.
//
// It is a composability demo with a working product hiding inside it. The
// realms it reads were written months apart, by nobody coordinating, and none
// of them knows this one exists. Not a line of them changed to make this
// possible: the only requirement any of them had to meet was NOT declaring
// `private = true`, which is what lets another realm import it at all.
//
// # Why this realm holds no state and extracts no library
//
// Every other realm here splits into a pure `p/` and a thin `r/`. This one
// cannot, and that is the point: its entire content is the wiring. Fifteen
// import lines, one call each, and a table. Extract a library and what is left
// on both sides is empty.
//
// # What it measures, besides balances
//
// The signatures below are the honest half of the demo. Eight of these realms
// hold a per-address balance and they agree on nothing:
//
// erc20, vestoken, wrapped BalanceOf(address) uint64
// pairs/aaa, pairs/bbb BalanceOf(address) int64
// grc20faucet BalanceOf(symbol string, owner address) int64
// wikicoin BalanceOf(address) uint
// dice RollsOf(addr string) []int
//
// Three integer widths, two signednesses, one that takes the address as a
// string, one that needs a symbol first. Composition works anyway, because a
// gno import is a type-checked contract with a permanent artifact behind it and
// not a call convention anyone has to agree on in advance. It just costs one
// hand-written adapter per source, which is what the report function is.
//
// # The two sources that could not be read
//
// - r/moul/x/amm/v0 exports SharesOf(keyA, keyB string, owner address), and
// it routes through mustPool, which PANICS when the pool does not exist.
// A getter that panics is not composable: fifteen sources on one page means
// one panicking source takes the whole page down, and a wrapper cannot
// recover, because recover() does not catch a cross-realm abort. So amm
// contributes PoolCount() to the index and nothing to a report.
// - r/moul/demo/vault/v0 exports MyBalance() with no address parameter. It
// answers for whoever is calling, which from here is this realm, so the
// value is structurally unreadable from outside.
//
// Neither is a bug in those realms. Both are what a getter looks like when
// nobody has tried to call it from another realm yet.
package across
import (
"strconv"
"strings"
"gno.land/p/moul/kit/ui/v0"
"gno.land/r/moul/demo/wikicoin/v0"
"gno.land/r/moul/reactions/v0"
"gno.land/r/moul/x/amm/v0"
"gno.land/r/moul/x/daily/dice/v0"
"gno.land/r/moul/x/daily/erc20/v0"
"gno.land/r/moul/x/daily/lottery/v0"
"gno.land/r/moul/x/daily/ratelimitdemo/v0"
"gno.land/r/moul/x/daily/staking/v0"
"gno.land/r/moul/x/daily/streak/v0"
"gno.land/r/moul/x/daily/vestoken/v0"
"gno.land/r/moul/x/daily/wrapped/v0"
"gno.land/r/moul/x/grc20faucet/v0"
"gno.land/r/moul/x/pairs/aaa/v0"
"gno.land/r/moul/x/pairs/aaabbb/v0"
"gno.land/r/moul/x/pairs/bbb/v0"
)
// reactionPage is the page key handed to r/moul/reactions. Reactions are keyed
// on an arbitrary page string rather than on a realm, so a reader of this
// report is asked about the same page /u/moul shows.
const reactionPage = "gno.land/r/moul/home"
// Self is this realm's own package path, exported so a caller can build a link
// back without hardcoding the version.
const Self = "gno.land/r/moul/x/across/v0"
// A fact is one answer from one source realm about one address.
//
// zero is carried separately from value because "0" and "none" are the common
// answer and the table is worth reading only for the rows that are not that.
type fact struct {
source string // package path, without the gno.land/ prefix
what string
value string
zero bool
}
// Report collects what every wired source says about who.
//
// Every call in here is a plain exported read on another realm: no cur realm,
// no cross, nothing written. A read with no realm parameter is borrowed rather
// than entered (AGENTS.md), so none of these realms even observes a caller.
func Report(who address) []fact {
out := []fact{}
add := func(source, what string, n int64, unit string) {
v := strconv.FormatInt(n, 10)
if unit != "" {
v += " " + unit
}
out = append(out, fact{source: source, what: what, value: v, zero: n == 0})
}
// Balances. Eight sources, five signatures.
add("r/moul/x/daily/erc20/v0", "balance", int64(erc20.BalanceOf(who)), erc20.Symbol())
add("r/moul/x/daily/vestoken/v0", "liquid balance", int64(vestoken.BalanceOf(who)), vestoken.Symbol())
add("r/moul/x/daily/wrapped/v0", "wrapped balance", int64(wrapped.BalanceOf(who)), "WGNOT")
add("r/moul/demo/wikicoin/v0", "balance", int64(wikicoin.BalanceOf(who)), "WIKI")
add("r/moul/x/pairs/aaa/v0", "balance", aaa.BalanceOf(who), "AAA")
add("r/moul/x/pairs/bbb/v0", "balance", bbb.BalanceOf(who), "BBB")
add("r/moul/x/grc20faucet/v0", "balance", grc20faucet.BalanceOf("RED", who), "RED")
add("r/moul/x/grc20faucet/v0", "balance", grc20faucet.BalanceOf("BLUE", who), "BLUE")
// A grant is three numbers, so it does not fit the add helper.
total, claimed, claimable := vestoken.GrantOf(who)
out = append(out, fact{
source: "r/moul/x/daily/vestoken/v0",
what: "vesting grant",
value: strconv.FormatUint(total, 10) + " total, " +
strconv.FormatUint(claimed, 10) + " claimed, " +
strconv.FormatUint(claimable, 10) + " claimable",
zero: total == 0,
})
// Positions.
add("r/moul/x/pairs/aaabbb/v0", "LP shares", aaabbb.SharesOf(who), "")
add("r/moul/x/daily/staking/v0", "staked", int64(staking.StakedOf(who)), "")
add("r/moul/x/daily/staking/v0", "unclaimed rewards", int64(staking.Earned(who)), "")
// Activity.
add("r/moul/x/daily/streak/v0", "current streak", int64(streak.Current(who)), "day(s)")
add("r/moul/x/daily/streak/v0", "best streak", int64(streak.Best(who)), "day(s)")
add("r/moul/x/daily/dice/v0", "dice rolled", int64(len(dice.RollsOf(who.String()))), "")
add("r/moul/x/daily/ratelimitdemo/v0", "rate-limit tokens left", int64(ratelimitdemo.Tokens(who)), "")
entered := "no"
if lottery.HasEntered(who) {
entered = "yes, round " + strconv.Itoa(lottery.CurrentRound())
}
out = append(out, fact{
source: "r/moul/x/daily/lottery/v0",
what: "entered the lottery",
value: entered,
zero: !lottery.HasEntered(who),
})
// reactions answers with a palette key, already validated at write time, so
// it never carries caller markdown into this table.
react := reactions.ReactionOf(reactionPage, who)
shown := react
if shown == "" {
shown = "none"
}
out = append(out, fact{
source: "r/moul/reactions/v0",
what: "reaction on " + reactionPage,
value: shown,
zero: react == "",
})
return out
}
// sourceRealms is every realm this one imports for its data, in import order.
// It is the single place the count comes from: the index table has one row per
// entry and TestSourcesCoverReport checks that Report names nothing else.
//
// Fifteen entries, which is the number the prose says. TestSourceCount pins it.
var sourceRealms = []string{
"r/moul/x/daily/erc20/v0",
"r/moul/x/daily/vestoken/v0",
"r/moul/x/daily/wrapped/v0",
"r/moul/demo/wikicoin/v0",
"r/moul/x/pairs/aaa/v0",
"r/moul/x/pairs/bbb/v0",
"r/moul/x/grc20faucet/v0",
"r/moul/x/pairs/aaabbb/v0",
"r/moul/x/amm/v0",
"r/moul/x/daily/staking/v0",
"r/moul/x/daily/streak/v0",
"r/moul/x/daily/dice/v0",
"r/moul/x/daily/lottery/v0",
"r/moul/x/daily/ratelimitdemo/v0",
"r/moul/reactions/v0",
}
// Sources reports how many realms this one reads.
func Sources() int { return len(sourceRealms) }
func Render(path string) string {
if path == "" {
return renderIndex()
}
addr := address(path)
if !addr.IsValid() {
return "# across\n\n" + ui.Empty(ui.Inline(ui.Short(path))+" is not a valid address.") +
"\n[back to the index](/r/moul/x/across/v0)\n"
}
return renderAddress(addr)
}
func renderAddress(who address) string {
var b strings.Builder
b.WriteString("# across: " + ui.AddrFull(who) + "\n\n")
b.WriteString("What " + strconv.Itoa(Sources()) + " independently deployed realms say about this address. ")
b.WriteString("None of them was changed to answer.\n\n")
facts := Report(who)
live := ui.NewTable("source", "fact", "value")
quiet := 0
for _, f := range facts {
if f.zero {
quiet++
continue
}
live.Row("[`"+f.source+"`](/"+f.source+")", f.what, f.value)
}
if live.Len() == 0 {
b.WriteString(ui.Empty("Every one of the " + strconv.Itoa(len(facts)) +
" reads came back empty. This address has never touched any of them."))
b.WriteString("\n")
} else {
b.WriteString("## Answers\n\n")
b.WriteString(live.String())
b.WriteString("\n")
b.WriteString(strconv.Itoa(quiet) + " of " + strconv.Itoa(len(facts)) +
" reads came back empty and are hidden.\n\n")
}
b.WriteString("[back to the index](/r/moul/x/across/v0)\n")
return b.String()
}
func renderIndex() string {
var b strings.Builder
b.WriteString("# across\n\n")
b.WriteString("One address, read across " + strconv.Itoa(Sources()) + " realms that are already on chain.\n\n")
b.WriteString("Open `/r/moul/x/across/v0:<address>` to get the report. ")
b.WriteString("Example: [/r/moul/x/across/v0:" + exampleAddr + "](/r/moul/x/across/v0:" + exampleAddr + ").\n\n")
b.WriteString("## Why this realm exists\n\n")
b.WriteString("A gno realm that does not declare `private = true` can be imported by any ")
b.WriteString("other realm, forever, at a path that can never be replaced. That makes every ")
b.WriteString("exported read on it a permanent public API. This realm is what happens when ")
b.WriteString("somebody actually uses " + strconv.Itoa(Sources()) + " of them at once.\n\n")
b.WriteString("Nothing here was negotiated. No registry, no interface, no shared base ")
b.WriteString("package, no version handshake, and no change to any source realm.\n\n")
b.WriteString("## The sources, and what each one is showing right now\n\n")
t := ui.NewTable("realm", "global reading")
t.Row("[`r/moul/x/daily/erc20/v0`](/r/moul/x/daily/erc20/v0)",
erc20.Symbol()+" supply "+strconv.FormatUint(erc20.TotalSupply(), 10))
t.Row("[`r/moul/x/daily/vestoken/v0`](/r/moul/x/daily/vestoken/v0)",
vestoken.Symbol()+" supply "+strconv.FormatUint(vestoken.TotalSupply(), 10))
t.Row("[`r/moul/x/daily/wrapped/v0`](/r/moul/x/daily/wrapped/v0)",
"wrapped supply "+strconv.FormatUint(wrapped.TotalSupply(), 10))
t.Row("[`r/moul/demo/wikicoin/v0`](/r/moul/demo/wikicoin/v0)", "per-address balances only")
t.Row("[`r/moul/x/pairs/aaa/v0`](/r/moul/x/pairs/aaa/v0)",
"AAA supply "+strconv.FormatInt(aaa.TotalSupply(), 10))
t.Row("[`r/moul/x/pairs/bbb/v0`](/r/moul/x/pairs/bbb/v0)",
"BBB supply "+strconv.FormatInt(bbb.TotalSupply(), 10))
t.Row("[`r/moul/x/grc20faucet/v0`](/r/moul/x/grc20faucet/v0)",
"RED supply "+strconv.FormatInt(grc20faucet.TotalSupply("RED"), 10)+
", BLUE supply "+strconv.FormatInt(grc20faucet.TotalSupply("BLUE"), 10))
t.Row("[`r/moul/x/pairs/aaabbb/v0`](/r/moul/x/pairs/aaabbb/v0)",
"LP shares "+strconv.FormatInt(aaabbb.TotalShares(), 10))
t.Row("[`r/moul/x/amm/v0`](/r/moul/x/amm/v0)",
strconv.Itoa(amm.PoolCount())+" pool(s), index only (see below)")
t.Row("[`r/moul/x/daily/staking/v0`](/r/moul/x/daily/staking/v0)", "per-address stake and rewards")
t.Row("[`r/moul/x/daily/streak/v0`](/r/moul/x/daily/streak/v0)",
"day "+strconv.FormatInt(streak.Day(), 10))
t.Row("[`r/moul/x/daily/dice/v0`](/r/moul/x/daily/dice/v0)",
strconv.Itoa(dice.Total())+" roll(s) all time")
t.Row("[`r/moul/x/daily/lottery/v0`](/r/moul/x/daily/lottery/v0)",
"round "+strconv.Itoa(lottery.CurrentRound())+", "+
strconv.Itoa(lottery.NumEntrants())+" entrant(s)")
t.Row("[`r/moul/x/daily/ratelimitdemo/v0`](/r/moul/x/daily/ratelimitdemo/v0)", "per-address bucket")
t.Row("[`r/moul/reactions/v0`](/r/moul/reactions/v0)",
strconv.Itoa(reactions.Pages())+" page(s) reacted to")
b.WriteString(t.String())
b.WriteString("\n")
b.WriteString("## What it cost\n\n")
b.WriteString("Eight of these hold a per-address balance, and they share no signature: ")
b.WriteString("`uint64` three times, `int64` twice, `uint` once, one that wants a symbol ")
b.WriteString("before the address, one that wants the address as a `string`. Every source ")
b.WriteString("therefore needs its own adapter line. That is the whole cost, and it is paid ")
b.WriteString("once by the reader rather than up front by fifteen authors.\n\n")
b.WriteString("## The two that could not be read\n\n")
b.WriteString("- `r/moul/x/amm/v0` routes `SharesOf` through a helper that panics on an ")
b.WriteString("unknown pool. One panicking source takes down a page built from " +
strconv.Itoa(Sources()) + ", and ")
b.WriteString("`recover()` does not catch a cross-realm abort, so only its `PoolCount()` is ")
b.WriteString("safe to call from here.\n")
b.WriteString("- `r/moul/demo/vault/v0` exports `MyBalance()` with no address parameter. It ")
b.WriteString("answers for its caller, which from here is this realm.\n\n")
b.WriteString("Neither is a bug. Both are what a getter looks like before anyone has tried ")
b.WriteString("to call it from another realm.\n")
return b.String()
}
// exampleAddr is moul's address, used only to give the index a link that
// resolves to a real report instead of an empty one.
const exampleAddr = "g1manfred47kzduec920z88wfr64ylksmdcedlf5"
across_test.gno
package across
import (
"strings"
"testing"
"gno.land/p/nt/testutils/v0"
"gno.land/p/nt/uassert/v0"
"gno.land/r/moul/x/daily/erc20/v0"
"gno.land/r/moul/x/daily/streak/v0"
"gno.land/r/moul/x/pairs/aaa/v0"
"gno.land/r/moul/x/pairs/bbb/v0"
)
// seeded is the address the mutation tests build state for. Everything after
// TestReportReadsCrossRealmState sees whatever they left in the imported
// realms, which is the whole point of the demo but also makes test order load
// bearing: nothing below may assume a clean source realm for this address.
var seeded = testutils.TestAddress("across-seeded")
// fresh is never written to by anything here, so every source answers it with
// its zero value. ExampleRender pins that page.
var fresh = testutils.TestAddress("across-fresh")
func TestSourceCount(t *testing.T) {
// The package doc and the rendered prose both say fifteen. If this fails,
// one of them is now a lie.
uassert.Equal(t, 15, Sources())
}
// Report must never name a realm that sourceRealms does not list, or the index
// table silently stops covering what the report reads.
func TestSourcesCoverReport(t *testing.T) {
known := map[string]bool{}
for _, s := range sourceRealms {
known[s] = true
}
for _, f := range Report(fresh) {
uassert.True(t, known[f.source], "report names an unlisted source: "+f.source)
}
}
// Every source answers, and no source panics. The count is asserted rather
// than the values, so this stays true as the sources gain state.
func TestReportAnswersFromEverySource(t *testing.T) {
facts := Report(fresh)
uassert.Equal(t, 18, len(facts))
seen := map[string]bool{}
for _, f := range facts {
seen[f.source] = true
}
// amm is the one listed source that contributes to the index only.
uassert.Equal(t, 14, len(seen))
uassert.False(t, seen["r/moul/x/amm/v0"], "amm must not be read per address")
}
// The load-bearing test: write to four realms that know nothing about this one,
// then read all four back through Report. Break any of the four reads and this
// goes red.
func TestReportReadsCrossRealmState(cur realm, t *testing.T) {
// SetRealm governs only the crossing calls made from this frame, so the
// switch has to stay inline with the calls it is meant to cover.
testing.SetRealm(testing.NewUserRealm(seeded))
erc20.Mint(cross(cur), seeded, 4_200)
aaa.Faucet(cross(cur))
bbb.Faucet(cross(cur))
days := streak.CheckIn(cross(cur))
uassert.Equal(t, 1, days, "first check-in is day 1")
got := map[string]string{}
for _, f := range Report(seeded) {
if f.zero {
continue
}
got[f.source+" "+f.what] = f.value
}
uassert.Equal(t, "4200 "+erc20.Symbol(), got["r/moul/x/daily/erc20/v0 balance"])
uassert.Equal(t, "1 day(s)", got["r/moul/x/daily/streak/v0 current streak"])
uassert.Equal(t, "1 day(s)", got["r/moul/x/daily/streak/v0 best streak"])
uassert.True(t, got["r/moul/x/pairs/aaa/v0 balance"] != "", "aaa faucet must show up")
uassert.True(t, got["r/moul/x/pairs/bbb/v0 balance"] != "", "bbb faucet must show up")
}
// The rendered page for the seeded address must carry the same four facts, and
// must hide the reads that came back empty rather than printing a wall of zeros.
func TestRenderAddressShowsOnlyLiveFacts(t *testing.T) {
out := Render(seeded.String())
uassert.True(t, strings.Contains(out, "4200 "+erc20.Symbol()), "erc20 balance on the page")
uassert.True(t, strings.Contains(out, "current streak"), "streak on the page")
uassert.True(t, strings.Contains(out, "r/moul/x/pairs/aaa/v0"), "aaa on the page")
uassert.True(t, strings.Contains(out, "reads came back empty and are hidden"), "quiet rows hidden")
uassert.False(t, strings.Contains(out, "vesting grant"), "an empty grant must not render")
}
// An address-shaped path that is not an address must not reach a source realm.
func TestRenderRejectsNonAddress(t *testing.T) {
out := Render("not-an-address")
uassert.True(t, strings.Contains(out, "is not a valid address"), "explicit refusal")
uassert.False(t, strings.Contains(out, "## Answers"), "no report for a bad path")
}
// The index reads a global from every source. Pinned by Contains rather than by
// an example, because most of those globals move with chain state.
func TestRenderIndexReadsEverySource(t *testing.T) {
out := Render("")
for _, s := range sourceRealms {
uassert.True(t, strings.Contains(out, s), "index must list "+s)
}
uassert.True(t, strings.Contains(out, "pool(s), index only"), "amm is flagged index-only")
uassert.True(t, strings.Contains(out, "roll(s) all time"), "dice global is read")
uassert.True(t, strings.Contains(out, "page(s) reacted to"), "reactions global is read")
}
// ExampleRender pins the report for an address no test writes to. Every value
// in it comes from a source realm's zero state, except the rate limiter, which
// answers a full bucket for an address it has never seen: absence and a fresh
// allowance are the same thing there, and the report says so rather than
// pretending the read was empty.
func ExampleRender() {
print(Render(fresh.String()))
// Output:
// # across: `g1v93hymmnwvkkvun9wd597h6lta047h6l220muq`
//
// What 15 independently deployed realms say about this address. None of them was changed to answer.
//
// ## Answers
//
// | source | fact | value |
// | --- | --- | --- |
// | [`r/moul/x/daily/ratelimitdemo/v0`](/r/moul/x/daily/ratelimitdemo/v0) | rate-limit tokens left | 5 |
//
// 17 of 18 reads came back empty and are hidden.
//
// [back to the index](/r/moul/x/across/v0)
}
gnomod.toml
module = "gno.land/r/moul/x/across/v0" gno = "0.9" private = true [addpkg] creator = "g1manfred47kzduec920z88wfr64ylksmdcedlf5" height = 410016 max_deposit = "10000000ugnot"