/
msg_server.go
111 lines (89 loc) · 3.48 KB
/
msg_server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package keeper
import (
"context"
"crypto/sha256"
"encoding/hex"
"fmt"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/skip-mev/pob/x/builder/types"
)
var _ types.MsgServer = MsgServer{}
// MsgServer is the wrapper for the builder module's msg service.
type MsgServer struct {
Keeper
}
// NewMsgServerImpl returns an implementation of the builder MsgServer interface.
func NewMsgServerImpl(keeper Keeper) *MsgServer {
return &MsgServer{Keeper: keeper}
}
func (m MsgServer) AuctionBid(goCtx context.Context, msg *types.MsgAuctionBid) (*types.MsgAuctionBidResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// This should never return an error because the address was validated when
// the message was ingressed.
bidder, err := sdk.AccAddressFromBech32(msg.Bidder)
if err != nil {
return nil, err
}
params, err := m.GetParams(ctx)
if err != nil {
return nil, err
}
if uint32(len(msg.Transactions)) > params.MaxBundleSize {
return nil, fmt.Errorf("the number of transactions in the bid is greater than the maximum allowed; expected <= %d, got %d", params.MaxBundleSize, len(msg.Transactions))
}
escrowAddress := params.EscrowAccountAddress
var proposerReward sdk.Coins
if params.ProposerFee.IsZero() {
// send the entire bid to the escrow account when no proposer fee is set
if err := m.bankKeeper.SendCoins(ctx, bidder, escrowAddress, sdk.NewCoins(msg.Bid)); err != nil {
return nil, err
}
} else {
rewardsAddress, err := m.rewardsAddressProvider.GetRewardsAddress(ctx)
if err != nil {
// In the case where the rewards address provider returns an error, the
// escrow account will receive the entire bid.
rewardsAddress = escrowAddress
}
// determine the amount of the bid that goes to the (previous) proposer
bid := sdk.NewDecCoinsFromCoins(msg.Bid)
proposerReward, _ = bid.MulDecTruncate(params.ProposerFee).TruncateDecimal()
if err := m.bankKeeper.SendCoins(ctx, bidder, rewardsAddress, proposerReward); err != nil {
return nil, err
}
// Determine the amount of the remaining bid that goes to the escrow account.
// If a decimal remainder exists, it'll stay with the bidding account.
escrowTotal := bid.Sub(sdk.NewDecCoinsFromCoins(proposerReward...))
escrowReward, _ := escrowTotal.TruncateDecimal()
if err := m.bankKeeper.SendCoins(ctx, bidder, escrowAddress, escrowReward); err != nil {
return nil, err
}
}
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.EventAttrProposerReward, proposerReward.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
}