/
msg.go
301 lines (250 loc) · 7.29 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
package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
const (
MaxPoolNameLength = 128
createPoolMsgType = "create_pool"
destroyPoolMsgType = "destroy_pool"
provideMsgType = "provide"
lockMsgType = "lock"
unlockMsgType = "unlock"
claimMsgType = "claim"
)
type MsgCreatePool struct {
Owner sdk.AccAddress `json:"owner" yaml:"owner"`
PoolName string `json:"pool_name" yaml:"pool_name"`
MinLockAmount sdk.SysCoin `json:"min_lock_amount" yaml:"min_lock_amount"`
YieldedSymbol string `json:"yielded_symbol" yaml:"yielded_symbol"`
}
var _ sdk.Msg = MsgCreatePool{}
func NewMsgCreatePool(address sdk.AccAddress, poolName string, minLockAmount sdk.SysCoin, yieldedSymbol string) MsgCreatePool {
return MsgCreatePool{
Owner: address,
PoolName: poolName,
MinLockAmount: minLockAmount,
YieldedSymbol: yieldedSymbol,
}
}
func (m MsgCreatePool) Route() string {
return RouterKey
}
func (m MsgCreatePool) Type() string {
return createPoolMsgType
}
func (m MsgCreatePool) ValidateBasic() sdk.Error {
if m.Owner.Empty() {
return ErrNilAddress()
}
if m.PoolName == "" || len(m.PoolName) > MaxPoolNameLength {
return ErrPoolNameLength(m.PoolName, len(m.PoolName), MaxPoolNameLength)
}
if m.MinLockAmount.Amount.LT(sdk.ZeroDec()) || !m.MinLockAmount.IsValid() {
return ErrInvalidInputAmount(m.MinLockAmount.String())
}
if m.YieldedSymbol == "" {
return ErrInvalidInput("yielded symbol is empty")
}
return nil
}
func (m MsgCreatePool) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(m)
return sdk.MustSortJSON(bz)
}
func (m MsgCreatePool) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{m.Owner}
}
type MsgDestroyPool struct {
Owner sdk.AccAddress `json:"owner" yaml:"owner"`
PoolName string `json:"pool_name" yaml:"pool_name"`
}
var _ sdk.Msg = MsgDestroyPool{}
func NewMsgDestroyPool(address sdk.AccAddress, poolName string) MsgDestroyPool {
return MsgDestroyPool{
Owner: address,
PoolName: poolName,
}
}
func (m MsgDestroyPool) Route() string {
return RouterKey
}
func (m MsgDestroyPool) Type() string {
return destroyPoolMsgType
}
func (m MsgDestroyPool) ValidateBasic() sdk.Error {
if m.Owner.Empty() {
return ErrNilAddress()
}
if m.PoolName == "" || len(m.PoolName) > MaxPoolNameLength {
return ErrPoolNameLength(m.PoolName, len(m.PoolName), MaxPoolNameLength)
}
return nil
}
func (m MsgDestroyPool) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(m)
return sdk.MustSortJSON(bz)
}
func (m MsgDestroyPool) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{m.Owner}
}
type MsgProvide struct {
PoolName string `json:"pool_name" yaml:"pool_name"`
Address sdk.AccAddress `json:"address" yaml:"address"`
Amount sdk.SysCoin `json:"amount" yaml:"amount"`
AmountYieldedPerBlock sdk.Dec `json:"amount_yielded_per_block" yaml:"amount_yielded_per_block"`
StartHeightToYield int64 `json:"start_height_to_yield" yaml:"start_height_to_yield"`
}
func NewMsgProvide(poolName string, address sdk.AccAddress, amount sdk.SysCoin,
amountYieldedPerBlock sdk.Dec, startHeightToYield int64) MsgProvide {
return MsgProvide{
PoolName: poolName,
Address: address,
Amount: amount,
AmountYieldedPerBlock: amountYieldedPerBlock,
StartHeightToYield: startHeightToYield,
}
}
var _ sdk.Msg = MsgProvide{}
func (m MsgProvide) Route() string {
return RouterKey
}
func (m MsgProvide) Type() string {
return provideMsgType
}
func (m MsgProvide) ValidateBasic() sdk.Error {
if m.PoolName == "" || len(m.PoolName) > MaxPoolNameLength {
return ErrInvalidInput(m.PoolName)
}
if m.Address.Empty() {
return ErrNilAddress()
}
if m.Amount.Amount.LTE(sdk.ZeroDec()) || !m.Amount.IsValid() {
return ErrInvalidInputAmount(m.Amount.String())
}
if m.AmountYieldedPerBlock.LTE(sdk.ZeroDec()) {
return ErrInvalidInput("amount yielded per block must be > 0")
}
if m.Amount.Amount.LT(m.AmountYieldedPerBlock) {
return ErrInvalidInput("provided amount must be bigger than amount_yielded_per_block")
}
if m.StartHeightToYield <= 0 {
return ErrInvalidInput("start height to yield must be > 0")
}
return nil
}
func (m MsgProvide) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(m)
return sdk.MustSortJSON(bz)
}
func (m MsgProvide) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{m.Address}
}
type MsgLock struct {
PoolName string `json:"pool_name" yaml:"pool_name"`
Address sdk.AccAddress `json:"address" yaml:"address"`
Amount sdk.SysCoin `json:"amount" yaml:"amount"`
}
func NewMsgLock(poolName string, address sdk.AccAddress, amount sdk.SysCoin) MsgLock {
return MsgLock{
PoolName: poolName,
Address: address,
Amount: amount,
}
}
var _ sdk.Msg = MsgLock{}
func (m MsgLock) Route() string {
return RouterKey
}
func (m MsgLock) Type() string {
return lockMsgType
}
func (m MsgLock) ValidateBasic() sdk.Error {
if m.PoolName == "" || len(m.PoolName) > MaxPoolNameLength {
return ErrInvalidInput(m.PoolName)
}
if m.Address.Empty() {
return ErrNilAddress()
}
if m.Amount.Amount.LTE(sdk.ZeroDec()) || !m.Amount.IsValid() {
return ErrInvalidInputAmount(m.Amount.Amount.String())
}
return nil
}
func (m MsgLock) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(m)
return sdk.MustSortJSON(bz)
}
func (m MsgLock) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{m.Address}
}
type MsgUnlock struct {
PoolName string `json:"pool_name" yaml:"pool_name"`
Address sdk.AccAddress `json:"address" yaml:"address"`
Amount sdk.SysCoin `json:"amount" yaml:"amount"`
}
func NewMsgUnlock(poolName string, address sdk.AccAddress, amount sdk.SysCoin) MsgUnlock {
return MsgUnlock{
PoolName: poolName,
Address: address,
Amount: amount,
}
}
var _ sdk.Msg = MsgUnlock{}
func (m MsgUnlock) Route() string {
return RouterKey
}
func (m MsgUnlock) Type() string {
return unlockMsgType
}
func (m MsgUnlock) ValidateBasic() sdk.Error {
if m.PoolName == "" || len(m.PoolName) > MaxPoolNameLength {
return ErrInvalidInput(m.PoolName)
}
if m.Address.Empty() {
return ErrNilAddress()
}
if m.Amount.Amount.LTE(sdk.ZeroDec()) || !m.Amount.IsValid() {
return ErrInvalidInputAmount(m.Amount.Amount.String())
}
return nil
}
func (m MsgUnlock) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(m)
return sdk.MustSortJSON(bz)
}
func (m MsgUnlock) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{m.Address}
}
type MsgClaim struct {
PoolName string `json:"pool_name" yaml:"pool_name"`
Address sdk.AccAddress `json:"address" yaml:"address"`
}
func NewMsgClaim(poolName string, address sdk.AccAddress) MsgClaim {
return MsgClaim{
PoolName: poolName,
Address: address,
}
}
var _ sdk.Msg = MsgClaim{}
func (m MsgClaim) Route() string {
return RouterKey
}
func (m MsgClaim) Type() string {
return claimMsgType
}
func (m MsgClaim) ValidateBasic() sdk.Error {
if m.PoolName == "" || len(m.PoolName) > MaxPoolNameLength {
return ErrInvalidInput(m.PoolName)
}
if m.Address.Empty() {
return ErrNilAddress()
}
return nil
}
func (m MsgClaim) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(m)
return sdk.MustSortJSON(bz)
}
func (m MsgClaim) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{m.Address}
}