Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

readonly.gno

5.77 Kb · 194 lines
  1package commondao
  2
  3import (
  4	"time"
  5
  6	"gno.land/p/nt/addrset/v0"
  7)
  8
  9// ReadonlyCommonDAO is a read only view of a CommonDAO. It exposes only
 10// read side methods, holds the *CommonDAO in an unexported field, and every
 11// reachable value is itself readonly or a copy — so cross-realm holders
 12// cannot mutate the DAO through it. Views are live handles, not snapshots.
 13//
 14// This is the only safe handle to a DAO across a realm boundary: hosting
 15// realms must never return the *CommonDAO itself.
 16type ReadonlyCommonDAO struct {
 17	dao *CommonDAO
 18}
 19
 20// Readonly returns a read only view of the DAO.
 21func (dao *CommonDAO) Readonly() ReadonlyCommonDAO {
 22	return ReadonlyCommonDAO{dao}
 23}
 24
 25// ID returns DAO's unique identifier.
 26func (r ReadonlyCommonDAO) ID() uint64 {
 27	return r.dao.id
 28}
 29
 30// Name returns DAO's name.
 31func (r ReadonlyCommonDAO) Name() string {
 32	return r.dao.name
 33}
 34
 35// Purpose returns DAO's purpose (part of the Charter).
 36func (r ReadonlyCommonDAO) Purpose() string {
 37	return r.dao.purpose
 38}
 39
 40// Description returns DAO's description.
 41func (r ReadonlyCommonDAO) Description() string {
 42	return r.dao.description
 43}
 44
 45// Address returns the DAO's treasury address, or empty when unset.
 46func (r ReadonlyCommonDAO) Address() address {
 47	return r.dao.addr
 48}
 49
 50// IsDeleted returns true when DAO has been soft deleted.
 51func (r ReadonlyCommonDAO) IsDeleted() bool {
 52	return r.dao.deleted
 53}
 54
 55// IsTreasuryFrozen checks if the DAO's treasury is frozen.
 56func (r ReadonlyCommonDAO) IsTreasuryFrozen() bool {
 57	return r.dao.treasuryFrozen
 58}
 59
 60// Council returns a read only view of the DAO council.
 61func (r ReadonlyCommonDAO) Council() *addrset.ReadonlySet {
 62	return r.dao.council.Readonly()
 63}
 64
 65// HasKind checks if a proposal kind is registered on the DAO.
 66func (r ReadonlyCommonDAO) HasKind(name string) bool {
 67	return r.dao.HasKind(name)
 68}
 69
 70// KindNames returns the names of the registered proposal kinds, sorted.
 71func (r ReadonlyCommonDAO) KindNames() []string {
 72	return r.dao.KindNames()
 73}
 74
 75// Parent returns a read only view of the parent DAO when there is one.
 76func (r ReadonlyCommonDAO) Parent() (_ ReadonlyCommonDAO, found bool) {
 77	if r.dao.parent == nil {
 78		return ReadonlyCommonDAO{}, false
 79	}
 80	return r.dao.parent.Readonly(), true
 81}
 82
 83// ChildrenCount returns the number of direct children DAOs.
 84func (r ReadonlyCommonDAO) ChildrenCount() int {
 85	return r.dao.children.Len()
 86}
 87
 88// IterateChildren iterates the direct children DAOs.
 89// The callback can return true to stop iteration.
 90func (r ReadonlyCommonDAO) IterateChildren(fn func(ReadonlyCommonDAO) bool) (stopped bool) {
 91	r.dao.children.ForEach(func(_ int, v any) bool {
 92		stopped = fn(v.(*CommonDAO).Readonly())
 93		return stopped
 94	})
 95	return stopped
 96}
 97
 98// GetProposal returns a read only view of a proposal when it exists.
 99func (r ReadonlyCommonDAO) GetProposal(proposalID uint64) (_ ReadonlyProposal, found bool) {
100	p := r.dao.GetProposal(proposalID)
101	if p == nil {
102		return ReadonlyProposal{}, false
103	}
104	return p.Readonly(), true
105}
106
107// ActiveProposalsSize returns the number of active proposals.
108func (r ReadonlyCommonDAO) ActiveProposalsSize() int {
109	return r.dao.activeProposals.Size()
110}
111
112// FinishedProposalsSize returns the number of finished proposals.
113func (r ReadonlyCommonDAO) FinishedProposalsSize() int {
114	return r.dao.finishedProposals.Size()
115}
116
117// IterateActiveProposals iterates read only views of the active proposals.
118// The callback can return true to stop iteration.
119func (r ReadonlyCommonDAO) IterateActiveProposals(offset, count int, reverse bool, fn func(ReadonlyProposal) bool) bool {
120	return r.dao.activeProposals.Iterate(offset, count, reverse, func(p *Proposal) bool {
121		return fn(p.Readonly())
122	})
123}
124
125// IterateFinishedProposals iterates read only views of the finished proposals.
126// The callback can return true to stop iteration.
127func (r ReadonlyCommonDAO) IterateFinishedProposals(offset, count int, reverse bool, fn func(ReadonlyProposal) bool) bool {
128	return r.dao.finishedProposals.Iterate(offset, count, reverse, func(p *Proposal) bool {
129		return fn(p.Readonly())
130	})
131}
132
133// ReadonlyProposal is a read only view of a Proposal. Proposal content is
134// exposed flattened (Title, Body): the view never exposes the underlying
135// ProposalDefinition, whose Executor would otherwise be callable by any
136// holder with the hosting realm's authority.
137type ReadonlyProposal struct {
138	p *Proposal
139}
140
141// Readonly returns a read only view of the proposal.
142func (p *Proposal) Readonly() ReadonlyProposal {
143	return ReadonlyProposal{p}
144}
145
146// ID returns the unique proposal identifier.
147func (r ReadonlyProposal) ID() uint64 {
148	return r.p.id
149}
150
151// Status returns the current proposal status.
152func (r ReadonlyProposal) Status() ProposalStatus {
153	return r.p.status
154}
155
156// StatusReason returns an optional reason that led to the current proposal status.
157func (r ReadonlyProposal) StatusReason() string {
158	return r.p.statusReason
159}
160
161// Creator returns the address of the account that created the proposal.
162func (r ReadonlyProposal) Creator() address {
163	return r.p.creator
164}
165
166// CreatedAt returns the time that proposal was created.
167func (r ReadonlyProposal) CreatedAt() time.Time {
168	return r.p.createdAt
169}
170
171// VotingDeadline returns the deadline after which no more votes are allowed.
172func (r ReadonlyProposal) VotingDeadline() time.Time {
173	return r.p.votingDeadline
174}
175
176// Title returns the proposal definition's title.
177func (r ReadonlyProposal) Title() string {
178	return r.p.definition.Title()
179}
180
181// Body returns the proposal definition's body.
182func (r ReadonlyProposal) Body() string {
183	return r.p.definition.Body()
184}
185
186// VotingRecord returns a read only record with the submitted votes.
187func (r ReadonlyProposal) VotingRecord() ReadonlyVotingRecord {
188	return r.p.record.Readonly()
189}
190
191// Electorate returns a read only view of the proposal's electorate.
192func (r ReadonlyProposal) Electorate() *addrset.ReadonlySet {
193	return r.p.Electorate()
194}