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.

caps.gno

5.28 Kb · 165 lines
  1// Package agentvault is the reusable spend-cap library behind the
  2// AgentVault realm: the first wallet an AI agent cannot drain.
  3//
  4// It is pure: no realm state, no chain calls, no imports from any
  5// realm. Every function takes explicit inputs (including the current
  6// unix time for day rollovers) so the logic is unit-testable without
  7// a chain. The vault realm in
  8// gno.land/r/g16740fm9kd28luuktn3g42jfq3r998fm35chclv/agentvault
  9// stores AgentBudget values and calls these functions; any other
 10// realm can import this package and get the same caps.
 11package agentvault
 12
 13// DaySeconds is the length of one daily budget window, in seconds.
 14// Days are wall-clock UTC days counted from the Unix epoch.
 15const DaySeconds = int64(86400)
 16
 17// AgentBudget is one agent's spend limit. It is a plain value type:
 18// the owning realm stores it and passes it back in. SpentToday and
 19// DayIndex are bookkeeping the realm must not forge; RefreshDay is
 20// the only function that moves them, and only forward in time.
 21type AgentBudget struct {
 22	PerTxCap   int64 // max ugnot in a single spend
 23	DailyCap   int64 // max ugnot per day
 24	SpentToday int64 // ugnot already spent in DayIndex
 25	DayIndex   int64 // unix day (nowUnix / DaySeconds) SpentToday belongs to
 26}
 27
 28// Attempt is one entry in a vault's append-only spend log. Allowed
 29// false means the guard stopped the spend; Reason names why, so the
 30// blocked attempt is evidence, not just a failed transaction.
 31type Attempt struct {
 32	ID      int64
 33	Kind    string // "spend", "escrow", "release", "refund", "fund"
 34	Agent   address
 35	Name    string // resolved agent name, if the attempt named one
 36	Amount  int64
 37	Purpose string
 38	Allowed bool
 39	Reason  string
 40	Day     int64 // unix day of the attempt
 41}
 42
 43// EscrowStatus is the lifecycle state of a name payment held in escrow.
 44type EscrowStatus int
 45
 46const (
 47	EscrowHeld EscrowStatus = iota
 48	EscrowReleased
 49	EscrowRefunded
 50)
 51
 52// Escrow is one name payment held by the vault until the named
 53// recipient confirms delivery (release) or the owner refunds it
 54// after the deadline block (refund). The coins never leave the vault
 55// while held; release sends them, refund only flips the status.
 56type Escrow struct {
 57	ID             int64
 58	Payer          address
 59	RecipientName  string
 60	Recipient      address
 61	Amount         int64
 62	DeadlineHeight int64
 63	Status         EscrowStatus
 64}
 65
 66// DayIndex returns the unix day for a unix timestamp.
 67func DayIndex(nowUnix int64) int64 {
 68	return nowUnix / DaySeconds
 69}
 70
 71// ValidateCaps reports "" when a cap pair is usable, otherwise the
 72// reason it is not. Both caps must be positive and the
 73// per-transaction cap must not exceed the daily cap (a per-tx cap
 74// above the daily cap could never be spent in full).
 75func ValidateCaps(perTxCap, dailyCap int64) string {
 76	if perTxCap <= 0 {
 77		return "per-transaction cap must be positive"
 78	}
 79	if dailyCap <= 0 {
 80		return "daily cap must be positive"
 81	}
 82	if perTxCap > dailyCap {
 83		return "per-transaction cap must not exceed daily cap"
 84	}
 85	return ""
 86}
 87
 88// NewBudget builds a budget. Callers must validate first with
 89// ValidateCaps; an invalid pair panics rather than silently
 90// installing a budget that can never be spent.
 91func NewBudget(perTxCap, dailyCap int64) AgentBudget {
 92	if reason := ValidateCaps(perTxCap, dailyCap); reason != "" {
 93		panic(reason)
 94	}
 95	return AgentBudget{PerTxCap: perTxCap, DailyCap: dailyCap}
 96}
 97
 98// RefreshDay resets SpentToday when the day has rolled over. Call it
 99// before CheckSpend so a stale day never blocks a new day's spend.
100// It only ever moves the day forward and clears the spent amount;
101// it never restores spent funds.
102func RefreshDay(b *AgentBudget, nowUnix int64) {
103	day := DayIndex(nowUnix)
104	if b.DayIndex != day {
105		b.SpentToday = 0
106		b.DayIndex = day
107	}
108}
109
110// CheckSpend reports whether amount may be spent under this budget.
111// It does not mutate the budget; call RefreshDay first, then
112// ApplySpend after a successful check. The order of the checks is
113// deliberate: the cheapest, most specific rejection comes first so
114// the logged reason tells the agent exactly which limit it hit.
115func CheckSpend(b AgentBudget, amount int64) (bool, string) {
116	if amount <= 0 {
117		return false, "amount must be positive"
118	}
119	if amount > b.PerTxCap {
120		return false, "exceeds per-transaction cap"
121	}
122	if b.SpentToday+amount > b.DailyCap {
123		return false, "exceeds daily cap"
124	}
125	return true, "ok"
126}
127
128// ApplySpend records a spend that already passed CheckSpend.
129func ApplySpend(b *AgentBudget, amount int64) {
130	b.SpentToday += amount
131}
132
133// RemainingToday returns how much of the daily cap is left.
134func RemainingToday(b AgentBudget, nowUnix int64) int64 {
135	if DayIndex(nowUnix) != b.DayIndex {
136		return b.DailyCap
137	}
138	rem := b.DailyCap - b.SpentToday
139	if rem < 0 {
140		return 0
141	}
142	return rem
143}
144
145// EscrowRefundable reports whether a held escrow may be refunded:
146// the deadline block has passed and it is still held. A released or
147// already-refunded escrow is never refundable again.
148func EscrowRefundable(e Escrow, height int64) bool {
149	return e.Status == EscrowHeld && height > e.DeadlineHeight
150}
151
152// EscrowStatusString renders an escrow status for the log and the
153// Render page.
154func EscrowStatusString(s EscrowStatus) string {
155	switch s {
156	case EscrowHeld:
157		return "HELD"
158	case EscrowReleased:
159		return "RELEASED"
160	case EscrowRefunded:
161		return "REFUNDED"
162	default:
163		return "UNKNOWN"
164	}
165}