github.com/Finschia/finschia-sdk@v0.49.1/x/feegrant/client/cli/query.go (about) 1 package cli 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/spf13/cobra" 8 9 "github.com/Finschia/finschia-sdk/client" 10 "github.com/Finschia/finschia-sdk/client/flags" 11 sdk "github.com/Finschia/finschia-sdk/types" 12 "github.com/Finschia/finschia-sdk/version" 13 "github.com/Finschia/finschia-sdk/x/feegrant" 14 ) 15 16 // GetQueryCmd returns the cli query commands for this module 17 func GetQueryCmd() *cobra.Command { 18 feegrantQueryCmd := &cobra.Command{ 19 Use: feegrant.ModuleName, 20 Short: "Querying commands for the feegrant module", 21 DisableFlagParsing: true, 22 SuggestionsMinimumDistance: 2, 23 RunE: client.ValidateCmd, 24 } 25 26 feegrantQueryCmd.AddCommand( 27 GetCmdQueryFeeGrant(), 28 GetCmdQueryFeeGrantsByGrantee(), 29 GetCmdQueryFeeGrantsByGranter(), 30 ) 31 32 return feegrantQueryCmd 33 } 34 35 // GetCmdQueryFeeGrant returns cmd to query for a grant between granter and grantee. 36 func GetCmdQueryFeeGrant() *cobra.Command { 37 cmd := &cobra.Command{ 38 Use: "grant [granter] [grantee]", 39 Args: cobra.ExactArgs(2), 40 Short: "Query details of a single grant", 41 Long: strings.TrimSpace( 42 fmt.Sprintf(`Query details for a grant. 43 You can find the fee-grant of a granter and grantee. 44 45 Example: 46 $ %s query feegrant grant [granter] [grantee] 47 `, version.AppName), 48 ), 49 RunE: func(cmd *cobra.Command, args []string) error { 50 clientCtx := client.GetClientContextFromCmd(cmd) 51 queryClient := feegrant.NewQueryClient(clientCtx) 52 53 granterAddr, err := sdk.AccAddressFromBech32(args[0]) 54 if err != nil { 55 return err 56 } 57 58 granteeAddr, err := sdk.AccAddressFromBech32(args[1]) 59 if err != nil { 60 return err 61 } 62 63 res, err := queryClient.Allowance( 64 cmd.Context(), 65 &feegrant.QueryAllowanceRequest{ 66 Granter: granterAddr.String(), 67 Grantee: granteeAddr.String(), 68 }, 69 ) 70 if err != nil { 71 return err 72 } 73 74 return clientCtx.PrintProto(res.Allowance) 75 }, 76 } 77 78 flags.AddQueryFlagsToCmd(cmd) 79 80 return cmd 81 } 82 83 // GetCmdQueryFeeGrantsByGrantee returns cmd to query for all grants for a grantee. 84 func GetCmdQueryFeeGrantsByGrantee() *cobra.Command { 85 cmd := &cobra.Command{ 86 Use: "grants-by-grantee [grantee]", 87 Aliases: []string{"grants"}, 88 Args: cobra.ExactArgs(1), 89 Short: "Query all grants of a grantee", 90 Long: strings.TrimSpace( 91 fmt.Sprintf(`Queries all the grants for a grantee address. 92 93 Example: 94 $ %s query feegrant grants-by-grantee [grantee] 95 `, version.AppName), 96 ), 97 RunE: func(cmd *cobra.Command, args []string) error { 98 clientCtx := client.GetClientContextFromCmd(cmd) 99 queryClient := feegrant.NewQueryClient(clientCtx) 100 101 granteeAddr, err := sdk.AccAddressFromBech32(args[0]) 102 if err != nil { 103 return err 104 } 105 106 pageReq, err := client.ReadPageRequest(cmd.Flags()) 107 if err != nil { 108 return err 109 } 110 111 res, err := queryClient.Allowances( 112 cmd.Context(), 113 &feegrant.QueryAllowancesRequest{ 114 Grantee: granteeAddr.String(), 115 Pagination: pageReq, 116 }, 117 ) 118 if err != nil { 119 return err 120 } 121 122 return clientCtx.PrintProto(res) 123 }, 124 } 125 126 flags.AddQueryFlagsToCmd(cmd) 127 flags.AddPaginationFlagsToCmd(cmd, "grants") 128 129 return cmd 130 } 131 132 // GetCmdQueryFeeGrantsByGranter returns cmd to query for all grants by a granter. 133 func GetCmdQueryFeeGrantsByGranter() *cobra.Command { 134 cmd := &cobra.Command{ 135 Use: "grants-by-granter [granter]", 136 Args: cobra.ExactArgs(1), 137 Short: "Query all grants by a granter", 138 Long: strings.TrimSpace( 139 fmt.Sprintf(`Queries all the grants issued for a granter address. 140 141 Example: 142 $ %s query feegrant grants-by-granter [granter] 143 `, version.AppName), 144 ), 145 RunE: func(cmd *cobra.Command, args []string) error { 146 clientCtx := client.GetClientContextFromCmd(cmd) 147 queryClient := feegrant.NewQueryClient(clientCtx) 148 149 granterAddr, err := sdk.AccAddressFromBech32(args[0]) 150 if err != nil { 151 return err 152 } 153 154 pageReq, err := client.ReadPageRequest(cmd.Flags()) 155 if err != nil { 156 return err 157 } 158 159 res, err := queryClient.AllowancesByGranter( 160 cmd.Context(), 161 &feegrant.QueryAllowancesByGranterRequest{ 162 Granter: granterAddr.String(), 163 Pagination: pageReq, 164 }, 165 ) 166 if err != nil { 167 return err 168 } 169 170 return clientCtx.PrintProto(res) 171 }, 172 } 173 174 flags.AddQueryFlagsToCmd(cmd) 175 flags.AddPaginationFlagsToCmd(cmd, "grants") 176 177 return cmd 178 }