Playground
gnomod.toml
module = "gno.land/p/nym-moultest123/safe/v0" gno = "0.9" [addpkg] creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv" height = 186405 max_deposit = "5000000ugnot"
safe.gno
// Package safe is inspired by Gnosis Safe
package safe
import (
"chain/runtime/unsafe"
)
// Gnosis safe style using this with threshold
// : affMember + update threshold at the same time
type Safe interface {
Name() string
Members() []address
Threshold() uint
Size() uint
NextProposal() Proposal
UpcomingProposals(page uint) (props []Proposal, total uint)
PreviousProposals(page uint) (props []Proposal, total uint)
// balances (native, grc20, grc721)
ProposeAddingMember(addr address, newThreshold uint) (prop Proposal, err error)
ProposeRemovingMember(addr address, newThreshold uint) (prop Proposal, err error)
ProposeUpdatingThreshold(newThreshold uint) (prop Proposal, err error)
// ProposeClosure(cl func()) (prop Proposal, err error)
// ProposeBankXXX()
// ProposeGRC20XXX()
// ProposeGRC721XXX()
GetProposal(id uint) Proposal
ApproveProposal(p Proposal) error
ExecuteProposal(p Proposal) error
CancelProposal(p Proposal) error // XXX: should be ProposeToCancel?
Render(path string) string
}
type Proposal interface {
ID() uint
Active() bool
Executed() bool
Voters() []address
}
func NewSafe() Safe {
caller := unsafe.PreviousRealm().Address()
return safe{
members: []address{caller},
threshold: 1,
prevProps: []Proposal{},
nextProps: []Proposal{},
}
}
type safe struct {
name string
members []address // member addresses
threshold uint
prevProps []Proposal // executed/cancelled proposals
nextProps []Proposal // upcoming proposals
}
func (s safe) String() string { panic("not implemented") }
func (s safe) Name() string { return s.name }
func (s safe) Members() []address { return s.members }
func (s safe) Threshold() uint { return s.threshold }
func (s safe) Size() uint { return uint(len(s.members)) }
func (s safe) NextProposal() Proposal { panic("not implemented") }
func (s safe) UpcomingProposals(page uint) (props []Proposal, total uint) {
panic("not implemented")
}
func (s safe) PreviousProposals(page uint) (props []Proposal, total uint) {
panic("not implemented")
}
func (s safe) ProposeAddingMember(addr address, newThreshold uint) (prop Proposal, err error) {
panic("not implemented")
}
func (s safe) ProposeRemovingMember(addr address, newThreshold uint) (prop Proposal, err error) {
panic("not implemented")
}
func (s safe) ProposeUpdatingThreshold(newThreshold uint) (prop Proposal, err error) {
panic("not implemented")
}
func (s safe) GetProposal(id uint) Proposal { panic("not implemented") }
func (s safe) ApproveProposal(p Proposal) error { panic("not implemented") }
func (s safe) ExecuteProposal(p Proposal) error { panic("not implemented") }
func (s safe) CancelProposal(p Proposal) error { panic("not implemented") }
func (s safe) Render(path string) string { panic("not implemented") }
type proposal struct{}
func (p proposal) String() string { panic("not implemented") }
safe_test.gno
package safe
import (
"testing"
"gno.land/p/nt/testutils/v0"
"gno.land/p/nt/uassert/v0"
)
// This package is a design sketch: only the constructor and the four read
// accessors are implemented, and every proposal method is `panic("not
// implemented")`. The tests below pin the implemented behaviour and pin the
// stubs as stubs, so implementing one is a deliberate, visible change.
var alice = testutils.TestAddress("alice")
// NewSafe reads unsafe.PreviousRealm(), which needs a real realm frame: a user
// realm gives "frame not found: cannot seek beyond origin caller override".
func TestNewSafeDefaults(cur realm, t *testing.T) {
testing.SetRealm(testing.NewCodeRealm("gno.land/r/test/safeone"))
s := NewSafe()
uassert.Equal(t, "", s.Name(), "a fresh safe is unnamed")
uassert.Equal(t, uint(1), s.Threshold(), "threshold starts at 1")
uassert.Equal(t, uint(1), s.Size(), "the creator is the only member")
uassert.Equal(t, 1, len(s.Members()))
uassert.True(t, s.Members()[0].IsValid(), "the sole member is a valid address")
}
// Note: caller-sensitivity of NewSafe is NOT asserted here. From a `p/` test
// package there is a single realm frame, so unsafe.PreviousRealm() resolves to
// the origin no matter what testing.SetRealm is given — two different code
// realms seed the identical address. Proving "membership follows the caller"
// needs an `r/` test package that can actually cross into this one.
func TestSizeTracksMembers(t *testing.T) {
s := safe{members: []address{alice, testutils.TestAddress("bob")}, threshold: 2}
uassert.Equal(t, uint(2), s.Size())
uassert.Equal(t, uint(2), s.Threshold())
}
// TestUnimplementedMethodsPanic documents the sketch surface. If one of these
// starts working, this test fails and forces the contract change to be noticed.
func TestUnimplementedMethodsPanic(cur realm, t *testing.T) {
s := safe{members: []address{alice}, threshold: 1}
uassert.PanicsWithMessage(t, cur, "not implemented", func() { s.NextProposal() })
uassert.PanicsWithMessage(t, cur, "not implemented", func() { s.UpcomingProposals(0) })
uassert.PanicsWithMessage(t, cur, "not implemented", func() { s.PreviousProposals(0) })
uassert.PanicsWithMessage(t, cur, "not implemented", func() { s.ProposeAddingMember(alice, 1) })
uassert.PanicsWithMessage(t, cur, "not implemented", func() { s.ProposeRemovingMember(alice, 1) })
uassert.PanicsWithMessage(t, cur, "not implemented", func() { s.ProposeUpdatingThreshold(2) })
uassert.PanicsWithMessage(t, cur, "not implemented", func() { s.GetProposal(0) })
uassert.PanicsWithMessage(t, cur, "not implemented", func() { s.ApproveProposal(nil) })
uassert.PanicsWithMessage(t, cur, "not implemented", func() { s.ExecuteProposal(nil) })
uassert.PanicsWithMessage(t, cur, "not implemented", func() { s.CancelProposal(nil) })
uassert.PanicsWithMessage(t, cur, "not implemented", func() { s.Render("") })
}
// TestSafeSatisfiesInterface is a compile-time check that the concrete type
// still implements the exported interface.
func TestSafeSatisfiesInterface(t *testing.T) {
var _ Safe = safe{}
uassert.True(t, true, "safe implements Safe")
}