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.

budget.gno

3.46 Kb · 146 lines
  1// Package budget implements the value-only accounting used by Shared Budget.
  2package budget
  3
  4import (
  5	"crypto/bech32"
  6	"crypto/sha256"
  7	"encoding/hex"
  8	"strconv"
  9	"strings"
 10)
 11
 12const (
 13	Denom          = "ugnot"
 14	Maintenance    = "maintenance"
 15	Reimbursements = "reimbursements"
 16	Refund         = "refund"
 17	maxInt64       = 1<<63 - 1
 18)
 19
 20type Ledger struct {
 21	Funded    int64
 22	Available int64
 23	Reserved  int64
 24	Claimable int64
 25	Paid      int64
 26}
 27
 28func Add(a, b int64) int64 {
 29	if a < 0 || b < 0 || a > maxInt64-b {
 30		panic("amount or height overflow")
 31	}
 32	return a + b
 33}
 34
 35func Subtract(a, b int64) int64 {
 36	if b < 0 || a < b {
 37		panic("amount underflow")
 38	}
 39	return a - b
 40}
 41
 42func RequirePositive(amount int64) {
 43	if amount <= 0 {
 44		panic("amount must be positive")
 45	}
 46}
 47
 48func (l Ledger) Fund(amount int64) Ledger {
 49	RequirePositive(amount)
 50	l.Funded = Add(l.Funded, amount)
 51	l.Available = Add(l.Available, amount)
 52	return l
 53}
 54
 55func (l Ledger) Reserve(amount int64) Ledger {
 56	RequirePositive(amount)
 57	l.Available = Subtract(l.Available, amount)
 58	l.Reserved = Add(l.Reserved, amount)
 59	return l
 60}
 61
 62func (l Ledger) Approve(amount int64) Ledger {
 63	RequirePositive(amount)
 64	l.Reserved = Subtract(l.Reserved, amount)
 65	l.Claimable = Add(l.Claimable, amount)
 66	return l
 67}
 68
 69func (l Ledger) Release(amount int64) Ledger {
 70	RequirePositive(amount)
 71	l.Reserved = Subtract(l.Reserved, amount)
 72	l.Available = Add(l.Available, amount)
 73	return l
 74}
 75
 76func (l Ledger) RefundAvailable() Ledger {
 77	l.Claimable = Add(l.Claimable, l.Available)
 78	l.Available = 0
 79	return l
 80}
 81
 82func (l Ledger) Pay(amount int64) Ledger {
 83	RequirePositive(amount)
 84	l.Claimable = Subtract(l.Claimable, amount)
 85	l.Paid = Add(l.Paid, amount)
 86	return l
 87}
 88
 89func PeriodAt(height, start, periodBlocks int64) int64 {
 90	if start < 0 || height < start || periodBlocks <= 0 {
 91		panic("invalid period")
 92	}
 93	return (height - start) / periodBlocks
 94}
 95
 96func CheckDeadline(height, deadline, maxHold int64) {
 97	if height < 0 || deadline <= height || deadline-height > maxHold {
 98		panic("deadline outside reservation window")
 99	}
100}
101
102func RequireAddress(addr address) {
103	s := string(addr)
104	if len(s) != 40 || !strings.HasPrefix(s, "g1") {
105		panic("invalid Gno address")
106	}
107	hrp, data, version, err := bech32.DecodeGeneric(s)
108	if err != nil || hrp != "g" || len(data) != 32 || version != bech32.Version0 {
109		panic("invalid Gno address")
110	}
111}
112
113func RequireDigest(digest string) {
114	if len(digest) != 64 || strings.ToLower(digest) != digest {
115		panic("evidence must be a lowercase SHA-256 digest")
116	}
117	if _, err := hex.DecodeString(digest); err != nil {
118		panic("evidence must be a lowercase SHA-256 digest")
119	}
120}
121
122// EvidenceCommitment binds a salted proof digest to its program and item.
123func EvidenceCommitment(programID int64, lane string, itemID int64, proofDigest string) string {
124	RequireDigest(proofDigest)
125	payload := "shared-budget/v1\n" + strconv.FormatInt(programID, 10) + "\n" +
126		lane + "\n" + strconv.FormatInt(itemID, 10) + "\n" + proofDigest + "\n"
127	hash := sha256.Sum256([]byte(payload))
128	return hex.EncodeToString(hash[:])
129}
130
131// ParsePolicyViewPath reads programID or programID/itemID for policy renders.
132func ParsePolicyViewPath(path string) (int64, int64) {
133	parts := strings.SplitN(path, "/", 3)
134	programID, err := strconv.ParseInt(parts[0], 10, 64)
135	if err != nil || programID <= 0 {
136		return 0, 0
137	}
138	if len(parts) == 1 {
139		return programID, 0
140	}
141	itemID, err := strconv.ParseInt(parts[1], 10, 64)
142	if len(parts) != 2 || err != nil || itemID <= 0 {
143		return 0, 0
144	}
145	return programID, itemID
146}