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

7.47 Kb · 228 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/v2"
 11	"gno.land/p/moul/txlink/v0"
 12	"gno.land/r/g1aj9pzthj36aav0kff2zcmx9llapa7ng7xzcfct/shared_budget/treasury/v3"
 13)
 14
 15const packagePath = "gno.land/r/g1aj9pzthj36aav0kff2zcmx9llapa7ng7xzcfct/shared_budget/reimbursements/v3"
 16
 17type Roles struct {
 18	Lane           string
 19	Clerk          address
 20	FinanceOfficer address
 21}
 22
 23type expense struct {
 24	digest   string
 25	decision string
 26}
 27
 28type program struct {
 29	roles    Roles
 30	expenses map[int64]*expense
 31}
 32
 33type ExpenseView struct {
 34	Evidence    string
 35	Decision    string
 36	Reservation treasury.ReservationView
 37}
 38
 39var programs = make(map[int64]*program)
 40
 41func Configure(cur realm, programID int64, lane string, clerk, financeOfficer address) {
 42	caller := userCaller(cur)
 43	config := treasury.GetProgram(programID)
 44	if config.Sponsor != caller {
 45		panic("only the program sponsor can configure reimbursements")
 46	}
 47	if config.Status != treasury.Draft || programs[programID] != nil {
 48		panic("reimbursements configuration requires an unconfigured draft")
 49	}
 50	requireRoles(clerk, financeOfficer)
 51	runtime.AssertOriginCall()
 52	treasury.RegisterPolicy(cross(cur), programID, lane)
 53	programs[programID] = &program{
 54		roles:    Roles{Lane: lane, Clerk: clerk, FinanceOfficer: financeOfficer},
 55		expenses: make(map[int64]*expense),
 56	}
 57	chain.Emit("ReimbursementsConfigured", "program", number(programID), "lane", lane,
 58		"clerk", string(clerk), "finance", string(financeOfficer))
 59}
 60
 61func SubmitExpense(cur realm, programID int64, recipient address, amount, holdBlocks int64, proofDigest, memo string) (int64, int64) {
 62	caller := userCaller(cur)
 63	p := mustProgram(programID)
 64	if caller != p.roles.Clerk {
 65		panic("only the expense clerk can submit")
 66	}
 67	if recipient == p.roles.FinanceOfficer {
 68		panic("finance officer cannot be the recipient")
 69	}
 70	budget.RequireDigest(proofDigest)
 71	budget.RequirePositive(holdBlocks)
 72	deadline := budget.Add(runtime.ChainHeight(), holdBlocks)
 73	runtime.AssertOriginCall()
 74	itemID, available := treasury.Reserve(cross(cur), programID, recipient, amount, deadline, memo)
 75	lane := p.roles.Lane
 76	evidence := budget.EvidenceCommitment(programID, lane, itemID, proofDigest)
 77	p.expenses[itemID] = &expense{digest: evidence}
 78	chain.Emit("ExpenseSubmitted", "program", number(programID), "lane", lane, "item", number(itemID),
 79		"actor", string(caller), "evidence", evidence)
 80	return itemID, available
 81}
 82
 83func Decide(cur realm, programID, itemID int64, evidence string, approve bool) string {
 84	caller := userCaller(cur)
 85	p := mustProgram(programID)
 86	if caller != p.roles.FinanceOfficer {
 87		panic("only the finance officer can decide")
 88	}
 89	item := p.mustExpense(itemID)
 90	if item.digest != evidence {
 91		panic("expense evidence does not match")
 92	}
 93	if item.decision != "" {
 94		panic("expense already decided")
 95	}
 96	if caller == treasury.GetReservation(programID, p.roles.Lane, itemID).Recipient {
 97		panic("finance officer cannot be the recipient")
 98	}
 99	runtime.AssertOriginCall()
100	if approve {
101		treasury.Approve(cross(cur), programID, itemID)
102		item.decision = treasury.Approved
103	} else {
104		treasury.Reject(cross(cur), programID, itemID)
105		item.decision = treasury.Rejected
106	}
107	chain.Emit("ExpenseDecided", "program", number(programID), "lane", p.roles.Lane, "item", number(itemID),
108		"officer", string(caller), "decision", item.decision, "evidence", evidence)
109	return item.decision
110}
111
112func CancelExpense(cur realm, programID, itemID int64) {
113	caller := userCaller(cur)
114	p := mustProgram(programID)
115	if caller != p.roles.Clerk {
116		panic("only the expense clerk can cancel")
117	}
118	if p.mustExpense(itemID).decision != "" {
119		panic("expense already decided")
120	}
121	runtime.AssertOriginCall()
122	treasury.Cancel(cross(cur), programID, itemID)
123}
124
125func RotateRoles(cur realm, programID int64, clerk, financeOfficer address) {
126	caller := userCaller(cur)
127	if treasury.GetProgram(programID).Sponsor != caller {
128		panic("only the program sponsor can rotate reimbursement roles")
129	}
130	requireRoles(clerk, financeOfficer)
131	p := mustProgram(programID)
132	runtime.AssertOriginCall()
133	p.roles.Clerk, p.roles.FinanceOfficer = clerk, financeOfficer
134	chain.Emit("RolesRotated", "program", number(programID), "lane", p.roles.Lane,
135		"clerk", string(clerk), "finance", string(financeOfficer))
136}
137
138func GetRoles(programID int64) Roles {
139	return mustProgram(programID).roles
140}
141
142func GetExpense(programID, itemID int64) ExpenseView {
143	p := mustProgram(programID)
144	item := p.mustExpense(itemID)
145	return ExpenseView{Evidence: item.digest, Decision: item.decision,
146		Reservation: treasury.GetReservation(programID, p.roles.Lane, itemID)}
147}
148
149func Render(path string) string {
150	id, itemID := budget.ParsePolicyViewPath(path)
151	p := programs[id]
152	if p == nil {
153		return "# Reimbursements\n\nOpen a configured program with `:programID` or an expense with `:programID/itemID`."
154	}
155	program := treasury.GetProgram(id)
156	tx := txlink.Realm(packagePath)
157	args := []string{"programID", number(id)}
158	if itemID == 0 {
159		page := "# Reimbursements · Program " + number(id) + "\n\n**Lane** " + budget.MarkdownText(p.roles.Lane) +
160			"\n\n| Role | Address |\n| --- | --- |\n| Expense clerk | " + string(p.roles.Clerk) +
161			" |\n| Finance officer | " + string(p.roles.FinanceOfficer) + " |\n\n" +
162			"[Rotate roles](" + tx.Call("RotateRoles", args...) + ")"
163		if program.Status == treasury.Active {
164			page += " · [Submit an expense](" + tx.Call("SubmitExpense", args...) + ")"
165		}
166		return page + "\n"
167	}
168	if program.Pruned {
169		return "# Reimbursements\n\nThe treasury pruned this program's reservation records."
170	}
171	if p.expenses[itemID] == nil {
172		return "# Reimbursements\n\nExpense not found."
173	}
174	expense := GetExpense(id, itemID)
175	reservation := expense.Reservation
176	page := "# Expense " + number(itemID) + "\n\n**Status** " + reservation.Status +
177		" · **Amount** " + budget.FormatUGNOT(reservation.Amount) +
178		"\n\n**Memo** " + budget.MarkdownText(reservation.Memo) + "\n\n**Evidence** `" + expense.Evidence + "`\n"
179	if reservation.Status != treasury.Reserved || reservation.ExpiredAtHeight {
180		return page
181	}
182	args = append(args, "itemID", number(itemID))
183	page += "\n[Cancel expense](" + tx.Call("CancelExpense", args...) + ")\n"
184	if reservation.Recipient == p.roles.FinanceOfficer {
185		return page + "\nThe current finance officer is the recipient and cannot decide this expense.\n"
186	}
187	args = append(args, "evidence", expense.Evidence)
188	return page + "\n[Approve expense](" + tx.Call("Decide", append(args, "approve", "true")...) +
189		") · [Reject expense](" + tx.Call("Decide", append(args, "approve", "false")...) + ")\n"
190}
191
192func requireRoles(clerk, financeOfficer address) {
193	budget.RequireAddress(clerk)
194	budget.RequireAddress(financeOfficer)
195	if clerk == financeOfficer {
196		panic("expense clerk and finance officer must be distinct")
197	}
198}
199
200func mustProgram(id int64) *program {
201	p := programs[id]
202	if p == nil {
203		panic("reimbursements is not configured for this program")
204	}
205	return p
206}
207
208func (p *program) mustExpense(itemID int64) *expense {
209	item := p.expenses[itemID]
210	if item == nil {
211		panic("expense not found")
212	}
213	return item
214}
215
216func userCaller(cur realm) address {
217	if !cur.IsCurrent() || cur.PkgPath() != packagePath || !cur.Previous().IsUserCall() {
218		panic("direct reimbursements call required")
219	}
220	if len(unsafe.OriginSend()) != 0 {
221		panic("unexpected attached coins")
222	}
223	return cur.Previous().Address()
224}
225
226func number(n int64) string {
227	return strconv.FormatInt(n, 10)
228}