-
Notifications
You must be signed in to change notification settings - Fork 12
/
query.go
88 lines (71 loc) · 2.36 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
package cli
import (
"fmt"
"github.com/commercionetwork/commercionetwork/x/pricefeed/internal/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/spf13/cobra"
)
func GetQueryCmd(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: types.ModuleName,
Short: fmt.Sprintf("Querying commands for %s module", types.ModuleName),
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
cmd.AddCommand(getCmdOraclesList(cdc), getCmdCurrentPrice(cdc), getCmdCurrentPrices(cdc))
return cmd
}
func getCmdOraclesList(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "oracles",
Short: "Get a list of all trusted oracles",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryGetOracles)
res, _, err := cliCtx.QueryWithData(route, nil)
if err != nil {
fmt.Printf("Could not get oracles' list: \n %s", err)
}
fmt.Println(string(res))
return nil
},
}
}
func getCmdCurrentPrice(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "price [token-name]",
Short: "Get the current price of the token",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
route := fmt.Sprintf("custom/%s/%s/%s", types.QuerierRoute, types.QueryGetCurrentPrice, args[0])
res, _, err := cliCtx.QueryWithData(route, nil)
if err != nil {
fmt.Printf("Could not get token's price: \n %s", err)
}
fmt.Println(string(res))
return nil
},
}
}
func getCmdCurrentPrices(cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "prices",
Short: "Get the current prices of all tokens",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryGetCurrentPrices)
res, _, err := cliCtx.QueryWithData(route, nil)
if err != nil {
fmt.Printf("Could not get tokens' price: \n %s", err)
}
fmt.Println(string(res))
return nil
},
}
}