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): hall of fame #2842

Open
wants to merge 91 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
91 commits
Select commit Hold shift + click to select a range
ac0d79e
feat: add p/avl/pager
moul Jul 14, 2024
a17edb2
chore: fixup
moul Jul 14, 2024
1a44f35
chore: fixup
moul Jul 14, 2024
291b92f
chore: fixup
moul Jul 14, 2024
6038562
chore: fixup
moul Jul 14, 2024
23db6aa
chore: fixup
moul Jul 14, 2024
fa7286b
chore: fixup
moul Jul 14, 2024
c0d435d
chore: fixup
moul Jul 14, 2024
1cff9f1
chore: fixup
moul Jul 14, 2024
04d62c7
Merge remote-tracking branch 'origin/master' into dev/moul/pagination
moul Jul 14, 2024
b2294ac
chore: fixup
moul Jul 14, 2024
2a1093e
chore: fixup
moul Jul 14, 2024
903fb48
Update pager_test.gno
moul Jul 14, 2024
f1adda0
chore: fixup
moul Jul 14, 2024
5d8a12b
add initial hof
leohhhn Sep 24, 2024
538f6f1
add
leohhhn Sep 24, 2024
7d44835
Merge branch 'master' into dev/moul/pagination
moul Sep 24, 2024
c8e4e1f
chore: fixup
moul Sep 24, 2024
a0be035
chore: fixup
moul Sep 24, 2024
5c3524d
chore: fixup
moul Sep 24, 2024
17b7ba6
add item & exh rendering
leohhhn Sep 25, 2024
29bb8dc
save
leohhhn Sep 25, 2024
5393e7b
update rendering, add upvote/downvote
leohhhn Sep 25, 2024
9a171ff
register temp
leohhhn Sep 26, 2024
97794d7
Merge remote-tracking branch 'origin/master' into dev/moul/pagination
moul Sep 26, 2024
1da8805
chore: new API
moul Sep 26, 2024
e2dcbca
Merge branch 'master' into hof
leohhhn Sep 30, 2024
ec2687b
add cmts
leohhhn Sep 30, 2024
3c21976
cmts
leohhhn Sep 30, 2024
fa11b61
start cs funcs
leohhhn Sep 30, 2024
48ebe1a
rm cmts
leohhhn Oct 1, 2024
5a5c1b5
add widget
leohhhn Oct 1, 2024
2cef5df
add temp exhib
leohhhn Oct 1, 2024
54e414b
rendering
leohhhn Oct 1, 2024
8f3d796
rendering
leohhhn Oct 1, 2024
4a6dad6
rm blog register
leohhhn Oct 1, 2024
5dc3d0f
save
leohhhn Oct 1, 2024
f0ce5de
save
leohhhn Oct 1, 2024
943df0c
Merge branch 'master' into dev/moul/pagination
moul Oct 3, 2024
296f7d8
rm errs
leohhhn Oct 4, 2024
1c10732
Update examples/gno.land/p/demo/avl/pager/pager.gno
moul Oct 5, 2024
79ffd7d
chore: fixup
moul Oct 5, 2024
adf95c0
Merge branch 'master' into dev/moul/pagination
moul Oct 5, 2024
3754a70
chore: fixup
moul Oct 5, 2024
19f7fa9
rm temp exhib
leohhhn Oct 7, 2024
a646518
add pausable
leohhhn Oct 7, 2024
94fc361
expose apis, add pausable
leohhhn Oct 7, 2024
cde9fe2
fix errors
leohhhn Oct 7, 2024
84ef0ea
update ownable/pausable, home, leon/home
leohhhn Oct 7, 2024
0c9b8a6
move
leohhhn Oct 7, 2024
d1d60ed
add shortlink
leohhhn Oct 7, 2024
f6d07a9
add shortpath, fix
leohhhn Oct 7, 2024
dd528a5
newline
leohhhn Oct 7, 2024
57fbda6
add tests
leohhhn Oct 7, 2024
e14f677
add sort avltree
leohhhn Oct 7, 2024
d3de1e1
add tests
leohhhn Oct 7, 2024
04061a8
add tests
leohhhn Oct 7, 2024
c3ea204
add godoc
leohhhn Oct 7, 2024
20b6d9a
Merge branch 'master' into hof
leohhhn Oct 8, 2024
f32070f
ownable
leohhhn Oct 8, 2024
1cecdd5
Merge remote-tracking branch 'moulfork/dev/moul/pagination' into hof
leohhhn Oct 8, 2024
822ae4b
register to hof, add pagination
leohhhn Oct 8, 2024
5b7afc8
renaming
leohhhn Oct 8, 2024
b40e128
add codeowner
leohhhn Oct 8, 2024
8a55c2a
add codeowner
leohhhn Oct 8, 2024
edc6915
rm hof
leohhhn Oct 8, 2024
f31c2e9
fix tests, fmt
leohhhn Oct 8, 2024
7602379
format import
leohhhn Oct 8, 2024
6e0f3c5
Merge branch 'master' into hof
leohhhn Oct 15, 2024
8bcf672
rm panics, just return
leohhhn Oct 15, 2024
3f35e68
fix tests
leohhhn Oct 15, 2024
2f88736
mod tidy
leohhhn Oct 15, 2024
0352dfc
Merge branch 'master' into hof
leohhhn Oct 16, 2024
1cc0a9e
update codeowners
leohhhn Oct 16, 2024
5f1d73a
Merge branch 'master' into hof
leohhhn Oct 22, 2024
cd2814e
using helplink
leohhhn Oct 22, 2024
40ba854
rm codeowners
leohhhn Oct 25, 2024
b2155e0
Merge branch 'master' into hof
leohhhn Oct 25, 2024
fe9092d
txlink
leohhhn Oct 25, 2024
72e5089
dashboard render
leohhhn Oct 25, 2024
3e5a640
tidy
leohhhn Oct 25, 2024
80b07e6
Merge branch 'master' into hof
leohhhn Nov 6, 2024
68c8ac6
Merge branch 'master' into hof
leohhhn Nov 7, 2024
882baf0
fix tests
leohhhn Nov 7, 2024
a43ccc1
update page size
leohhhn Nov 7, 2024
c8803c3
Update examples/gno.land/r/demo/hof/hof_test.gno
leohhhn Nov 8, 2024
ff06493
fix test, rm condition in rendering
leohhhn Nov 8, 2024
4344b56
Merge branch 'master' into hof
leohhhn Nov 12, 2024
c685a46
rm fqname addition
leohhhn Nov 12, 2024
6c35688
update home
leohhhn Nov 12, 2024
7d4f866
update tests
leohhhn Nov 12, 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
20 changes: 19 additions & 1 deletion examples/gno.land/p/demo/fqname/fqname.gno
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
// package-level declaration.
package fqname

