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

budget.gno

// Package budget implements the value-only accounting used by Shared Budget.
package budget

import (
	"crypto/bech32"
	"crypto/sha256"
	"encoding/hex"
	"strconv"
	"strings"
)

const (
	Denom          = "ugnot"
	Maintenance    = "maintenance"
	Reimbursements = "reimbursements"
	Refund         = "refund"
	maxInt64       = 1<<63 - 1
)

type Ledger struct {
	Funded    int64
	Available int64
	Reserved  int64
	Claimable int64
	Paid      int64
}

func Add(a, b int64) int64 {
	if a < 0 || b < 0 || a > maxInt64-b {
		panic("amount or height overflow")
	}
	return a + b
}

func Subtract(a, b int64) int64 {
	if b < 0 || a < b {
		panic("amount underflow")
	}
	return a - b
}

func RequirePositive(amount int64) {
	if amount <= 0 {
		panic("amount must be positive")
	}
}

func (l Ledger) Fund(amount int64) Ledger {
	RequirePositive(amount)
	l.Funded = Add(l.Funded, amount)
	l.Available = Add(l.Available, amount)
	return l
}

func (l Ledger) Reserve(amount int64) Ledger {
	RequirePositive(amount)
	l.Available = Subtract(l.Available, amount)
	l.Reserved = Add(l.Reserved, amount)
	return l
}

func (l Ledger) Approve(amount int64) Ledger {
	RequirePositive(amount)
	l.Reserved = Subtract(l.Reserved, amount)
	l.Claimable = Add(l.Claimable, amount)
	return l
}

func (l Ledger) Release(amount int64) Ledger {
	RequirePositive(amount)
	l.Reserved = Subtract(l.Reserved, amount)
	l.Available = Add(l.Available, amount)
	return l
}

func (l Ledger) RefundAvailable() Ledger {
	l.Claimable = Add(l.Claimable, l.Available)
	l.Available = 0
	return l
}

func (l Ledger) Pay(amount int64) Ledger {
	RequirePositive(amount)
	l.Claimable = Subtract(l.Claimable, amount)
	l.Paid = Add(l.Paid, amount)
	return l
}

func PeriodAt(height, start, periodBlocks int64) int64 {
	if start < 0 || height < start || periodBlocks <= 0 {
		panic("invalid period")
	}
	return (height - start) / periodBlocks
}

func CheckDeadline(height, deadline, maxHold int64) {
	if height < 0 || deadline <= height || deadline-height > maxHold {
		panic("deadline outside reservation window")
	}
}

func RequireAddress(addr address) {
	s := string(addr)
	if len(s) != 40 || !strings.HasPrefix(s, "g1") {
		panic("invalid Gno address")
	}
	hrp, data, version, err := bech32.DecodeGeneric(s)
	if err != nil || hrp != "g" || len(data) != 32 || version != bech32.Version0 {
		panic("invalid Gno address")
	}
}

func RequireDigest(digest string) {
	if len(digest) != 64 || strings.ToLower(digest) != digest {
		panic("evidence must be a lowercase SHA-256 digest")
	}
	if _, err := hex.DecodeString(digest); err != nil {
		panic("evidence must be a lowercase SHA-256 digest")
	}
}

// EvidenceCommitment binds a salted proof digest to its program and item.
func EvidenceCommitment(programID int64, lane string, itemID int64, proofDigest string) string {
	RequireDigest(proofDigest)
	payload := "shared-budget/v1\n" + strconv.FormatInt(programID, 10) + "\n" +
		lane + "\n" + strconv.FormatInt(itemID, 10) + "\n" + proofDigest + "\n"
	hash := sha256.Sum256([]byte(payload))
	return hex.EncodeToString(hash[:])
}

