fix ceiling remainder payout + implied auction completion status

This commit is contained in:
2022-09-05 21:18:04 +00:00
parent 6c4ad8c55d
commit cf3b28217b
7 changed files with 61 additions and 105 deletions

View File

@@ -12,7 +12,7 @@ import (
"github.com/pkg/errors"
)
func (k *Keeper) AuctionIsExpired(ctx sdk.Context, auctionId string) (bool, error) {
func (k Keeper) AuctionIsExpired(ctx sdk.Context, auctionId string) (bool, error) {
auction, found := k.GetAuction(ctx, auctionId)
// make sure the auction exists on-chain
@@ -23,7 +23,7 @@ func (k *Keeper) AuctionIsExpired(ctx sdk.Context, auctionId string) (bool, erro
return uint64(ctx.BlockHeight()) >= auction.Deadline, nil
}
func (k *Keeper) FinalizeExpiredAuctions(ctx sdk.Context) {
func (k Keeper) FinalizeExpiredAuctions(ctx sdk.Context) {
memdb.BidDB.ForEachAuction(func(auctionId string) error {
auction, found := k.GetAuction(ctx, auctionId)
// make sure the auction exists on-chain
@@ -42,9 +42,11 @@ func (k *Keeper) FinalizeExpiredAuctions(ctx sdk.Context) {
auction.Remaining = auction.Best.Amount
// clear auction
memdb.BidDB.ClearAuction(auctionId)
if err := memdb.BidDB.ClearAuction(auctionId); err != nil {
return errors.Errorf("failed to clear auction from memcache: %s", err)
}
// pay out remainder to auction creator
// pay out unpaid remainder to auction creator
ceiling := new(big.Int)
ceiling.SetString(auction.Ceiling, 10)
lowestBidAmt := new(big.Int)
@@ -53,12 +55,17 @@ func (k *Keeper) FinalizeExpiredAuctions(ctx sdk.Context) {
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,
})
if err := k.bank.SendCoinsFromModuleToAccount(ctx, types.ModuleName, sdk.AccAddress(auction.Owner), coins); err != nil {
coins := sdk.NewCoins(sdk.NewCoin(
auction.Denom,
sdk.NewIntFromBigInt(amtRemaining),
))
recipAddr, err := sdk.AccAddressFromBech32(auction.Owner)
if err != nil {
return errors.Errorf("failed to parse address %s", auction.Owner)
}
if err := k.bank.SendCoinsFromModuleToAccount(ctx, types.ModuleName, recipAddr, coins); err != nil {
log.Printf("Failed to send coins from module: %s\n", err)
// log.Fatalf("Failed to send coins from module: %s\n", err)
}
}

View File

@@ -26,6 +26,7 @@ func (k msgServer) NewAuction(goCtx context.Context, msg *types.MsgNewAuction) (
Index: index,
Name: msg.Name,
Description: msg.Description,
// best bid -> null
// Best: new(types.Bid),
Deadline: uint64(ctx.BlockHeight()) + auctionconfig.AuctionTime,
Denom: msg.Denom,
@@ -33,8 +34,8 @@ func (k msgServer) NewAuction(goCtx context.Context, msg *types.MsgNewAuction) (
Ceiling: msg.Ceiling,
LeaseStart: uint64(ctx.BlockTime().Unix()),
LeaseEnd: msg.LeaseEnd,
Closed: false,
Remaining: "0",
// remaining payout -> null
// Remaining: "0",
}
senderAddr, err := sdk.AccAddressFromBech32(msg.Creator)
@@ -55,7 +56,7 @@ func (k msgServer) NewAuction(goCtx context.Context, msg *types.MsgNewAuction) (
Denom: auction.Denom,
})
if err := k.bank.SendCoinsFromAccountToModule(ctx, senderAddr, types.ModuleName, coins); err != nil {
if err := k.Keeper.bank.SendCoinsFromAccountToModule(ctx, senderAddr, types.ModuleName, coins); err != nil {
return nil, fmt.Errorf("failed to transfer %s%s", auction.Ceiling, auction.Denom)
}