@@ -11,6 +11,7 @@ import (
1111 "gno.land/p/demo/ufmt"
1212)
1313
14+ // Retrieves a proposal by its ID
1415func mustGetProposal(proposalId uint64) ProposalInfo {
1516 result, exists := proposals.Get(formatUint(proposalId))
1617 if !exists {
@@ -23,6 +24,7 @@ func mustGetProposal(proposalId uint64) ProposalInfo {
2324 return result.(ProposalInfo)
2425}
2526
27+ // Retrieves a vote by its key
2628func mustGetVote(key string) bool {
2729 vote, exists := votes.Get(key)
2830 if !exists {
@@ -38,8 +40,10 @@ func mustGetVote(key string) bool {
3840func getVoteInfoFromKey(voteKey string) (voteWithWeight, bool) {
3941 mustGetVote(voteKey)
4042
43+ // Splits the vote key into proposal ID and user address
4144 pid, addr := divideVoteKeyToProposalIdAndUser(voteKey)
4245
46+ // Retrieves user vote information
4347 voteInfo, exists := getUserVote(addr, pid)
4448 if !exists {
4549 panic(addDetailToError(
@@ -51,15 +55,18 @@ func getVoteInfoFromKey(voteKey string) (voteWithWeight, bool) {
5155 return voteInfo, true
5256}
5357
58+ // Retrieves vote information by key
5459func mustGetVoteInfo(voteKey string) voteWithWeight {
5560 voteInfo, _ := getVoteInfoFromKey(voteKey)
5661 return voteInfo
5762}
5863
64+ // Iterates over an AVL tree and applies a callback function to each element
5965func iterTree(tree *avl.Tree, cb func(key string, value interface{}) bool) {
6066 tree.Iterate("", "", cb)
6167}
6268
69+ // Converts a string to an integer, panics if conversion fails
6370func strToInt(str string) int {
6471 res, err := strconv.Atoi(str)
6572 if err != nil {
@@ -69,13 +76,15 @@ func strToInt(str string) int {
6976 return res
7077}
7178
79+ // Converts a boolean vote to a string representation
7280func voteToString(b bool) string {
7381 if b {
7482 return "yes"
7583 }
7684 return "no"
7785}
7886
87+ // Marshals a JSON node to a string, panics if marshalling fails
7988func marshal(data *json.Node) string {
8089 b, err := json.Marshal(data)
8190 if err != nil {
@@ -85,10 +94,12 @@ func marshal(data *json.Node) string {
8594 return string(b)
8695}
8796
97+ // Encodes a string to base64
8898func b64Encode(data string) string {
8999 return string(base64.StdEncoding.EncodeToString([]byte(data)))
90100}
91101
102+ // Decodes a base64 string, panics if decoding fails
92103func b64Decode(data string) string {
93104 decoded, err := base64.StdEncoding.DecodeString(data)
94105 if err != nil {
@@ -97,15 +108,18 @@ func b64Decode(data string) string {
97108 return string(decoded)
98109}
99110
111+ // Returns the package path of the previous realm
100112func prevRealm() string {
101113 return std.PrevRealm().PkgPath()
102114}
103115
116+ // Returns the address and package path of the previous realm
104117func getPrev() (string, string) {
105118 prev := std.PrevRealm()
106119 return prev.Addr().String(), prev.PkgPath()
107120}
108121
122+ // Formats an unsigned integer to a string
109123func formatUint(v interface{}) string {
110124 switch v := v.(type) {
111125 case uint8:
@@ -119,10 +133,12 @@ func formatUint(v interface{}) string {
119133 }
120134}
121135
136+ // Formats a boolean to a string
122137func formatBool(v bool) string {
123138 return strconv.FormatBool(v)
124139}
125140
141+ // Parses a string to an integer, panics if parsing fails
126142func parseInt(s string) int {
127143 num, err := strconv.ParseInt(s, 10, 64)
128144 if err != nil {
@@ -131,6 +147,7 @@ func parseInt(s string) int {
131147 return int(num)
132148}
133149
150+ // Parses a string to an unsigned 64-bit integer, panics if parsing fails
134151func parseUint64(s string) uint64 {
135152 num, err := strconv.ParseUint(s, 10, 64)
136153 if err != nil {
@@ -139,6 +156,7 @@ func parseUint64(s string) uint64 {
139156 return num
140157}
141158
159+ // Parses a string to a boolean, panics if parsing fails
142160func parseBool(s string) bool {
143161 switch s {
144162 case "true":
0 commit comments