-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement realm for manual rewards
- Loading branch information
Showing
18 changed files
with
127 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |