Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(examples): implement r/events, p/authorizable #2372

Merged
merged 72 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
269ec83
rm
leohhhn Jun 17, 2024
a8f05aa
add authorizable, modify ownable, add initial r/events
leohhhn Jun 17, 2024
2147978
Merge branch 'master' into feat/r/events
leohhhn Jun 18, 2024
aa40767
Merge branch 'master' into feat/r/events
leohhhn Jun 18, 2024
22ba744
save events
leohhhn Jun 18, 2024
de5ec06
save
leohhhn Jun 18, 2024
8c3bbf1
Update examples/gno.land/p/demo/ownable/exts/authorizable/authorizabl…
leohhhn Jun 18, 2024
9a9e215
save
leohhhn Jun 18, 2024
f6b809a
save
leohhhn Jun 18, 2024
e66aea8
save
leohhhn Jun 18, 2024
a5c9191
save
leohhhn Jun 18, 2024
7569d08
Merge branch 'master' into feat/r/events
leohhhn Jun 19, 2024
55e4e90
save
leohhhn Jun 19, 2024
f3409e0
Merge branch 'master' into feat/r/events
leohhhn Jun 25, 2024
a55c2e5
Merge branch 'master' into feat/r/events
leohhhn Jun 26, 2024
8a20f6f
add upcoming & past rendering
leohhhn Jun 26, 2024
c47fc12
del func
leohhhn Jun 26, 2024
26104ec
rm
leohhhn Jun 27, 2024
f7513b3
add
leohhhn Jun 27, 2024
9d9140a
del
leohhhn Jun 27, 2024
dd2f4aa
edit func
leohhhn Jun 27, 2024
fb63100
renderHome edit
leohhhn Jun 27, 2024
4993c62
admin page
leohhhn Jun 30, 2024
aa42d70
past evts
leohhhn Jun 30, 2024
ffd686a
comment
leohhhn Jun 30, 2024
dfcd1a1
Merge branch 'master' into feat/r/events
leohhhn Jul 6, 2024
668ccd2
Merge branch 'refs/heads/master' into feat/r/events
leohhhn Jul 13, 2024
bacab7b
play around with gnoweb links
leohhhn Jul 13, 2024
2490a25
Merge branch 'master' into feat/r/events
leohhhn Jul 18, 2024
edf77e1
add endtime, delete admin button
leohhhn Jul 18, 2024
b9cdfbb
add tests
leohhhn Jul 18, 2024
f3d9d8b
update error
leohhhn Jul 18, 2024
8288c1f
tests
leohhhn Jul 18, 2024
a7b4178
mod tidy
leohhhn Jul 18, 2024
9eeeec1
add tests
leohhhn Jul 19, 2024
f481506
add godoc
leohhhn Jul 19, 2024
3dae9f4
edit tests
leohhhn Jul 19, 2024
ed0ada1
update authorizable test
leohhhn Jul 19, 2024
097dc6a
mod tidy and lint
leohhhn Jul 19, 2024
b84b11d
Merge branch 'master' into feat/r/events
leohhhn Jul 19, 2024
ef89f74
update home test
leohhhn Jul 19, 2024
af4a043
a>auth
leohhhn Aug 6, 2024
79cca29
update event var names
leohhhn Aug 6, 2024
11d500d
trimspace
leohhhn Aug 6, 2024
486d21d
flip if case
leohhhn Aug 6, 2024
d89e797
update widget arg
leohhhn Aug 6, 2024
b96bc60
update naming in widget
leohhhn Aug 6, 2024
acb9b14
fix widget
leohhhn Aug 6, 2024
aad4123
typo
leohhhn Aug 6, 2024
555f123
rename index
leohhhn Aug 6, 2024
3245bd0
update authorizable err
leohhhn Aug 6, 2024
6bfd617
update test names
leohhhn Aug 6, 2024
82ae082
update test names v2
leohhhn Aug 6, 2024
0447cc4
split events.gno
leohhhn Aug 6, 2024
fc99cce
Merge branch 'master' into feat/r/events
leohhhn Aug 7, 2024
0852ad2
update home test
leohhhn Aug 7, 2024
2a24cfe
delevent
leohhhn Aug 7, 2024
ecf3b04
replace panic with errors in rendering
leohhhn Aug 8, 2024
c3bb3e9
wip save
leohhhn Aug 8, 2024
be2a6d1
update home
leohhhn Aug 8, 2024
cb92015
update rendering, update errors, remove panics
leohhhn Aug 9, 2024
91bea8b
update widget, rendering, home
leohhhn Aug 9, 2024
5c33eba
help git diff
leohhhn Aug 9, 2024
c6a81f9
add back events folder
leohhhn Aug 9, 2024
815b482
parseTimes, update home, update rendering
leohhhn Aug 9, 2024
3d643f0
update tests, home
leohhhn Aug 9, 2024
9ba3ea6
update tests
leohhhn Aug 9, 2024
61c1de1
Merge branch 'master' into feat/r/events
leohhhn Aug 9, 2024
731cad1
Merge branch 'master' into feat/r/events
leohhhn Aug 10, 2024
647c9b2
update doc
leohhhn Aug 10, 2024
04b0049
update types, update golden tests
leohhhn Aug 11, 2024
335848b
update tests/rendering
leohhhn Aug 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Package authorizable is an extension of p/demo/ownable;
// It allows the user to instantiate an Authorizable struct, which extends
// p/demo/ownable with a list of users that are authorized for something.
// By using authorizable, you have a superuser (ownable), as well as another
// authorization level, which can be used for adding moderators or similar to your realm.
package authorizable

