2020-09-16 18:58:11 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2020-09-16 18:58:11 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
2022-01-08 00:39:27 +00:00
|
|
|
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
|
2020-09-16 18:58:11 +00:00
|
|
|
|
|
|
|
"github.com/kava-labs/kava/x/auction/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultPage = 1
|
|
|
|
defaultLimit = 100
|
|
|
|
)
|
|
|
|
|
|
|
|
// QueryAuctionByID returns an auction from state if present or falls back to searching old blocks
|
2022-01-08 00:39:27 +00:00
|
|
|
func QueryAuctionByID(cliCtx client.Context, cdc *codec.Codec, queryRoute string, auctionID uint64) (types.Auction, int64, error) {
|
|
|
|
bz, err := cliCtx.LegacyAmino.MarshalJSON(types.NewQueryAuctionParams(auctionID))
|
2020-09-16 18:58:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryGetAuction), bz)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
var auction types.Auction
|
2022-01-08 00:39:27 +00:00
|
|
|
cliCtx.LegacyAmino.MustUnmarshalJSON(res, &auction)
|
2020-09-16 18:58:11 +00:00
|
|
|
|
|
|
|
return auction, height, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil && !strings.Contains(err.Error(), "auction not found") {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, height, err = cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryNextAuctionID), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var nextAuctionID uint64
|
2022-01-08 00:39:27 +00:00
|
|
|
cliCtx.LegacyAmino.MustUnmarshalJSON(res, &nextAuctionID)
|
2020-09-16 18:58:11 +00:00
|
|
|
|
|
|
|
if auctionID >= nextAuctionID {
|
|
|
|
return nil, 0, sdkerrors.Wrapf(types.ErrAuctionNotFound, "%d", auctionID)
|
|
|
|
}
|
|
|
|
|
|
|
|
events := []string{
|
|
|
|
fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, "place_bid"),
|
|
|
|
fmt.Sprintf("%s.%s='%s'", types.EventTypeAuctionBid, types.AttributeKeyAuctionID, []byte(fmt.Sprintf("%d", auctionID))),
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the auction is closed, query for previous bid transactions
|
|
|
|
// note, will only fetch a maximum of 100 bids, so if an auction had more than that this
|
|
|
|
// query may fail to retreive the final state of the auction
|
2022-01-08 00:39:27 +00:00
|
|
|
searchResult, err := authtx.QueryTxsByEvents(cliCtx, events, defaultPage, defaultLimit, "")
|
2020-09-16 18:58:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
maxHeight := int64(0)
|
|
|
|
found := false
|
|
|
|
|
|
|
|
for _, info := range searchResult.Txs {
|
2022-01-08 00:39:27 +00:00
|
|
|
for _, msg := range info.GetTx().GetMsgs() {
|
|
|
|
_, ok := msg.(*types.MsgPlaceBid)
|
|
|
|
if ok {
|
2020-09-16 18:58:11 +00:00
|
|
|
found = true
|
|
|
|
if info.Height > maxHeight {
|
|
|
|
maxHeight = info.Height
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
return nil, 0, sdkerrors.Wrapf(types.ErrAuctionNotFound, "%d", auctionID)
|
|
|
|
}
|
|
|
|
|
|
|
|
queryCLIContext := cliCtx.WithHeight(maxHeight)
|
|
|
|
res, height, err = queryCLIContext.QueryWithData(fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryGetAuction), bz)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode and print results
|
|
|
|
var auction types.Auction
|
2022-01-08 00:39:27 +00:00
|
|
|
cliCtx.LegacyAmino.MustUnmarshalJSON(res, &auction)
|
2020-09-16 18:58:11 +00:00
|
|
|
return auction, height, nil
|
|
|
|
}
|