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.

Playground

gnomod.toml

module = "gno.land/p/nym-moultest123/udao/v0"
gno = "0.9"

[addpkg]
  creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv"
  height = 186405
  max_deposit = "5000000ugnot"

udao.gno

// Package udao defines minimal interfaces for Decentralized Autonomous Organizations (DAOs).
// It intentionally does not expose members and votes, as these details are implementation-specific.
// Instead, it focuses on providing an external view of proposals and their statuses,
// which is what non-members and non-voters typically care about.
//
// The package is designed to allow for composable DAO patterns, enabling flexible
// and modular implementations of various DAO structures and behaviors.
package udao

// DAO defines a minimal interface for a Decentralized Autonomous Organization
// from an external point of view, hiding the internal details of members and voting.
type DAO interface {
	// Propose submits a new proposal to the DAO
	Propose(prop Proposal) (id uint64, err error)

	// GetProposalStatus retrieves the current status and metrics of a specific proposal
	GetProposalStatus(id uint64) (ProposalStatus, error)

	// Execute attempts to execute a proposal if it has passed
	Execute(id uint64) error

	// GetProposal retrieves the details of a specific proposal
	GetProposal(id uint64) (Proposal, error)

	// XXX: find a smart way to list proposals
	// // ListProposalss retrieves a list of Proposals with pagination and optional filters
	// ListDAOs(offset, limit int, filters ...ProposalFilter) ([]DAO, error)
}

// Proposal defines the interface for a DAO proposal
type Proposal interface {
	Title() string
	Body() string
	Constraints() []Constraint
}

// ProposalStatus represents the current status and metrics of a proposal
type ProposalStatus struct {
	// status
	State ProposalState

	// metrics
	YeaPercentage      float64
	NayPercentage      float64
	NonVoterPercentage float64
	// XXX: other metrics?
}

// ProposalState represents the current state of a proposal
type ProposalState int

const (
	Pending ProposalState = iota
	Active
	Passed
	Rejected
	Executed
	Expired // XXX: better name for when it's expired but not because of time?
)

// Constraint defines an interface for proposal constraints
type Constraint interface {
	Validate() (bool, string)
	Description() string
}

// ValidateConstraints checks if all constraints of a proposal are met
func ValidateConstraints(p Proposal) (bool, []string) {
	var unmetReasons []string
	for _, constraint := range p.Constraints() {
		valid, reason := constraint.Validate()
		if !valid {
			unmetReasons = append(unmetReasons, reason)
		}
	}
	return len(unmetReasons) == 0, unmetReasons
}

udao_test.gno

package udao

import (
	"testing"

	"gno.land/p/nt/uassert/v0"
)

// stubConstraint is a Constraint whose verdict is fixed at construction.
type stubConstraint struct {
	ok     bool
	reason string
	desc   string
}

func (c stubConstraint) Validate() (bool, string) { return c.ok, c.reason }
func (c stubConstraint) Description() string      { return c.desc }

// stubProposal is a Proposal carrying an arbitrary constraint list.
type stubProposal struct {
	title, body string
	constraints []Constraint
}

func (p stubProposal) Title() string             { return p.title }
func (p stubProposal) Body() string              { return p.body }
func (p stubProposal) Constraints() []Constraint { return p.constraints }

func TestValidateConstraintsAllMet(t *testing.T) {
	p := stubProposal{constraints: []Constraint{
		stubConstraint{ok: true},
		stubConstraint{ok: true},
	}}

	ok, reasons := ValidateConstraints(p)
	uassert.True(t, ok, "every constraint met")
	uassert.Equal(t, 0, len(reasons))
}

func TestValidateConstraintsNoConstraints(t *testing.T) {
	// A proposal with no constraints is vacuously valid.
	ok, reasons := ValidateConstraints(stubProposal{})
	uassert.True(t, ok, "no constraints means nothing unmet")
	uassert.Equal(t, 0, len(reasons))
}

func TestValidateConstraintsCollectsEveryFailure(t *testing.T) {
	p := stubProposal{constraints: []Constraint{
		stubConstraint{ok: false, reason: "quorum not reached"},
		stubConstraint{ok: true, reason: "ignored when valid"},
		stubConstraint{ok: false, reason: "voting still open"},
	}}

	ok, reasons := ValidateConstraints(p)
	uassert.False(t, ok, "one unmet constraint invalidates the proposal")
	// It reports *all* failures, not just the first, and in order.
	uassert.Equal(t, 2, len(reasons))
	uassert.Equal(t, "quorum not reached", reasons[0])
	uassert.Equal(t, "voting still open", reasons[1])
}

func TestValidateConstraintsIgnoresReasonWhenValid(t *testing.T) {
	p := stubProposal{constraints: []Constraint{
		stubConstraint{ok: true, reason: "should not be collected"},
	}}

	ok, reasons := ValidateConstraints(p)
	uassert.True(t, ok)
	uassert.Equal(t, 0, len(reasons))
}

// TestProposalStateValues pins the iota block: these states are likely to be
// persisted or compared numerically, so inserting one in the middle is breaking.
func TestProposalStateValues(t *testing.T) {
	cases := []struct {
		name string
		got  ProposalState
		want int
	}{
		{"Pending", Pending, 0},
		{"Active", Active, 1},
		{"Passed", Passed, 2},
		{"Rejected", Rejected, 3},
		{"Executed", Executed, 4},
		{"Expired", Expired, 5},
	}
	for _, c := range cases {
		uassert.Equal(t, c.want, int(c.got), c.name)
	}
}

// TestProposalStatusZeroValue documents that a zero ProposalStatus reads as a
// Pending proposal with no recorded votes.
func TestProposalStatusZeroValue(t *testing.T) {
	var s ProposalStatus
	uassert.Equal(t, int(Pending), int(s.State))
	uassert.True(t, s.YeaPercentage == 0.0, "no yea votes recorded")
	uassert.True(t, s.NayPercentage == 0.0, "no nay votes recorded")
	uassert.True(t, s.NonVoterPercentage == 0.0, "no non-voters recorded")
}