Skip to content

Commit

Permalink
feat: implement realm for manual rewards
Browse files Browse the repository at this point in the history
  • Loading branch information
harry-hov committed Sep 26, 2023
1 parent d7e1de6 commit 4927740
Show file tree
Hide file tree
Showing 18 changed files with 127 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/rules.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: rules
on:
pull_request:
paths:
- "realm/rules.gno"
- "realm/chess/rules.gno"
push:
branches:
- master
Expand All @@ -19,4 +19,4 @@ jobs:
with:
go-version: 'stable'
- run: go mod download -x
- run: go run github.com/gnolang/gno/gnovm/cmd/gno test -verbose -run 'TestPerft' ./realm
- run: go run github.com/gnolang/gno/gnovm/cmd/gno test -verbose -run 'TestPerft' ./realm/chess
20 changes: 15 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,23 @@ help: ## Display this help message.
cd web; npm run build
cd web; npm run dev

4_deploy_realm: ## Deploy GnoChess realm on local node.
4_deploy_chess_realm: ## Deploy GnoChess realm on local node.
echo | $(GNOKEY) maketx addpkg \
--insecure-password-stdin \
--gas-wanted 20000000 \
--gas-fee 1ugnot \
--pkgpath gno.land/r/demo/chess \
--pkgdir ./realm \
--pkgdir ./realm/chess \
--broadcast \
DeployKey

z_deploy_reward_entry_realm: ## Deploy reward entry realm on local node.
echo | $(GNOKEY) maketx addpkg \
--insecure-password-stdin \
--gas-wanted 2000000 \
--gas-fee 1ugnot \
--pkgpath gno.land/r/demo/reward_entry \
--pkgdir ./realm/reward_entry \
--broadcast \
DeployKey

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

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

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

z_build_realm: ## Precompile and build the generated Go files. Assumes a working clone of gno in ../gno.
z_build_chess_realm: ## Precompile and build the generated Go files. Assumes a working clone of gno in ../gno.
mkdir -p ../gno/examples/gno.land/r/gnochess
cp -rf realm/*.gno ../gno/examples/gno.land/r/gnochess
cp -rf realm/chess/*.gno ../gno/examples/gno.land/r/gnochess
go run github.com/gnolang/gno/gnovm/cmd/gno precompile --verbose ../gno/examples/gno.land
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.
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.
87 changes: 87 additions & 0 deletions realm/reward_entry/entry.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package reward_entry

import (
"sort"
"std"
"time"

"gno.land/p/demo/avl"
"gno.land/p/demo/ufmt"
)

var entries avl.Tree // address -> *RewardEntry

// RewardEntry represents a reward entry
type RewardEntry struct {
address std.Address
points uint64
reason string

updatedAt time.Time
updatedBy std.Address
}

func SetRewardEntry(address std.Address, points uint64, reason string) {
std.AssertOriginCall()
caller := std.GetOrigCaller()
assertIsWhiteListed(caller)

entry := &RewardEntry{
address: address,
points: points,
reason: reason,

updatedAt: time.Now(),
updatedBy: caller,
}
entries.Set(address.String(), entry)
}

func rewardEntrySorted() []RewardEntry {
sorted := []RewardEntry{}
entries.Iterate("", "", func(key string, value interface{}) bool {
entry := value.(*RewardEntry)
i := sort.Search(len(sorted), func(i int) bool { return sorted[i].points <= entry.points })
if i > len(sorted) && sorted[i].points == entry.points {
i++
}
sorted = append(sorted, RewardEntry{})
copy(sorted[i+1:], sorted[i:])
sorted[i] = *entry
return false
})

return sorted
}

func Render(path string) string {
switch {
case path == "":
entries := rewardEntrySorted()
return markdown(entries)
default:
return "404\n"
}
}

func markdown(in []RewardEntry) string {
res := "# Reward entries:\n\n"

if len(in) == 0 {
res += "*No reward entry found*\n"
return res
}

// Create a table header
res += "| Address | Points | Reason | Updated-by | Updated-at |\n"
res += "| --------------- | --------- | --------------- | ---------- | ---------- |\n"

// Iterate over reward entries and format them as Markdown rows
for _, entry := range in {
updatedAt := entry.updatedAt.Format(time.UnixDate)
row := ufmt.Sprintf("| %s | %dpoints | %s | %s | %s |\n", entry.address.String(), entry.points, entry.reason, entry.updatedBy.String(), updatedAt)
res += row
}

return res
}
6 changes: 6 additions & 0 deletions realm/reward_entry/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module gno.land/r/demo/reward_entry

require (
"gno.land/p/demo/avl" v0.0.0-latest
"gno.land/p/demo/ufmt" v0.0.0-latest
)
17 changes: 17 additions & 0 deletions realm/reward_entry/whitelist.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package reward_entry

import "std"

// XXX: Update as required
var whitelist = []string{
"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5",
}

func assertIsWhiteListed(address std.Address) {
for _, e := range whitelist {
if e == address.String() {
return
}
}
panic("restricted access")
}

0 comments on commit 4927740

Please sign in to comment.