|
| 1 | +package keeper |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "cosmossdk.io/core/address" |
| 7 | + errorsmod "cosmossdk.io/errors" |
| 8 | + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" |
| 9 | + |
| 10 | + "github.com/cosmos/cosmos-sdk/telemetry" |
| 11 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 12 | + bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" |
| 13 | + "github.com/cosmos/cosmos-sdk/x/bank/types" |
| 14 | + "github.com/hashicorp/go-metrics" |
| 15 | +) |
| 16 | + |
| 17 | +type msgServer struct { |
| 18 | + types.MsgServer |
| 19 | + |
| 20 | + keeper bankkeeper.Keeper |
| 21 | + addressCodec address.Codec |
| 22 | +} |
| 23 | + |
| 24 | +var _ types.MsgServer = msgServer{} |
| 25 | + |
| 26 | +func NewMsgServerImpl(keeper Keeper, addressCodec address.Codec) types.MsgServer { |
| 27 | + return &msgServer{ |
| 28 | + MsgServer: bankkeeper.NewMsgServerImpl(keeper), |
| 29 | + keeper: keeper, |
| 30 | + addressCodec: addressCodec, |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func (k msgServer) Send(goCtx context.Context, msg *types.MsgSend) (*types.MsgSendResponse, error) { |
| 35 | + from, err := k.addressCodec.StringToBytes(msg.FromAddress) |
| 36 | + if err != nil { |
| 37 | + return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid from address: %s", err) |
| 38 | + } |
| 39 | + to, err := k.addressCodec.StringToBytes(msg.ToAddress) |
| 40 | + if err != nil { |
| 41 | + return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid to address: %s", err) |
| 42 | + } |
| 43 | + |
| 44 | + if !msg.Amount.IsValid() { |
| 45 | + return nil, errorsmod.Wrap(sdkerrors.ErrInvalidCoins, msg.Amount.String()) |
| 46 | + } |
| 47 | + |
| 48 | + if !msg.Amount.IsAllPositive() { |
| 49 | + return nil, errorsmod.Wrap(sdkerrors.ErrInvalidCoins, msg.Amount.String()) |
| 50 | + } |
| 51 | + |
| 52 | + ctx := sdk.UnwrapSDKContext(goCtx) |
| 53 | + if err := k.keeper.IsSendEnabledCoins(ctx, msg.Amount...); err != nil { |
| 54 | + return nil, err |
| 55 | + } |
| 56 | + |
| 57 | + if k.keeper.BlockedAddr(to) { |
| 58 | + return nil, errorsmod.Wrapf(sdkerrors.ErrUnauthorized, "%s is not allowed to receive funds", msg.ToAddress) |
| 59 | + } |
| 60 | + |
| 61 | + err = k.keeper.SendCoins(ctx, from, to, msg.Amount) |
| 62 | + if err != nil { |
| 63 | + return nil, err |
| 64 | + } |
| 65 | + |
| 66 | + defer func() { |
| 67 | + for _, a := range msg.Amount { |
| 68 | + if a.Amount.IsInt64() { |
| 69 | + telemetry.SetGaugeWithLabels( |
| 70 | + []string{"tx", "msg", "send"}, |
| 71 | + float32(a.Amount.Int64()), |
| 72 | + []metrics.Label{telemetry.NewLabel("denom", a.Denom)}, |
| 73 | + ) |
| 74 | + } |
| 75 | + } |
| 76 | + }() |
| 77 | + |
| 78 | + return &types.MsgSendResponse{}, nil |
| 79 | +} |
| 80 | + |
| 81 | +func (k msgServer) MultiSend(goCtx context.Context, msg *types.MsgMultiSend) (*types.MsgMultiSendResponse, error) { |
| 82 | + if len(msg.Inputs) == 0 { |
| 83 | + return nil, types.ErrNoInputs |
| 84 | + } |
| 85 | + |
| 86 | + if len(msg.Inputs) != 1 { |
| 87 | + return nil, types.ErrMultipleSenders |
| 88 | + } |
| 89 | + |
| 90 | + if len(msg.Outputs) == 0 { |
| 91 | + return nil, types.ErrNoOutputs |
| 92 | + } |
| 93 | + |
| 94 | + if err := types.ValidateInputOutputs(msg.Inputs[0], msg.Outputs); err != nil { |
| 95 | + return nil, err |
| 96 | + } |
| 97 | + |
| 98 | + ctx := sdk.UnwrapSDKContext(goCtx) |
| 99 | + |
| 100 | + // NOTE: totalIn == totalOut should already have been checked |
| 101 | + for _, in := range msg.Inputs { |
| 102 | + if err := k.keeper.IsSendEnabledCoins(ctx, in.Coins...); err != nil { |
| 103 | + return nil, err |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + for _, out := range msg.Outputs { |
| 108 | + accAddr, err := k.addressCodec.StringToBytes(out.Address) |
| 109 | + if err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + |
| 113 | + if k.keeper.BlockedAddr(accAddr) { |
| 114 | + return nil, errorsmod.Wrapf(sdkerrors.ErrUnauthorized, "%s is not allowed to receive funds", out.Address) |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + err := k.keeper.InputOutputCoins(ctx, msg.Inputs[0], msg.Outputs) |
| 119 | + if err != nil { |
| 120 | + return nil, err |
| 121 | + } |
| 122 | + |
| 123 | + return &types.MsgMultiSendResponse{}, nil |
| 124 | +} |
0 commit comments