-
Notifications
You must be signed in to change notification settings - Fork 182
/
query.go
106 lines (88 loc) · 2.76 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package cli
import (
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/okex/exchain/libs/cosmos-sdk/client"
"github.com/okex/exchain/libs/cosmos-sdk/client/context"
"github.com/okex/exchain/libs/cosmos-sdk/client/flags"
"github.com/okex/exchain/libs/cosmos-sdk/codec"
sdk "github.com/okex/exchain/libs/cosmos-sdk/types"
"github.com/okex/exchain/libs/cosmos-sdk/version"
"github.com/okex/exchain/libs/cosmos-sdk/x/supply/internal/types"
)
// GetQueryCmd returns the cli query commands for this module
func GetQueryCmd(cdc *codec.Codec) *cobra.Command {
// Group supply queries under a subcommand
supplyQueryCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Querying commands for the supply module",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
supplyQueryCmd.AddCommand(flags.GetCommands(
GetCmdQueryTotalSupply(cdc),
)...)
return supplyQueryCmd
}
// GetCmdQueryTotalSupply implements the query total supply command.
func GetCmdQueryTotalSupply(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "total [denom]",
Args: cobra.MaximumNArgs(1),
Short: "Query the total supply of coins of the chain",
Long: strings.TrimSpace(
fmt.Sprintf(`Query total supply of coins that are held by accounts in the
chain.
Example:
$ %s query %s total
To query for the total supply of a specific coin denomination use:
$ %s query %s total stake
`,
version.ClientName, types.ModuleName, version.ClientName, types.ModuleName,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
if len(args) == 0 {
return queryTotalSupply(cliCtx, cdc)
}
return querySupplyOf(cliCtx, cdc, args[0])
},
}
}
func queryTotalSupply(cliCtx context.CLIContext, cdc *codec.Codec) error {
params := types.NewQueryTotalSupplyParams(1, 0) // no pagination
bz, err := cdc.MarshalJSON(params)
if err != nil {
return err
}
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryTotalSupply), bz)
if err != nil {
return err
}
var totalSupply sdk.Coins
err = cdc.UnmarshalJSON(res, &totalSupply)
if err != nil {
return err
}
return cliCtx.PrintOutput(totalSupply)
}
func querySupplyOf(cliCtx context.CLIContext, cdc *codec.Codec, denom string) error {
params := types.NewQuerySupplyOfParams(denom)
bz, err := cdc.MarshalJSON(params)
if err != nil {
return err
}
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QuerySupplyOf), bz)
if err != nil {
return err
}
var supply sdk.Dec
err = cdc.UnmarshalJSON(res, &supply)
if err != nil {
return err
}
return cliCtx.PrintOutput(supply)
}