import "strings"
import (
"strings"
)

// Parse splits a fully qualified identifier into its package path and name
// components. It handles cases with and without slashes in the package path.
Expand Down Expand Up @@ -63,10 +65,26 @@ func RenderLink(pkgPath, slug string) string {
if slug != "" {
return "[" + pkgPath + "](" + pkgLink + ")." + slug
}

return "[" + pkgPath + "](" + pkgLink + ")"
}

if slug != "" {
return pkgPath + "." + slug
}

return pkgPath
}

// ShortPath returns the part of the pkgpath after the base domain.
// sp := fqname.ShortPath("gno.land/r/gnoland/home")
// fmt.Println(sp)
// // Output: /r/gnoland/home
func ShortPath(pkgPath string) string {
idx := strings.Index(pkgPath, "/")
if idx == -1 {
return ""
}

return pkgPath[idx:]
}
6 changes: 3 additions & 3 deletions examples/gno.land/p/demo/ownable/ownable.gno
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func (o *Ownable) TransferOwnership(newOwner std.Address) error {
o.owner = newOwner
std.Emit(
OwnershipTransferEvent,
"from", string(prevOwner),
"to", string(newOwner),
"from", prevOwner.String(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is not the same as before?

Copy link
Contributor Author

@leohhhn leohhhn Nov 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, just thought its better to use the methods available on std.Address (showcasing the fact that it exists)

"to", newOwner.String(),
)

return nil
Expand All @@ -58,7 +58,7 @@ func (o *Ownable) DropOwnership() error {

std.Emit(
OwnershipTransferEvent,
"from", string(prevOwner),
"from", prevOwner.String(),
leohhhn marked this conversation as resolved.
Show resolved Hide resolved
"to", "",
)

Expand Down
10 changes: 9 additions & 1 deletion examples/gno.land/p/demo/pausable/pausable.gno
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package pausable

import "gno.land/p/demo/ownable"
import (
"std"

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

type Pausable struct {
*ownable.Ownable
Expand Down Expand Up @@ -35,6 +39,8 @@ func (p *Pausable) Pause() error {
}

p.paused = true
std.Emit("Paused", "account", p.Owner().String())

return nil
}

Expand All @@ -45,5 +51,7 @@ func (p *Pausable) Unpause() error {
}

p.paused = false
std.Emit("Unpaused", "account", p.Owner().String())

return nil
}
24 changes: 24 additions & 0 deletions examples/gno.land/r/demo/hof/administration.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package hof

import "std"

// Exposing the ownable & pausable APIs
// Should not be needed as soon as MsgCall supports calling methods on exported variables

func Pause() error {
return exhibition.Pause()
}

func Unpause() error {
return exhibition.Unpause()
}

func GetOwner() std.Address {
return owner.Owner()
}

func TransferOwnership(newOwner std.Address) {
if err := owner.TransferOwnership(newOwner); err != nil {
panic(err)
}
}
11 changes: 11 additions & 0 deletions examples/gno.land/r/demo/hof/errors.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package hof

import (
"errors"
)

var (
ErrNoSuchItem = errors.New("hof: no such item exists")
ErrDoubleUpvote = errors.New("hof: cannot upvote twice")
ErrDoubleDownvote = errors.New("hof: cannot downvote twice")
)
15 changes: 15 additions & 0 deletions examples/gno.land/r/demo/hof/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module gno.land/r/demo/hof

require (
gno.land/p/demo/avl v0.0.0-latest
gno.land/p/demo/avl/pager v0.0.0-latest
gno.land/p/demo/fqname v0.0.0-latest
gno.land/p/demo/ownable v0.0.0-latest
gno.land/p/demo/pausable v0.0.0-latest
gno.land/p/demo/seqid v0.0.0-latest
gno.land/p/demo/testutils v0.0.0-latest
gno.land/p/demo/uassert v0.0.0-latest
gno.land/p/demo/ufmt v0.0.0-latest
gno.land/p/demo/urequire v0.0.0-latest
gno.land/p/moul/txlink v0.0.0-latest
)
132 changes: 132 additions & 0 deletions examples/gno.land/r/demo/hof/hof.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// Package hof is the hall of fame realm.
// The Hall of Fame is an exhibition that holds items. Users can add their realms to the Hall of Fame by
// importing the Hall of Fame realm and calling hof.Register() from their init function.
package hof

import (
"std"

"gno.land/p/demo/avl"
"gno.land/p/demo/ownable"
"gno.land/p/demo/pausable"
"gno.land/p/demo/seqid"
)

var (
exhibition *Exhibition
owner *ownable.Ownable
)

type (
Exhibition struct {
itemCounter seqid.ID
description string
items *avl.Tree // pkgPath > Item
itemsSorted *avl.Tree // same data but sorted, storing pointers
*pausable.Pausable
}

Item struct {
id seqid.ID
pkgpath string
blockNum int64
upvote *avl.Tree // std.Addr > struct{}{}
downvote *avl.Tree // std.Addr > struct{}{}
}
)

func init() {
exhibition = &Exhibition{
items: avl.NewTree(),
itemsSorted: avl.NewTree(),
}

owner = ownable.NewWithAddress(std.Address("g125em6arxsnj49vx35f0n0z34putv5ty3376fg5"))
exhibition.Pausable = pausable.NewFromOwnable(owner)
}

// Register registers your realm to the Hall of Fame
// Should be called from within code
func Register() {
if exhibition.IsPaused() {
return
}

submission := std.PrevRealm()
pkgpath := submission.PkgPath()

// Must be called from code
if submission.IsUser() {
return
}

// Must not yet exist
if exhibition.items.Has(pkgpath) {
return
}

id := exhibition.itemCounter.Next()
i := &Item{
id: id,
pkgpath: pkgpath,
blockNum: std.GetHeight(),
upvote: avl.NewTree(),
downvote: avl.NewTree(),
}

exhibition.items.Set(pkgpath, i)
exhibition.itemsSorted.Set(id.String(), i)

std.Emit("Registration")
}

func Upvote(pkgpath string) {
rawItem, ok := exhibition.items.Get(pkgpath)
if !ok {
panic(ErrNoSuchItem.Error())
}

item := rawItem.(*Item)
caller := std.PrevRealm().Addr().String()

if item.upvote.Has(caller) {
panic(ErrDoubleUpvote.Error())
}

item.upvote.Set(caller, struct{}{})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just true instead of struct{}{}? If you want to be picky about using less space you should use interface{}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't struct{}{} the most optimal memory-wise (0 bytes)?

}

func Downvote(pkgpath string) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you downvote and upvote the same item?

Copy link
Contributor Author

@leohhhn leohhhn Nov 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I thought about leaving it that way, but maybe a better option is to always reset the opposite vote if the user is voting.

rawItem, ok := exhibition.items.Get(pkgpath)
if !ok {
panic(ErrNoSuchItem.Error())
}

item := rawItem.(*Item)
caller := std.PrevRealm().Addr().String()

if item.downvote.Has(caller) {
panic(ErrDoubleDownvote.Error())
}

item.downvote.Set(caller, struct{}{})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you can only upvote or downvote, why don't just item.vote.Set(caller, UP)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's easier to get the num of upvotes/downvotes if we have two trees and just get their size; otherwise you'd have to iterate and count

}

func Delete(pkgpath string) {
if err := owner.CallerIsOwner(); err != nil {
panic(err)
}

i, ok := exhibition.items.Get(pkgpath)
if !ok {
panic(ErrNoSuchItem.Error())
}

if _, removed := exhibition.itemsSorted.Remove(i.(*Item).id.String()); !removed {
panic(ErrNoSuchItem.Error())
}

if _, removed := exhibition.items.Remove(pkgpath); !removed {
panic(ErrNoSuchItem.Error())
}
}
Loading
Loading