-
Notifications
You must be signed in to change notification settings - Fork 0
/
msgs.go
273 lines (226 loc) · 7.41 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package types
import (
"math"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/bank"
)
var _ sdk.Msg = MsgSetMemoRequired{}
type MsgSetMemoRequired struct {
Address sdk.AccAddress `json:"address"`
Required bool `json:"required"`
}
func NewMsgSetTransferMemoRequired(addr sdk.AccAddress, required bool) MsgSetMemoRequired {
return MsgSetMemoRequired{Address: addr, Required: required}
}
func (msg *MsgSetMemoRequired) SetAccAddress(addr sdk.AccAddress) {
msg.Address = addr
}
// --------------------------------------------------------
// sdk.Msg Implementation
func (msg MsgSetMemoRequired) Route() string { return RouterKey }
func (msg MsgSetMemoRequired) Type() string { return "set_memo_required" }
func (msg MsgSetMemoRequired) ValidateBasic() sdk.Error {
if msg.Address.Empty() {
return sdk.ErrInvalidAddress("missing address")
}
return nil
}
func (msg MsgSetMemoRequired) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
}
func (msg MsgSetMemoRequired) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Address}
}
var _ sdk.Msg = MsgSend{}
type MsgSend struct {
FromAddress sdk.AccAddress `json:"from_address"`
ToAddress sdk.AccAddress `json:"to_address"`
Amount sdk.Coins `json:"amount"`
UnlockTime int64 `json:"unlock_time"`
}
func (msg *MsgSend) SetAccAddress(addr sdk.AccAddress) {
msg.FromAddress = addr
}
func NewMsgSend(fromAddr, toAddr sdk.AccAddress, amount sdk.Coins, unlockTime int64) MsgSend {
return MsgSend{FromAddress: fromAddr, ToAddress: toAddr, Amount: amount, UnlockTime: unlockTime}
}
func (msg MsgSend) Route() string {
return RouterKey
}
func (msg MsgSend) Type() string {
return "send"
}
func (msg MsgSend) ValidateBasic() sdk.Error {
if msg.FromAddress.Empty() {
return sdk.ErrInvalidAddress("missing sender address")
}
if msg.ToAddress.Empty() {
return sdk.ErrInvalidAddress("missing recipient address")
}
if !msg.Amount.IsValid() {
return sdk.ErrInvalidCoins("send amount is invalid: " + msg.Amount.String())
}
if !msg.Amount.IsAllPositive() {
return sdk.ErrInsufficientCoins("send amount must be positive")
}
if msg.UnlockTime < 0 {
return ErrUnlockTime("negative unlock time ")
}
if msg.UnlockTime > math.MaxInt64/int64(time.Second) {
return ErrUnlockTime("unlock time is too large")
}
return nil
}
func (msg MsgSend) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
}
func (msg MsgSend) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.FromAddress}
}
// MsgMultiSend - high level transaction of the coin module
type MsgMultiSend struct {
Inputs []bank.Input `json:"inputs" yaml:"inputs"`
Outputs []bank.Output `json:"outputs" yaml:"outputs"`
}
var _ sdk.Msg = MsgMultiSend{}
// NewMsgMultiSend - construct arbitrary multi-in, multi-out send msg.
func NewMsgMultiSend(in []bank.Input, out []bank.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 "multisend" }
// ValidateBasic Implements Msg.
func (msg MsgMultiSend) ValidateBasic() sdk.Error {
// this just makes sure all the inputs and outputs are properly formatted,
// not that they actually have the money inside
if len(msg.Inputs) == 0 {
return ErrNoInputs()
}
if len(msg.Outputs) == 0 {
return ErrNoOutputs()
}
return 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 {
addrs[i] = in.Address
}
return addrs
}
// ValidateInputsOutputs validates that each respective input and output is
// valid and that the sum of inputs is equal to the sum of outputs.
func ValidateInputsOutputs(inputs []bank.Input, outputs []bank.Output) sdk.Error {
var totalIn, totalOut sdk.Coins
for _, in := range inputs {
if err := in.ValidateBasic(); err != nil {
return err.TraceSDK("")
}
totalIn = totalIn.Add(in.Coins)
}
for _, out := range outputs {
if err := out.ValidateBasic(); err != nil {
return err.TraceSDK("")
}
totalOut = totalOut.Add(out.Coins)
}
// make sure inputs and outputs match
if !totalIn.IsEqual(totalOut) {
return ErrInputOutputMismatch("inputs outputs mismatch")
}
return nil
}
var _ sdk.Msg = MsgSupervisedSend{}
// MsgSupervisedSend
type MsgSupervisedSend struct {
FromAddress sdk.AccAddress `json:"from_address"`
Supervisor sdk.AccAddress `json:"supervisor,omitempty"`
ToAddress sdk.AccAddress `json:"to_address"`
Amount sdk.Coin `json:"amount"`
UnlockTime int64 `json:"unlock_time"`
Reward int64 `json:"reward"`
Operation byte `json:"operation"`
}
const (
Create byte = 0
Return byte = 1
EarlierUnlockBySender byte = 2
EarlierUnlockBySupervisor byte = 3
)
// NewMsgSupervisedSend
func NewMsgSupervisedSend(fromAddress sdk.AccAddress, supervisor sdk.AccAddress, toAddress sdk.AccAddress, amount sdk.Coin,
unlockTime int64, reward int64, operation byte) MsgSupervisedSend {
return MsgSupervisedSend{
FromAddress: fromAddress,
Supervisor: supervisor,
ToAddress: toAddress,
Amount: amount,
UnlockTime: unlockTime,
Reward: reward,
Operation: operation,
}
}
func (msg *MsgSupervisedSend) SetAccAddress(addr sdk.AccAddress) {
if msg.Operation == Return || msg.Operation == EarlierUnlockBySupervisor {
msg.Supervisor = addr
} else {
msg.FromAddress = addr
}
}
// Route Implements Msg
func (msg MsgSupervisedSend) Route() string { return RouterKey }
// Type Implements Msg
func (msg MsgSupervisedSend) Type() string { return "supervised_send" }
// ValidateBasic Implements Msg.
func (msg MsgSupervisedSend) ValidateBasic() sdk.Error {
if msg.FromAddress.Empty() {
return sdk.ErrInvalidAddress("missing sender address")
}
if msg.ToAddress.Empty() {
return sdk.ErrInvalidAddress("missing recipient address")
}
if msg.Supervisor.Empty() && (msg.Operation == Return || msg.Operation == EarlierUnlockBySupervisor) {
return sdk.ErrInvalidAddress("missing supervisor address")
}
if !msg.Amount.IsValid() {
return sdk.ErrInvalidCoins("send amount is invalid: " + msg.Amount.String())
}
if !msg.Amount.IsPositive() {
return sdk.ErrInsufficientCoins("send amount must be positive")
}
if msg.UnlockTime <= 0 {
return ErrUnlockTime("unlock time must be positive")
}
if msg.UnlockTime > math.MaxInt64/int64(time.Second) {
return ErrUnlockTime("unlock time is too large")
}
if msg.Reward < 0 {
return sdk.ErrInsufficientCoins("reward can not be negative")
}
if sdk.NewInt(msg.Reward).GT(msg.Amount.Amount) {
return ErrRewardExceedsAmount()
}
if msg.Operation < Create || msg.Operation > EarlierUnlockBySupervisor {
return ErrInvalidOperation()
}
return nil
}
// GetSignBytes Implements Msg.
func (msg MsgSupervisedSend) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
}
// GetSigners Implements Msg.
func (msg MsgSupervisedSend) GetSigners() []sdk.AccAddress {
if msg.Operation == Return || msg.Operation == EarlierUnlockBySupervisor {
return []sdk.AccAddress{msg.Supervisor}
}
return []sdk.AccAddress{msg.FromAddress}
}