Use stable sort for sorting bids
Some checks failed
Protobuf / lint (pull_request) Successful in 18s
Build / build (pull_request) Successful in 2m11s
E2E Tests / test-e2e (pull_request) Successful in 3m8s
Integration Tests / test-integration (pull_request) Successful in 1m41s
SDK Tests / sdk_tests_nameservice_expiry (pull_request) Successful in 7m19s
SDK Tests / sdk_tests_auctions (pull_request) Failing after 11m52s
Unit Tests / test-unit (pull_request) Successful in 1m49s
SDK Tests / sdk_tests (pull_request) Failing after 9m8s

This commit is contained in:
Prathamesh Musale 2024-09-20 11:54:36 +05:30
parent 3f9b494480
commit 93f7f17cc0

View File

@ -825,7 +825,7 @@ func (k Keeper) pickProviderAuctionWinners(ctx sdk.Context, auction *auctiontype
}
// 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) {
return -1
} 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).
totalLockedAmount := auction.MaxPrice.Amount.Mul(math.NewInt(int64(auction.NumProviders)))
if len(winnerBids) > 0 {
for _, bid := range winnerBids {
winnerAddress, err := sdk.AccAddressFromBech32(bid.BidderAddress)
@ -903,32 +902,23 @@ func (k Keeper) pickProviderAuctionWinners(ctx sdk.Context, auction *auctiontype
panic(sdkErr)
}
}
}
// Send back extra amount to auction creator
totalAmountPaid := auction.WinningPrice.Amount.Mul(math.NewInt(int64(len(winnerBids))))
extraAmount := sdk.NewCoin(auction.MaxPrice.Denom, totalLockedAmount.Sub(totalAmountPaid))
// Send back any leftover locked amount to auction creator
// All of it in case of no winners
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(
ctx,
auctiontypes.ModuleName,
sdk.AccAddress(auction.OwnerAddress),
sdk.NewCoins(extraAmount),
)
if sdkErr != nil {
k.Logger(ctx).Error(fmt.Sprintf("Auction error returning extra locked amount: %v", 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)
}
sdkErr := k.bankKeeper.SendCoinsFromModuleToAccount(
ctx,
auctiontypes.ModuleName,
sdk.AccAddress(auction.OwnerAddress),
sdk.NewCoins(creatorLeftOverAmount),
)
if sdkErr != nil {
k.Logger(ctx).Error(fmt.Sprintf("Auction error returning leftover locked amount: %v", sdkErr))
panic(sdkErr)
}
// Notify other modules (hook).