Skip to content

Commit 00b6e7b

Browse files
committed
feat: implement realm for manual rewards
1 parent 7f4ad17 commit 00b6e7b

18 files changed

+127
-7
lines changed

.github/workflows/rules.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: rules
33
on:
44
pull_request:
55
paths:
6-
- "realm/rules.gno"
6+
- "realm/chess/rules.gno"
77
push:
88
branches:
99
- master
@@ -19,4 +19,4 @@ jobs:
1919
with:
2020
go-version: 'stable'
2121
- run: go mod download -x
22-
- run: go run github.com/gnolang/gno/gnovm/cmd/gno test -verbose -run 'TestPerft' ./realm
22+
- run: go run github.com/gnolang/gno/gnovm/cmd/gno test -verbose -run 'TestPerft' ./realm/chess

Makefile

+15-5
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,23 @@ help: ## Display this help message.
7878
cd web; npm run build
7979
cd web; npm run dev
8080

81-
4_deploy_realm: ## Deploy GnoChess realm on local node.
81+
4_deploy_chess_realm: ## Deploy GnoChess realm on local node.
8282
echo | $(GNOKEY) maketx addpkg \
8383
--insecure-password-stdin \
8484
--gas-wanted 20000000 \
8585
--gas-fee 1ugnot \
8686
--pkgpath gno.land/r/demo/chess \
87-
--pkgdir ./realm \
87+
--pkgdir ./realm/chess \
88+
--broadcast \
89+
DeployKey
90+
91+
z_deploy_reward_entry_realm: ## Deploy reward entry realm on local node.
92+
echo | $(GNOKEY) maketx addpkg \
93+
--insecure-password-stdin \
94+
--gas-wanted 2000000 \
95+
--gas-fee 1ugnot \
96+
--pkgpath gno.land/r/demo/reward_entry \
97+
--pkgdir ./realm/reward_entry \
8898
--broadcast \
8999
DeployKey
90100

@@ -96,14 +106,14 @@ z_use_remote_gno: ## Use the remote 'github.com/gnolang/gno' module and remove a
96106
@echo "Switching to remote gno module..."
97107
@go mod edit -dropreplace github.com/gnolang/gno
98108

99-
z_test_realm: ## Test the realm.
109+
z_test_realms: ## Test the realms.
100110
go run github.com/gnolang/gno/gnovm/cmd/gno test --verbose ./realm
101111

102112
z_test_integration: ## Test the realm.
103113
go test -v -run='TestIntegration/.*' .
104114

105-
z_build_realm: ## Precompile and build the generated Go files. Assumes a working clone of gno in ../gno.
115+
z_build_chess_realm: ## Precompile and build the generated Go files. Assumes a working clone of gno in ../gno.
106116
mkdir -p ../gno/examples/gno.land/r/gnochess
107-
cp -rf realm/*.gno ../gno/examples/gno.land/r/gnochess
117+
cp -rf realm/chess/*.gno ../gno/examples/gno.land/r/gnochess
108118
go run github.com/gnolang/gno/gnovm/cmd/gno precompile --verbose ../gno/examples/gno.land
109119
go run github.com/gnolang/gno/gnovm/cmd/gno build --verbose ../gno/examples/gno.land/r/gnochess
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

realm/gno.mod realm/chess/gno.mod

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

realm/reward_entry/entry.gno

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package reward_entry
2+
3+
import (
4+
"sort"
5+
"std"
6+
"time"
7+
8+
"gno.land/p/demo/avl"
9+
"gno.land/p/demo/ufmt"
10+
)
11+
12+
var entries avl.Tree // address -> *RewardEntry
13+
14+
// RewardEntry represents a reward entry
15+
type RewardEntry struct {
16+
address std.Address
17+
points uint64
18+
reason string
19+
20+
updatedAt time.Time
21+
updatedBy std.Address
22+
}
23+
24+
func SetRewardEntry(address std.Address, points uint64, reason string) {
25+
std.AssertOriginCall()
26+
caller := std.GetOrigCaller()
27+
assertIsWhiteListed(caller)
28+
29+
entry := &RewardEntry{
30+
address: address,
31+
points: points,
32+
reason: reason,
33+
34+
updatedAt: time.Now(),
35+
updatedBy: caller,
36+
}
37+
entries.Set(address.String(), entry)
38+
}
39+
40+
func rewardEntrySorted() []RewardEntry {
41+
sorted := []RewardEntry{}
42+
entries.Iterate("", "", func(key string, value interface{}) bool {
43+
entry := value.(*RewardEntry)
44+
i := sort.Search(len(sorted), func(i int) bool { return sorted[i].points <= entry.points })
45+
if i > len(sorted) && sorted[i].points == entry.points {
46+
i++
47+
}
48+
sorted = append(sorted, RewardEntry{})
49+
copy(sorted[i+1:], sorted[i:])
50+
sorted[i] = *entry
51+
return false
52+
})
53+
54+
return sorted
55+
}
56+
57+
func Render(path string) string {
58+
switch {
59+
case path == "":
60+
entries := rewardEntrySorted()
61+
return markdown(entries)
62+
default:
63+
return "404\n"
64+
}
65+
}
66+
67+
func markdown(in []RewardEntry) string {
68+
res := "# Reward entries:\n\n"
69+
70+
if len(in) == 0 {
71+
res += "*No reward entry found*\n"
72+
return res
73+
}
74+
75+
// Create a table header
76+
res += "| Address | Points | Reason | Updated-by | Updated-at |\n"
77+
res += "| --------------- | --------- | --------------- | ---------- | ---------- |\n"
78+
79+
// Iterate over reward entries and format them as Markdown rows
80+
for _, entry := range in {
81+
updatedAt := entry.updatedAt.Format(time.UnixDate)
82+
row := ufmt.Sprintf("| %s | %dpoints | %s | %s | %s |\n", entry.address.String(), entry.points, entry.reason, entry.updatedBy.String(), updatedAt)
83+
res += row
84+
}
85+
86+
return res
87+
}

realm/reward_entry/gno.mod

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module gno.land/r/demo/reward_entry
2+
3+
require (
4+
"gno.land/p/demo/avl" v0.0.0-latest
5+
"gno.land/p/demo/ufmt" v0.0.0-latest
6+
)

realm/reward_entry/whitelist.gno

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package reward_entry
2+
3+
import "std"
4+
5+
// XXX: Update as required
6+
var whitelist = []string{
7+
"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
8+
}
9+
10+
func assertIsWhiteListed(address std.Address) {
11+
for _, e := range whitelist {
12+
if e == address.String() {
13+
return
14+
}
15+
}
16+
panic("restricted access")
17+
}

0 commit comments

Comments
 (0)