-
Notifications
You must be signed in to change notification settings - Fork 5
/
msgs.go
232 lines (197 loc) · 6.63 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
package types
import (
"fmt"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
dextypes "github.com/duality-labs/duality/x/dex/types"
)
const (
TypeMsgCreateGauge = "create_gauge"
TypeMsgAddToGauge = "add_to_gauge"
TypeMsgStakeTokens = "stake_tokens"
TypeMsgBeginUnstakingAll = "begin_unstaking_all"
TypeMsgBeginUnstaking = "begin_unstaking"
TypeMsgExtendStakeup = "edit_stakeup"
)
var _ sdk.Msg = &MsgCreateGauge{}
// NewMsgCreateGauge creates a message to create a gauge with the provided parameters.
func NewMsgCreateGauge(
isPerpetual bool,
owner sdk.AccAddress,
distributeTo QueryCondition,
coins sdk.Coins,
startTime time.Time,
numEpochsPaidOver uint64,
pricingTick int64,
) *MsgCreateGauge {
return &MsgCreateGauge{
IsPerpetual: isPerpetual,
Owner: owner.String(),
DistributeTo: distributeTo,
Coins: coins,
StartTime: startTime,
NumEpochsPaidOver: numEpochsPaidOver,
PricingTick: pricingTick,
}
}
// Route takes a create gauge message, then returns the RouterKey used for slashing.
func (m MsgCreateGauge) Route() string { return RouterKey }
// Type takes a create gauge message, then returns a create gauge message type.
func (m MsgCreateGauge) Type() string { return TypeMsgCreateGauge }
// ValidateBasic checks that the create gauge message is valid.
func (m MsgCreateGauge) ValidateBasic() error {
if m.Owner == "" {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "owner should be set")
}
// TODO: If this is not set, infer start time as "now"
if m.StartTime.Equal(time.Time{}) {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "distribution start time should be set")
}
if m.NumEpochsPaidOver == 0 {
return sdkerrors.Wrapf(
sdkerrors.ErrInvalidRequest,
"distribution period should be at least 1 epoch",
)
}
if m.IsPerpetual && m.NumEpochsPaidOver != 1 {
return sdkerrors.Wrapf(
sdkerrors.ErrInvalidRequest,
"distribution period should be 1 epoch for perpetual gauge",
)
}
if dextypes.IsTickOutOfRange(m.PricingTick) {
return sdkerrors.Wrapf(
sdkerrors.ErrInvalidRequest,
"pricing tick is out of range, must be between %d and %d",
int64(dextypes.MaxTickExp)*-1,
dextypes.MaxTickExp,
)
}
if dextypes.IsTickOutOfRange(m.DistributeTo.StartTick) {
return sdkerrors.Wrapf(
sdkerrors.ErrInvalidRequest,
"start tick is out of range, must be between %d and %d",
int64(dextypes.MaxTickExp)*-1,
dextypes.MaxTickExp,
)
}
if dextypes.IsTickOutOfRange(m.DistributeTo.EndTick) {
return sdkerrors.Wrapf(
sdkerrors.ErrInvalidRequest,
"start tick is out of range, must be between %d and %d",
int64(dextypes.MaxTickExp)*-1,
dextypes.MaxTickExp,
)
}
return nil
}
// GetSignBytes takes a create gauge message and turns it into a byte array.
func (m MsgCreateGauge) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m))
}
// GetSigners takes a create gauge message and returns the owner in a byte array.
func (m MsgCreateGauge) GetSigners() []sdk.AccAddress {
owner, _ := sdk.AccAddressFromBech32(m.Owner)
return []sdk.AccAddress{owner}
}
var _ sdk.Msg = &MsgAddToGauge{}
// NewMsgAddToGauge creates a message to add rewards to a specific gauge.
func NewMsgAddToGauge(owner sdk.AccAddress, gaugeID uint64, rewards sdk.Coins) *MsgAddToGauge {
return &MsgAddToGauge{
Owner: owner.String(),
GaugeId: gaugeID,
Rewards: rewards,
}
}
// Route takes an add to gauge message, then returns the RouterKey used for slashing.
func (m MsgAddToGauge) Route() string { return RouterKey }
// Type takes an add to gauge message, then returns an add to gauge message type.
func (m MsgAddToGauge) Type() string { return TypeMsgAddToGauge }
// ValidateBasic checks that the add to gauge message is valid.
func (m MsgAddToGauge) ValidateBasic() error {
if m.Owner == "" {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "owner should be set")
}
if m.Rewards.Empty() {
return sdkerrors.Wrapf(
sdkerrors.ErrInvalidRequest,
"additional rewards should not be empty",
)
}
return nil
}
// GetSignBytes takes an add to gauge message and turns it into a byte array.
func (m MsgAddToGauge) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m))
}
// GetSigners takes an add to gauge message and returns the owner in a byte array.
func (m MsgAddToGauge) GetSigners() []sdk.AccAddress {
owner, _ := sdk.AccAddressFromBech32(m.Owner)
return []sdk.AccAddress{owner}
}
var _ sdk.Msg = &MsgStake{}
// NewMsgStakeTokens creates a message to stake tokens.
func NewMsgSetupStake(owner sdk.AccAddress, duration time.Duration, coins sdk.Coins) *MsgStake {
return &MsgStake{
Owner: owner.String(),
Coins: coins,
}
}
func (m MsgStake) Route() string { return RouterKey }
func (m MsgStake) Type() string { return TypeMsgStakeTokens }
func (m MsgStake) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(m.Owner)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "Invalid owner address (%s)", err)
}
if !m.Coins.IsAllPositive() {
return fmt.Errorf("cannot stake up a zero or negative amount")
}
return nil
}
func (m MsgStake) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m))
}
func (m MsgStake) GetSigners() []sdk.AccAddress {
owner, _ := sdk.AccAddressFromBech32(m.Owner)
return []sdk.AccAddress{owner}
}
var _ sdk.Msg = &MsgUnstake{}
func NewMsgUnstakeDescriptor(id uint64, coins sdk.Coins) *MsgUnstake_UnstakeDescriptor {
return &MsgUnstake_UnstakeDescriptor{
ID: id,
Coins: coins,
}
}
// NewMsgUnstake creates a message to unstake the tokens of a set of stake records.
func NewMsgUnstake(owner sdk.AccAddress, unstakes []*MsgUnstake_UnstakeDescriptor) *MsgUnstake {
return &MsgUnstake{
Owner: owner.String(),
Unstakes: unstakes,
}
}
func (m MsgUnstake) Route() string { return RouterKey }
func (m MsgUnstake) Type() string { return TypeMsgBeginUnstaking }
func (m MsgUnstake) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(m.Owner)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "Invalid owner address (%s)", err)
}
for _, unstake := range m.Unstakes {
if unstake.ID == 0 {
return fmt.Errorf("invalid stakeup ID, got %v", unstake.ID)
}
if !unstake.Coins.Empty() && !unstake.Coins.IsAllPositive() {
return fmt.Errorf("cannot unstake a zero or negative amount")
}
}
return nil
}
func (m MsgUnstake) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m))
}
func (m MsgUnstake) GetSigners() []sdk.AccAddress {
owner, _ := sdk.AccAddressFromBech32(m.Owner)
return []sdk.AccAddress{owner}
}