Playground
gnomod.toml
module = "gno.land/r/nym-moultest123/x/daily/splitter/v0" gno = "0.9" [addpkg] creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv" height = 186408 max_deposit = "5000000ugnot"
render_example_test.gno
package splitter
// ExampleRender pins the realm's Render output as a testable example.
func ExampleRender() {
print(Render(""))
// Output:
// # Payment Splitter
//
// _No groups registered yet._
}
splitter.gno
// Package splitter is a share-based payment splitter for gno.land.
//
// It is an accounting-only port of the Solidity PaymentSplitter pattern:
// no real coins move. A group is registered with a list of payees and a
// matching list of integer shares. Income recorded against a group is
// pooled, and each payee is owed a slice of that pool proportional to
// their shares: owed = pool * share / totalShares.
package splitter
import (
"errors"
"strconv"
"strings"
"chain"
"chain/runtime/unsafe"
"gno.land/p/nt/avl/v0"
)
// payee is a single share holder inside a group.
type payee struct {
addr address
shares int
}
// group is one payment-splitting arrangement.
type group struct {
id int
owner address
payees []payee
totalShares int
pool int // total income recorded, in abstract units
}
var (
groups avl.Tree // id (zero-padded string) -> *group
nextID int
errEmpty = errors.New("splitter: no payees")
)
// Register creates a new group from comma-separated payees and shares.
// payees: "g1abc...,g1def..." (addresses)
// shares: "3,1" (positive integers, same count as payees)
// Returns the new group id. Panics on malformed input.
func Register(cur realm, payees string, shares string) int {
caller := unsafe.PreviousRealm().Address()
addrParts := splitTrim(payees)
shareParts := splitTrim(shares)
if len(addrParts) == 0 {
panic(errEmpty)
}
if len(addrParts) != len(shareParts) {
panic("splitter: payees and shares count mismatch")
}
g := &group{
id: nextID,
owner: caller,
}
for i := range addrParts {
if addrParts[i] == "" {
panic("splitter: empty payee address")
}
s, err := strconv.Atoi(shareParts[i])
if err != nil {
panic("splitter: bad share value: " + shareParts[i])
}
if s <= 0 {
panic("splitter: share must be positive")
}
g.payees = append(g.payees, payee{
addr: address(addrParts[i]),
shares: s,
})
g.totalShares += s
}
groups.Set(key(g.id), g)
nextID++
chain.Emit(
"GroupRegistered",
"id", strconv.Itoa(g.id),
"payees", strconv.Itoa(len(g.payees)),
"totalShares", strconv.Itoa(g.totalShares),
)
return g.id
}
// RecordIncome adds amount to the pool of group id. amount must be positive.
func RecordIncome(cur realm, id int, amount int) {
if amount <= 0 {
panic("splitter: amount must be positive")
}
v := groups.Get(key(id))
if v == nil {
panic("splitter: unknown group id: " + strconv.Itoa(id))
}
g := v.(*group)
g.pool += amount
groups.Set(key(id), g)
chain.Emit(
"IncomeRecorded",
"id", strconv.Itoa(id),
"amount", strconv.Itoa(amount),
"pool", strconv.Itoa(g.pool),
)
}
// owed returns the amount owed to a payee: pool * shares / totalShares.
func (g *group) owed(p payee) int {
if g.totalShares == 0 {
return 0
}
return g.pool * p.shares / g.totalShares
}
// Render shows all groups, their payees, shares, and computed owed amounts.
func Render(path string) string {
if groups.Size() == 0 {
return "# Payment Splitter\n\n_No groups registered yet._\n"
}
var b strings.Builder
b.WriteString("# Payment Splitter\n\n")
b.WriteString("Share-based accounting. `owed = pool * share / totalShares`.\n\n")
groups.Iterate("", "", func(k string, v interface{}) bool {
g := v.(*group)
b.WriteString("## Group #" + strconv.Itoa(g.id) + "\n\n")
b.WriteString("- Owner: `" + g.owner.String() + "`\n")
b.WriteString("- Pool: " + strconv.Itoa(g.pool) + "\n")
b.WriteString("- Total shares: " + strconv.Itoa(g.totalShares) + "\n\n")
b.WriteString("| Payee | Shares | Owed |\n")
b.WriteString("|---|---:|---:|\n")
distributed := 0
for _, p := range g.payees {
o := g.owed(p)
distributed += o
b.WriteString("| `" + p.addr.String() + "` | " +
strconv.Itoa(p.shares) + " | " + strconv.Itoa(o) + " |\n")
}
remainder := g.pool - distributed
if remainder > 0 {
b.WriteString("| _remainder (rounding)_ | | " +
strconv.Itoa(remainder) + " |\n")
}
b.WriteString("\n")
return false
})
return b.String()
}
// splitTrim splits on commas and trims whitespace around each element.
func splitTrim(s string) []string {
if strings.TrimSpace(s) == "" {
return nil
}
parts := strings.Split(s, ",")
out := make([]string, 0, len(parts))
for _, p := range parts {
out = append(out, strings.TrimSpace(p))
}
return out
}
// key produces a zero-padded, lexicographically-sortable avl key for an id,
// so Render iterates groups in ascending numeric order.
func key(id int) string {
s := strconv.Itoa(id)
for len(s) < 12 {
s = "0" + s
}
return s
}
splitter_test.gno
package splitter
import (
"strings"
"testing"
"gno.land/p/nt/avl/v0"
"gno.land/p/nt/testutils/v0"
"gno.land/p/nt/uassert/v0"
)
func reset() {
groups = avl.Tree{}
nextID = 0
}
func TestRegisterAndOwed(cur realm, t *testing.T) {
// reset package state for a deterministic run
reset()
owner := testutils.TestAddress("owner")
testing.SetRealm(testing.NewUserRealm(owner))
a := testutils.TestAddress("alice").String()
b := testutils.TestAddress("bob").String()
id := Register(cross(cur), a+","+b, "3,1")
uassert.Equal(t, 0, id, "first group id should be 0")
// pool 100, shares 3:1 -> alice 75, bob 25
RecordIncome(cross(cur), id, 100)
out := Render("")
uassert.True(t, strings.Contains(out, "Group #0"), "render shows group")
uassert.True(t, strings.Contains(out, "| 3 | 75 |"), "alice owed 75")
uassert.True(t, strings.Contains(out, "| 1 | 25 |"), "bob owed 25")
uassert.True(t, strings.Contains(out, "Pool: 100"), "pool shown")
}
func TestRoundingRemainder(cur realm, t *testing.T) {
reset()
owner := testutils.TestAddress("owner2")
testing.SetRealm(testing.NewUserRealm(owner))
a := testutils.TestAddress("a").String()
b := testutils.TestAddress("b").String()
c := testutils.TestAddress("c").String()
id := Register(cross(cur), a+","+b+","+c, "1,1,1")
RecordIncome(cross(cur), id, 100) // 100/3 = 33 each, remainder 1
out := Render("")
uassert.True(t, strings.Contains(out, "| 1 | 33 |"), "each owed 33")
uassert.True(t, strings.Contains(out, "remainder"), "remainder row shown")
uassert.True(t, strings.Contains(out, "| 1 |\n"), "remainder value 1")
}
func TestBadInputAborts(cur realm, t *testing.T) {
reset()
owner := testutils.TestAddress("owner3")
testing.SetRealm(testing.NewUserRealm(owner))
a := testutils.TestAddress("alice").String()
// mismatched payees/shares count aborts
uassert.AbortsWithMessage(t, cur, "splitter: payees and shares count mismatch", func() {
Register(cross(cur), a, "1,2")
})
// non-positive share aborts
uassert.AbortsWithMessage(t, cur, "splitter: share must be positive", func() {
Register(cross(cur), a, "0")
})
}