From 552db0322740240346b91f4c93fbee3ee8001de7 Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Tue, 24 Sep 2024 11:12:57 +0530 Subject: [PATCH] Save auction object on releasing funds --- api/cerc/auction/v1/tx.pulsar.go | 3 +- proto/cerc/auction/v1/auction.proto | 6 +-- proto/cerc/auction/v1/tx.proto | 3 +- x/auction/keeper/keeper.go | 75 ++++++++++++++--------------- x/auction/keeper/msg_server.go | 3 -- x/auction/tx.pb.go | 3 +- 6 files changed, 44 insertions(+), 49 deletions(-) diff --git a/api/cerc/auction/v1/tx.pulsar.go b/api/cerc/auction/v1/tx.pulsar.go index 7f679762..6b35dcc4 100644 --- a/api/cerc/auction/v1/tx.pulsar.go +++ b/api/cerc/auction/v1/tx.pulsar.go @@ -5668,7 +5668,8 @@ func (x *MsgReleaseFunds) GetSigner() string { return "" } -// MsgReleaseFundsResponse returns the state of the auction after releasing the funds +// MsgReleaseFundsResponse returns the state of the auction after releasing the +// funds type MsgReleaseFundsResponse struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache diff --git a/proto/cerc/auction/v1/auction.proto b/proto/cerc/auction/v1/auction.proto index 91985750..9aea35fa 100644 --- a/proto/cerc/auction/v1/auction.proto +++ b/proto/cerc/auction/v1/auction.proto @@ -95,9 +95,9 @@ message Auction { // Only applicable in provider auctions int32 num_providers = 15; - bool funds_released = 16 [ - (gogoproto.moretags) = - "json:\"funds_released\" yaml:\"funds_released\"" ]; + bool funds_released = 16 + [ (gogoproto.moretags) = + "json:\"funds_released\" yaml:\"funds_released\"" ]; } // Auctions represent all the auctions in the module diff --git a/proto/cerc/auction/v1/tx.proto b/proto/cerc/auction/v1/tx.proto index c93e1be6..27673d96 100644 --- a/proto/cerc/auction/v1/tx.proto +++ b/proto/cerc/auction/v1/tx.proto @@ -192,7 +192,8 @@ message MsgReleaseFunds { [ (gogoproto.moretags) = "json:\"signer\" yaml:\"signer\"" ]; } -// MsgReleaseFundsResponse returns the state of the auction after releasing the funds +// MsgReleaseFundsResponse returns the state of the auction after releasing the +// funds message MsgReleaseFundsResponse { option (gogoproto.goproto_getters) = false; diff --git a/x/auction/keeper/keeper.go b/x/auction/keeper/keeper.go index babdebfa..040517d4 100644 --- a/x/auction/keeper/keeper.go +++ b/x/auction/keeper/keeper.go @@ -892,16 +892,16 @@ func (k Keeper) pickProviderAuctionWinners(ctx sdk.Context, auction *auctiontype totalAmountPaid := auction.WinningPrice.Amount.Mul(math.NewInt(int64(len(auction.WinnerAddresses)))) creatorLeftOverAmount := sdk.NewCoin(auction.MaxPrice.Denom, totalLockedAmount.Sub(totalAmountPaid)) - creatorAddress, err := sdk.AccAddressFromBech32(auction.OwnerAddress) + ownerAccAddress, err := sdk.AccAddressFromBech32(auction.OwnerAddress) if err != nil { - k.Logger(ctx).Error(fmt.Sprintf("Invalid creatorAddress address. %v", err)) - panic("Invalid creator address.") + k.Logger(ctx).Error(fmt.Sprintf("Invalid auction owner address. %v", err)) + panic("Invalid auction owner address.") } sdkErr := k.bankKeeper.SendCoinsFromModuleToAccount( ctx, auctiontypes.ModuleName, - creatorAddress, + ownerAccAddress, sdk.NewCoins(creatorLeftOverAmount), ) if sdkErr != nil { @@ -920,13 +920,6 @@ func (k Keeper) pickProviderAuctionWinners(ctx sdk.Context, auction *auctiontype } func (k Keeper) ReleaseFunds(ctx sdk.Context, msg auctiontypes.MsgReleaseFunds) (*auctiontypes.Auction, error) { - if has, err := k.HasAuction(ctx, msg.AuctionId); !has { - if err != nil { - return nil, err - } - return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "Auction not found.") - } - auction, err := k.GetAuctionById(ctx, msg.AuctionId) if err != nil { return nil, err @@ -936,43 +929,45 @@ func (k Keeper) ReleaseFunds(ctx sdk.Context, msg auctiontypes.MsgReleaseFunds) return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "Auction kind must be provider.") } - if auction.Status != auctiontypes.AuctionStatusCompleted { - return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "Auction is not completed.") - } - - if auction.Status == auctiontypes.AuctionStatusFundsReleased { - return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "Funds have already been released.") - } - // Only the auction owner can release funds. if msg.Signer != auction.OwnerAddress { return nil, errorsmod.Wrap(sdkerrors.ErrUnauthorized, "Auction owner mismatch.") } - // Process winner accounts (if nobody bids, there won't be a winner). - if len(auction.WinnerAddresses) > 0 { - for _, winnerAddress := range auction.WinnerAddresses { - winnerAccAddress, err := sdk.AccAddressFromBech32(winnerAddress) - if err != nil { - k.Logger(ctx).Error(fmt.Sprintf("Invalid winner address. %v", err)) - panic("Invalid winner address.") - } + if auction.Status != auctiontypes.AuctionStatusCompleted { + return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "Auction is not completed.") + } - // Send winning price to bidders - sdkErr := k.bankKeeper.SendCoinsFromModuleToAccount( - ctx, - auctiontypes.ModuleName, - winnerAccAddress, - sdk.NewCoins(auction.WinningPrice), - ) - if sdkErr != nil { - k.Logger(ctx).Error(fmt.Sprintf("Auction error sending funds to winner: %v", sdkErr)) - panic(sdkErr) - } + if auction.Status == auctiontypes.AuctionStatusFundsReleased { + return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "Auction funds already released.") + } + + // Mark funds as released in the stored auction + auction.FundsReleased = true + if err = k.SaveAuction(ctx, &auction); err != nil { + return nil, err + } + + // Process winner accounts. + for _, winnerAddress := range auction.WinnerAddresses { + winnerAccAddress, err := sdk.AccAddressFromBech32(winnerAddress) + if err != nil { + k.Logger(ctx).Error(fmt.Sprintf("Invalid winner address. %v", err)) + panic("Invalid winner address.") + } + + // Send winning price to winning bidders + sdkErr := k.bankKeeper.SendCoinsFromModuleToAccount( + ctx, + auctiontypes.ModuleName, + winnerAccAddress, + sdk.NewCoins(auction.WinningPrice), + ) + if sdkErr != nil { + k.Logger(ctx).Error(fmt.Sprintf("Auction error sending funds to winner: %v", sdkErr)) + panic(sdkErr) } } - auction.FundsReleased = true - return &auction, err } diff --git a/x/auction/keeper/msg_server.go b/x/auction/keeper/msg_server.go index 7cc9ff30..68012679 100644 --- a/x/auction/keeper/msg_server.go +++ b/x/auction/keeper/msg_server.go @@ -56,7 +56,6 @@ func (ms msgServer) CreateAuction(c context.Context, msg *auctiontypes.MsgCreate } // CommitBid is the command for committing a bid -// nolint: all func (ms msgServer) CommitBid(c context.Context, msg *auctiontypes.MsgCommitBid) (*auctiontypes.MsgCommitBidResponse, error) { if err := msg.ValidateBasic(); err != nil { return nil, err @@ -94,7 +93,6 @@ func (ms msgServer) CommitBid(c context.Context, msg *auctiontypes.MsgCommitBid) } // RevealBid is the command for revealing a bid -// nolint: all func (ms msgServer) RevealBid(c context.Context, msg *auctiontypes.MsgRevealBid) (*auctiontypes.MsgRevealBidResponse, error) { if err := msg.ValidateBasic(); err != nil { return nil, err @@ -151,7 +149,6 @@ func (ms msgServer) UpdateParams(c context.Context, msg *auctiontypes.MsgUpdateP } // ReleaseFunds is the command to pay the winning amounts to provider auction winners -// nolint: all func (ms msgServer) ReleaseFunds(c context.Context, msg *auctiontypes.MsgReleaseFunds) (*auctiontypes.MsgReleaseFundsResponse, error) { if err := msg.ValidateBasic(); err != nil { return nil, err diff --git a/x/auction/tx.pb.go b/x/auction/tx.pb.go index 1d4946d7..91cbdc5b 100644 --- a/x/auction/tx.pb.go +++ b/x/auction/tx.pb.go @@ -434,7 +434,8 @@ func (m *MsgReleaseFunds) XXX_DiscardUnknown() { var xxx_messageInfo_MsgReleaseFunds proto.InternalMessageInfo -// MsgReleaseFundsResponse returns the state of the auction after releasing the funds +// MsgReleaseFundsResponse returns the state of the auction after releasing the +// funds type MsgReleaseFundsResponse struct { // Auction details Auction *Auction `protobuf:"bytes,1,opt,name=auction,proto3" json:"auction,omitempty" json:"auction" yaml:"auction"`