/
balances.go
51 lines (42 loc) · 1.04 KB
/
balances.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package castcmd
import (
"fmt"
"strings"
gocmd "github.com/go-cmd/cmd"
"github.com/lasthyphen/ecctools/pkg/utils"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)
func newBalancesCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "balances",
Short: "",
Long: ``,
RunE: func(cmd *cobra.Command, args []string) error {
var err error
accounts, err := utils.LoadJSON(viper.GetString("accounts"))
cobra.CheckErr(err)
balances := "{}"
accounts.ForEach(func(key gjson.Result, value gjson.Result) bool {
envCmd := gocmd.NewCmd("cast", "balance", value.Get("addr").String())
status := <-envCmd.Start()
if len(status.Stderr) > 0 {
err = fmt.Errorf(strings.Join(status.Stderr, "\n"))
return false
}
result := status.Stdout[0]
ether := utils.ToDecimal(result, 18)
balances, _ = sjson.Set(balances, key.String(), ether)
return true
})
if err != nil {
return err
}
fmt.Println(balances)
return nil
},
}
return cmd
}