-
Notifications
You must be signed in to change notification settings - Fork 19
/
msgs.go
148 lines (115 loc) · 4.1 KB
/
msgs.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package types
import (
"errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)
// bank message types
const (
TypeMsgSend = "send"
TypeMsgMultiSend = "multisend"
TypeMsgSetPlatformPercentage = "setplatformpercentage"
)
var (
_ sdk.Msg = &MsgSend{}
_ sdk.Msg = &MsgMultiSend{}
_ sdk.Msg = &MsgSetPlatformPercentage{}
)
// NewMsgSend - construct a msg to send coins from one account to another.
//
//nolint:interfacer
func NewMsgSend(fromAddr, toAddr sdk.AccAddress, amount sdk.Coins) *MsgSend {
return &MsgSend{FromAddress: fromAddr.String(), ToAddress: toAddr.String(), Amount: amount}
}
// Route Implements Msg.
func (msg MsgSend) Route() string { return RouterKey }
// Type Implements Msg.
func (msg MsgSend) Type() string { return TypeMsgSend }
// ValidateBasic Implements Msg.
func (msg MsgSend) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.FromAddress); err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid from address: %s", err)
}
if _, err := sdk.AccAddressFromBech32(msg.ToAddress); err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid to address: %s", err)
}
if !msg.Amount.IsValid() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, msg.Amount.String())
}
if !msg.Amount.IsAllPositive() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, msg.Amount.String())
}
return nil
}
// GetSignBytes Implements Msg.
func (msg MsgSend) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}
// GetSigners Implements Msg.
func (msg MsgSend) GetSigners() []sdk.AccAddress {
fromAddress, _ := sdk.AccAddressFromBech32(msg.FromAddress)
return []sdk.AccAddress{fromAddress}
}
// NewMsgMultiSend - construct arbitrary multi-in, multi-out send msg.
func NewMsgMultiSend(in []banktypes.Input, out []banktypes.Output) *MsgMultiSend {
return &MsgMultiSend{Inputs: in, Outputs: out}
}
// Route Implements Msg
func (msg MsgMultiSend) Route() string { return RouterKey }
// Type Implements Msg
func (msg MsgMultiSend) Type() string { return TypeMsgMultiSend }
// ValidateBasic Implements Msg.
func (msg MsgMultiSend) ValidateBasic() error {
// this just makes sure the input and all the outputs are properly formatted,
// not that they actually have the money inside
if len(msg.Inputs) == 0 {
return banktypes.ErrNoInputs
}
if len(msg.Inputs) != 1 {
return banktypes.ErrMultipleSenders
}
if len(msg.Outputs) == 0 {
return banktypes.ErrNoOutputs
}
return banktypes.ValidateInputsOutputs(msg.Inputs, msg.Outputs)
}
// GetSignBytes Implements Msg.
func (msg MsgMultiSend) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}
// GetSigners Implements Msg.
func (msg MsgMultiSend) GetSigners() []sdk.AccAddress {
addrs := make([]sdk.AccAddress, len(msg.Inputs))
for i, in := range msg.Inputs {
inAddr, _ := sdk.AccAddressFromBech32(in.Address)
addrs[i] = inAddr
}
return addrs
}
// NewMsgMultiSend - construct arbitrary multi-in, multi-out send msg.
func NewMsgSetPlatformPercentage(percentage uint32) *MsgSetPlatformPercentage {
return &MsgSetPlatformPercentage{PlatformPercentage: percentage}
}
// Route Implements Msg
func (msg MsgSetPlatformPercentage) Route() string { return RouterKey }
// Type Implements Msg
func (msg MsgSetPlatformPercentage) Type() string { return TypeMsgSetPlatformPercentage }
// ValidateBasic Implements Msg.
func (msg MsgSetPlatformPercentage) ValidateBasic() error {
// this just makes sure the input and all the outputs are properly formatted,
// not that they actually have the money inside
if msg.PlatformPercentage > 10000 {
return errors.New("unable to have a platform percentage that exceeds 100%")
}
return nil
}
// GetSignBytes Implements Msg.
func (msg MsgSetPlatformPercentage) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}
// GetSigners Implements Msg.
func (msg MsgSetPlatformPercentage) GetSigners() []sdk.AccAddress {
addr, _ := sdk.AccAddressFromBech32(msg.Authority)
return []sdk.AccAddress{addr}
}