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.

views.gno

5.38 Kb · 190 lines
  1package treasury
  2
  3import (
  4	"chain"
  5	"chain/banker"
  6	"chain/runtime"
  7
  8	"gno.land/p/g1aj9pzthj36aav0kff2zcmx9llapa7ng7xzcfct/shared_budget/budget/v2"
  9)
 10
 11const MaxPageSize = 20
 12
 13type PolicyView struct {
 14	Lane           string
 15	Path           string
 16	Address        address
 17	PeriodCap      int64
 18	ItemCap        int64
 19	CanBreachFloor bool
 20	Registered     bool
 21	Used           int64
 22}
 23
 24type ProgramView struct {
 25	ID                 int64
 26	Title              string
 27	Sponsor            address
 28	Status             string
 29	ReserveFloor       int64
 30	StartHeight        int64
 31	IssueEndHeight     int64
 32	IssueBlocks        int64
 33	PeriodBlocks       int64
 34	MaxHoldBlocks      int64
 35	CurrentPeriod      int64
 36	ClosedHeight       int64
 37	ClosedPeriod       int64
 38	Pruned             bool
 39	ActiveReservations int64
 40	Balance            budget.Ledger
 41	Policies           []PolicyView
 42}
 43
 44type LaneUsageView struct {
 45	Lane      string
 46	Used      int64
 47	PeriodCap int64
 48}
 49
 50type ReservationView struct {
 51	ID              int64
 52	Lane            string
 53	Recipient       address
 54	Amount          int64
 55	Status          string
 56	Period          int64
 57	Deadline        int64
 58	Memo            string
 59	ExpiredAtHeight bool
 60	Paid            bool
 61}
 62
 63type ClaimView struct {
 64	Exists    bool
 65	Recipient address
 66	Amount    int64
 67	Paid      bool
 68}
 69
 70func LatestProgramID() int64 {
 71	return latestProgramID
 72}
 73
 74// ListPrograms returns programs in descending ID order.
 75func ListPrograms(offset, limit int) []ProgramView {
 76	requirePage(offset, limit)
 77	height := runtime.ChainHeight()
 78	page := make([]ProgramView, 0, limit)
 79	programs.ReverseIterateByOffset(offset, limit, func(_ string, value any) bool {
 80		page = append(page, value.(*program).view(height))
 81		return false
 82	})
 83	return page
 84}
 85
 86func GetProgram(programID int64) ProgramView {
 87	return mustProgram(programID).view(runtime.ChainHeight())
 88}
 89
 90func (p *program) view(height int64) ProgramView {
 91	period := p.periodAt(height)
 92	rules := make([]PolicyView, 0, len(p.policies))
 93	for _, rule := range p.policies {
 94		rules = append(rules, PolicyView{
 95			Lane: rule.lane, Path: rule.path, Address: rule.addr, PeriodCap: rule.periodCap, ItemCap: rule.itemCap,
 96			CanBreachFloor: rule.canBreachFloor, Registered: rule.registered, Used: rule.used(period),
 97		})
 98	}
 99	return ProgramView{
100		ID: p.id, Title: p.title, Sponsor: p.sponsor, Status: p.statusAt(height), ReserveFloor: p.reserveFloor,
101		StartHeight: p.start, IssueEndHeight: p.issueEnd, IssueBlocks: p.issueBlocks,
102		PeriodBlocks: p.periodBlocks, MaxHoldBlocks: p.maxHold, CurrentPeriod: period,
103		ClosedHeight: p.closedHeight, ClosedPeriod: p.closedPeriod, Pruned: p.pruned,
104		ActiveReservations: p.activeCount, Balance: p.ledger, Policies: rules,
105	}
106}
107
108// CheckSpend preflights a reservation amount against an active, registered lane.
109func CheckSpend(programID int64, lane string, amount int64) budget.SpendCheck {
110	p := mustProgram(programID)
111	rule := p.rule(lane)
112	height := runtime.ChainHeight()
113	if p.statusAt(height) != Active || !rule.registered {
114		panic("program is not accepting reservations")
115	}
116	return p.evaluate(rule, p.periodAt(height), amount)
117}
118
119func GetLanePeriodUsage(programID int64, lane string, period int64) int64 {
120	requirePeriod(period)
121	return mustProgram(programID).rule(lane).used(period)
122}
123
124func GetPeriodUsage(programID, period int64) []LaneUsageView {
125	requirePeriod(period)
126	p := mustProgram(programID)
127	usage := make([]LaneUsageView, 0, len(p.policies))
128	for _, rule := range p.policies {
129		usage = append(usage, LaneUsageView{Lane: rule.lane, Used: rule.used(period), PeriodCap: rule.periodCap})
130	}
131	return usage
132}
133
134func GetReservation(programID int64, lane string, itemID int64) ReservationView {
135	item := mustProgram(programID).rule(lane).item(itemID)
136	if item == nil {
137		panic("reservation not found")
138	}
139	return item.view(lane, runtime.ChainHeight())
140}
141
142// ListReservations returns a lane's items in ascending ID order.
143func ListReservations(programID int64, lane string, offset, limit int) []ReservationView {
144	requirePage(offset, limit)
145	rule := mustProgram(programID).rule(lane)
146	page := make([]ReservationView, 0, limit)
147	if rule.items == nil {
148		return page
149	}
150	height := runtime.ChainHeight()
151	rule.items.IterateByOffset(offset, limit, func(_ string, value any) bool {
152		page = append(page, value.(*reservation).view(lane, height))
153		return false
154	})
155	return page
156}
157
158func GetClaim(programID int64, lane string, itemID int64) ClaimView {
159	c := mustProgram(programID).findClaim(lane, itemID)
160	if c == nil {
161		return ClaimView{}
162	}
163	return ClaimView{Exists: true, Recipient: c.recipient, Amount: c.amount, Paid: c.paid}
164}
165
166// GetTreasuryBalance returns total bank cash and the portion owed to programs.
167func GetTreasuryBalance() (int64, int64) {
168	addr := chain.PackageAddress(packagePath)
169	return banker.NewReadonlyBanker().GetCoin(addr, budget.Denom), outstanding
170}
171
172func (item *reservation) view(lane string, height int64) ReservationView {
173	return ReservationView{
174		ID: item.id, Lane: lane, Recipient: item.payment.recipient, Amount: item.payment.amount,
175		Status: item.status, Period: item.period, Deadline: item.deadline, Memo: item.memo,
176		ExpiredAtHeight: item.status == Reserved && height >= item.deadline, Paid: item.payment.paid,
177	}
178}
179
180func requirePage(offset, limit int) {
181	if offset < 0 || limit < 1 || limit > MaxPageSize {
182		panic("offset must be non-negative and limit must be 1 to 20")
183	}
184}
185
186func requirePeriod(period int64) {
187	if period < 0 {
188		panic("period must be non-negative")
189	}
190}