Add service provider auctions #59

Merged
nabarun merged 29 commits from deep-stack/laconicd:iv-service-provider-auction into main 2024-09-25 12:38:50 +00:00
Showing only changes of commit 93f7f17cc0 - Show all commits

View File

@ -825,7 +825,7 @@ func (k Keeper) pickProviderAuctionWinners(ctx sdk.Context, auction *auctiontype
} }
// Sort the valid bids // Sort the valid bids
slices.SortFunc(revealedBids, func(a, b *auctiontypes.Bid) int { slices.SortStableFunc(revealedBids, func(a, b *auctiontypes.Bid) int {
if a.BidAmount.Amount.LT(b.BidAmount.Amount) { if a.BidAmount.Amount.LT(b.BidAmount.Amount) {
return -1 return -1
} else if a.BidAmount.Amount.GT(b.BidAmount.Amount) { } else if a.BidAmount.Amount.GT(b.BidAmount.Amount) {
@ -887,7 +887,6 @@ func (k Keeper) pickProviderAuctionWinners(ctx sdk.Context, auction *auctiontype
} }
// Process winner accounts (if nobody bids, there won't be a winner). // Process winner accounts (if nobody bids, there won't be a winner).
totalLockedAmount := auction.MaxPrice.Amount.Mul(math.NewInt(int64(auction.NumProviders)))
if len(winnerBids) > 0 { if len(winnerBids) > 0 {
for _, bid := range winnerBids { for _, bid := range winnerBids {
winnerAddress, err := sdk.AccAddressFromBech32(bid.BidderAddress) winnerAddress, err := sdk.AccAddressFromBech32(bid.BidderAddress)
@ -903,32 +902,23 @@ func (k Keeper) pickProviderAuctionWinners(ctx sdk.Context, auction *auctiontype
panic(sdkErr) panic(sdkErr)
} }
} }
}
// Send back extra amount to auction creator // Send back any leftover locked amount to auction creator
totalAmountPaid := auction.WinningPrice.Amount.Mul(math.NewInt(int64(len(winnerBids)))) // All of it in case of no winners
extraAmount := sdk.NewCoin(auction.MaxPrice.Denom, totalLockedAmount.Sub(totalAmountPaid)) totalLockedAmount := auction.MaxPrice.Amount.Mul(math.NewInt(int64(auction.NumProviders)))
totalAmountPaid := auction.WinningPrice.Amount.Mul(math.NewInt(int64(len(auction.WinnerAddresses))))
creatorLeftOverAmount := sdk.NewCoin(auction.MaxPrice.Denom, totalLockedAmount.Sub(totalAmountPaid))
sdkErr := k.bankKeeper.SendCoinsFromModuleToAccount( sdkErr := k.bankKeeper.SendCoinsFromModuleToAccount(
ctx, ctx,
auctiontypes.ModuleName, auctiontypes.ModuleName,
sdk.AccAddress(auction.OwnerAddress), sdk.AccAddress(auction.OwnerAddress),
sdk.NewCoins(extraAmount), sdk.NewCoins(creatorLeftOverAmount),
) )
if sdkErr != nil { if sdkErr != nil {
k.Logger(ctx).Error(fmt.Sprintf("Auction error returning extra locked amount: %v", sdkErr)) k.Logger(ctx).Error(fmt.Sprintf("Auction error returning leftover locked amount: %v", sdkErr))
panic(sdkErr) panic(sdkErr)
}
} else {
// Return all locked amount in case of no winners
sdkErr := k.bankKeeper.SendCoinsFromModuleToAccount(
ctx,
auctiontypes.ModuleName,
sdk.AccAddress(auction.OwnerAddress),
sdk.NewCoins(sdk.NewCoin(auction.MaxPrice.Denom, totalLockedAmount)))
if sdkErr != nil {
k.Logger(ctx).Error(fmt.Sprintf("Auction error returning bid amount: %v", sdkErr))
panic(sdkErr)
}
} }
// Notify other modules (hook). // Notify other modules (hook).