forked from lino-network/lino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msg.go
310 lines (259 loc) · 8.2 KB
/
msg.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package vote
// nolint
import (
"fmt"
"github.com/lino-network/lino/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
var _ types.Msg = StakeInMsg{}
var _ types.Msg = StakeOutMsg{}
var _ types.Msg = DelegateMsg{}
var _ types.Msg = DelegatorWithdrawMsg{}
var _ types.Msg = ClaimInterestMsg{}
// StakeInMsg - voter deposit
type StakeInMsg struct {
Username types.AccountKey `json:"username"`
Deposit types.LNO `json:"deposit"`
}
// StakeOutMsg - voter withdraw
type StakeOutMsg struct {
Username types.AccountKey `json:"username"`
Amount types.LNO `json:"amount"`
}
// DelegateMsg - delegator delegate money to a voter
type DelegateMsg struct {
Delegator types.AccountKey `json:"delegator"`
Voter types.AccountKey `json:"voter"`
Amount types.LNO `json:"amount"`
}
// DelegatorWithdrawMsg - delegator withdraw delegation from a voter
type DelegatorWithdrawMsg struct {
Delegator types.AccountKey `json:"delegator"`
Voter types.AccountKey `json:"voter"`
Amount types.LNO `json:"amount"`
}
// ClaimInterestMsg - claim interest generated from lino power
type ClaimInterestMsg struct {
Username types.AccountKey `json:"username"`
}
// NewStakeInMsg - return a StakeInMsg
func NewStakeInMsg(username string, deposit types.LNO) StakeInMsg {
return StakeInMsg{
Username: types.AccountKey(username),
Deposit: deposit,
}
}
// Type - implements sdk.Msg
func (msg StakeInMsg) Type() string { return types.VoteRouterName } // TODO: "account/register"
// ValidateBasic - implements sdk.Msg
func (msg StakeInMsg) ValidateBasic() sdk.Error {
if len(msg.Username) < types.MinimumUsernameLength ||
len(msg.Username) > types.MaximumUsernameLength {
return ErrInvalidUsername()
}
_, err := types.LinoToCoin(msg.Deposit)
if err != nil {
return err
}
return nil
}
func (msg StakeInMsg) String() string {
return fmt.Sprintf("StakeInMsg{Username:%v, Deposit:%v}", msg.Username, msg.Deposit)
}
// GetPermission - implements types.Msg
func (msg StakeInMsg) GetPermission() types.Permission {
return types.TransactionPermission
}
// GetSignBytes - implements sdk.Msg
func (msg StakeInMsg) GetSignBytes() []byte {
b, err := msgCdc.MarshalJSON(msg) // XXX: ensure some canonical form
if err != nil {
panic(err)
}
return b
}
// GetSigners - implements sdk.Msg
func (msg StakeInMsg) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{sdk.AccAddress(msg.Username)}
}
// GetConsumeAmount - implement types.Msg
func (msg StakeInMsg) GetConsumeAmount() types.Coin {
return types.NewCoinFromInt64(0)
}
// NewStakeOutMsg - return StakeOutMsg
func NewStakeOutMsg(username string, amount types.LNO) StakeOutMsg {
return StakeOutMsg{
Username: types.AccountKey(username),
Amount: amount,
}
}
// Type - implements sdk.Msg
func (msg StakeOutMsg) Type() string { return types.VoteRouterName } // TODO: "account/register"
// ValidateBasic - implements sdk.Msg
func (msg StakeOutMsg) ValidateBasic() sdk.Error {
if len(msg.Username) < types.MinimumUsernameLength ||
len(msg.Username) > types.MaximumUsernameLength {
return ErrInvalidUsername()
}
_, err := types.LinoToCoin(msg.Amount)
if err != nil {
return err
}
return nil
}
func (msg StakeOutMsg) String() string {
return fmt.Sprintf("StakeOutMsg{Username:%v, Amount:%v}", msg.Username, msg.Amount)
}
// GetPermission - implements types.Msg
func (msg StakeOutMsg) GetPermission() types.Permission {
return types.TransactionPermission
}
// GetSignBytes - implements sdk.Msg
func (msg StakeOutMsg) GetSignBytes() []byte {
b, err := msgCdc.MarshalJSON(msg)
if err != nil {
panic(err)
}
return b
}
// GetSigners - implements sdk.Msg
func (msg StakeOutMsg) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{sdk.AccAddress(msg.Username)}
}
// GetConsumeAmount - implement types.Msg
func (msg StakeOutMsg) GetConsumeAmount() types.Coin {
return types.NewCoinFromInt64(0)
}
// NewDelegateMsg - return DelegateMsg
func NewDelegateMsg(delegator string, voter string, amount types.LNO) DelegateMsg {
return DelegateMsg{
Delegator: types.AccountKey(delegator),
Voter: types.AccountKey(voter),
Amount: amount,
}
}
// Type - implements sdk.Msg
func (msg DelegateMsg) Type() string { return types.VoteRouterName } // TODO: "account/register"
// ValidateBasic - implements sdk.Msg
func (msg DelegateMsg) ValidateBasic() sdk.Error {
if len(msg.Delegator) < types.MinimumUsernameLength ||
len(msg.Delegator) > types.MaximumUsernameLength ||
len(msg.Voter) < types.MinimumUsernameLength ||
len(msg.Voter) > types.MaximumUsernameLength {
return ErrInvalidUsername()
}
_, err := types.LinoToCoin(msg.Amount)
if err != nil {
return err
}
return nil
}
func (msg DelegateMsg) String() string {
return fmt.Sprintf("DelegateMsg{Delegator:%v, Voter:%v, Amount:%v}", msg.Delegator, msg.Voter, msg.Amount)
}
// GetPermission - implements types.Msg
func (msg DelegateMsg) GetPermission() types.Permission {
return types.TransactionPermission
}
// GetSignBytes - implements sdk.Msg
func (msg DelegateMsg) GetSignBytes() []byte {
b, err := msgCdc.MarshalJSON(msg)
if err != nil {
panic(err)
}
return b
}
// GetSigners - implements sdk.Msg
func (msg DelegateMsg) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{sdk.AccAddress(msg.Delegator)}
}
// GetConsumeAmount - implement types.Msg
func (msg DelegateMsg) GetConsumeAmount() types.Coin {
return types.NewCoinFromInt64(0)
}
// NewDelegatorWithdrawMsg - return NewDelegatorWithdrawMsg
func NewDelegatorWithdrawMsg(delegator string, voter string, amount types.LNO) DelegatorWithdrawMsg {
return DelegatorWithdrawMsg{
Delegator: types.AccountKey(delegator),
Voter: types.AccountKey(voter),
Amount: amount,
}
}
// Type - implements sdk.Msg
func (msg DelegatorWithdrawMsg) Type() string { return types.VoteRouterName } // TODO: "account/register"
// ValidateBasic - implements sdk.Msg
func (msg DelegatorWithdrawMsg) ValidateBasic() sdk.Error {
if len(msg.Delegator) < types.MinimumUsernameLength ||
len(msg.Delegator) > types.MaximumUsernameLength ||
len(msg.Voter) < types.MinimumUsernameLength ||
len(msg.Voter) > types.MaximumUsernameLength {
return ErrInvalidUsername()
}
_, err := types.LinoToCoin(msg.Amount)
if err != nil {
return err
}
return nil
}
func (msg DelegatorWithdrawMsg) String() string {
return fmt.Sprintf("DelegatorWithdrawMsg{Delegator:%v, Voter:%v, Amount:%v}", msg.Delegator, msg.Voter, msg.Amount)
}
// GetPermission - implements types.Msg
func (msg DelegatorWithdrawMsg) GetPermission() types.Permission {
return types.TransactionPermission
}
// GetSignBytes - implements sdk.Msg
func (msg DelegatorWithdrawMsg) GetSignBytes() []byte {
b, err := msgCdc.MarshalJSON(msg)
if err != nil {
panic(err)
}
return b
}
// GetSigners - implements sdk.Msg
func (msg DelegatorWithdrawMsg) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{sdk.AccAddress(msg.Delegator)}
}
// GetConsumeAmount - implement types.Msg
func (msg DelegatorWithdrawMsg) GetConsumeAmount() types.Coin {
return types.NewCoinFromInt64(0)
}
// NewClaimInterestMsg - return a ClaimInterestMsg
func NewClaimInterestMsg(username string) ClaimInterestMsg {
return ClaimInterestMsg{
Username: types.AccountKey(username),
}
}
// Type - implements sdk.Msg
func (msg ClaimInterestMsg) Type() string { return types.VoteRouterName }
// ValidateBasic - implements sdk.Msg
func (msg ClaimInterestMsg) ValidateBasic() sdk.Error {
if len(msg.Username) < types.MinimumUsernameLength ||
len(msg.Username) > types.MaximumUsernameLength {
return ErrInvalidUsername()
}
return nil
}
func (msg ClaimInterestMsg) String() string {
return fmt.Sprintf("ClaimInterestMsg{Username:%v}", msg.Username)
}
// GetPermission - implements types.Msg
func (msg ClaimInterestMsg) GetPermission() types.Permission {
return types.AppPermission
}
// GetSignBytes - implements sdk.Msg
func (msg ClaimInterestMsg) GetSignBytes() []byte {
b, err := msgCdc.MarshalJSON(msg) // XXX: ensure some canonical form
if err != nil {
panic(err)
}
return b
}
// GetSigners - implements sdk.Msg
func (msg ClaimInterestMsg) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{sdk.AccAddress(msg.Username)}
}
// GetConsumeAmount - implements types.Msg
func (msg ClaimInterestMsg) GetConsumeAmount() types.Coin {
return types.NewCoinFromInt64(0)
}