github.com/Finschia/finschia-sdk@v0.48.1/x/token/client/cli/query.go (about) 1 package cli 2 3 import ( 4 "fmt" 5 6 "github.com/spf13/cobra" 7 8 "github.com/Finschia/finschia-sdk/client" 9 "github.com/Finschia/finschia-sdk/client/flags" 10 "github.com/Finschia/finschia-sdk/version" 11 "github.com/Finschia/finschia-sdk/x/token" 12 ) 13 14 // NewQueryCmd returns the cli query commands for this module 15 func NewQueryCmd() *cobra.Command { 16 queryCmd := &cobra.Command{ 17 Use: token.ModuleName, 18 Short: fmt.Sprintf("Querying commands for the %s module", token.ModuleName), 19 Long: "", 20 DisableFlagParsing: true, 21 SuggestionsMinimumDistance: 2, 22 RunE: client.ValidateCmd, 23 } 24 25 queryCmd.AddCommand( 26 NewQueryCmdBalance(), 27 NewQueryCmdSupply(), 28 NewQueryCmdMinted(), 29 NewQueryCmdBurnt(), 30 NewQueryCmdContract(), 31 NewQueryCmdGranteeGrants(), 32 NewQueryCmdIsOperatorFor(), 33 NewQueryCmdHoldersByOperator(), 34 ) 35 36 return queryCmd 37 } 38 39 func NewQueryCmdBalance() *cobra.Command { 40 cmd := &cobra.Command{ 41 Use: "token-balance [class-id] [address]", 42 Args: cobra.ExactArgs(2), 43 Short: "query for token balances by a given address", 44 Example: fmt.Sprintf(`$ %s query %s token-balance <class-id> <address>`, version.AppName, token.ModuleName), 45 RunE: func(cmd *cobra.Command, args []string) error { 46 clientCtx, err := client.GetClientQueryContext(cmd) 47 if err != nil { 48 return err 49 } 50 queryClient := token.NewQueryClient(clientCtx) 51 res, err := queryClient.Balance(cmd.Context(), &token.QueryBalanceRequest{ 52 ContractId: args[0], 53 Address: args[1], 54 }) 55 if err != nil { 56 return err 57 } 58 return clientCtx.PrintProto(res) 59 }, 60 } 61 62 flags.AddQueryFlagsToCmd(cmd) 63 return cmd 64 } 65 66 func NewQueryCmdSupply() *cobra.Command { 67 cmd := &cobra.Command{ 68 Use: "supply [class-id]", 69 Args: cobra.ExactArgs(1), 70 Short: "query the supply of tokens of the class", 71 Example: fmt.Sprintf(`$ %s query %s supply <class-id>`, version.AppName, token.ModuleName), 72 RunE: func(cmd *cobra.Command, args []string) error { 73 clientCtx, err := client.GetClientQueryContext(cmd) 74 if err != nil { 75 return err 76 } 77 queryClient := token.NewQueryClient(clientCtx) 78 res, err := queryClient.Supply(cmd.Context(), &token.QuerySupplyRequest{ 79 ContractId: args[0], 80 }) 81 if err != nil { 82 return err 83 } 84 return clientCtx.PrintProto(res) 85 }, 86 } 87 88 flags.AddQueryFlagsToCmd(cmd) 89 return cmd 90 } 91 92 func NewQueryCmdMinted() *cobra.Command { 93 cmd := &cobra.Command{ 94 Use: "minted [class-id]", 95 Args: cobra.ExactArgs(1), 96 Short: "query the minted tokens of the class", 97 Example: fmt.Sprintf(`$ %s query %s supply <class-id>`, version.AppName, token.ModuleName), 98 RunE: func(cmd *cobra.Command, args []string) error { 99 clientCtx, err := client.GetClientQueryContext(cmd) 100 if err != nil { 101 return err 102 } 103 queryClient := token.NewQueryClient(clientCtx) 104 res, err := queryClient.Minted(cmd.Context(), &token.QueryMintedRequest{ 105 ContractId: args[0], 106 }) 107 if err != nil { 108 return err 109 } 110 return clientCtx.PrintProto(res) 111 }, 112 } 113 114 flags.AddQueryFlagsToCmd(cmd) 115 return cmd 116 } 117 118 func NewQueryCmdBurnt() *cobra.Command { 119 cmd := &cobra.Command{ 120 Use: "burnt [class-id]", 121 Args: cobra.ExactArgs(1), 122 Short: "query the burnt tokens of the class", 123 Example: fmt.Sprintf(`$ %s query %s supply <class-id>`, version.AppName, token.ModuleName), 124 RunE: func(cmd *cobra.Command, args []string) error { 125 clientCtx, err := client.GetClientQueryContext(cmd) 126 if err != nil { 127 return err 128 } 129 queryClient := token.NewQueryClient(clientCtx) 130 res, err := queryClient.Burnt(cmd.Context(), &token.QueryBurntRequest{ 131 ContractId: args[0], 132 }) 133 if err != nil { 134 return err 135 } 136 return clientCtx.PrintProto(res) 137 }, 138 } 139 140 flags.AddQueryFlagsToCmd(cmd) 141 return cmd 142 } 143 144 func NewQueryCmdContract() *cobra.Command { 145 cmd := &cobra.Command{ 146 Use: "token [contract-id]", 147 Args: cobra.ExactArgs(1), 148 Short: "query token metadata based on its id", 149 Example: fmt.Sprintf(`$ %s query %s token <contract-id>`, version.AppName, token.ModuleName), 150 RunE: func(cmd *cobra.Command, args []string) error { 151 clientCtx, err := client.GetClientQueryContext(cmd) 152 if err != nil { 153 return err 154 } 155 queryClient := token.NewQueryClient(clientCtx) 156 res, err := queryClient.Contract(cmd.Context(), &token.QueryContractRequest{ 157 ContractId: args[0], 158 }) 159 if err != nil { 160 return err 161 } 162 return clientCtx.PrintProto(res) 163 }, 164 } 165 166 flags.AddQueryFlagsToCmd(cmd) 167 return cmd 168 } 169 170 func NewQueryCmdGranteeGrants() *cobra.Command { 171 cmd := &cobra.Command{ 172 Use: "grantee-grants [class-id] [grantee]", 173 Args: cobra.ExactArgs(2), 174 Short: "query grants on a given grantee", 175 Example: fmt.Sprintf(`$ %s query %s grantee-grants <class-id> <grantee>`, version.AppName, token.ModuleName), 176 RunE: func(cmd *cobra.Command, args []string) error { 177 clientCtx, err := client.GetClientQueryContext(cmd) 178 if err != nil { 179 return err 180 } 181 queryClient := token.NewQueryClient(clientCtx) 182 res, err := queryClient.GranteeGrants(cmd.Context(), &token.QueryGranteeGrantsRequest{ 183 ContractId: args[0], 184 Grantee: args[1], 185 }) 186 if err != nil { 187 return err 188 } 189 return clientCtx.PrintProto(res) 190 }, 191 } 192 193 flags.AddQueryFlagsToCmd(cmd) 194 return cmd 195 } 196 197 func NewQueryCmdIsOperatorFor() *cobra.Command { 198 cmd := &cobra.Command{ 199 Use: "is-operator-for [class-id] [operator] [holder]", 200 Args: cobra.ExactArgs(3), 201 Short: "query authorization on its operator and the token holder", 202 Example: fmt.Sprintf(`$ %s query %s is-operator-for <class-id> <operator> <holder>`, version.AppName, token.ModuleName), 203 RunE: func(cmd *cobra.Command, args []string) error { 204 clientCtx, err := client.GetClientQueryContext(cmd) 205 if err != nil { 206 return err 207 } 208 queryClient := token.NewQueryClient(clientCtx) 209 res, err := queryClient.IsOperatorFor(cmd.Context(), &token.QueryIsOperatorForRequest{ 210 ContractId: args[0], 211 Operator: args[1], 212 Holder: args[2], 213 }) 214 if err != nil { 215 return err 216 } 217 return clientCtx.PrintProto(res) 218 }, 219 } 220 221 flags.AddQueryFlagsToCmd(cmd) 222 return cmd 223 } 224 225 func NewQueryCmdHoldersByOperator() *cobra.Command { 226 cmd := &cobra.Command{ 227 Use: "holders-by-operator [class-id] [operator]", 228 Args: cobra.ExactArgs(2), 229 Short: "query all authorizations on a given operator", 230 Example: fmt.Sprintf(`$ %s query %s holders-by-operator <class-id> <operator>`, version.AppName, token.ModuleName), 231 RunE: func(cmd *cobra.Command, args []string) error { 232 clientCtx, err := client.GetClientQueryContext(cmd) 233 if err != nil { 234 return err 235 } 236 queryClient := token.NewQueryClient(clientCtx) 237 pageReq, err := client.ReadPageRequest(cmd.Flags()) 238 if err != nil { 239 return err 240 } 241 res, err := queryClient.HoldersByOperator(cmd.Context(), &token.QueryHoldersByOperatorRequest{ 242 ContractId: args[0], 243 Operator: args[1], 244 Pagination: pageReq, 245 }) 246 if err != nil { 247 return err 248 } 249 return clientCtx.PrintProto(res) 250 }, 251 } 252 253 flags.AddQueryFlagsToCmd(cmd) 254 flags.AddPaginationFlagsToCmd(cmd, "authorizations") 255 return cmd 256 }