@@ -2,8 +2,10 @@ package internal
2
2
3
3
import (
4
4
"context"
5
+ "encoding/json"
5
6
"errors"
6
7
"fmt"
8
+ "io"
7
9
"time"
8
10
9
11
"github.com/algorandfoundation/hack-tui/api"
@@ -27,6 +29,78 @@ type Account struct {
27
29
LastModified int
28
30
}
29
31
32
+ // Gets the list of addresses created at genesis from the genesis file
33
+ func getAddressesFromGenesis (client * api.ClientWithResponses ) ([]string , string , string , error ) {
34
+ resp , err := client .GetGenesis (context .Background ())
35
+ if err != nil {
36
+ return []string {}, "" , "" , err
37
+ }
38
+
39
+ if resp .StatusCode != 200 {
40
+ return []string {}, "" , "" , errors .New (fmt .Sprintf ("Failed to get genesis file. Received error code: %d" , resp .StatusCode ))
41
+ }
42
+
43
+ defer resp .Body .Close ()
44
+
45
+ // Read the response body
46
+ body , err := io .ReadAll (resp .Body )
47
+ if err != nil {
48
+ return []string {}, "" , "" , err
49
+ }
50
+
51
+ // Unmarshal the JSON response into a map
52
+ var jsonResponse map [string ]interface {}
53
+ err = json .Unmarshal (body , & jsonResponse )
54
+ if err != nil {
55
+ return []string {}, "" , "" , err
56
+ }
57
+
58
+ // Two special addresses
59
+ rewardsPool := "7777777777777777777777777777777777777777777777777774MSJUVU"
60
+ feeSink := "A7NMWS3NT3IUDMLVO26ULGXGIIOUQ3ND2TXSER6EBGRZNOBOUIQXHIBGDE"
61
+ rewardsPoolIncluded := false
62
+ feeSinkIncluded := false
63
+
64
+ // Loop over each entry in the "alloc" list and collect the "addr" values
65
+ var addresses []string
66
+ if allocList , ok := jsonResponse ["alloc" ].([]interface {}); ok {
67
+ for _ , entry := range allocList {
68
+ if entryMap , ok := entry .(map [string ]interface {}); ok {
69
+ if addr , ok := entryMap ["addr" ].(string ); ok {
70
+ if addr == rewardsPool {
71
+ rewardsPoolIncluded = true
72
+ } else if addr == feeSink {
73
+ feeSinkIncluded = true
74
+ } else {
75
+ addresses = append (addresses , addr )
76
+ }
77
+ } else {
78
+ return []string {}, "" , "" , fmt .Errorf ("In genesis.json no addr string found in list element entry: %+v" , entry )
79
+ }
80
+ } else {
81
+ return []string {}, "" , "" , fmt .Errorf ("In genesis.json list element of alloc-field is not a map: %+v" , entry )
82
+ }
83
+ }
84
+ } else {
85
+ return []string {}, "" , "" , errors .New ("alloc is not a list" )
86
+ }
87
+
88
+ if ! rewardsPoolIncluded || ! feeSinkIncluded {
89
+ return []string {}, "" , "" , errors .New ("Expected RewardsPool and/or FeeSink addresses NOT found in genesis file" )
90
+ }
91
+
92
+ return addresses , rewardsPool , feeSink , nil
93
+ }
94
+
95
+ func isValidStatus (status string ) bool {
96
+ validStatuses := map [string ]bool {
97
+ "Online" : true ,
98
+ "Offline" : true ,
99
+ "Not Participating" : true ,
100
+ }
101
+ return validStatuses [status ]
102
+ }
103
+
30
104
// Get Online Status of Account
31
105
func getAccountOnlineStatus (client * api.ClientWithResponses , address string ) (string , error ) {
32
106
var format api.AccountInformationParamsFormat = "json"
@@ -45,6 +119,10 @@ func getAccountOnlineStatus(client *api.ClientWithResponses, address string) (st
45
119
return "N/A" , errors .New (fmt .Sprintf ("Failed to get account information. Received error code: %d" , r .StatusCode ()))
46
120
}
47
121
122
+ if r .JSON200 == nil {
123
+ return "N/A" , errors .New ("Failed to get account information. JSON200 is nil" )
124
+ }
125
+
48
126
return r .JSON200 .Status , nil
49
127
}
50
128
0 commit comments