gpu-compute-chain/x/cosmostest/keeper/auction_expiry.go

64 lines
1.7 KiB
Go
Raw Normal View History

2022-08-31 15:40:28 -07:00
package keeper
import (
"cosmos-test/x/cosmostest/memdb"
"math/big"
2022-08-31 15:40:28 -07:00
"github.com/cosmos/cosmos-sdk/types"
sdk "github.com/cosmos/cosmos-sdk/types"
2022-08-31 15:40:28 -07:00
"github.com/pkg/errors"
)
func (k *Keeper) AuctionIsExpired(ctx types.Context, auctionId string) (bool, error) {
auction, found := k.GetAuction(ctx, auctionId)
// make sure the auction exists on-chain
if !found {
return true, errors.Errorf("auction %s not found", auctionId)
}
return uint64(ctx.BlockHeight()) >= auction.Deadline, nil
}
func (k *Keeper) EndExpiredAuctions(ctx types.Context) {
memdb.BidDB.ForEachAuction(func(auctionId string) error {
auction, found := k.GetAuction(ctx, auctionId)
// make sure the auction exists on-chain
if !found {
return errors.Errorf("auction %s not found on-chain", auctionId)
2022-08-31 15:40:28 -07:00
}
if uint64(ctx.BlockHeight()) >= auction.Deadline {
2022-08-31 15:40:28 -07:00
var err error
auction.Best, err = memdb.BidDB.GetLowestBid(auctionId)
2022-08-31 15:40:28 -07:00
if err != nil {
return errors.Errorf("could not get highest bid for auction %s: %s", auctionId, err)
}
// clear auction
2022-08-31 15:40:28 -07:00
memdb.BidDB.ClearAuction(auctionId)
// pay out remainder to auction creator
ceiling := new(big.Int)
ceiling.SetString(auction.Ceiling, 10)
lowestBidAmt := new(big.Int)
lowestBidAmt.SetString(auction.Best.Amount, 10)
// only pay out if there is a difference
if ceiling.Cmp(lowestBidAmt) == 1 {
amtRemaining := new(big.Int)
amtRemaining.Sub(ceiling, lowestBidAmt)
coins := sdk.NewCoins(sdk.Coin{
Amount: sdk.NewIntFromBigInt(amtRemaining),
Denom: auction.Denom,
})
k.bank.SendCoinsFromModuleToAccount(ctx, "cosmostest", sdk.AccAddress(auction.Owner), coins)
}
// end auction
k.SetAuction(ctx, auction)
2022-08-31 15:40:28 -07:00
}
return nil
})
}