Skip to content

Commit

Permalink
feat: add update to dao_interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
MikaelVallenet committed Dec 17, 2024
1 parent 63e3de2 commit 6834121
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 14 deletions.
2 changes: 1 addition & 1 deletion examples/gno.land/p/demo/dao_maker/dao_core/dao_core.gno
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (d *daoCore) VotingModule() dao_interfaces.IVotingModule {
}

func (d *daoCore) VotingPowerAtHeight(address std.Address, height int64) uint64 {
return d.VotingModule().VotingPowerAtHeight(address, height)
return d.VotingModule().VotingPowerAtHeight(address, height, []string{})
}

func (d *daoCore) ActiveProposalModuleCount() int {
Expand Down
8 changes: 6 additions & 2 deletions examples/gno.land/p/demo/dao_maker/dao_core/dao_core_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,19 @@ func (vm *votingModule) ConfigJSON() string {
return "{}"
}

func (vm *votingModule) GetMembersJSON(start, end string, limit uint64, height int64) string {
return "[]"
}

func (vm *votingModule) Render(path string) string {
return "# Test Voting Module"
}

func (vm *votingModule) VotingPowerAtHeight(address std.Address, height int64) uint64 {
func (vm *votingModule) VotingPowerAtHeight(address std.Address, height int64, resources []string) uint64 {
return 0
}

func (vm *votingModule) TotalPowerAtHeight(height int64) uint64 {
func (vm *votingModule) TotalPowerAtHeight(height int64, resources []string) uint64 {
return 0
}

Expand Down
5 changes: 3 additions & 2 deletions examples/gno.land/p/demo/dao_maker/dao_interfaces/modules.gno
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ func (mi ModuleInfo) String() string {
type IVotingModule interface {
Info() ModuleInfo
ConfigJSON() string
GetMembersJSON(start, end string, limit uint64, height int64) string
Render(path string) string
VotingPowerAtHeight(address std.Address, height int64) (power uint64)
TotalPowerAtHeight(height int64) uint64
VotingPowerAtHeight(address std.Address, height int64, resources []string) (power uint64)
TotalPowerAtHeight(height int64, resources []string) uint64
}

type VotingModuleFactory func(core IDAOCore) IVotingModule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func (d *DAOProposalSingle) ConfigJSON() string {
func (d *DAOProposalSingle) Propose(title string, description string, messages []dao_interfaces.ExecutableMessage) int {
// TODO: creation policy

totalPower := d.core.VotingModule().TotalPowerAtHeight(0)
totalPower := d.core.VotingModule().TotalPowerAtHeight(0, []string{})

expiration := d.opts.MaxVotingPeriod.AfterCurrentBlock()
minVotingPeriod := dao_utils.Expiration(nil)
Expand Down Expand Up @@ -331,7 +331,7 @@ func (d *DAOProposalSingle) VoteJSON(proposalID int, voteJSON string) {
panic("proposal is expired")
}

votePower := d.core.VotingModule().VotingPowerAtHeight(voter, proposal.StartHeight)
votePower := d.core.VotingModule().VotingPowerAtHeight(voter, proposal.StartHeight, []string{})
if votePower == 0 {
panic("not registered")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,22 @@ func (v *VotingGroup) Info() dao_interfaces.ModuleInfo {

func (v *VotingGroup) ConfigJSON() string {
return json.ObjectNode("", map[string]*json.Node{
"totalPower": jsonutil.Uint64Node(v.TotalPowerAtHeight(havl.Latest)),
"totalPower": jsonutil.Uint64Node(v.TotalPowerAtHeight(havl.Latest, []string{})),
"members": jsonutil.Uint32Node(v.MemberCount(havl.Latest)),
}).String()
}

func (v *VotingGroup) VotingPowerAtHeight(addr std.Address, height int64) uint64 {
func (v *VotingGroup) GetMembersJSON(start, end string, limit uint64, height int64) string {
members := v.GetMembers(start, end, limit, height)
membersJSON := make([]*json.Node, len(members))
for i, m := range members {
membersJSON[i] = m.ToJSON()
}
return json.ArrayNode("", membersJSON).String()
}

func (v *VotingGroup) VotingPowerAtHeight(addr std.Address, height int64, resources []string) uint64 {
_ = resources
p, ok := v.powerByAddr.Get(addr.String(), height)
if !ok {
return 0
Expand All @@ -68,7 +78,8 @@ func (v *VotingGroup) VotingPowerAtHeight(addr std.Address, height int64) uint64
return p.(uint64)
}

func (v *VotingGroup) TotalPowerAtHeight(height int64) uint64 {
func (v *VotingGroup) TotalPowerAtHeight(height int64, resources []string) uint64 {
_ = resources
p, ok := v.totalPower.Get("", height)
if !ok {
return 0
Expand Down Expand Up @@ -116,7 +127,7 @@ func (g *VotingGroup) RemoveMember(addr std.Address) (uint64, bool) {

g.memberCount.Set("", g.MemberCount(havl.Latest)-1)
power := p.(uint64)
g.totalPower.Set("", g.TotalPowerAtHeight(havl.Latest)-power)
g.totalPower.Set("", g.TotalPowerAtHeight(havl.Latest, []string{})-power)
return power, true
}

Expand Down Expand Up @@ -156,7 +167,7 @@ func (v *VotingGroup) Render(path string) string {
sb.WriteString(strconv.FormatUint(uint64(v.MemberCount(havl.Latest)), 10))
sb.WriteString("\n\n")
sb.WriteString("Total power: ")
sb.WriteString(strconv.FormatUint(v.TotalPowerAtHeight(havl.Latest), 10))
sb.WriteString(strconv.FormatUint(v.TotalPowerAtHeight(havl.Latest, []string{}), 10))
sb.WriteString("\n\n")
sb.WriteString("Members:\n")
v.powerByAddr.Iterate("", "", havl.Latest, func(k string, v interface{}) bool {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dao_voting_group

import (
"std"
"testing"

dao_interfaces "gno.land/p/demo/dao_maker/dao_interfaces"
Expand All @@ -13,7 +12,7 @@ func TestVotingGroup(t *testing.T) {
i = v

{
got := i.TotalPowerAtHeight(0)
got := i.TotalPowerAtHeight(0, []string{})
expected := uint64(0)
if got != expected {
t.Fatalf("expected %s, got %s.", expected, got)
Expand Down

0 comments on commit 6834121

Please sign in to comment.