views.gno
8.00 Kb · 265 lines
1package treasury
2
3import (
4 "chain"
5 "chain/banker"
6 "chain/runtime"
7 "strconv"
8 "strings"
9
10 "gno.land/p/g1aj9pzthj36aav0kff2zcmx9llapa7ng7xzcfct/shared_budget/budget/v1"
11)
12
13const MaxPageSize = 20
14
15type PolicyView struct {
16 Path string
17 Address address
18 PeriodCap int64
19 ItemCap int64
20 Registered bool
21 Used int64
22}
23
24type ProgramView struct {
25 ID int64
26 Sponsor address
27 Status string
28 StartHeight int64
29 IssueEndHeight int64
30 PeriodBlocks int64
31 MaxHoldBlocks int64
32 CurrentPeriod int64
33 ActiveReservations int64
34 Balance budget.Ledger
35 Maintenance PolicyView
36 Reimbursements PolicyView
37}
38
39type ReservationView struct {
40 ID int64
41 Lane string
42 Recipient address
43 Amount int64
44 Status string
45 Period int64
46 Deadline int64
47 ExpiredAtHeight bool
48 Paid bool
49}
50
51type ClaimView struct {
52 Exists bool
53 Recipient address
54 Amount int64
55 Paid bool
56}
57
58func LatestProgramID() int64 {
59 return latestProgramID
60}
61
62func GetProgram(programID int64) ProgramView {
63 p := mustProgram(programID)
64 height := runtime.ChainHeight()
65 period := int64(0)
66 if p.status != Draft {
67 period = p.periodAt(height)
68 }
69 return ProgramView{
70 ID: programID,
71 Sponsor: p.sponsor,
72 Status: p.statusAt(height),
73 StartHeight: p.start,
74 IssueEndHeight: p.issueEnd,
75 PeriodBlocks: p.periodBlocks,
76 MaxHoldBlocks: p.maxHold,
77 CurrentPeriod: period,
78 ActiveReservations: p.activeCount,
79 Balance: p.ledger,
80 Maintenance: p.maintenance.view(period),
81 Reimbursements: p.reimbursements.view(period),
82 }
83}
84
85func GetPeriodUsage(programID, period int64) (int64, int64) {
86 if period < 0 {
87 panic("period must be non-negative")
88 }
89 p := mustProgram(programID)
90 return p.maintenance.usage[period], p.reimbursements.usage[period]
91}
92
93func GetReservation(programID int64, lane string, itemID int64) ReservationView {
94 item := mustProgram(programID).rule(lane).items[itemID]
95 if item == nil {
96 panic("reservation not found")
97 }
98 return item.view(lane, itemID, runtime.ChainHeight())
99}
100
101func ListReservations(programID int64, lane string, offset, limit int) []ReservationView {
102 if offset < 0 || limit < 1 || limit > MaxPageSize {
103 panic("offset must be non-negative and limit must be 1 to 20")
104 }
105 rule := mustProgram(programID).rule(lane)
106 remaining := len(rule.itemIDs) - offset
107 if remaining <= 0 {
108 return []ReservationView{}
109 }
110 if remaining < limit {
111 limit = remaining
112 }
113 height := runtime.ChainHeight()
114 page := make([]ReservationView, limit)
115 for i, id := range rule.itemIDs[offset : offset+limit] {
116 page[i] = rule.items[id].view(lane, id, height)
117 }
118 return page
119}
120
121func GetClaim(programID int64, lane string, itemID int64) ClaimView {
122 c := mustProgram(programID).findClaim(lane, itemID)
123 if c == nil {
124 return ClaimView{}
125 }
126 return ClaimView{Exists: true, Recipient: c.recipient, Amount: c.amount, Paid: c.paid}
127}
128
129// GetTreasuryBalance returns bank cash and the sum owed to all programs.
130func GetTreasuryBalance() (int64, int64) {
131 addr := chain.PackageAddress(packagePath)
132 return banker.NewReadonlyBanker().GetCoin(addr, budget.Denom), outstanding
133}
134
135func (rule *policy) view(period int64) PolicyView {
136 return PolicyView{
137 Path: rule.path, Address: rule.addr, PeriodCap: rule.periodCap, ItemCap: rule.itemCap,
138 Registered: rule.registered, Used: rule.usage[period],
139 }
140}
141
142func (item *reservation) view(lane string, id, height int64) ReservationView {
143 return ReservationView{
144 ID: id, Lane: lane, Recipient: item.payment.recipient, Amount: item.payment.amount,
145 Status: item.status, Period: item.period, Deadline: item.deadline,
146 ExpiredAtHeight: item.status == Reserved && height >= item.deadline,
147 Paid: item.payment.paid,
148 }
149}
150
151func Render(path string) string {
152 if path == "" {
153 return "# Shared Budget\n\nLatest program ID: " + number(latestProgramID) +
154 "\n\nOpen a program with `:programID` or an item with `:programID/lane/itemID`."
155 }
156 parts := strings.SplitN(path, "/", 4)
157 id, err := strconv.ParseInt(parts[0], 10, 64)
158 if err != nil || programs[id] == nil {
159 return "# Shared Budget\n\nProgram not found."
160 }
161 if len(parts) != 1 {
162 if len(parts) == 3 {
163 if parts[1] == "period" {
164 return renderPeriod(id, parts[2])
165 }
166 if parts[1] == budget.Refund && parts[2] == "0" {
167 return renderRefund(id)
168 }
169 return renderItem(id, parts[1], parts[2])
170 }
171 return "# Shared Budget\n\nUse a program, item, or period link."
172 }
173
174 p := GetProgram(id)
175 b := p.Balance
176 return "# Shared Budget · Program " + number(id) + "\n\n" +
177 "- Status: " + p.Status + "\n" +
178 "- Sponsor: " + string(p.Sponsor) + "\n" +
179 "- Funded: " + number(b.Funded) + " ugnot\n" +
180 "- Available: " + number(b.Available) + " ugnot\n" +
181 "- Reserved: " + number(b.Reserved) + " ugnot\n" +
182 "- Claimable: " + number(b.Claimable) + " ugnot\n" +
183 "- Paid: " + number(b.Paid) + " ugnot\n" +
184 "- Active reservations: " + number(p.ActiveReservations) + "\n" +
185 "- Opening height: " + number(p.StartHeight) + "\n" +
186 "- Issuance end: " + number(p.IssueEndHeight) + "\n" +
187 "- Current period: [" + number(p.CurrentPeriod) + "](" + programURL(id) + "/period/" +
188 number(p.CurrentPeriod) + ")\n" +
189 renderPolicy(id, "Maintenance", budget.Maintenance, p.Maintenance) +
190 renderPolicy(id, "Reimbursements", budget.Reimbursements, p.Reimbursements) +
191 renderRefundLink(id)
192}
193
194func renderPolicy(id int64, name, lane string, p PolicyView) string {
195 rule := programs[id].rule(lane)
196 n := len(rule.itemIDs)
197 start := 0
198 if n > 4 {
199 start = n - 4
200 }
201 recent := ""
202 for _, itemID := range rule.itemIDs[start:] {
203 item := rule.items[itemID]
204 recent += "- [Item " + number(itemID) + "](" + programURL(id) + "/" + lane + "/" +
205 number(itemID) + ") · " + item.status + " · " + number(item.payment.amount) + " ugnot\n"
206 }
207 link := strings.TrimPrefix(p.Path, "gno.land") + ":" + number(id)
208 return "\n## " + name + "\n\n" +
209 "- [Policy and roles](" + link + ")\n" +
210 "- Period usage: " + number(p.Used) + " / " + number(p.PeriodCap) + " ugnot\n" +
211 "- Item cap: " + number(p.ItemCap) + " ugnot\n" +
212 recent
213}
214
215func programURL(id int64) string {
216 return strings.TrimPrefix(packagePath, "gno.land") + ":" + number(id)
217}
218
219func renderRefundLink(id int64) string {
220 if !GetClaim(id, budget.Refund, 0).Exists {
221 return ""
222 }
223 return "\n[View sponsor refund](" + programURL(id) + "/refund/0)\n"
224}
225
226func renderRefund(id int64) string {
227 c := GetClaim(id, budget.Refund, 0)
228 if !c.Exists {
229 return "# Sponsor refund\n\nNo refund claim."
230 }
231 return "# Sponsor refund\n\n" +
232 "- Recipient: " + string(c.Recipient) + "\n" +
233 "- Amount: " + number(c.Amount) + " ugnot\n" +
234 "- Paid: " + strconv.FormatBool(c.Paid) + "\n"
235}
236
237func renderPeriod(id int64, text string) string {
238 period, err := strconv.ParseInt(text, 10, 64)
239 if err != nil || period < 0 || period > GetProgram(id).CurrentPeriod {
240 return "# Shared Budget\n\nPeriod not found."
241 }
242 maintenance, reimbursements := GetPeriodUsage(id, period)
243 return "# Shared Budget · Period " + number(period) + "\n\n" +
244 "- Maintenance used: " + number(maintenance) + " ugnot\n" +
245 "- Reimbursements used: " + number(reimbursements) + " ugnot\n"
246}
247
248func renderItem(programID int64, lane, itemText string) string {
249 if lane != budget.Maintenance && lane != budget.Reimbursements {
250 return "# Shared Budget\n\nPolicy lane not found."
251 }
252 id, err := strconv.ParseInt(itemText, 10, 64)
253 if err != nil || programs[programID].rule(lane).items[id] == nil {
254 return "# Shared Budget\n\nReservation not found."
255 }
256 item := GetReservation(programID, lane, id)
257 return "# " + lane + " · Item " + number(id) + "\n\n" +
258 "- Status: " + item.Status + "\n" +
259 "- Recipient: " + string(item.Recipient) + "\n" +
260 "- Amount: " + number(item.Amount) + " ugnot\n" +
261 "- Period: " + number(item.Period) + "\n" +
262 "- Deadline: " + number(item.Deadline) + "\n" +
263 "- Eligible for expiry: " + strconv.FormatBool(item.ExpiredAtHeight) + "\n" +
264 "- Paid: " + strconv.FormatBool(item.Paid) + "\n"
265}