import (
"std"

"gno.land/p/demo/ownable"
)

type Authorizable struct {
*ownable.Ownable
authorized map[std.Address]struct{}
leohhhn marked this conversation as resolved.
Show resolved Hide resolved
}

func NewAuthorizable() *Authorizable {
deelawn marked this conversation as resolved.
Show resolved Hide resolved
a := &Authorizable{
ownable.NewOwnable(),
make(map[std.Address]struct{}),
}

// Add owner to auth list
a.authorized[a.Owner()] = struct{}{}
return a
}

func NewAuthorizableWithAddress(addr std.Address) *Authorizable {
a := &Authorizable{
ownable.NewOwnableWithAddress(addr),
make(map[std.Address]struct{}),
}

// Add owner to auth list
a.authorized[addr] = struct{}{}
return a
}

func (a *Authorizable) AddToAuthList(addr std.Address) error {
leohhhn marked this conversation as resolved.
Show resolved Hide resolved
if err := a.CallerIsOwner(); err != nil {
return ErrNotInAuthList
}

if _, exists := a.authorized[addr]; exists {
return ErrAlreadyInList
}

a.authorized[addr] = struct{}{}
return nil
}

func (a Authorizable) OnAuthList() error {
leohhhn marked this conversation as resolved.
Show resolved Hide resolved
caller := std.PrevRealm().Addr()

if _, exists := a.authorized[caller]; exists {
return nil
}

return ErrNotInAuthList
leohhhn marked this conversation as resolved.
Show resolved Hide resolved
}

