Skip to content

Commit

Permalink
Merge pull request DongCoNY#45 from onomyprotocol/dong/cli-auction
Browse files Browse the repository at this point in the history
auction: cli tx and query
  • Loading branch information
DongLieu authored Oct 14, 2024
2 parents 3f2588b + 428ade5 commit 4809242
Show file tree
Hide file tree
Showing 12 changed files with 692 additions and 68 deletions.
2 changes: 1 addition & 1 deletion app/app_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
oraclemodulev1 "github.com/onomyprotocol/reserve/api/reserve/oracle/module"
psmmodulev1 "github.com/onomyprotocol/reserve/api/reserve/psm/module/v1"
vaultmodulev1 "github.com/onomyprotocol/reserve/api/reserve/vaults/module"
auctionmoduletypes "github.com/onomyprotocol/reserve/x/auction/types"
_ "github.com/onomyprotocol/reserve/x/oracle/module" // import for side-effects

_ "github.com/onomyprotocol/reserve/x/auction/module" // import for side-effects
auctionmoduletypes "github.com/onomyprotocol/reserve/x/auction/types"
oraclemoduletypes "github.com/onomyprotocol/reserve/x/oracle/types"
psmtypes "github.com/onomyprotocol/reserve/x/psm/types"
_ "github.com/onomyprotocol/reserve/x/vaults/module" // import for side-effects
Expand Down
12 changes: 12 additions & 0 deletions proto/reserve/auction/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "amino/amino.proto";
import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "reserve/auction/v1/params.proto";
import "reserve/auction/v1/auction.proto";
import "cosmos_proto/cosmos.proto";

option go_package = "github.com/onomyprotocol/reserve/x/auction/types";
Expand All @@ -15,6 +16,10 @@ service Query {
rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {
option (google.api.http).get = "/reserve/auction/params";
}

rpc QueryAllAuction(QueryAllAuctionRequest) returns (QueryAllAuctionResponse){
option (google.api.http).get = "/reserve/auction/auction";
}
}

// QueryParamsRequest is request type for the Query/Params RPC method.
Expand All @@ -27,3 +32,10 @@ message QueryParamsResponse {
[ (gogoproto.nullable) = false, (amino.dont_omitempty) = true ];
}

message QueryAllAuctionRequest {}

