Skip to content

Commit

Permalink
feat: add fundings cli query
Browse files Browse the repository at this point in the history
  • Loading branch information
shifty11 committed Oct 25, 2023
1 parent 427f930 commit b143cea
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
1 change: 1 addition & 0 deletions x/query/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
// Funders
cmd.AddCommand(CmdShowFunder())
cmd.AddCommand(CmdListFunders())
cmd.AddCommand(CmdListFundings())

return cmd
}
83 changes: 83 additions & 0 deletions x/query/client/cli/query_fundings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package cli

import (
"context"
"github.com/KYVENetwork/chain/x/query/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/spf13/cobra"
"strconv"
)

func byFunder(
clientCtx client.Context,
queryClient types.QueryFundersClient,
pageReq *query.PageRequest,
address string,
) error {
params := &types.QueryFundingsByFunderRequest{
Pagination: pageReq,
Address: address,
WithInactiveFundings: true,
}

res, err := queryClient.FundingsByFunder(context.Background(), params)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
}

func byPool(
clientCtx client.Context,
queryClient types.QueryFundersClient,
pageReq *query.PageRequest,
poolId uint64,
) error {
params := &types.QueryFundingsByPoolRequest{
Pagination: pageReq,
PoolId: poolId,
WithInactiveFundings: true,
}

res, err := queryClient.FundingsByPool(context.Background(), params)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
}

func CmdListFundings() *cobra.Command {
cmd := &cobra.Command{
Use: "fundings [address | pool-id]",
Short: "list all fundings of a user or a pool",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
reqAddressOrPool := args[0]

clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}

queryClient := types.NewQueryFundersClient(clientCtx)

poolId, err := strconv.ParseUint(reqAddressOrPool, 10, 64)
if err != nil {
return byFunder(clientCtx, queryClient, pageReq, reqAddressOrPool)
}
return byPool(clientCtx, queryClient, pageReq, poolId)
},
}

flags.AddPaginationFlagsToCmd(cmd, cmd.Use)
flags.AddQueryFlagsToCmd(cmd)

return cmd
}

0 comments on commit b143cea

Please sign in to comment.