package treasury import ( "chain" "chain/banker" "chain/runtime" "gno.land/p/g1aj9pzthj36aav0kff2zcmx9llapa7ng7xzcfct/shared_budget/budget/v2" ) const MaxPageSize = 20 type PolicyView struct { Lane string Path string Address address PeriodCap int64 ItemCap int64 CanBreachFloor bool Registered bool Used int64 } type ProgramView struct { ID int64 Title string Sponsor address Status string ReserveFloor int64 StartHeight int64 IssueEndHeight int64 IssueBlocks int64 PeriodBlocks int64 MaxHoldBlocks int64 CurrentPeriod int64 ClosedHeight int64 ClosedPeriod int64 Pruned bool ActiveReservations int64 Balance budget.Ledger Policies []PolicyView } type LaneUsageView struct { Lane string Used int64 PeriodCap int64 } type ReservationView struct { ID int64 Lane string Recipient address Amount int64 Status string Period int64 Deadline int64 Memo string ExpiredAtHeight bool Paid bool } type ClaimView struct { Exists bool Recipient address Amount int64 Paid bool } func LatestProgramID() int64 { return latestProgramID } // ListPrograms returns programs in descending ID order. func ListPrograms(offset, limit int) []ProgramView { requirePage(offset, limit) height := runtime.ChainHeight() page := make([]ProgramView, 0, limit) programs.ReverseIterateByOffset(offset, limit, func(_ string, value any) bool { page = append(page, value.(*program).view(height)) return false }) return page } func GetProgram(programID int64) ProgramView { return mustProgram(programID).view(runtime.ChainHeight()) } func (p *program) view(height int64) ProgramView { period := p.periodAt(height) rules := make([]PolicyView, 0, len(p.policies)) for _, rule := range p.policies { rules = append(rules, PolicyView{ Lane: rule.lane, Path: rule.path, Address: rule.addr, PeriodCap: rule.periodCap, ItemCap: rule.itemCap, CanBreachFloor: rule.canBreachFloor, Registered: rule.registered, Used: rule.used(period), }) } return ProgramView{ ID: p.id, Title: p.title, Sponsor: p.sponsor, Status: p.statusAt(height), ReserveFloor: p.reserveFloor, StartHeight: p.start, IssueEndHeight: p.issueEnd, IssueBlocks: p.issueBlocks, PeriodBlocks: p.periodBlocks, MaxHoldBlocks: p.maxHold, CurrentPeriod: period, ClosedHeight: p.closedHeight, ClosedPeriod: p.closedPeriod, Pruned: p.pruned, ActiveReservations: p.activeCount, Balance: p.ledger, Policies: rules, } } // CheckSpend preflights a reservation amount against an active, registered lane. func CheckSpend(programID int64, lane string, amount int64) budget.SpendCheck { p := mustProgram(programID) rule := p.rule(lane) height := runtime.ChainHeight() if p.statusAt(height) != Active || !rule.registered { panic("program is not accepting reservations") } return p.evaluate(rule, p.periodAt(height), amount) } func GetLanePeriodUsage(programID int64, lane string, period int64) int64 { requirePeriod(period) return mustProgram(programID).rule(lane).used(period) } func GetPeriodUsage(programID, period int64) []LaneUsageView { requirePeriod(period) p := mustProgram(programID) usage := make([]LaneUsageView, 0, len(p.policies)) for _, rule := range p.policies { usage = append(usage, LaneUsageView{Lane: rule.lane, Used: rule.used(period), PeriodCap: rule.periodCap}) } return usage } func GetReservation(programID int64, lane string, itemID int64) ReservationView { item := mustProgram(programID).rule(lane).item(itemID) if item == nil { panic("reservation not found") } return item.view(lane, runtime.ChainHeight()) } // ListReservations returns a lane's items in ascending ID order. func ListReservations(programID int64, lane string, offset, limit int) []ReservationView { requirePage(offset, limit) rule := mustProgram(programID).rule(lane) page := make([]ReservationView, 0, limit) if rule.items == nil { return page } height := runtime.ChainHeight() rule.items.IterateByOffset(offset, limit, func(_ string, value any) bool { page = append(page, value.(*reservation).view(lane, height)) return false }) return page } func GetClaim(programID int64, lane string, itemID int64) ClaimView { c := mustProgram(programID).findClaim(lane, itemID) if c == nil { return ClaimView{} } return ClaimView{Exists: true, Recipient: c.recipient, Amount: c.amount, Paid: c.paid} } // GetTreasuryBalance returns total bank cash and the portion owed to programs. func GetTreasuryBalance() (int64, int64) { addr := chain.PackageAddress(packagePath) return banker.NewReadonlyBanker().GetCoin(addr, budget.Denom), outstanding } func (item *reservation) view(lane string, height int64) ReservationView { return ReservationView{ ID: item.id, Lane: lane, Recipient: item.payment.recipient, Amount: item.payment.amount, Status: item.status, Period: item.period, Deadline: item.deadline, Memo: item.memo, ExpiredAtHeight: item.status == Reserved && height >= item.deadline, Paid: item.payment.paid, } } func requirePage(offset, limit int) { if offset < 0 || limit < 1 || limit > MaxPageSize { panic("offset must be non-negative and limit must be 1 to 20") } } func requirePeriod(period int64) { if period < 0 { panic("period must be non-negative") } }