planetmint-go/x/dao/client/cli/query_distribution.go
Julian Strobl bead62bd04
refactor(query): align with cosmos-sdk style (#466)
- use one `cmd.AddCommand(...)`
- sort commands
- use `GetCmd...` function name style

Signed-off-by: Julian Strobl <jmastr@mailbox.org>
2024-11-12 15:59:52 +01:00

63 lines
1.3 KiB
Go

package cli
import (
"fmt"
"strconv"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/rpc"
"github.com/planetmint/planetmint-go/x/dao/types"
"github.com/spf13/cast"
"github.com/spf13/cobra"
)
var _ = strconv.Itoa(0)
func GetCmdDistribution() *cobra.Command {
cmd := &cobra.Command{
Use: "distribution [height]",
Short: "Query for distributions by height",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
requestHeight, err := cast.ToInt64E(args[0])
if err != nil {
return err
}
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
latestHeight, err := rpc.GetChainHeight(clientCtx)
if err != nil {
return err
}
if requestHeight > latestHeight {
err = fmt.Errorf("height %d must be less than or equal to the current blockchain height %d",
requestHeight, latestHeight)
return err
}
queryClient := types.NewQueryClient(clientCtx)
params := &types.QueryGetDistributionRequest{
Height: requestHeight,
}
res, err := queryClient.GetDistribution(cmd.Context(), params)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}