// ParsePolicyViewPath reads programID or programID/itemID for policy renders.
func ParsePolicyViewPath(path string) (int64, int64) {
	parts := strings.SplitN(path, "/", 3)
	programID, err := strconv.ParseInt(parts[0], 10, 64)
	if err != nil || programID <= 0 {
		return 0, 0
	}
	if len(parts) == 1 {
		return programID, 0
	}
	itemID, err := strconv.ParseInt(parts[1], 10, 64)
	if len(parts) != 2 || err != nil || itemID <= 0 {
		return 0, 0
	}
	return programID, itemID
}

budget_test.gno

package budget

import "testing"

func TestLedgerConservesFunds(t *testing.T) {
	ledger := Ledger{}.Fund(1000000).Reserve(600000).Reserve(300000)
	checkLedger(t, ledger, Ledger{Funded: 1000000, Available: 100000, Reserved: 900000})
	ledger = ledger.Approve(300000).Pay(300000).Approve(600000).Reserve(100000)
	checkLedger(t, ledger, Ledger{Funded: 1000000, Reserved: 100000, Claimable: 600000, Paid: 300000})
	ledger = ledger.Release(100000).RefundAvailable()
	checkLedger(t, ledger, Ledger{Funded: 1000000, Claimable: 700000, Paid: 300000})
	ledger = ledger.Pay(100000).Pay(600000)
	checkLedger(t, ledger, Ledger{Funded: 1000000, Paid: 1000000})
}

func TestPeriodAndDeadlineBoundaries(t *testing.T) {
	if PeriodAt(119, 100, 20) != 0 || PeriodAt(120, 100, 20) != 1 || PeriodAt(140, 100, 20) != 2 {
		t.Fatal("period must change at its first height")
	}
	CheckDeadline(100, 120, 20)
	expectPanic(t, func() { CheckDeadline(100, 100, 20) })
	expectPanic(t, func() { CheckDeadline(100, 121, 20) })
}

func TestInvalidLedgerTransitions(t *testing.T) {
	expectPanic(t, func() { Ledger{}.Fund(0) })
	expectPanic(t, func() { Ledger{}.Reserve(1) })
	expectPanic(t, func() { Ledger{}.Approve(1) })
	expectPanic(t, func() { Ledger{}.Release(1) })
	expectPanic(t, func() { Ledger{}.Pay(1) })
	expectPanic(t, func() { Add(maxInt64, 1) })
	expectPanic(t, func() { Subtract(1, 2) })
}

func TestAddressAndEvidenceValidation(t *testing.T) {
	RequireAddress(address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5"))
	RequireDigest("29b3d9914b5af7272b4a89af0a9427ff6dc8ee75cf28739414fc2a46372fd38f")
	expectPanic(t, func() { RequireAddress(address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf6")) })
	expectPanic(t, func() { RequireDigest("invalid") })
}

func TestEvidenceCommitmentBindsContext(t *testing.T) {
	proof := "29b3d9914b5af7272b4a89af0a9427ff6dc8ee75cf28739414fc2a46372fd38f"
	a := EvidenceCommitment(1, Maintenance, 1, proof)
	RequireDigest(a)
	if a == EvidenceCommitment(2, Maintenance, 1, proof) ||
		a == EvidenceCommitment(1, Reimbursements, 1, proof) ||
		a == EvidenceCommitment(1, Maintenance, 2, proof) {
		t.Fatal("evidence commitment did not bind the context")
	}
}

func checkLedger(t *testing.T, actual, expected Ledger) {
	if actual != expected {
		t.Fatalf("ledger got %+v, want %+v", actual, expected)
	}
	if actual.Funded != actual.Available+actual.Reserved+actual.Claimable+actual.Paid {
		t.Fatal("ledger does not conserve funds")
	}
}

func expectPanic(t *testing.T, run func()) {
	defer func() {
		if recover() == nil {
			t.Error("expected panic")
		}
	}()
	run()
}

gnomod.toml

module = "gno.land/p/g1aj9pzthj36aav0kff2zcmx9llapa7ng7xzcfct/shared_budget/budget/v1"
gno = "0.9"

[addpkg]
  creator = "g1aj9pzthj36aav0kff2zcmx9llapa7ng7xzcfct"
  height = 1466525
  max_deposit = "10000000ugnot"