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.

reimbursements.gno

8.62 Kb · 256 lines
  1// Package reimbursements separates expense submissions from finance decisions.
  2package reimbursements
  3
  4import (
  5	"chain"
  6	"chain/runtime"
  7	"chain/runtime/unsafe"
  8	"strconv"
  9
 10	"gno.land/p/g1aj9pzthj36aav0kff2zcmx9llapa7ng7xzcfct/shared_budget/budget/v3"
 11	"gno.land/p/moul/txlink/v0"
 12	"gno.land/p/nt/avl/v0"
 13	"gno.land/r/g1aj9pzthj36aav0kff2zcmx9llapa7ng7xzcfct/shared_budget/treasury/v4"
 14)
 15
 16type Roles struct {
 17	Lane           string
 18	Clerk          address
 19	FinanceOfficer address
 20}
 21
 22type expense struct {
 23	digest   string
 24	decision string
 25}
 26
 27type laneConfig struct {
 28	roles    Roles
 29	expenses *avl.Tree
 30}
 31
 32type ExpenseView struct {
 33	Pruned      bool
 34	Evidence    string
 35	Decision    string
 36	Reservation treasury.ReservationView
 37}
 38
 39var (
 40	realmPath = unsafe.CurrentRealm().PkgPath()
 41	lanes     = avl.NewTree()
 42)
 43
 44func Configure(cur realm, programID int64, lane string, clerk, financeOfficer address) {
 45	caller := callerAddress(cur)
 46	config := treasury.GetProgram(programID)
 47	if config.Sponsor != caller {
 48		panic("only the program sponsor can configure reimbursements")
 49	}
 50	requireRoles(clerk, financeOfficer)
 51	treasury.RegisterPolicy(cross(cur), programID, lane)
 52	lanes.Set(budget.PolicyLaneKey(programID, lane), &laneConfig{
 53		roles:    Roles{Lane: lane, Clerk: clerk, FinanceOfficer: financeOfficer},
 54		expenses: avl.NewTree(),
 55	})
 56	chain.Emit("ReimbursementsConfigured", "program", number(programID), "lane", lane,
 57		"clerk", string(clerk), "finance", string(financeOfficer))
 58}
 59
 60func SubmitExpense(cur realm, programID int64, lane string, recipient address, amount, holdBlocks int64, proofDigest, memo string) (int64, int64) {
 61	caller := callerAddress(cur)
 62	p := mustLane(programID, lane)
 63	if caller != p.roles.Clerk {
 64		panic("only the expense clerk can submit")
 65	}
 66	if recipient == p.roles.FinanceOfficer {
 67		panic("finance officer cannot be the recipient")
 68	}
 69	budget.RequireDigest(proofDigest)
 70	budget.RequirePositive(holdBlocks)
 71	deadline := budget.Add(runtime.ChainHeight(), holdBlocks)
 72	itemID, available := treasury.Reserve(cross(cur), programID, lane, recipient, amount, deadline, memo)
 73	evidence := budget.EvidenceCommitment(programID, lane, itemID, proofDigest)
 74	p.expenses.Set(number(itemID), &expense{digest: evidence})
 75	chain.Emit("ExpenseSubmitted", "program", number(programID), "lane", lane, "item", number(itemID),
 76		"actor", string(caller), "evidence", evidence)
 77	return itemID, available
 78}
 79
 80func Decide(cur realm, programID int64, lane string, itemID int64, evidence string, approve bool) string {
 81	caller := callerAddress(cur)
 82	p := mustLane(programID, lane)
 83	if caller != p.roles.FinanceOfficer {
 84		panic("only the finance officer can decide")
 85	}
 86	item := p.mustExpense(itemID)
 87	if item.digest != evidence {
 88		panic("expense evidence does not match")
 89	}
 90	if item.decision != "" {
 91		panic("expense already decided")
 92	}
 93	if caller == treasury.GetReservation(programID, p.roles.Lane, itemID).Recipient {
 94		panic("finance officer cannot be the recipient")
 95	}
 96	if approve {
 97		treasury.Approve(cross(cur), programID, lane, itemID)
 98		item.decision = treasury.Approved
 99	} else {
100		treasury.Reject(cross(cur), programID, lane, itemID)
101		item.decision = treasury.Rejected
102	}
103	chain.Emit("ExpenseDecided", "program", number(programID), "lane", p.roles.Lane, "item", number(itemID),
104		"officer", string(caller), "decision", item.decision, "evidence", evidence)
105	return item.decision
106}
107
108func CancelExpense(cur realm, programID int64, lane string, itemID int64) {
109	caller := callerAddress(cur)
110	p := mustLane(programID, lane)
111	if caller != p.roles.Clerk {
112		panic("only the expense clerk can cancel")
113	}
114	if p.mustExpense(itemID).decision != "" {
115		panic("expense already decided")
116	}
117	treasury.Cancel(cross(cur), programID, lane, itemID)
118}
119
120func RotateRoles(cur realm, programID int64, lane string, clerk, financeOfficer address) {
121	caller := callerAddress(cur)
122	if treasury.GetProgram(programID).Sponsor != caller {
123		panic("only the program sponsor can rotate reimbursement roles")
124	}
125	requireRoles(clerk, financeOfficer)
126	p := mustLane(programID, lane)
127	p.roles.Clerk, p.roles.FinanceOfficer = clerk, financeOfficer
128	chain.Emit("RolesRotated", "program", number(programID), "lane", p.roles.Lane,
129		"clerk", string(clerk), "finance", string(financeOfficer))
130}
131
132func GetRoles(programID int64, lane string) Roles {
133	return mustLane(programID, lane).roles
134}
135
136func PruneClosed(cur realm, programID int64, lane string) int64 {
137	caller := callerAddress(cur)
138	program := treasury.GetProgram(programID)
139	if caller != program.Sponsor {
140		panic("only the program sponsor can prune reimbursements")
141	}
142	p := mustLane(programID, lane)
143	if program.Status != treasury.Closed || program.Balance.Claimable != 0 ||
144		program.ActiveReservations != 0 || p.expenses == nil {
145		panic("pruning requires an unpruned closed lane with no unpaid claims or pending reservations")
146	}
147	count := int64(p.expenses.Size())
148	p.expenses = nil
149	chain.Emit("ReimbursementsPruned", "program", number(programID), "lane", lane, "items", number(count))
150	return count
151}
152
153func GetExpense(programID int64, lane string, itemID int64) ExpenseView {
154	p := mustLane(programID, lane)
155	if p.expenses == nil || treasury.GetProgram(programID).Pruned {
156		return ExpenseView{Pruned: true}
157	}
158	item := p.mustExpense(itemID)
159	return ExpenseView{Evidence: item.digest, Decision: item.decision,
160		Reservation: treasury.GetReservation(programID, lane, itemID)}
161}
162
163func Render(path string) string {
164	program, names, itemID := treasury.PolicyRenderRoute(path, realmPath)
165	if len(names) == 0 {
166		return "# Reimbursements\n\nOpen a configured lane with `:programID/lane` or a expense with `:programID/lane/itemID`."
167	}
168	if len(names) > 1 {
169		return "# Reimbursements lanes\n\n" + budget.PolicyLaneLinks(realmPath, program.ID, names)
170	}
171	id, lane := program.ID, names[0]
172	p := mustLane(id, lane)
173	tx := txlink.Realm(realmPath)
174	args := []string{"programID", number(id), "lane", lane}
175	if itemID == 0 {
176		page := "# Reimbursements · Program " + number(id) + "\n\n**Lane** " + budget.MarkdownText(p.roles.Lane) +
177			"\n\n| Role | Address |\n| --- | --- |\n| Expense clerk | " + string(p.roles.Clerk) +
178			" |\n| Finance officer | " + string(p.roles.FinanceOfficer) + " |\n\n" +
179			"[Rotate roles](" + tx.Call("RotateRoles", args...) + ")"
180		if program.Status == treasury.Active {
181			page += " · [Submit an expense](" + tx.Call("SubmitExpense", args...) + ")"
182		}
183		if program.Status == treasury.Closed && program.Balance.Claimable == 0 && p.expenses != nil {
184			page += " · [Prune expenses](" + tx.Call("PruneClosed", args...) + ")"
185		}
186		return page + "\n"
187	}
188	if program.Pruned || p.expenses == nil {
189		return "# Reimbursements\n\nExpense records were pruned."
190	}
191	if p.expense(itemID) == nil {
192		return "# Reimbursements\n\nExpense not found."
193	}
194	expense := GetExpense(id, lane, itemID)
195	reservation := expense.Reservation
196	page := "# Expense " + number(itemID) + "\n\n**Status** " + reservation.Status +
197		" · **Amount** " + budget.MarkdownText(program.FormatAmount(reservation.Amount)) +
198		"\n\n**Memo** " + budget.MarkdownText(reservation.Memo) + "\n\n**Evidence** `" + expense.Evidence + "`\n"
199	if reservation.Status != treasury.Reserved || reservation.ExpiredAtHeight {
200		return page
201	}
202	args = append(args, "itemID", number(itemID))
203	page += "\n[Cancel expense](" + tx.Call("CancelExpense", args...) + ")\n"
204	if reservation.Recipient == p.roles.FinanceOfficer {
205		return page + "\nThe current finance officer is the recipient and cannot decide this expense.\n"
206	}
207	args = append(args, "evidence", expense.Evidence)
208	return page + "\n[Approve expense](" + tx.Call("Decide", append(args, "approve", "true")...) +
209		") · [Reject expense](" + tx.Call("Decide", append(args, "approve", "false")...) + ")\n"
210}
211
212func requireRoles(clerk, financeOfficer address) {
213	budget.RequireAddress(clerk)
214	budget.RequireAddress(financeOfficer)
215	if clerk == financeOfficer {
216		panic("expense clerk and finance officer must be distinct")
217	}
218}
219
220func mustLane(programID int64, lane string) *laneConfig {
221	p, _ := lanes.Get(budget.PolicyLaneKey(programID, lane)).(*laneConfig)
222	if p == nil {
223		panic("reimbursements lane is not configured")
224	}
225	return p
226}
227
228func (p *laneConfig) expense(itemID int64) *expense {
229	if p.expenses == nil {
230		return nil
231	}
232	item, _ := p.expenses.Get(number(itemID)).(*expense)
233	return item
234}
235
236func (p *laneConfig) mustExpense(itemID int64) *expense {
237	item := p.expense(itemID)
238	if item == nil {
239		panic("expense not found")
240	}
241	return item
242}
243
244func callerAddress(cur realm) address {
245	if !cur.IsCurrent() || cur.PkgPath() != realmPath {
246		panic("invalid reimbursements context")
247	}
248	if len(unsafe.OriginSend()) != 0 {
249		panic("unexpected attached coins")
250	}
251	return cur.Previous().Address()
252}
253
254func number(n int64) string {
255	return strconv.FormatInt(n, 10)
256}