mirror of
				https://github.com/0glabs/0g-chain.git
				synced 2025-11-04 02:17:31 +00:00 
			
		
		
		
	replace endTime type
This commit is contained in:
		
							parent
							
								
									d8a428e1d8
								
							
						
					
					
						commit
						231aa75774
					
				@ -3,6 +3,7 @@ package keeper
 | 
			
		||||
import (
 | 
			
		||||
	"bytes"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"github.com/cosmos/cosmos-sdk/codec"
 | 
			
		||||
	sdk "github.com/cosmos/cosmos-sdk/types"
 | 
			
		||||
@ -21,7 +22,7 @@ type Keeper struct {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// NewKeeper returns a new auction keeper.
 | 
			
		||||
func NewKeeper(cdc *codec.Codec, storeKey sdk.StoreKey, bankKeeper types.BankKeeper, supplyKeeper types.SupplyKeeper, paramstore subspace.Subspace) Keeper {
 | 
			
		||||
func NewKeeper(cdc *codec.Codec, storeKey sdk.StoreKey, supplyKeeper types.SupplyKeeper, paramstore subspace.Subspace) Keeper {
 | 
			
		||||
	return Keeper{
 | 
			
		||||
		supplyKeeper:  supplyKeeper,
 | 
			
		||||
		storeKey:      storeKey,
 | 
			
		||||
@ -33,7 +34,7 @@ func NewKeeper(cdc *codec.Codec, storeKey sdk.StoreKey, bankKeeper types.BankKee
 | 
			
		||||
// StartForwardAuction starts a normal auction. Known as flap in maker.
 | 
			
		||||
func (k Keeper) StartForwardAuction(ctx sdk.Context, seller string, lot sdk.Coin, bidDenom string) (types.ID, sdk.Error) {
 | 
			
		||||
	// create auction
 | 
			
		||||
	auction := types.NewForwardAuction(seller, lot, bidDenom, types.EndTime(ctx.BlockHeight())+types.DefaultMaxAuctionDuration)
 | 
			
		||||
	auction := types.NewForwardAuction(seller, lot, bidDenom, ctx.BlockTime().Add(types.DefaultMaxAuctionDuration))
 | 
			
		||||
 | 
			
		||||
	// take coins from module account
 | 
			
		||||
	err := k.supplyKeeper.SendCoinsFromModuleToModule(ctx, seller, types.ModuleName, sdk.NewCoins(lot))
 | 
			
		||||
@ -41,7 +42,7 @@ func (k Keeper) StartForwardAuction(ctx sdk.Context, seller string, lot sdk.Coin
 | 
			
		||||
		return 0, err
 | 
			
		||||
	}
 | 
			
		||||
	// store the auction
 | 
			
		||||
	auctionID, err := k.storeNewAuction(ctx, auction) // TODO does this need to be a pointer to satisfy the interface
 | 
			
		||||
	auctionID, err := k.storeNewAuction(ctx, auction) // TODO does this need to be a pointer to satisfy the interface?
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return 0, err
 | 
			
		||||
	}
 | 
			
		||||
@ -51,7 +52,7 @@ func (k Keeper) StartForwardAuction(ctx sdk.Context, seller string, lot sdk.Coin
 | 
			
		||||
// StartReverseAuction starts an auction where sellers compete by offering decreasing prices. Known as flop in maker.
 | 
			
		||||
func (k Keeper) StartReverseAuction(ctx sdk.Context, buyer string, bid sdk.Coin, initialLot sdk.Coin) (types.ID, sdk.Error) {
 | 
			
		||||
	// create auction
 | 
			
		||||
	auction := types.NewReverseAuction(buyer, bid, initialLot, types.EndTime(ctx.BlockHeight())+types.DefaultMaxAuctionDuration)
 | 
			
		||||
	auction := types.NewReverseAuction(buyer, bid, initialLot, ctx.BlockTime().Add(types.DefaultMaxAuctionDuration))
 | 
			
		||||
 | 
			
		||||
	// This auction type mints coins at close. Need to check module account has minting privileges to avoid potential err in endblocker.
 | 
			
		||||
	macc := k.supplyKeeper.GetModuleAccount(ctx, buyer)
 | 
			
		||||
@ -69,7 +70,7 @@ func (k Keeper) StartReverseAuction(ctx sdk.Context, buyer string, bid sdk.Coin,
 | 
			
		||||
// StartForwardReverseAuction starts an auction where bidders bid up to a maxBid, then switch to bidding down on price. Known as flip in maker.
 | 
			
		||||
func (k Keeper) StartForwardReverseAuction(ctx sdk.Context, seller string, lot sdk.Coin, maxBid sdk.Coin, otherPerson sdk.AccAddress) (types.ID, sdk.Error) {
 | 
			
		||||
	// create auction
 | 
			
		||||
	auction := types.NewForwardReverseAuction(seller, lot, types.EndTime(ctx.BlockHeight())+types.DefaultMaxAuctionDuration, maxBid, otherPerson)
 | 
			
		||||
	auction := types.NewForwardReverseAuction(seller, lot, ctx.BlockTime().Add(types.DefaultMaxAuctionDuration), maxBid, otherPerson)
 | 
			
		||||
 | 
			
		||||
	// take coins from module account
 | 
			
		||||
	err := k.supplyKeeper.SendCoinsFromModuleToModule(ctx, seller, types.ModuleName, sdk.Coins{lot})
 | 
			
		||||
@ -100,8 +101,6 @@ func (k Keeper) storeNewAuction(ctx sdk.Context, auction types.Auction) (types.I
 | 
			
		||||
	return newAuctionID, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ==============================================================================================================================
 | 
			
		||||
 | 
			
		||||
// PlaceBid places a bid on any auction.
 | 
			
		||||
func (k Keeper) PlaceBid(ctx sdk.Context, auctionID types.ID, bidder sdk.AccAddress, bid sdk.Coin, lot sdk.Coin) sdk.Error {
 | 
			
		||||
 | 
			
		||||
@ -112,7 +111,7 @@ func (k Keeper) PlaceBid(ctx sdk.Context, auctionID types.ID, bidder sdk.AccAddr
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// check end time
 | 
			
		||||
	if ctx.BlockHeight() > auction.GetEndTime() {
 | 
			
		||||
	if ctx.BlockTime().After(auction.GetEndTime()) {
 | 
			
		||||
		return sdk.ErrInternal("auction has closed")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@ -139,7 +138,7 @@ func (k Keeper) PlaceBid(ctx sdk.Context, auctionID types.ID, bidder sdk.AccAddr
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// store updated auction
 | 
			
		||||
	k.SetAuction(ctx, a) // maybe move into above funcs
 | 
			
		||||
	k.SetAuction(ctx, a) // TODO maybe move into above funcs
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
@ -180,7 +179,7 @@ func (k Keeper) PlaceBidForward(ctx sdk.Context, a types.ForwardAuction, bidder
 | 
			
		||||
	a.Bidder = bidder
 | 
			
		||||
	a.Bid = bid
 | 
			
		||||
	// increment timeout
 | 
			
		||||
	a.EndTime = EndTime(min(int64(ctx.BlockHeight()+types.DefaultMaxBidDuration), int64(a.MaxEndTime)))
 | 
			
		||||
	a.EndTime = earliestTime(ctx.BlockTime().Add(types.DefaultMaxBidDuration), a.MaxEndTime) // TODO write a min func for time types
 | 
			
		||||
 | 
			
		||||
	return a, nil
 | 
			
		||||
}
 | 
			
		||||
@ -242,7 +241,7 @@ func (k Keeper) PlaceBidForwardReverse(ctx sdk.Context, a types.ForwardReverseAu
 | 
			
		||||
	a.Lot = lot
 | 
			
		||||
	a.Bid = bid
 | 
			
		||||
	// increment timeout
 | 
			
		||||
	a.EndTime = EndTime(min(int64(currentBlockHeight+DefaultMaxBidDuration), int64(a.MaxEndTime)))
 | 
			
		||||
	a.EndTime = earliestTime(ctx.BlockTime().Add(types.DefaultMaxBidDuration), a.MaxEndTime)
 | 
			
		||||
 | 
			
		||||
	return types.ForwardReverseAuction{}, nil
 | 
			
		||||
}
 | 
			
		||||
@ -276,13 +275,11 @@ func (k Keeper) PlaceBidReverse(ctx sdk.Context, a types.ReverseAuction, bidder
 | 
			
		||||
	a.Bidder = bidder
 | 
			
		||||
	a.Lot = lot
 | 
			
		||||
	// increment timeout
 | 
			
		||||
	a.EndTime = EndTime(min(int64(ctx.BlockHeight()+types.DefaultMaxBidDuration), int64(a.MaxEndTime)))
 | 
			
		||||
	a.EndTime = earliestTime(ctx.BlockTime().Add(types.DefaultMaxBidDuration), a.MaxEndTime)
 | 
			
		||||
 | 
			
		||||
	return a, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ==========================================================================================================
 | 
			
		||||
 | 
			
		||||
// CloseAuction closes an auction and distributes funds to the highest bidder.
 | 
			
		||||
func (k Keeper) CloseAuction(ctx sdk.Context, auctionID types.ID) sdk.Error {
 | 
			
		||||
 | 
			
		||||
@ -292,8 +289,8 @@ func (k Keeper) CloseAuction(ctx sdk.Context, auctionID types.ID) sdk.Error {
 | 
			
		||||
		return sdk.ErrInternal("auction doesn't exist")
 | 
			
		||||
	}
 | 
			
		||||
	// error if auction has not reached the end time
 | 
			
		||||
	if ctx.BlockHeight() < int64(auction.GetEndTime()) {
 | 
			
		||||
		return sdk.ErrInternal(fmt.Sprintf("auction can't be closed as curent block height (%v) is under auction end time (%v)", ctx.BlockHeight(), auction.GetEndTime()))
 | 
			
		||||
	if ctx.BlockTime().Before(auction.GetEndTime()) {
 | 
			
		||||
		return sdk.ErrInternal(fmt.Sprintf("auction can't be closed as curent block time (%v) is under auction end time (%v)", ctx.BlockTime(), auction.GetEndTime()))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// payout to the last bidder
 | 
			
		||||
@ -330,19 +327,18 @@ func (k Keeper) MintAndPayoutAuctionLot(ctx sdk.Context, a types.ReverseAuction)
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
func (k Keeper) PayoutAuctionLot(ctx sdk.Context, a types.Auction) sdk.Error {
 | 
			
		||||
	err := k.supplyKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, a.GetBid(), sdk.NewCoins(a.GetLot()))
 | 
			
		||||
	err := k.supplyKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, a.GetBidder(), sdk.NewCoins(a.GetLot()))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// =====================================================================================================================
 | 
			
		||||
// ---------- Store methods ----------
 | 
			
		||||
// Use these to add and remove auction from the store.
 | 
			
		||||
 | 
			
		||||
// getNextAuctionID gets the next available global AuctionID
 | 
			
		||||
func (k Keeper) getNextAuctionID(ctx sdk.Context) (types.ID, sdk.Error) { // TODO don't need error return here
 | 
			
		||||
func (k Keeper) getNextAuctionID(ctx sdk.Context) (types.ID, sdk.Error) {
 | 
			
		||||
	// get next ID from store
 | 
			
		||||
	store := ctx.KVStore(k.storeKey)
 | 
			
		||||
	bz := store.Get(k.getNextAuctionIDKey())
 | 
			
		||||
@ -433,7 +429,7 @@ func (k Keeper) getAuctionKey(auctionID types.ID) []byte {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Inserts a AuctionID into the queue at endTime
 | 
			
		||||
func (k Keeper) InsertIntoQueue(ctx sdk.Context, endTime types.EndTime, auctionID types.ID) {
 | 
			
		||||
func (k Keeper) InsertIntoQueue(ctx sdk.Context, endTime time.Time, auctionID types.ID) {
 | 
			
		||||
	// get the store
 | 
			
		||||
	store := ctx.KVStore(k.storeKey)
 | 
			
		||||
	// marshal thing to be inserted
 | 
			
		||||
@ -446,13 +442,13 @@ func (k Keeper) InsertIntoQueue(ctx sdk.Context, endTime types.EndTime, auctionI
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// removes an auctionID from the queue
 | 
			
		||||
func (k Keeper) removeFromQueue(ctx sdk.Context, endTime types.EndTime, auctionID types.ID) {
 | 
			
		||||
func (k Keeper) removeFromQueue(ctx sdk.Context, endTime time.Time, auctionID types.ID) {
 | 
			
		||||
	store := ctx.KVStore(k.storeKey)
 | 
			
		||||
	store.Delete(getQueueElementKey(endTime, auctionID))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Returns an iterator for all the auctions in the queue that expire by endTime
 | 
			
		||||
func (k Keeper) GetQueueIterator(ctx sdk.Context, endTime types.EndTime) sdk.Iterator { // TODO rename to "getAuctionsByExpiry" ?
 | 
			
		||||
func (k Keeper) GetQueueIterator(ctx sdk.Context, endTime time.Time) sdk.Iterator { // TODO rename to "getAuctionsByExpiry" ?
 | 
			
		||||
	// get store
 | 
			
		||||
	store := ctx.KVStore(k.storeKey)
 | 
			
		||||
	// get an interator
 | 
			
		||||
@ -472,7 +468,7 @@ var queueKeyPrefix = []byte("queue")
 | 
			
		||||
var keyDelimiter = []byte(":")
 | 
			
		||||
 | 
			
		||||
// Returns half a key for an auctionID in the queue, it missed the id off the end
 | 
			
		||||
func getQueueElementKeyPrefix(endTime types.EndTime) []byte {
 | 
			
		||||
func getQueueElementKeyPrefix(endTime time.Time) []byte {
 | 
			
		||||
	return bytes.Join([][]byte{
 | 
			
		||||
		queueKeyPrefix,
 | 
			
		||||
		sdk.Uint64ToBigEndian(uint64(endTime)), // TODO check this gives correct ordering
 | 
			
		||||
@ -480,7 +476,7 @@ func getQueueElementKeyPrefix(endTime types.EndTime) []byte {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Returns the key for an auctionID in the queue
 | 
			
		||||
func getQueueElementKey(endTime types.EndTime, auctionID types.ID) []byte {
 | 
			
		||||
func getQueueElementKey(endTime time.Time, auctionID types.ID) []byte {
 | 
			
		||||
	return bytes.Join([][]byte{
 | 
			
		||||
		queueKeyPrefix,
 | 
			
		||||
		sdk.Uint64ToBigEndian(uint64(endTime)), // TODO check this gives correct ordering
 | 
			
		||||
 | 
			
		||||
@ -3,6 +3,7 @@ package types
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	sdk "github.com/cosmos/cosmos-sdk/types"
 | 
			
		||||
	"github.com/cosmos/cosmos-sdk/x/supply"
 | 
			
		||||
@ -12,9 +13,9 @@ import (
 | 
			
		||||
type Auction interface {
 | 
			
		||||
	GetID() ID
 | 
			
		||||
	SetID(ID)
 | 
			
		||||
	// PlaceBid(currentBlockHeight EndTime, bidder sdk.AccAddress, lot sdk.Coin, bid sdk.Coin) ([]BankOutput, []BankInput, sdk.Error)
 | 
			
		||||
	GetEndTime() EndTime // auctions close at the end of the block with blockheight EndTime (ie bids placed in that block are valid)
 | 
			
		||||
	// GetPayout() BankInput
 | 
			
		||||
	GetBidder() sdk.AccAddress
 | 
			
		||||
	GetLot() sdk.Coin
 | 
			
		||||
	GetEndTime() time.Time
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// BaseAuction type shared by all Auctions
 | 
			
		||||
@ -24,8 +25,8 @@ type BaseAuction struct {
 | 
			
		||||
	Lot        sdk.Coin       // Amount of coins up being given by initiator (FA - amount for sale by seller, RA - cost of good by buyer (bid))
 | 
			
		||||
	Bidder     sdk.AccAddress // Person who bids in the auction. Receiver of Lot. (aka buyer in forward auction, seller in RA)
 | 
			
		||||
	Bid        sdk.Coin       // Amount of coins being given by the bidder (FA - bid, RA - amount being sold)
 | 
			
		||||
	EndTime    EndTime        // Block height at which the auction closes. It closes at the end of this block // TODO change to time type
 | 
			
		||||
	MaxEndTime EndTime        // Maximum closing time. Auctions can close before this but never after.
 | 
			
		||||
	EndTime    time.Time      // Auction closing time. Triggers at the end of the block with time ≥ endTime (bids placed in that block are valid) // TODO ensure everything is consistent with this
 | 
			
		||||
	MaxEndTime time.Time      // Maximum closing time. Auctions can close before this but never after.
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ID type for auction IDs
 | 
			
		||||
@ -40,21 +41,6 @@ func NewIDFromString(s string) (ID, error) {
 | 
			
		||||
	return ID(n), nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// EndTime type for end time of auctions
 | 
			
		||||
type EndTime int64 // TODO rename to Blockheight or don't define custom type
 | 
			
		||||
 | 
			
		||||
// BankInput the input and output types from the bank module where used here. But they use sdk.Coins instad of sdk.Coin. So it caused a lot of type conversion as auction mainly uses sdk.Coin.
 | 
			
		||||
type BankInput struct {
 | 
			
		||||
	Address sdk.AccAddress
 | 
			
		||||
	Coin    sdk.Coin
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// BankOutput output type for auction bids
 | 
			
		||||
type BankOutput struct {
 | 
			
		||||
	Address sdk.AccAddress
 | 
			
		||||
	Coin    sdk.Coin
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetID getter for auction ID
 | 
			
		||||
func (a *BaseAuction) GetID() ID { return a.ID }
 | 
			
		||||
 | 
			
		||||
@ -62,22 +48,13 @@ func (a *BaseAuction) GetID() ID { return a.ID }
 | 
			
		||||
func (a *BaseAuction) SetID(id ID) { a.ID = id }
 | 
			
		||||
 | 
			
		||||
// GetBid getter for auction bid
 | 
			
		||||
func (a *BaseAuction) GetBid() sdk.Coin { return a.Bid }
 | 
			
		||||
func (a *BaseAuction) GetBidder() sdk.AccAddress { return a.Bidder }
 | 
			
		||||
 | 
			
		||||
// GetLot getter for auction lot
 | 
			
		||||
func (a *BaseAuction) GetLot() sdk.Coin { return a.Lot }
 | 
			
		||||
 | 
			
		||||
// GetEndTime getter for auction end time
 | 
			
		||||
func (a *BaseAuction) GetEndTime() EndTime { return a.EndTime }
 | 
			
		||||
 | 
			
		||||
// GetPayout implements Auction
 | 
			
		||||
// func (a BaseAuction) GetPayout() BankInput {
 | 
			
		||||
// 	return BankInput{a.Bidder, a.Lot}
 | 
			
		||||
// }
 | 
			
		||||
 | 
			
		||||
func (e EndTime) String() string {
 | 
			
		||||
	return string(e)
 | 
			
		||||
}
 | 
			
		||||
func (a *BaseAuction) GetEndTime() time.Time { return a.EndTime }
 | 
			
		||||
 | 
			
		||||
func (a *BaseAuction) String() string {
 | 
			
		||||
	return fmt.Sprintf(`Auction %d:
 | 
			
		||||
@ -99,7 +76,7 @@ type ForwardAuction struct {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// NewForwardAuction creates a new forward auction
 | 
			
		||||
func NewForwardAuction(seller string, lot sdk.Coin, bidDenom string, EndTime EndTime) ForwardAuction {
 | 
			
		||||
func NewForwardAuction(seller string, lot sdk.Coin, bidDenom string, EndTime time.Time) ForwardAuction {
 | 
			
		||||
	auction := ForwardAuction{&BaseAuction{
 | 
			
		||||
		// no ID
 | 
			
		||||
		Initiator:  seller,
 | 
			
		||||
@ -113,42 +90,17 @@ func NewForwardAuction(seller string, lot sdk.Coin, bidDenom string, EndTime End
 | 
			
		||||
	return auction
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// PlaceBid implements Auction
 | 
			
		||||
// func (a *ForwardAuction) PlaceBid(currentBlockHeight EndTime, bidder sdk.AccAddress, lot sdk.Coin, bid sdk.Coin) ([]BankOutput, []BankInput, sdk.Error) {
 | 
			
		||||
// 	// TODO check lot size matches lot?
 | 
			
		||||
// 	// check auction has not closed
 | 
			
		||||
// 	if currentBlockHeight > a.EndTime {
 | 
			
		||||
// 		return []BankOutput{}, []BankInput{}, sdk.ErrInternal("auction has closed")
 | 
			
		||||
// 	}
 | 
			
		||||
// 	// check bid is greater than last bid
 | 
			
		||||
// 	if !a.Bid.IsLT(bid) { // TODO add minimum bid size
 | 
			
		||||
// 		return []BankOutput{}, []BankInput{}, sdk.ErrInternal("bid not greater than last bid")
 | 
			
		||||
// 	}
 | 
			
		||||
// 	// calculate coin movements
 | 
			
		||||
// 	outputs := []BankOutput{{bidder, bid}}                                  // new bidder pays bid now
 | 
			
		||||
// 	inputs := []BankInput{{a.Bidder, a.Bid}, {a.Initiator, bid.Sub(a.Bid)}} // old bidder is paid back, extra goes to seller
 | 
			
		||||
 | 
			
		||||
// 	// update auction
 | 
			
		||||
// 	a.Bidder = bidder
 | 
			
		||||
// 	a.Bid = bid
 | 
			
		||||
// 	// increment timeout // TODO into keeper?
 | 
			
		||||
// 	a.EndTime = EndTime(min(int64(currentBlockHeight+DefaultMaxBidDuration), int64(a.MaxEndTime))) // TODO is there a better way to structure these types?
 | 
			
		||||
 | 
			
		||||
// 	return outputs, inputs, nil
 | 
			
		||||
// }
 | 
			
		||||
 | 
			
		||||
// ReverseAuction type for reverse auctions
 | 
			
		||||
// TODO  when exporting state and initializing a new genesis, we'll need a way to differentiate forward from reverse auctions
 | 
			
		||||
type ReverseAuction struct {
 | 
			
		||||
	*BaseAuction
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// NewReverseAuction creates a new reverse auction
 | 
			
		||||
func NewReverseAuction(buyerModAccName string, bid sdk.Coin, initialLot sdk.Coin, EndTime EndTime) ReverseAuction {
 | 
			
		||||
	// Bidder set here receives the proceeds from the first bid placed. This is set to the address of the module account.
 | 
			
		||||
	// When this happens it uses supply.SendCoinsFromModuleToAccount, rather than SendCoinsFromModuleToModule.
 | 
			
		||||
	// Currently not a problem but if extra checks are added to module accounts this will skip them.
 | 
			
		||||
	// TODO description
 | 
			
		||||
func NewReverseAuction(buyerModAccName string, bid sdk.Coin, initialLot sdk.Coin, EndTime time.Time) ReverseAuction {
 | 
			
		||||
	// TODO setting the bidder here is a bit hacky
 | 
			
		||||
	// Needs to be set so that when the first bid is placed, it is paid out to the initiator.
 | 
			
		||||
	// Setting to the module account address bypasses calling supply.SendCoinsFromModuleToModule, instead calls SendCoinsFromModuleToModule. Not a problem currently but if checks/logic regarding modules accounts where added to those methods they would be bypassed.
 | 
			
		||||
	// Alternative: set address to nil, and catch it in an if statement in place bid
 | 
			
		||||
	auction := ReverseAuction{&BaseAuction{
 | 
			
		||||
		// no ID
 | 
			
		||||
		Initiator:  buyerModAccName,
 | 
			
		||||
@ -158,35 +110,9 @@ func NewReverseAuction(buyerModAccName string, bid sdk.Coin, initialLot sdk.Coin
 | 
			
		||||
		EndTime:    EndTime,
 | 
			
		||||
		MaxEndTime: EndTime,
 | 
			
		||||
	}}
 | 
			
		||||
	//output := BankOutput{buyer, initialLot}
 | 
			
		||||
	return auction
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// PlaceBid implements Auction
 | 
			
		||||
// func (a *ReverseAuction) PlaceBid(currentBlockHeight EndTime, bidder sdk.AccAddress, lot sdk.Coin, bid sdk.Coin) ([]BankOutput, []BankInput, sdk.Error) {
 | 
			
		||||
 | 
			
		||||
// 	// check bid size matches bid?
 | 
			
		||||
// 	// check auction has not closed
 | 
			
		||||
// 	if currentBlockHeight > a.EndTime {
 | 
			
		||||
// 		return []BankOutput{}, []BankInput{}, sdk.ErrInternal("auction has closed")
 | 
			
		||||
// 	}
 | 
			
		||||
// 	// check bid is less than last bid
 | 
			
		||||
// 	if !lot.IsLT(a.Lot) { // TODO add min bid decrements
 | 
			
		||||
// 		return []BankOutput{}, []BankInput{}, sdk.ErrInternal("lot not smaller than last lot")
 | 
			
		||||
// 	}
 | 
			
		||||
// 	// calculate coin movements
 | 
			
		||||
// 	outputs := []BankOutput{{bidder, a.Bid}}                                // new bidder pays bid now
 | 
			
		||||
// 	inputs := []BankInput{{a.Bidder, a.Bid}, {a.Initiator, a.Lot.Sub(lot)}} // old bidder is paid back, decrease in price for goes to buyer
 | 
			
		||||
 | 
			
		||||
// 	// update auction
 | 
			
		||||
// 	a.Bidder = bidder
 | 
			
		||||
// 	a.Lot = lot
 | 
			
		||||
// 	// increment timeout // TODO into keeper?
 | 
			
		||||
// 	a.EndTime = EndTime(min(int64(currentBlockHeight+DefaultMaxBidDuration), int64(a.MaxEndTime))) // TODO is there a better way to structure these types?
 | 
			
		||||
 | 
			
		||||
// 	return outputs, inputs, nil
 | 
			
		||||
// }
 | 
			
		||||
 | 
			
		||||
// ForwardReverseAuction type for forward reverse auction
 | 
			
		||||
type ForwardReverseAuction struct {
 | 
			
		||||
	*BaseAuction
 | 
			
		||||
@ -211,7 +137,7 @@ func (a *ForwardReverseAuction) String() string {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// NewForwardReverseAuction creates a new forward reverse auction
 | 
			
		||||
func NewForwardReverseAuction(seller string, lot sdk.Coin, EndTime EndTime, maxBid sdk.Coin, otherPerson sdk.AccAddress) ForwardReverseAuction {
 | 
			
		||||
func NewForwardReverseAuction(seller string, lot sdk.Coin, EndTime time.Time, maxBid sdk.Coin, otherPerson sdk.AccAddress) ForwardReverseAuction {
 | 
			
		||||
	auction := ForwardReverseAuction{
 | 
			
		||||
		BaseAuction: &BaseAuction{
 | 
			
		||||
			// no ID
 | 
			
		||||
@ -227,53 +153,3 @@ func NewForwardReverseAuction(seller string, lot sdk.Coin, EndTime EndTime, maxB
 | 
			
		||||
	//output := BankOutput{seller, lot}
 | 
			
		||||
	return auction
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// PlaceBid implements auction
 | 
			
		||||
// func (a *ForwardReverseAuction) PlaceBid(currentBlockHeight EndTime, bidder sdk.AccAddress, lot sdk.Coin, bid sdk.Coin) (outputs []BankOutput, inputs []BankInput, err sdk.Error) {
 | 
			
		||||
// 	// check auction has not closed
 | 
			
		||||
// 	if currentBlockHeight > a.EndTime {
 | 
			
		||||
// 		return []BankOutput{}, []BankInput{}, sdk.ErrInternal("auction has closed")
 | 
			
		||||
// 	}
 | 
			
		||||
 | 
			
		||||
// 	// determine phase of auction
 | 
			
		||||
// 	switch {
 | 
			
		||||
// 	case a.Bid.IsLT(a.MaxBid) && bid.IsLT(a.MaxBid):
 | 
			
		||||
// 		// Forward auction phase
 | 
			
		||||
// 		if !a.Bid.IsLT(bid) { // TODO add min bid increments
 | 
			
		||||
// 			return []BankOutput{}, []BankInput{}, sdk.ErrInternal("bid not greater than last bid")
 | 
			
		||||
// 		}
 | 
			
		||||
// 		outputs = []BankOutput{{bidder, bid}}                                  // new bidder pays bid now
 | 
			
		||||
// 		inputs = []BankInput{{a.Bidder, a.Bid}, {a.Initiator, bid.Sub(a.Bid)}} // old bidder is paid back, extra goes to seller
 | 
			
		||||
// 	case a.Bid.IsLT(a.MaxBid):
 | 
			
		||||
// 		// Switch over phase
 | 
			
		||||
// 		if !bid.IsEqual(a.MaxBid) { // require bid == a.MaxBid
 | 
			
		||||
// 			return []BankOutput{}, []BankInput{}, sdk.ErrInternal("bid greater than the max bid")
 | 
			
		||||
// 		}
 | 
			
		||||
// 		outputs = []BankOutput{{bidder, bid}} // new bidder pays bid now
 | 
			
		||||
// 		inputs = []BankInput{
 | 
			
		||||
// 			{a.Bidder, a.Bid},               // old bidder is paid back
 | 
			
		||||
// 			{a.Initiator, bid.Sub(a.Bid)},   // extra goes to seller
 | 
			
		||||
// 			{a.OtherPerson, a.Lot.Sub(lot)}, //decrease in price for goes to original CDP owner
 | 
			
		||||
// 		}
 | 
			
		||||
 | 
			
		||||
// 	case a.Bid.IsEqual(a.MaxBid):
 | 
			
		||||
// 		// Reverse auction phase
 | 
			
		||||
// 		if !lot.IsLT(a.Lot) { // TODO add min bid decrements
 | 
			
		||||
// 			return []BankOutput{}, []BankInput{}, sdk.ErrInternal("lot not smaller than last lot")
 | 
			
		||||
// 		}
 | 
			
		||||
// 		outputs = []BankOutput{{bidder, a.Bid}}                                  // new bidder pays bid now
 | 
			
		||||
// 		inputs = []BankInput{{a.Bidder, a.Bid}, {a.OtherPerson, a.Lot.Sub(lot)}} // old bidder is paid back, decrease in price for goes to original CDP owner
 | 
			
		||||
// 	default:
 | 
			
		||||
// 		panic("should never be reached") // TODO
 | 
			
		||||
// 	}
 | 
			
		||||
 | 
			
		||||
// 	// update auction
 | 
			
		||||
// 	a.Bidder = bidder
 | 
			
		||||
// 	a.Lot = lot
 | 
			
		||||
// 	a.Bid = bid
 | 
			
		||||
// 	// increment timeout
 | 
			
		||||
// 	// TODO use bid duration param
 | 
			
		||||
// 	a.EndTime = EndTime(min(int64(currentBlockHeight+DefaultMaxBidDuration), int64(a.MaxEndTime))) // TODO is there a better way to structure these types?
 | 
			
		||||
 | 
			
		||||
// 	return outputs, inputs, nil
 | 
			
		||||
// }
 | 
			
		||||
 | 
			
		||||
@ -3,16 +3,17 @@ package types
 | 
			
		||||
import (
 | 
			
		||||
	"bytes"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"github.com/cosmos/cosmos-sdk/x/params/subspace"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Defaults for auction params
 | 
			
		||||
const (
 | 
			
		||||
	// DefaultMaxAuctionDuration max length of auction, roughly 2 days in blocks
 | 
			
		||||
	DefaultMaxAuctionDuration EndTime = 2 * 24 * 3600 / 5
 | 
			
		||||
	// DefaultMaxAuctionDuration max length of auction
 | 
			
		||||
	DefaultMaxAuctionDuration time.Duration = 2 * 24 * time.Hour
 | 
			
		||||
	// DefaultBidDuration how long an auction gets extended when someone bids, roughly 3 hours in blocks
 | 
			
		||||
	DefaultMaxBidDuration EndTime = 3 * 3600 / 5
 | 
			
		||||
	DefaultMaxBidDuration time.Duration = 3 * time.Hour
 | 
			
		||||
	// DefaultStartingAuctionID what the id of the first auction will be
 | 
			
		||||
	DefaultStartingAuctionID ID = ID(0)
 | 
			
		||||
)
 | 
			
		||||
@ -21,24 +22,24 @@ const (
 | 
			
		||||
var (
 | 
			
		||||
	// ParamStoreKeyAuctionParams Param store key for auction params
 | 
			
		||||
	KeyAuctionBidDuration = []byte("MaxBidDuration")
 | 
			
		||||
	KeyAuctionDuration = []byte("MaxAuctionDuration")
 | 
			
		||||
	KeyAuctionStartingID = []byte("StartingAuctionID")
 | 
			
		||||
	KeyAuctionDuration    = []byte("MaxAuctionDuration")
 | 
			
		||||
	KeyAuctionStartingID  = []byte("StartingAuctionID")
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var _ subspace.ParamSet = &AuctionParams{}
 | 
			
		||||
 | 
			
		||||
// AuctionParams governance parameters for auction module
 | 
			
		||||
type AuctionParams struct {
 | 
			
		||||
	MaxAuctionDuration EndTime `json:"max_auction_duration" yaml:"max_auction_duration"` // max length of auction, in blocks
 | 
			
		||||
	MaxBidDuration        EndTime `json:"max_bid_duration" yaml:"max_bid_duration"`
 | 
			
		||||
	StartingAuctionID  ID      `json:"starting_auction_id" yaml:"starting_auction_id"`
 | 
			
		||||
	MaxAuctionDuration time.Duration `json:"max_auction_duration" yaml:"max_auction_duration"` // max length of auction, in blocks
 | 
			
		||||
	MaxBidDuration     time.Duration `json:"max_bid_duration" yaml:"max_bid_duration"`
 | 
			
		||||
	StartingAuctionID  ID            `json:"starting_auction_id" yaml:"starting_auction_id"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// NewAuctionParams creates a new AuctionParams object
 | 
			
		||||
func NewAuctionParams(maxAuctionDuration EndTime, bidDuration EndTime, startingID ID) AuctionParams {
 | 
			
		||||
func NewAuctionParams(maxAuctionDuration time.Duration, bidDuration time.Duration, startingID ID) AuctionParams {
 | 
			
		||||
	return AuctionParams{
 | 
			
		||||
		MaxAuctionDuration: maxAuctionDuration,
 | 
			
		||||
		MaxBidDuration:        bidDuration,
 | 
			
		||||
		MaxBidDuration:     bidDuration,
 | 
			
		||||
		StartingAuctionID:  startingID,
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@ -85,12 +86,6 @@ func (ap AuctionParams) String() string {
 | 
			
		||||
 | 
			
		||||
// Validate checks that the parameters have valid values.
 | 
			
		||||
func (ap AuctionParams) Validate() error {
 | 
			
		||||
	if ap.MaxAuctionDuration <= EndTime(0) {
 | 
			
		||||
		return fmt.Errorf("max auction duration should be positive, is %s", ap.MaxAuctionDuration)
 | 
			
		||||
	}
 | 
			
		||||
	if ap.MaxBidDuration <= EndTime(0) {
 | 
			
		||||
		return fmt.Errorf("bid duration should be positive, is %s", ap.MaxBidDuration)
 | 
			
		||||
	}
 | 
			
		||||
	if ap.StartingAuctionID <= ID(0) {
 | 
			
		||||
		return fmt.Errorf("starting auction ID should be positive, is %v", ap.StartingAuctionID)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user