message QueryAllAuctionResponse {
// params holds all the parameters of this module.
repeated Auction auctions = 1
[ (gogoproto.nullable) = false, (amino.dont_omitempty) = true ];
}
5 changes: 4 additions & 1 deletion proto/reserve/auction/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,17 @@ message MsgBid {
// MsgBid message.
message MsgBidResponse {
string response = 1;

uint64 bid_id = 2;
}

// MsgCancelBid is the Msg/CancelBid request type.
message MsgCancelBid {
option (cosmos.msg.v1.signer) = "bidder";
option (amino.name) = "reserve/x/auction/MsgCancelBid";

string bidder = 1;
// bidder is the address that submitting the bid entry.
string bidder = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];

// bid_id is the unique id.
uint64 bid_id = 2;
Expand Down
47 changes: 47 additions & 0 deletions x/auction/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cli

import (
"context"
"fmt"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/spf13/cobra"

"github.com/onomyprotocol/reserve/x/auction/types"
)

func GetQueryCmd() *cobra.Command {
cmd := &cobra.Command{
Use: types.ModuleName,
Short: fmt.Sprintf("Querying commands for the %s module", types.ModuleName),
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2, // nolint:gomnd
RunE: client.ValidateCmd,
}

cmd.AddCommand(CmdQueryAllAuctions())
return cmd
}

func CmdQueryAllAuctions() *cobra.Command {
cmd := &cobra.Command{
Use: "all-auction",
Short: "show all auctions",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

queryClient := types.NewQueryClient(clientCtx)

res, err := queryClient.QueryAllAuction(context.Background(), &types.QueryAllAuctionRequest{})
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
83 changes: 76 additions & 7 deletions x/auction/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@ package cli

import (
"fmt"
"time"
"strconv"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
// "github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/onomyprotocol/reserve/x/auction/types"
)

var DefaultRelativePacketTimeoutTimestamp = uint64((time.Duration(10) * time.Minute).Nanoseconds())

const listSeparator = ","

// GetTxCmd returns the transaction commands for this module.
func GetTxCmd() *cobra.Command {
cmd := &cobra.Command{
Expand All @@ -25,7 +23,78 @@ func GetTxCmd() *cobra.Command {
RunE: client.ValidateCmd,
}

// this line is used by starport scaffolding # 1
cmd.AddCommand(NewBidCmd())
cmd.AddCommand(NewCancelBidCmd())

return cmd
}

func NewBidCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "bid [auction-id] [amount] [recive_rate]",
Args: cobra.ExactArgs(3),
Short: "create vaults ",
Long: `create vaults.
Example:
$ onomyd tx bid 0 1000nomUSD 0.8 --from mykey
`,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
addr := clientCtx.GetFromAddress()

auctionID, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return err
}
amount, err := sdk.ParseCoinNormalized(args[1])
if err != nil {
return err
}
msg := types.NewMsgBid(addr.String(), auctionID, amount, args[2])

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}

func NewCancelBidCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "cancel-bid [bid-id] [auction_id]",
Args: cobra.ExactArgs(2),
Short: "create vaults ",
Long: `create vaults.
Example:
$ onomyd tx cancel-bid 1 0 --from mykey
`,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
addr := clientCtx.GetFromAddress()

auctionID, err := strconv.ParseUint(args[1], 10, 64)
if err != nil {
return err
}

bidID, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return err
}

msg := types.NewMsgCancelBid(addr.String(), bidID, auctionID)

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
31 changes: 29 additions & 2 deletions x/auction/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"context"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"google.golang.org/grpc/codes"
Expand All @@ -13,7 +14,11 @@ import (
var _ types.QueryServer = Querier{}

type Querier struct {
Keeper
k Keeper
}

func NewQueryServerImpl(k Keeper) types.QueryServer {
return Querier{k: k}
}

func (k Querier) Params(goCtx context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
Expand All @@ -22,5 +27,27 @@ func (k Querier) Params(goCtx context.Context, req *types.QueryParamsRequest) (*
}
ctx := sdk.UnwrapSDKContext(goCtx)

return &types.QueryParamsResponse{Params: k.GetParams(ctx)}, nil
return &types.QueryParamsResponse{Params: k.k.GetParams(ctx)}, nil
}

func (k Querier) QueryAllAuction(ctx context.Context, req *types.QueryAllAuctionRequest) (*types.QueryAllAuctionResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

t, _ := k.k.Auctions.Has(ctx, 0)
fmt.Println()
fmt.Println(t)

allAuction := []types.Auction{}

k.k.Auctions.Walk(ctx, nil, func(key uint64, value types.Auction) (stop bool, err error) {
fmt.Println("----1----")
allAuction = append(allAuction, value)
return false, nil
})

return &types.QueryAllAuctionResponse{
Auctions: allAuction,
}, nil
}
1 change: 1 addition & 0 deletions x/auction/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func (k msgServer) Bid(ctx context.Context, msg *types.MsgBid) (*types.MsgBidRes

return &types.MsgBidResponse{
Response: "Bid Accepted",
BidId: newBidId,
}, nil
}

Expand Down
9 changes: 6 additions & 3 deletions x/auction/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ func (a AppModuleBasic) GetTxCmd() *cobra.Command {
return cli.GetTxCmd()
}

// GetQueryCmd returns no root query command for the oracle module.
func (a AppModuleBasic) GetQueryCmd() *cobra.Command {
return cli.GetQueryCmd()
}

// ----------------------------------------------------------------------------
// AppModule
// ----------------------------------------------------------------------------
Expand All @@ -119,9 +124,7 @@ func NewAppModule(
// RegisterServices registers a gRPC query service to respond to the module-specific gRPC queries
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
types.RegisterQueryServer(cfg.QueryServer(), keeper.Querier{
Keeper: am.keeper,
})
types.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServerImpl(am.keeper))
}

// RegisterInvariants registers the invariants of the module. If an invariant deviates from its predicted value, the InvariantRegistry triggers appropriate logic (most often the chain will be halted)
Expand Down
19 changes: 19 additions & 0 deletions x/auction/types/msgs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
package types

import sdk "github.com/cosmos/cosmos-sdk/types"

var (
Query_serviceDesc = _Query_serviceDesc
Msg_serviceDesc = _Msg_serviceDesc
)

func NewMsgBid(addr string, auctionID uint64, amount sdk.Coin, ReciveRate string) MsgBid {
return MsgBid{
Bidder: addr,
AuctionId: auctionID,
ReciveRate: ReciveRate,
Amount: amount,
}
}

func NewMsgCancelBid(bider string, bidID, auctionID uint64) MsgCancelBid {
return MsgCancelBid{
Bidder: bider,
BidId: bidID,
AuctionId: auctionID,
}
}
Loading

0 comments on commit 4809242

Please sign in to comment.