package treasury import ( "chain" "chain/banker" "chain/runtime" "gno.land/p/g1aj9pzthj36aav0kff2zcmx9llapa7ng7xzcfct/shared_budget/budget/v3" ) const MaxPageSize = 20 type PolicyView struct { Lane string Path string Address address PeriodCap int64 ItemCap int64 CanBreachFloor bool Registered bool Used int64 PendingPeriodCap int64 PendingItemCap int64 PendingCapsETA int64 } type ProgramView struct { ID int64 Title string Sponsor address PendingSponsor address Status string Asset string AssetSymbol string AssetDecimals int ReserveFloor int64 GovDelay int64 PendingFloor int64 PendingFloorETA int64 CurrentHeight int64 StartHeight int64 IssueEndHeight int64 IssueClosedHeight 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 } type TokenView struct { Exists bool ID string Name string Symbol string Decimals int } func GetTokenMetadata(key string) TokenView { token := GetRegisteredToken(key) if token == nil { return TokenView{} } return TokenView{Exists: true, ID: token.ID(), Name: token.GetName(), Symbol: token.GetSymbol(), Decimals: token.GetDecimals()} } 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()) } // PolicyRenderRoute resolves a policy route against the lanes registered to its realm. // A bare program route may return several lanes for the caller to display. func PolicyRenderRoute(path, policyPath string) (ProgramView, []string, int64) { id, lane, itemID := budget.ParsePolicyLanePath(path) p := findProgram(id) if p == nil { return ProgramView{}, nil, 0 } var lanes []string for _, rule := range p.policies { if rule.path == policyPath && rule.registered && (lane == "" || lane == rule.lane) { lanes = append(lanes, rule.lane) } } return p.view(runtime.ChainHeight()), lanes, itemID } 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), PendingPeriodCap: rule.pendingCaps.periodCap, PendingItemCap: rule.pendingCaps.itemCap, PendingCapsETA: rule.pendingCaps.eta, }) } status := p.statusAt(height) symbol, decimals := p.assetDisplay() issueClosedHeight := int64(0) if status == IssuanceClosed || status == Closed { issueClosedHeight = p.issuanceCutoff() } return ProgramView{ ID: p.id, Title: p.title, Sponsor: p.sponsor, PendingSponsor: p.pendingSponsor, Status: status, Asset: p.asset, AssetSymbol: symbol, AssetDecimals: decimals, ReserveFloor: p.reserveFloor, GovDelay: p.govDelay, PendingFloor: p.pendingFloor, PendingFloorETA: p.pendingFloorETA, CurrentHeight: height, StartHeight: p.start, IssueEndHeight: p.issueEnd, IssueClosedHeight: issueClosedHeight, 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, } } func (p *program) assetDisplay() (string, int) { if isTokenAsset(p.asset) { token := tokenForAsset(p.asset) return token.GetSymbol(), token.GetDecimals() } if p.asset == budget.Denom { return p.asset, 6 } return p.asset, 0 } func (view ProgramView) FormatAmount(amount int64) string { if view.Asset == budget.Denom { return budget.FormatUGNOT(amount) } return budget.FormatAsset(amount, view.AssetSymbol, view.AssetDecimals) } // 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) { return GetTreasuryAssetBalance(budget.Denom) } // GetTreasuryAssetBalance returns physical custody and outstanding claims and allocations. func GetTreasuryAssetBalance(asset string) (int64, int64) { budget.RequireAsset(asset) addr := chain.PackageAddress(realmPath) if isTokenAsset(asset) { return tokenForAsset(asset).BalanceOf(addr), assetOutstanding(asset) } return banker.NewReadonlyBanker().GetCoin(addr, asset), assetOutstanding(asset) } 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") } }