reward_calculation_types.gno
1.28 Kb · 65 lines
1package v1
2
3import (
4 "strconv"
5 "strings"
6
7 ufmt "gno.land/p/nt/ufmt/v0"
8)
9
10// EncodeUint converts a uint64 number into a zero-padded 20-character string.
11//
12// Parameters:
13// - num (uint64): The number to encode.
14//
15// Returns:
16// - string: A zero-padded string representation of the number.
17//
18// Example:
19// Input: 12345
20// Output: "00000000000000012345"
21func EncodeUint(num uint64) string {
22 // Convert the value to a decimal string.
23 s := strconv.FormatUint(num, 10)
24
25 // Zero-pad to a total length of 20 characters.
26 zerosNeeded := 20 - len(s)
27 return strings.Repeat("0", zerosNeeded) + s
28}
29
30func EncodeInt64(num int64) string {
31 if num < 0 {
32 panic(ufmt.Sprintf("negative value not supported: %d", num))
33 }
34 return EncodeUint(uint64(num))
35}
36
37// DecodeUint converts a zero-padded string back into a uint64 number.
38//
39// Parameters:
40// - s (string): The zero-padded string.
41//
42// Returns:
43// - uint64: The decoded number.
44//
45// Panics:
46// - If the string cannot be parsed into a uint64.
47//
48// Example:
49// Input: "00000000000000012345"
50// Output: 12345
51func DecodeUint(s string) uint64 {
52 num, err := strconv.ParseUint(s, 10, 64)
53 if err != nil {
54 panic(err)
55 }
56 return num
57}
58
59func DecodeInt64(s string) int64 {
60 num, err := strconv.ParseInt(s, 10, 64)
61 if err != nil {
62 panic(err)
63 }
64 return num
65}