2019-12-12 00:16:10 +00:00
package keeper
import (
"fmt"
2019-12-21 01:04:04 +00:00
"time"
2019-12-12 00:16:10 +00:00
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/supply"
"github.com/kava-labs/kava/x/auction/types"
)
2020-01-09 16:09:19 +00:00
// StartSurplusAuction starts a normal auction that mints the sold coins.
func ( k Keeper ) StartSurplusAuction ( ctx sdk . Context , seller string , lot sdk . Coin , bidDenom string ) ( uint64 , sdk . Error ) {
2019-12-12 00:16:10 +00:00
// create auction
2020-01-09 16:09:19 +00:00
auction := types . NewSurplusAuction ( seller , lot , bidDenom , ctx . BlockTime ( ) . Add ( types . DefaultMaxAuctionDuration ) )
2019-12-12 00:16:10 +00:00
// take coins from module account
err := k . supplyKeeper . SendCoinsFromModuleToModule ( ctx , seller , types . ModuleName , sdk . NewCoins ( lot ) )
if err != nil {
return 0 , err
}
// store the auction
2019-12-31 11:56:39 +00:00
auctionID , err := k . StoreNewAuction ( ctx , auction )
2019-12-12 00:16:10 +00:00
if err != nil {
return 0 , err
}
return auctionID , nil
}
2020-01-09 16:09:19 +00:00
// StartDebtAuction starts an auction where sellers compete by offering decreasing prices.
func ( k Keeper ) StartDebtAuction ( ctx sdk . Context , buyer string , bid sdk . Coin , initialLot sdk . Coin ) ( uint64 , sdk . Error ) {
2019-12-12 00:16:10 +00:00
// create auction
2020-01-09 16:09:19 +00:00
auction := types . NewDebtAuction ( buyer , bid , initialLot , ctx . BlockTime ( ) . Add ( types . DefaultMaxAuctionDuration ) )
2019-12-12 00:16:10 +00:00
// 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 )
2020-01-09 13:55:45 +00:00
if ! macc . HasPermission ( supply . Minter ) {
2019-12-12 00:16:10 +00:00
return 0 , sdk . ErrInternal ( "module does not have minting permissions" )
}
// store the auction
2019-12-31 11:56:39 +00:00
auctionID , err := k . StoreNewAuction ( ctx , auction )
2019-12-12 00:16:10 +00:00
if err != nil {
return 0 , err
}
return auctionID , nil
}
2020-01-09 16:09:19 +00:00
// StartCollateralAuction starts an auction where bidders bid up to a maxBid, then switch to bidding down on price.
func ( k Keeper ) StartCollateralAuction ( ctx sdk . Context , seller string , lot sdk . Coin , maxBid sdk . Coin , lotReturnAddrs [ ] sdk . AccAddress , lotReturnWeights [ ] sdk . Int ) ( uint64 , sdk . Error ) {
2019-12-12 00:16:10 +00:00
// create auction
2019-12-31 11:10:15 +00:00
weightedAddresses , err := types . NewWeightedAddresses ( lotReturnAddrs , lotReturnWeights )
if err != nil {
return 0 , err
}
2020-01-09 16:09:19 +00:00
auction := types . NewCollateralAuction ( seller , lot , ctx . BlockTime ( ) . Add ( types . DefaultMaxAuctionDuration ) , maxBid , weightedAddresses )
2019-12-12 00:16:10 +00:00
// take coins from module account
2019-12-31 11:10:15 +00:00
err = k . supplyKeeper . SendCoinsFromModuleToModule ( ctx , seller , types . ModuleName , sdk . NewCoins ( lot ) )
2019-12-12 00:16:10 +00:00
if err != nil {
return 0 , err
}
// store the auction
2019-12-31 11:56:39 +00:00
auctionID , err := k . StoreNewAuction ( ctx , auction )
2019-12-12 00:16:10 +00:00
if err != nil {
return 0 , err
}
return auctionID , nil
}
// PlaceBid places a bid on any auction.
2020-01-09 14:58:47 +00:00
func ( k Keeper ) PlaceBid ( ctx sdk . Context , auctionID uint64 , bidder sdk . AccAddress , newAmount sdk . Coin ) sdk . Error {
2019-12-12 00:16:10 +00:00
// get auction from store
auction , found := k . GetAuction ( ctx , auctionID )
if ! found {
return sdk . ErrInternal ( "auction doesn't exist" )
}
2019-12-28 18:46:53 +00:00
// validate
2019-12-12 00:16:10 +00:00
if ctx . BlockTime ( ) . After ( auction . GetEndTime ( ) ) {
return sdk . ErrInternal ( "auction has closed" )
}
2019-12-28 18:46:53 +00:00
// place bid
2019-12-12 00:16:10 +00:00
var err sdk . Error
2020-01-09 15:43:42 +00:00
var updatedAuction types . Auction
switch a := auction . ( type ) {
2020-01-09 16:09:19 +00:00
case types . SurplusAuction :
if updatedAuction , err = k . PlaceBidSurplus ( ctx , a , bidder , newAmount ) ; err != nil {
2019-12-12 00:16:10 +00:00
return err
}
2020-01-09 16:09:19 +00:00
case types . DebtAuction :
if updatedAuction , err = k . PlaceBidDebt ( ctx , a , bidder , newAmount ) ; err != nil {
2019-12-12 00:16:10 +00:00
return err
}
2020-01-09 16:09:19 +00:00
case types . CollateralAuction :
2020-01-09 15:43:42 +00:00
if ! a . IsReversePhase ( ) {
2020-01-09 16:09:19 +00:00
updatedAuction , err = k . PlaceForwardBidCollateral ( ctx , a , bidder , newAmount )
2020-01-09 13:55:45 +00:00
} else {
2020-01-09 16:09:19 +00:00
updatedAuction , err = k . PlaceReverseBidCollateral ( ctx , a , bidder , newAmount )
2020-01-09 13:55:45 +00:00
}
2019-12-12 00:16:10 +00:00
if err != nil {
return err
}
default :
2019-12-28 17:17:46 +00:00
panic ( fmt . Sprintf ( "unrecognized auction type: %T" , auction ) )
2019-12-12 00:16:10 +00:00
}
// store updated auction
2020-01-09 15:43:42 +00:00
k . SetAuction ( ctx , updatedAuction )
2019-12-12 00:16:10 +00:00
return nil
}
2020-01-09 16:09:19 +00:00
func ( k Keeper ) PlaceBidSurplus ( ctx sdk . Context , a types . SurplusAuction , bidder sdk . AccAddress , bid sdk . Coin ) ( types . SurplusAuction , sdk . Error ) {
2020-01-09 15:43:42 +00:00
// Validate New Bid
2019-12-12 00:16:10 +00:00
if bid . Denom != a . Bid . Denom {
return a , sdk . ErrInternal ( "bid denom doesn't match auction" )
}
if ! a . Bid . IsLT ( bid ) { // TODO add minimum bid size
return a , sdk . ErrInternal ( "bid not greater than last bid" )
}
// Move Coins
2020-01-09 13:55:45 +00:00
if ! bidder . Equals ( a . Bidder ) && ! a . Bid . IsZero ( ) { // catch edge case of someone updating their bid with a low balance, also don't send if amt is zero
// pay back previous bidder
err := k . supplyKeeper . SendCoinsFromAccountToModule ( ctx , bidder , types . ModuleName , sdk . NewCoins ( a . Bid ) )
if err != nil {
return a , err
}
err = k . supplyKeeper . SendCoinsFromModuleToAccount ( ctx , types . ModuleName , a . Bidder , sdk . NewCoins ( a . Bid ) )
if err != nil {
return a , err
}
2019-12-12 00:16:10 +00:00
}
2020-01-09 13:55:45 +00:00
// burn increase in bid
err := k . supplyKeeper . SendCoinsFromAccountToModule ( ctx , bidder , a . Initiator , sdk . NewCoins ( bid . Sub ( a . Bid ) ) )
2019-12-12 00:16:10 +00:00
if err != nil {
return a , err
}
2020-01-09 13:55:45 +00:00
err = k . supplyKeeper . BurnCoins ( ctx , a . Initiator , sdk . NewCoins ( bid . Sub ( a . Bid ) ) )
2019-12-12 00:16:10 +00:00
if err != nil {
return a , err
}
2020-01-09 13:55:45 +00:00
// Update Auction
a . Bidder = bidder
a . Bid = bid
// increment timeout
a . EndTime = earliestTime ( ctx . BlockTime ( ) . Add ( types . DefaultBidDuration ) , a . MaxEndTime )
return a , nil
}
2020-01-09 16:09:19 +00:00
func ( k Keeper ) PlaceForwardBidCollateral ( ctx sdk . Context , a types . CollateralAuction , bidder sdk . AccAddress , bid sdk . Coin ) ( types . CollateralAuction , sdk . Error ) {
2020-01-09 15:43:42 +00:00
// Validate new bid
2020-01-09 14:58:47 +00:00
if bid . Denom != a . Bid . Denom {
return a , sdk . ErrInternal ( "bid denom doesn't match auction" )
}
2020-01-09 13:55:45 +00:00
if a . IsReversePhase ( ) {
return a , sdk . ErrInternal ( "auction is not in forward phase" )
}
if ! a . Bid . IsLT ( bid ) {
return a , sdk . ErrInternal ( "auction in forward phase, new bid not higher than last bid" )
2019-12-12 00:16:10 +00:00
}
2020-01-09 13:55:45 +00:00
if a . MaxBid . IsLT ( bid ) {
return a , sdk . ErrInternal ( "bid higher than max bid" )
}
// Move Coins
// pay back previous bidder
if ! bidder . Equals ( a . Bidder ) && ! a . Bid . IsZero ( ) { // catch edge case of someone updating their bid with a low balance, also don't send if amt is zero
err := k . supplyKeeper . SendCoinsFromAccountToModule ( ctx , bidder , types . ModuleName , sdk . NewCoins ( a . Bid ) )
if err != nil {
return a , err
}
err = k . supplyKeeper . SendCoinsFromModuleToAccount ( ctx , types . ModuleName , a . Bidder , sdk . NewCoins ( a . Bid ) )
if err != nil {
return a , err
}
}
// pay increase in bid to auction initiator
err := k . supplyKeeper . SendCoinsFromAccountToModule ( ctx , bidder , a . Initiator , sdk . NewCoins ( bid . Sub ( a . Bid ) ) )
2019-12-12 00:16:10 +00:00
if err != nil {
return a , err
}
// Update Auction
a . Bidder = bidder
a . Bid = bid
// increment timeout
2019-12-28 22:00:04 +00:00
a . EndTime = earliestTime ( ctx . BlockTime ( ) . Add ( types . DefaultBidDuration ) , a . MaxEndTime )
2019-12-12 00:16:10 +00:00
return a , nil
}
2020-01-09 16:09:19 +00:00
func ( k Keeper ) PlaceReverseBidCollateral ( ctx sdk . Context , a types . CollateralAuction , bidder sdk . AccAddress , lot sdk . Coin ) ( types . CollateralAuction , sdk . Error ) {
2020-01-09 13:55:45 +00:00
// Validate bid
2020-01-09 14:58:47 +00:00
if lot . Denom != a . Lot . Denom {
return a , sdk . ErrInternal ( "lot denom doesn't match auction" )
}
2020-01-09 13:55:45 +00:00
if ! a . IsReversePhase ( ) {
return a , sdk . ErrInternal ( "auction not in reverse phase" )
2019-12-12 00:16:10 +00:00
}
2020-01-09 13:55:45 +00:00
if lot . IsNegative ( ) {
return a , sdk . ErrInternal ( "can't bid negative amount" )
2019-12-12 00:16:10 +00:00
}
2020-01-09 13:55:45 +00:00
if ! lot . IsLT ( a . Lot ) {
return a , sdk . ErrInternal ( "auction in reverse phase, new bid not less than previous amount" )
2019-12-12 00:16:10 +00:00
}
2020-01-09 13:55:45 +00:00
// Move Coins
if ! bidder . Equals ( a . Bidder ) { // catch edge case of someone updating their bid with a low balance
err := k . supplyKeeper . SendCoinsFromAccountToModule ( ctx , bidder , types . ModuleName , sdk . NewCoins ( a . Bid ) )
if err != nil {
return a , err
}
err = k . supplyKeeper . SendCoinsFromModuleToAccount ( ctx , types . ModuleName , a . Bidder , sdk . NewCoins ( a . Bid ) )
if err != nil {
return a , err
}
2019-12-12 00:16:10 +00:00
}
2019-12-31 11:10:15 +00:00
// FIXME paying out rateably to cdp depositors is vulnerable to errors compounding over multiple bids
2020-01-09 13:55:45 +00:00
lotPayouts , err := splitCoinIntoWeightedBuckets ( a . Lot . Sub ( lot ) , a . LotReturns . Weights )
2019-12-12 00:16:10 +00:00
if err != nil {
return a , err
}
2019-12-31 11:10:15 +00:00
for i , payout := range lotPayouts {
err = k . supplyKeeper . SendCoinsFromModuleToAccount ( ctx , types . ModuleName , a . LotReturns . Addresses [ i ] , sdk . NewCoins ( payout ) )
if err != nil {
return a , err
}
}
2019-12-12 00:16:10 +00:00
// Update Auction
a . Bidder = bidder
a . Lot = lot
// increment timeout
2019-12-28 22:00:04 +00:00
a . EndTime = earliestTime ( ctx . BlockTime ( ) . Add ( types . DefaultBidDuration ) , a . MaxEndTime )
2019-12-12 00:16:10 +00:00
2019-12-28 18:46:53 +00:00
return a , nil
2019-12-12 00:16:10 +00:00
}
2020-01-09 13:55:45 +00:00
2020-01-09 16:09:19 +00:00
func ( k Keeper ) PlaceBidDebt ( ctx sdk . Context , a types . DebtAuction , bidder sdk . AccAddress , lot sdk . Coin ) ( types . DebtAuction , sdk . Error ) {
2019-12-12 00:16:10 +00:00
// Validate New Bid
if lot . Denom != a . Lot . Denom {
return a , sdk . ErrInternal ( "lot denom doesn't match auction" )
}
if lot . IsNegative ( ) {
return a , sdk . ErrInternal ( "lot less than 0" )
}
if ! lot . IsLT ( a . Lot ) { // TODO add min bid decrements
return a , sdk . ErrInternal ( "lot not smaller than last lot" )
}
// Move Coins
2020-01-09 13:55:45 +00:00
if ! bidder . Equals ( a . Bidder ) { // catch edge case of someone updating their bid with a low balance
err := k . supplyKeeper . SendCoinsFromAccountToModule ( ctx , bidder , types . ModuleName , sdk . NewCoins ( a . Bid ) )
if err != nil {
return a , err
}
err = k . supplyKeeper . SendCoinsFromModuleToAccount ( ctx , types . ModuleName , a . Bidder , sdk . NewCoins ( a . Bid ) )
if err != nil {
return a , err
}
2019-12-12 00:16:10 +00:00
}
// Update Auction
a . Bidder = bidder
a . Lot = lot
// increment timeout
2019-12-28 22:00:04 +00:00
a . EndTime = earliestTime ( ctx . BlockTime ( ) . Add ( types . DefaultBidDuration ) , a . MaxEndTime )
2019-12-12 00:16:10 +00:00
return a , nil
}
// CloseAuction closes an auction and distributes funds to the highest bidder.
2020-01-01 14:11:19 +00:00
func ( k Keeper ) CloseAuction ( ctx sdk . Context , auctionID uint64 ) sdk . Error {
2019-12-12 00:16:10 +00:00
// get the auction from the store
auction , found := k . GetAuction ( ctx , auctionID )
if ! found {
return sdk . ErrInternal ( "auction doesn't exist" )
}
// error if auction has not reached the end time
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
switch auc := auction . ( type ) {
2020-01-09 16:09:19 +00:00
case types . SurplusAuction :
if err := k . PayoutSurplusAuction ( ctx , auc ) ; err != nil {
2019-12-12 00:16:10 +00:00
return err
}
2020-01-09 16:09:19 +00:00
case types . DebtAuction :
if err := k . PayoutDebtAuction ( ctx , auc ) ; err != nil {
2020-01-09 15:43:42 +00:00
return err
}
2020-01-09 16:09:19 +00:00
case types . CollateralAuction :
if err := k . PayoutCollateralAuction ( ctx , auc ) ; err != nil {
2019-12-12 00:16:10 +00:00
return err
}
default :
panic ( "unrecognized auction type" )
}
k . DeleteAuction ( ctx , auctionID )
return nil
}
2020-01-09 15:43:42 +00:00
2020-01-09 16:09:19 +00:00
func ( k Keeper ) PayoutDebtAuction ( ctx sdk . Context , a types . DebtAuction ) sdk . Error {
2019-12-12 00:16:10 +00:00
err := k . supplyKeeper . MintCoins ( ctx , a . Initiator , sdk . NewCoins ( a . Lot ) )
if err != nil {
return err
}
err = k . supplyKeeper . SendCoinsFromModuleToAccount ( ctx , a . Initiator , a . Bidder , sdk . NewCoins ( a . Lot ) )
if err != nil {
return err
}
return nil
}
2020-01-09 15:43:42 +00:00
2020-01-09 16:09:19 +00:00
func ( k Keeper ) PayoutSurplusAuction ( ctx sdk . Context , a types . SurplusAuction ) sdk . Error {
2020-01-09 15:43:42 +00:00
err := k . supplyKeeper . SendCoinsFromModuleToAccount ( ctx , types . ModuleName , a . Bidder , sdk . NewCoins ( a . Lot ) )
if err != nil {
return err
}
return nil
}
2020-01-09 16:09:19 +00:00
func ( k Keeper ) PayoutCollateralAuction ( ctx sdk . Context , a types . CollateralAuction ) sdk . Error {
2020-01-09 15:43:42 +00:00
err := k . supplyKeeper . SendCoinsFromModuleToAccount ( ctx , types . ModuleName , a . Bidder , sdk . NewCoins ( a . Lot ) )
2019-12-12 00:16:10 +00:00
if err != nil {
return err
}
return nil
}
2019-12-21 01:04:04 +00:00
2019-12-28 18:46:53 +00:00
// earliestTime returns the earliest of two times.
2019-12-21 01:04:04 +00:00
func earliestTime ( t1 , t2 time . Time ) time . Time {
2019-12-28 18:46:53 +00:00
if t1 . Before ( t2 ) {
return t1
} else {
return t2 // also returned if times are equal
}
2019-12-21 01:04:04 +00:00
}
2019-12-31 11:10:15 +00:00
func splitCoinIntoWeightedBuckets ( coin sdk . Coin , buckets [ ] sdk . Int ) ( [ ] sdk . Coin , sdk . Error ) {
for _ , bucket := range buckets {
if bucket . IsNegative ( ) {
return nil , sdk . ErrInternal ( "cannot split coin into bucket with negative weight" )
}
}
amounts := splitIntIntoWeightedBuckets ( coin . Amount , buckets )
result := make ( [ ] sdk . Coin , len ( amounts ) )
for i , a := range amounts {
result [ i ] = sdk . NewCoin ( coin . Denom , a )
}
return result , nil
}