block-sdk/x/auction/keeper/msg_server.go
2024-01-24 09:06:20 -08:00

64 lines
1.8 KiB
Go

package keeper
import (
"context"
"crypto/sha256"
"encoding/hex"
"fmt"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/skip-mev/block-sdk/v2/x/auction/types"
)
var _ types.MsgServer = MsgServer{}
// MsgServer is the wrapper for the auction module's msg service.
type MsgServer struct {
Keeper
}
// NewMsgServerImpl returns an implementation of the auction MsgServer interface.
func NewMsgServerImpl(keeper Keeper) *MsgServer {
return &MsgServer{Keeper: keeper}
}
// AuctionBid is a no-op function that emits an event for the auction bid. The bid is extracted
// in the antehandler.
func (m MsgServer) AuctionBid(goCtx context.Context, msg *types.MsgAuctionBid) (*types.MsgAuctionBidResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
bundledTxHashes := make([]string, len(msg.Transactions))
for i, refTxRaw := range msg.Transactions {
hash := sha256.Sum256(refTxRaw)
bundledTxHashes[i] = hex.EncodeToString(hash[:])
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeAuctionBid,
sdk.NewAttribute(types.EventAttrBidder, msg.Bidder),
sdk.NewAttribute(types.EventAttrBid, msg.Bid.String()),
sdk.NewAttribute(types.EventAttrBundledTxs, strings.Join(bundledTxHashes, ",")),
),
)
return &types.MsgAuctionBidResponse{}, nil
}
func (m MsgServer) UpdateParams(goCtx context.Context, msg *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// ensure that the message signer is the authority
if msg.Authority != m.Keeper.GetAuthority() {
return nil, fmt.Errorf("this message can only be executed by the authority; expected %s, got %s", m.Keeper.GetAuthority(), msg.Authority)
}
if err := m.Keeper.SetParams(ctx, msg.Params); err != nil {
return nil, err
}
return &types.MsgUpdateParamsResponse{}, nil
}