Skip to content

Commit

Permalink
add utxo query cli
Browse files Browse the repository at this point in the history
  • Loading branch information
keithsue committed Jun 19, 2024
1 parent 454a233 commit 3f594f2
Show file tree
Hide file tree
Showing 4 changed files with 893 additions and 46 deletions.
27 changes: 26 additions & 1 deletion proto/side/btcbridge/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ service Query {
rpc QuerySigningRequest(QuerySigningRequestRequest) returns (QuerySigningRequestResponse) {
option (google.api.http).get = "/sideprotocol/side/btcbridge/signing/request";
}

// UTXOs queries all utxos.
rpc QueryUTXOs(QueryUTXOsRequest) returns (QueryUTXOsResponse) {
option (google.api.http).get = "/sideprotocol/side/btcbridge/utxos";
}
// UTXOsByAddress queries the utxos of the given address.
rpc QueryUTXOsByAddress(QueryUTXOsByAddressRequest) returns (QueryUTXOsByAddressResponse) {
option (google.api.http).get = "/sideprotocol/side/btcbridge/utxos/{address}";
}
}

// QuerySigningRequestRequest is request type for the Query/SigningRequest RPC method.
Expand Down Expand Up @@ -83,3 +90,21 @@ message QueryBlockHeaderByHashRequest {
message QueryBlockHeaderByHashResponse {
BlockHeader block_header = 1;
}

// QueryUTXOsRequest is the request type for the Query/UTXOs RPC method.
message QueryUTXOsRequest {}

// QueryUTXOsResponse is the response type for the Query/UTXOs RPC method.
message QueryUTXOsResponse {
repeated UTXO utxos = 1;
}

// QueryUTXOsByAddressRequest is the request type for the Query/UTXOsByAddress RPC method.
message QueryUTXOsByAddressRequest {
string address = 1;
}

// QueryUTXOsByAddressResponse is the response type for the Query/UTXOsByAddress RPC method.
message QueryUTXOsByAddressResponse {
repeated UTXO utxos = 1;
}
56 changes: 56 additions & 0 deletions x/btcbridge/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"context"
"fmt"
"strconv"

Expand All @@ -11,6 +12,7 @@ import (

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// GetQueryCmd returns the cli query commands for this module
Expand All @@ -27,6 +29,7 @@ func GetQueryCmd(_ string) *cobra.Command {
cmd.AddCommand(CmdQueryParams())
cmd.AddCommand(CmdBestBlock())
cmd.AddCommand(CmdQueryBlock())
cmd.AddCommand(CmdQueryUTXOs())
// this line is used by starport scaffolding # 1

return cmd
Expand Down Expand Up @@ -124,3 +127,56 @@ func CmdQueryBlock() *cobra.Command {

return cmd
}

func CmdQueryUTXOs() *cobra.Command {
cmd := &cobra.Command{
Use: "utxos [address]",
Short: "query utxos with an optional address",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

if len(args) == 0 {
return queryUTXOs(&clientCtx, cmd.Context())
}

return queryUTXOsByAddr(&clientCtx, cmd.Context(), args[0])
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}

func queryUTXOs(clientCtx *client.Context, cmdCtx context.Context) error {
queryClient := types.NewQueryClient(clientCtx)

res, err := queryClient.QueryUTXOs(cmdCtx, &types.QueryUTXOsRequest{})
if err != nil {
return err
}

return clientCtx.PrintProto(res)
}

func queryUTXOsByAddr(clientCtx *client.Context, cmdCtx context.Context, addr string) error {
queryClient := types.NewQueryClient(clientCtx)

_, err := sdk.AccAddressFromBech32(addr)
if err != nil {
return err
}

res, err := queryClient.QueryUTXOsByAddress(cmdCtx, &types.QueryUTXOsByAddressRequest{
Address: addr,
})
if err != nil {
return err
}

return clientCtx.PrintProto(res)
}
27 changes: 27 additions & 0 deletions x/btcbridge/keeper/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,30 @@ func (k Keeper) QuerySigningRequest(goCtx context.Context, req *types.QuerySigni
return &types.QuerySigningRequestResponse{Requests: requests}, nil

}

func (k Keeper) QueryUTXOs(goCtx context.Context, req *types.QueryUTXOsRequest) (*types.QueryUTXOsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(goCtx)

utxos := k.GetAllUTXOs(ctx)

return &types.QueryUTXOsResponse{Utxos: utxos}, nil
}

func (k Keeper) QueryUTXOsByAddress(goCtx context.Context, req *types.QueryUTXOsByAddressRequest) (*types.QueryUTXOsByAddressResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(goCtx)

_, err := sdk.AccAddressFromBech32(req.Address)
if err != nil {
return nil, err
}

utxos := k.GetUTXOsByAddr(ctx, req.Address)

return &types.QueryUTXOsByAddressResponse{Utxos: utxos}, nil
}
Loading

0 comments on commit 3f594f2

Please sign in to comment.