func (a Authorizable) AssertOnAuthList() {
caller := std.PrevRealm().Addr()

if _, exists := a.authorized[caller]; !exists {
panic(ErrNotInAuthList)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package authorizable

import (
"std"
"testing"

"gno.land/p/demo/testutils"
)

var (
alice = testutils.TestAddress("alice")
bob = testutils.TestAddress("bob")
charlie = testutils.TestAddress("charlie")
)

func TestNew(t *testing.T) {
deelawn marked this conversation as resolved.
Show resolved Hide resolved
std.TestSetRealm(std.NewUserRealm(alice))
std.TestSetOrigCaller(alice) // TODO(bug): should not be needed

a := NewAuthorizable()
got := a.Owner()

if alice != got {
t.Fatalf("Expected %s, got: %s", alice, got)
}
}

func TestNewWithAddress(t *testing.T) {
a := NewAuthorizableWithAddress(alice)

got := a.Owner()

if alice != got {
t.Fatalf("Expected %s, got: %s", alice, got)
}
}

//func TestAddToAuthList(t *testing.T) {
// a := NewAuthorizable()
//
// if err := a.AddToAuthList(bob); err != nil {
// t.Fatalf("Expected AddToAuth to not error, but it did")
// }
//
// std.TestSetRealm(std.NewUserRealm(bob))
//
// if err := a.AddToAuthList(charlie); err == nil {
// t.Fatalf("Expected AddToAuth to error, did not happen")
// }
//}
8 changes: 8 additions & 0 deletions examples/gno.land/p/demo/ownable/exts/authorizable/errors.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package authorizable

import "errors"

var (
ErrNotInAuthList = errors.New("unauthorized; caller is not in authorized list")
ErrAlreadyInList = errors.New("address is already in authorized list")
deelawn marked this conversation as resolved.
Show resolved Hide resolved
)
1 change: 1 addition & 0 deletions examples/gno.land/p/demo/ownable/exts/authorizable/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/p/demo/ownable/exts/authorizable
16 changes: 10 additions & 6 deletions examples/gno.land/p/demo/ownable/ownable.gno
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package ownable

import (
"std"
)
import "std"

const OwnershipTransferEvent = "OwnershipTransfer"

Expand All @@ -12,14 +10,16 @@ type Ownable struct {
owner std.Address
}

func New() *Ownable {
func NewOwnable() *Ownable {
leohhhn marked this conversation as resolved.
Show resolved Hide resolved
return &Ownable{
owner: std.PrevRealm().Addr(),
}
}

func NewWithAddress(addr std.Address) *Ownable {
return &Ownable{owner: addr}
func NewOwnableWithAddress(addr std.Address) *Ownable {
return &Ownable{
owner: addr,
}
}

// TransferOwnership transfers ownership of the Ownable struct to a new address
Expand All @@ -40,6 +40,7 @@ func (o *Ownable) TransferOwnership(newOwner std.Address) error {
"from", string(prevOwner),
"to", string(newOwner),
)

return nil
}

Expand All @@ -64,6 +65,7 @@ func (o *Ownable) DropOwnership() error {
return nil
}

// Owner returns the owner address from Ownable
func (o Ownable) Owner() std.Address {
return o.owner
}
Expand All @@ -73,9 +75,11 @@ func (o Ownable) CallerIsOwner() error {
if std.PrevRealm().Addr() == o.owner {
return nil
}

deelawn marked this conversation as resolved.
Show resolved Hide resolved
return ErrUnauthorized
}

// AssertCallerIsOwner panics if the caller is not the owner
func (o Ownable) AssertCallerIsOwner() {
if std.PrevRealm().Addr() != o.owner {
panic(ErrUnauthorized)
Expand Down
64 changes: 33 additions & 31 deletions examples/gno.land/p/demo/ownable/ownable_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,67 @@ package ownable
import (
"std"
"testing"

"gno.land/p/demo/testutils"
)

var (
firstCaller = std.Address("g1l9aypkr8xfvs82zeux486ddzec88ty69lue9de")
secondCaller = std.Address("g127jydsh6cms3lrtdenydxsckh23a8d6emqcvfa")
alice = testutils.TestAddress("alice")
bob = testutils.TestAddress("bob")
)

func TestNew(t *testing.T) {
std.TestSetRealm(std.NewUserRealm(firstCaller))
std.TestSetOrigCaller(firstCaller) // TODO(bug): should not be needed
std.TestSetRealm(std.NewUserRealm(alice))
std.TestSetOrigCaller(alice) // TODO(bug): should not be needed

o := New()
o := NewOwnable()
got := o.Owner()
if firstCaller != got {
t.Fatalf("Expected %s, got: %s", firstCaller, got)
if alice != got {
t.Fatalf("Expected %s, got: %s", alice, got)
}
}

func TestNewWithAddress(t *testing.T) {
o := NewWithAddress(firstCaller)
o := NewOwnableWithAddress(alice)

got := o.Owner()
if firstCaller != got {
t.Fatalf("Expected %s, got: %s", firstCaller, got)
if alice != got {
t.Fatalf("Expected %s, got: %s", alice, got)
}
}

func TestOwner(t *testing.T) {
std.TestSetRealm(std.NewUserRealm(firstCaller))
std.TestSetRealm(std.NewUserRealm(alice))

o := New()
expected := firstCaller
o := NewOwnable()
expected := alice
got := o.Owner()
if expected != got {
t.Fatalf("Expected %s, got: %s", expected, got)
}
}

func TestTransferOwnership(t *testing.T) {
std.TestSetRealm(std.NewUserRealm(firstCaller))
std.TestSetRealm(std.NewUserRealm(alice))

o := New()
o := NewOwnable()

err := o.TransferOwnership(secondCaller)
err := o.TransferOwnership(bob)
if err != nil {
t.Fatalf("TransferOwnership failed, %v", err)
}

got := o.Owner()
if secondCaller != got {
t.Fatalf("Expected: %s, got: %s", secondCaller, got)
if bob != got {
t.Fatalf("Expected: %s, got: %s", bob, got)
}
}

func TestCallerIsOwner(t *testing.T) {
std.TestSetRealm(std.NewUserRealm(firstCaller))
std.TestSetRealm(std.NewUserRealm(alice))

o := New()
unauthorizedCaller := secondCaller
o := NewOwnable()
unauthorizedCaller := bob

std.TestSetRealm(std.NewUserRealm(unauthorizedCaller))
std.TestSetOrigCaller(unauthorizedCaller) // TODO(bug): should not be needed
Expand All @@ -73,9 +75,9 @@ func TestCallerIsOwner(t *testing.T) {
}

func TestDropOwnership(t *testing.T) {
std.TestSetRealm(std.NewUserRealm(firstCaller))
std.TestSetRealm(std.NewUserRealm(alice))

o := New()
o := NewOwnable()

err := o.DropOwnership()
if err != nil {
Expand All @@ -91,15 +93,15 @@ func TestDropOwnership(t *testing.T) {
// Errors

func TestErrUnauthorized(t *testing.T) {
std.TestSetRealm(std.NewUserRealm(firstCaller))
std.TestSetOrigCaller(firstCaller) // TODO(bug): should not be needed
std.TestSetRealm(std.NewUserRealm(alice))
std.TestSetOrigCaller(alice) // TODO(bug): should not be needed

o := New()
o := NewOwnable()

std.TestSetRealm(std.NewUserRealm(secondCaller))
std.TestSetOrigCaller(secondCaller) // TODO(bug): should not be needed
std.TestSetRealm(std.NewUserRealm(bob))
std.TestSetOrigCaller(bob) // TODO(bug): should not be needed

err := o.TransferOwnership(firstCaller)
err := o.TransferOwnership(alice)
if err != ErrUnauthorized {
t.Fatalf("Should've been ErrUnauthorized, was %v", err)
}
Expand All @@ -111,9 +113,9 @@ func TestErrUnauthorized(t *testing.T) {
}

func TestErrInvalidAddress(t *testing.T) {
std.TestSetRealm(std.NewUserRealm(firstCaller))
std.TestSetRealm(std.NewUserRealm(alice))

o := New()
o := NewOwnable()

err := o.TransferOwnership("")
if err != ErrInvalidAddress {
Expand Down
9 changes: 9 additions & 0 deletions examples/gno.land/r/gnoland/events/errors.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package events

import "errors"

var (
errEmptyName = errors.New("event name cannot be empty")
leohhhn marked this conversation as resolved.
Show resolved Hide resolved
errInvalidStartTime = errors.New("event start time cannot be less than 0")
errDescTooLong = errors.New("event description cannot be more than 80 chars")
)
Loading
Loading