From 55f0f8d980bb1d8983056e4411112c985cc975b7 Mon Sep 17 00:00:00 2001 From: Kevin Davis Date: Wed, 29 Jan 2020 08:42:03 -0600 Subject: [PATCH] Display auction type and phase when querying auctions (#345) * feat: differentiate auction types when queried * feat: display auction type * feat: add phase of collateral auctions * fix: set reverse phase directly * feat: revert base auction, use querying specifc structs * fix: pass auction as interface to handlers * set reverse phase on max bid (#348) * Revert "set reverse phase on max bid (#348)" (#351) This reverts commit 4b855250d529a4cbecb16d9d32b25ffeaffa3a68. * fix: missing return * fix: include collateral auction type * fix: always include phase field for queries Co-authored-by: Denali Marsh --- x/auction/client/cli/query.go | 10 ++++++++-- x/auction/client/rest/query.go | 27 ++++++++++++++++++++++++--- x/auction/keeper/auctions.go | 9 ++++----- x/auction/types/auctions.go | 32 +++++++++++++++++++++++--------- x/auction/types/querier.go | 29 +++++++++++++++++++++++++++-- 5 files changed, 86 insertions(+), 21 deletions(-) diff --git a/x/auction/client/cli/query.go b/x/auction/client/cli/query.go index 96bc15b7..60bf2080 100644 --- a/x/auction/client/cli/query.go +++ b/x/auction/client/cli/query.go @@ -60,7 +60,8 @@ func QueryGetAuctionCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { // Decode and print results var auction types.Auction cdc.MustUnmarshalJSON(res, &auction) - return cliCtx.PrintOutput(auction) + auctionWithPhase := types.NewAuctionWithPhase(auction) + return cliCtx.PrintOutput(auctionWithPhase) }, } } @@ -83,7 +84,12 @@ func QueryGetAuctionsCmd(queryRoute string, cdc *codec.Codec) *cobra.Command { // Decode and print results var auctions types.Auctions cdc.MustUnmarshalJSON(res, &auctions) - return cliCtx.PrintOutput(auctions) + + var auctionsWithPhase []types.AuctionWithPhase + for _, a := range auctions { + auctionsWithPhase = append(auctionsWithPhase, types.NewAuctionWithPhase(a)) + } + return cliCtx.PrintOutput(auctionsWithPhase) }, } } diff --git a/x/auction/client/rest/query.go b/x/auction/client/rest/query.go index 3a315445..97e27aa7 100644 --- a/x/auction/client/rest/query.go +++ b/x/auction/client/rest/query.go @@ -51,10 +51,18 @@ func queryAuctionHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) return } - + var auction types.Auction + err = cliCtx.Codec.UnmarshalJSON(res, auction) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } // Decode and return results cliCtx = cliCtx.WithHeight(height) - rest.PostProcessResponse(w, cliCtx, res) + + auctionWithPhase := types.NewAuctionWithPhase(auction) + + rest.PostProcessResponse(w, cliCtx, cliCtx.Codec.MustMarshalJSON(auctionWithPhase)) } } @@ -73,7 +81,20 @@ func queryAuctionsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { } // Return auctions cliCtx = cliCtx.WithHeight(height) - rest.PostProcessResponse(w, cliCtx, res) + + var auctions types.Auctions + err = cliCtx.Codec.UnmarshalJSON(res, auctions) + if err != nil { + rest.WriteErrorResponse(w, http.StatusNotFound, err.Error()) + return + } + + var auctionsWithPhase []types.AuctionWithPhase + for _, a := range auctions { + auctionsWithPhase = append(auctionsWithPhase, types.NewAuctionWithPhase(a)) + } + + rest.PostProcessResponse(w, cliCtx, cliCtx.Codec.MustMarshalJSON(auctionsWithPhase)) } } diff --git a/x/auction/keeper/auctions.go b/x/auction/keeper/auctions.go index b8079c20..e192a503 100644 --- a/x/auction/keeper/auctions.go +++ b/x/auction/keeper/auctions.go @@ -32,7 +32,7 @@ func (k Keeper) StartSurplusAuction(ctx sdk.Context, seller string, lot sdk.Coin sdk.NewEvent( types.EventTypeAuctionStart, sdk.NewAttribute(types.AttributeKeyAuctionID, fmt.Sprintf("%d", auction.GetID())), - sdk.NewAttribute(types.AttributeKeyAuctionType, auction.Name()), + sdk.NewAttribute(types.AttributeKeyAuctionType, auction.GetType()), sdk.NewAttribute(types.AttributeKeyBidDenom, auction.Bid.Denom), sdk.NewAttribute(types.AttributeKeyLotDenom, auction.Lot.Denom), ), @@ -70,7 +70,7 @@ func (k Keeper) StartDebtAuction(ctx sdk.Context, buyer string, bid sdk.Coin, in sdk.NewEvent( types.EventTypeAuctionStart, sdk.NewAttribute(types.AttributeKeyAuctionID, fmt.Sprintf("%d", auction.GetID())), - sdk.NewAttribute(types.AttributeKeyAuctionType, auction.Name()), + sdk.NewAttribute(types.AttributeKeyAuctionType, auction.GetType()), sdk.NewAttribute(types.AttributeKeyBidDenom, auction.Bid.Denom), sdk.NewAttribute(types.AttributeKeyLotDenom, auction.Lot.Denom), ), @@ -111,7 +111,7 @@ func (k Keeper) StartCollateralAuction(ctx sdk.Context, seller string, lot sdk.C sdk.NewEvent( types.EventTypeAuctionStart, sdk.NewAttribute(types.AttributeKeyAuctionID, fmt.Sprintf("%d", auction.GetID())), - sdk.NewAttribute(types.AttributeKeyAuctionType, auction.Name()), + sdk.NewAttribute(types.AttributeKeyAuctionType, auction.GetType()), sdk.NewAttribute(types.AttributeKeyBidDenom, auction.Bid.Denom), sdk.NewAttribute(types.AttributeKeyLotDenom, auction.Lot.Denom), ), @@ -506,9 +506,8 @@ func (k Keeper) CloseExpiredAuctions(ctx sdk.Context) sdk.Error { func earliestTime(t1, t2 time.Time) time.Time { if t1.Before(t2) { return t1 - } else { - return t2 // also returned if times are equal } + return t2 // also returned if times are equal } // splitCoinIntoWeightedBuckets divides up some amount of coins according to some weights. diff --git a/x/auction/types/auctions.go b/x/auction/types/auctions.go index 6391138e..a9b0b3a5 100644 --- a/x/auction/types/auctions.go +++ b/x/auction/types/auctions.go @@ -22,6 +22,7 @@ type Auction interface { GetBidder() sdk.AccAddress GetBid() sdk.Coin GetEndTime() time.Time + GetType() string } // Auctions is a slice of auctions. @@ -57,6 +58,9 @@ func (a BaseAuction) GetBid() sdk.Coin { return a.Bid } // GetEndTime is a getter for auction end time. func (a BaseAuction) GetEndTime() time.Time { return a.EndTime } +// GetType returns theauction type. Used to identify auctions in event attributes. +func (a BaseAuction) GetType() string { return "base" } + // Validate verifies that the auction end time is before max end time func (a BaseAuction) Validate() error { if a.EndTime.After(a.MaxEndTime) { @@ -82,14 +86,14 @@ func (a BaseAuction) String() string { // SurplusAuction is a forward auction that burns what it receives from bids. // It is normally used to sell off excess pegged asset acquired by the CDP system. type SurplusAuction struct { - BaseAuction `json:"base_auction" yaml:"base_auction"` + BaseAuction } // WithID returns an auction with the ID set. func (a SurplusAuction) WithID(id uint64) Auction { a.ID = id; return a } -// Name returns a name for this auction type. Used to identify auctions in event attributes. -func (a SurplusAuction) Name() string { return "surplus" } +// GetType returns the auction type. Used to identify auctions in event attributes. +func (a SurplusAuction) GetType() string { return "surplus" } // GetModuleAccountCoins returns the total number of coins held in the module account for this auction. // It is used in genesis initialize the module account correctly. @@ -116,15 +120,16 @@ func NewSurplusAuction(seller string, lot sdk.Coin, bidDenom string, endTime tim // DebtAuction is a reverse auction that mints what it pays out. // It is normally used to acquire pegged asset to cover the CDP system's debts that were not covered by selling collateral. type DebtAuction struct { - BaseAuction `json:"base_auction" yaml:"base_auction"` + BaseAuction + CorrespondingDebt sdk.Coin `json:"corresponding_debt" yaml:"corresponding_debt"` } // WithID returns an auction with the ID set. func (a DebtAuction) WithID(id uint64) Auction { a.ID = id; return a } -// Name returns a name for this auction type. Used to identify auctions in event attributes. -func (a DebtAuction) Name() string { return "debt" } +// GetType returns the auction type. Used to identify auctions in event attributes. +func (a DebtAuction) GetType() string { return "debt" } // GetModuleAccountCoins returns the total number of coins held in the module account for this auction. // It is used in genesis initialize the module account correctly. @@ -160,7 +165,8 @@ func NewDebtAuction(buyerModAccName string, bid sdk.Coin, initialLot sdk.Coin, e // Unsold Lot is sent to LotReturns, being divided among the addresses by weight. // Collateral auctions are normally used to sell off collateral seized from CDPs. type CollateralAuction struct { - BaseAuction `json:"base_auction" yaml:"base_auction"` + BaseAuction + CorrespondingDebt sdk.Coin `json:"corresponding_debt" yaml:"corresponding_debt"` MaxBid sdk.Coin `json:"max_bid" yaml:"max_bid"` LotReturns WeightedAddresses `json:"lot_returns" yaml:"lot_returns"` @@ -169,8 +175,8 @@ type CollateralAuction struct { // WithID returns an auction with the ID set. func (a CollateralAuction) WithID(id uint64) Auction { a.ID = id; return a } -// Name returns a name for this auction type. Used to identify auctions in event attributes. -func (a CollateralAuction) Name() string { return "collateral" } +// GetType returns the auction type. Used to identify auctions in event attributes. +func (a CollateralAuction) GetType() string { return "collateral" } // GetModuleAccountCoins returns the total number of coins held in the module account for this auction. // It is used in genesis initialize the module account correctly. @@ -185,6 +191,14 @@ func (a CollateralAuction) IsReversePhase() bool { return a.Bid.IsEqual(a.MaxBid) } +// GetPhase returns the phase of a collateral auction +func (a CollateralAuction) GetPhase() string { + if a.IsReversePhase() { + return "reverse" + } + return "forward" +} + func (a CollateralAuction) String() string { return fmt.Sprintf(`Auction %d: Initiator: %s diff --git a/x/auction/types/querier.go b/x/auction/types/querier.go index c8019f10..44b1ca67 100644 --- a/x/auction/types/querier.go +++ b/x/auction/types/querier.go @@ -3,9 +3,9 @@ package types const ( // QueryGetAuction is the query path for querying one auction QueryGetAuction = "auction" - // QueryGetAuction is the query path for querying all auctions + // QueryGetAuctions is the query path for querying all auctions QueryGetAuctions = "auctions" - // QueryGetAuction is the query path for querying the global auction params + // QueryGetParams is the query path for querying the global auction params QueryGetParams = "params" ) @@ -27,3 +27,28 @@ func NewQueryAllAuctionParams(page int, limit int) QueryAllAuctionParams { Limit: limit, } } + +// AuctionWithPhase augmented type for collateral auctions which includes auction phase for querying +type AuctionWithPhase struct { + Auction Auction + + Type string `json:"type" yaml:"type"` + Phase string `json:"phase" yaml:"phase"` +} + +// NewAuctionWithPhase returns new AuctionWithPhase +func NewAuctionWithPhase(a Auction) AuctionWithPhase { + switch auc := a.(type) { + case CollateralAuction: + return AuctionWithPhase{ + Auction: auc, + Type: auc.GetType(), + Phase: auc.GetPhase(), + } + default: + return AuctionWithPhase{ + Auction: auc, + Type: auc.GetType(), + } + } +}