-
Notifications
You must be signed in to change notification settings - Fork 2
/
query.go
74 lines (58 loc) · 1.86 KB
/
query.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package cmd
import (
"encoding/json"
"fmt"
"github.com/cosmos/cosmos-sdk/client"
"github.com/rs/zerolog/log"
dessertclient "github.com/kogisin/dessert/client"
"github.com/kogisin/dessert/config"
"github.com/kogisin/dessert/models"
"github.com/kogisin/dessert/wallet"
"github.com/spf13/cobra"
resty "gopkg.in/resty.v1"
)
/*
Zerolog sample
log.Error().Err(err).Str("hash", tx.TxHash).Msg("failed to persist transaction")
*/
func getQueryCmd(cfg config.Config, cw dessertclient.ConfigWrapper) *cobra.Command {
queryCmd := &cobra.Command{
Use: "query [command]",
Aliases: []string{"q"},
Short: "Querying subcommands",
}
queryCmd.AddCommand(
balanceCmd(cfg, cw),
client.LineBreak,
)
return queryCmd
}
func balanceCmd(cfg config.Config, cw dessertclient.ConfigWrapper) *cobra.Command {
cmd := &cobra.Command{
Use: "balance",
Short: "Get your account balance",
RunE: func(cmd *cobra.Command, args []string) error {
// Get bech32 account address from mnemonic
cosmosAddress, _ := wallet.GetBech32AccAddrFromMnemonic(cfg.Wallet.Mnemonic, "")
// Query account information
resp, err := resty.R().Get(cfg.Client.LCDEndpoint + "/auth/accounts/" + cosmosAddress)
if err != nil {
log.Error().Err(err).Str(cfg.Client.LCDEndpoint, "/auth/accounts/"+cosmosAddress).Msg("failed to fetch data")
return err
}
var accountResp models.Account
err = json.Unmarshal(resp.Body(), &accountResp)
if err != nil {
log.Error().Err(err).Msg("failed to unmarshal models.Account data")
return err
}
fmt.Println("Address: ", accountResp.Value.Address)
fmt.Println("Denom: ", accountResp.Value.Coins[0].Denom)
fmt.Println("Amount: ", accountResp.Value.Coins[0].Amount)
fmt.Println("Address Number: ", accountResp.Value.AccountNumber)
fmt.Println("Sequence: ", accountResp.Value.Sequence)
return nil
},
}
return cmd
}