Skip to content

Commit

Permalink
add custom resource
Browse files Browse the repository at this point in the history
  • Loading branch information
Villaquiranm committed Feb 19, 2025
1 parent d5c5b1a commit 36a65e8
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
46 changes: 46 additions & 0 deletions gno/p/basedao/basedao_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -847,3 +847,49 @@ func TestAddRemoveResourceInvalidateProposals(t *testing.T) {
return false
})
}

func TestAddCustomReource(t *testing.T) {
resourceType := "killSwitch"
members := []Member{
{
alice.String(),
[]string{"admin"},
},
{
bob.String(),
[]string{"admin"},
},
}
tdao := newTestingDAO(t, 0.2, members)

if !tdao.dao.Members.HasRole(bob.String(), "admin") {
t.Errorf("Expected member %s to have role 'admin'", bob.String())
}

addResourceProposal := daokit.ProposalRequest{
Title: "My Proposal",
Description: "My Proposal Description",
Message: daokit.NewSetResourceMsg(&daokit.Resource{
Handler: daokit.NewCustomHandler(resourceType, func() {
tdao.dao = nil
}),
Condition: daocond.MembersThreshold(0.2, tdao.dao.Members.IsMember, tdao.dao.Members.MembersCount),
}),
}
std.TestSetOrigCaller(alice)
tdao.dao.InstantExecute(addResourceProposal)

if tdao.dao == nil {
t.Error("dao should still be alive")
}

proposalCallCustomResource := daokit.ProposalRequest{
Message: daokit.NewMessage(resourceType, nil),
}

tdao.dao.InstantExecute(proposalCallCustomResource)

if tdao.dao != nil {
t.Error("dao should have been killed")
}
}
20 changes: 20 additions & 0 deletions gno/p/daokit/messages.gno
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,23 @@ func NewRemoveResourceHandler(d *Core) MessageHandler {
func NewRemoveResourceMsg(payload *Resource) ExecutableMessage {
return NewMessage(MsgRemoveResourceKind, payload)
}

func NewCustomHandler(resourceType string, cb func()) MessageHandler {
return &CustomMessage{
cb: cb,
resourceType: resourceType,
}
}

type CustomMessage struct {
cb func()
resourceType string
}

func (m *CustomMessage) Execute(message ExecutableMessage) {
m.cb()
}

func (m *CustomMessage) Type() string {
return m.resourceType
}

0 comments on commit 36a65e8

Please sign in to comment.