block-sdk/lanes/mev/utils.go
2024-01-24 09:06:20 -08:00

37 lines
1.0 KiB
Go

package mev
import (
"errors"
sdk "github.com/cosmos/cosmos-sdk/types"
auctiontypes "github.com/skip-mev/block-sdk/v2/x/auction/types"
)
// GetMsgAuctionBidFromTx attempts to retrieve a MsgAuctionBid from an sdk.Tx if
// one exists. If a MsgAuctionBid does exist and other messages are also present,
// an error is returned. If no MsgAuctionBid is present, <nil, nil> is returned.
func GetMsgAuctionBidFromTx(tx sdk.Tx) (*auctiontypes.MsgAuctionBid, error) {
auctionBidMsgs := make([]*auctiontypes.MsgAuctionBid, 0)
for _, msg := range tx.GetMsgs() {
t, ok := msg.(*auctiontypes.MsgAuctionBid)
if ok {
auctionBidMsgs = append(auctionBidMsgs, t)
}
}
switch {
case len(auctionBidMsgs) == 0:
// a normal transaction without a MsgAuctionBid message
return nil, nil
case len(auctionBidMsgs) == 1 && len(tx.GetMsgs()) == 1:
// a single MsgAuctionBid message transaction
return auctionBidMsgs[0], nil
default:
// a transaction with at at least one MsgAuctionBid message
return nil, errors.New("invalid MsgAuctionBid transaction")
}
}