-
Notifications
You must be signed in to change notification settings - Fork 0
/
msg.go
254 lines (213 loc) · 8.91 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
// Copyright 2022 Serv Foundation
// This file is part of the Serv Network packages.
//
// Serv is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The Serv packages are distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Serv packages. If not, see https://github.com/twobitedd/serv/blob/main/LICENSE
package types
import (
"time"
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
sdkvesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
)
var (
_ sdk.Msg = &MsgCreateClawbackVestingAccount{}
_ sdk.Msg = &MsgClawback{}
_ sdk.Msg = &MsgConvertVestingAccount{}
_ sdk.Msg = &MsgUpdateVestingFunder{}
)
const (
TypeMsgCreateClawbackVestingAccount = "create_clawback_vesting_account"
TypeMsgClawback = "clawback"
TypeMsgUpdateVestingFunder = "update_vesting_funder"
TypeMsgConvertVestingAccount = "convert_vesting_account"
)
// NewMsgCreateClawbackVestingAccount creates new instance of MsgCreateClawbackVestingAccount
func NewMsgCreateClawbackVestingAccount(
fromAddr, toAddr sdk.AccAddress,
startTime time.Time,
lockupPeriods,
vestingPeriods sdkvesting.Periods,
merge bool,
) *MsgCreateClawbackVestingAccount {
return &MsgCreateClawbackVestingAccount{
FromAddress: fromAddr.String(),
ToAddress: toAddr.String(),
StartTime: startTime,
LockupPeriods: lockupPeriods,
VestingPeriods: vestingPeriods,
Merge: merge,
}
}
// Route returns the name of the module
func (msg MsgCreateClawbackVestingAccount) Route() string { return RouterKey }
// Type returns the the action
func (msg MsgCreateClawbackVestingAccount) Type() string { return TypeMsgCreateClawbackVestingAccount }
// ValidateBasic runs stateless checks on the message
func (msg MsgCreateClawbackVestingAccount) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.FromAddress); err != nil {
return errorsmod.Wrapf(err, "invalid from address")
}
if _, err := sdk.AccAddressFromBech32(msg.ToAddress); err != nil {
return errorsmod.Wrapf(err, "invalid to address")
}
lockupCoins := sdk.NewCoins()
for i, period := range msg.LockupPeriods {
if period.Length < 1 {
return errorsmod.Wrapf(errortypes.ErrInvalidRequest, "invalid period length of %d in period %d, length must be greater than 0", period.Length, i)
}
if !period.Amount.IsAllPositive() {
return errorsmod.Wrapf(errortypes.ErrInvalidRequest, "invalid amount in lockup periods, amounts must be positive")
}
lockupCoins = lockupCoins.Add(period.Amount...)
}
vestingCoins := sdk.NewCoins()
for i, period := range msg.VestingPeriods {
if period.Length < 1 {
return errorsmod.Wrapf(errortypes.ErrInvalidRequest, "invalid period length of %d in period %d, length must be greater than 0", period.Length, i)
}
if !period.Amount.IsValid() {
return errortypes.ErrInvalidCoins.Wrap(period.Amount.String())
}
if !period.Amount.IsAllPositive() {
return errorsmod.Wrapf(errortypes.ErrInvalidRequest, "invalid amount in vesting periods, amounts must be positive")
}
vestingCoins = vestingCoins.Add(period.Amount...)
}
// If neither schedule is present, the message is invalid.
if len(lockupCoins) == 0 && len(vestingCoins) == 0 {
return errorsmod.Wrapf(errortypes.ErrInvalidRequest, "vesting and/or lockup schedules must be present")
}
// If both schedules are present, the must describe the same total amount.
// IsEqual can panic, so use (a == b) <=> (a <= b && b <= a).
if len(msg.LockupPeriods) > 0 && len(msg.VestingPeriods) > 0 &&
!(lockupCoins.IsAllLTE(vestingCoins) && vestingCoins.IsAllLTE(lockupCoins)) {
return errorsmod.Wrapf(errortypes.ErrInvalidRequest, "vesting and lockup schedules must have same total coins")
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg *MsgCreateClawbackVestingAccount) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(msg))
}
// GetSigners defines whose signature is required
func (msg MsgCreateClawbackVestingAccount) GetSigners() []sdk.AccAddress {
from := sdk.MustAccAddressFromBech32(msg.FromAddress)
return []sdk.AccAddress{from}
}
// NewMsgClawback creates new instance of MsgClawback. The dest address may be
// nil - defaulting to the funder.
func NewMsgClawback(funder, addr, dest sdk.AccAddress) *MsgClawback {
destString := ""
if dest != nil {
destString = dest.String()
}
return &MsgClawback{
FunderAddress: funder.String(),
AccountAddress: addr.String(),
DestAddress: destString,
}
}
// Route returns the message route for a MsgClawback.
func (msg MsgClawback) Route() string { return RouterKey }
// Type returns the message type for a MsgClawback.
func (msg MsgClawback) Type() string { return TypeMsgClawback }
// ValidateBasic runs stateless checks on the message
func (msg MsgClawback) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.GetFunderAddress()); err != nil {
return errorsmod.Wrapf(err, "invalid funder address")
}
if _, err := sdk.AccAddressFromBech32(msg.GetAccountAddress()); err != nil {
return errorsmod.Wrapf(err, "invalid account address")
}
if msg.GetDestAddress() != "" {
if _, err := sdk.AccAddressFromBech32(msg.GetDestAddress()); err != nil {
return errorsmod.Wrapf(err, "invalid dest address")
}
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg *MsgClawback) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(msg))
}
// GetSigners defines whose signature is required
func (msg MsgClawback) GetSigners() []sdk.AccAddress {
funder := sdk.MustAccAddressFromBech32(msg.FunderAddress)
return []sdk.AccAddress{funder}
}
// NewMsgUpdateVestingFunder creates new instance of MsgUpdateVestingFunder
func NewMsgUpdateVestingFunder(funder, newFunder, vesting sdk.AccAddress) *MsgUpdateVestingFunder {
return &MsgUpdateVestingFunder{
FunderAddress: funder.String(),
NewFunderAddress: newFunder.String(),
VestingAddress: vesting.String(),
}
}
// Route returns the message route for a MsgUpdateVestingFunder.
func (msg MsgUpdateVestingFunder) Route() string { return RouterKey }
// Type returns the message type for a MsgUpdateVestingFunder.
func (msg MsgUpdateVestingFunder) Type() string { return TypeMsgUpdateVestingFunder }
// ValidateBasic runs stateless checks on the MsgUpdateVestingFunder message
func (msg MsgUpdateVestingFunder) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.GetFunderAddress()); err != nil {
return errorsmod.Wrapf(err, "invalid funder address")
}
if _, err := sdk.AccAddressFromBech32(msg.GetNewFunderAddress()); err != nil {
return errorsmod.Wrapf(err, "invalid new funder address")
}
// New funder address can not be equal to current funder address
if msg.FunderAddress == msg.NewFunderAddress {
return errorsmod.Wrapf(errortypes.ErrInvalidRequest, "new funder address is equal to current funder address")
}
if _, err := sdk.AccAddressFromBech32(msg.GetVestingAddress()); err != nil {
return errorsmod.Wrapf(err, "invalid vesting account address")
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg *MsgUpdateVestingFunder) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(msg))
}
// GetSigners defines whose signature is required
func (msg MsgUpdateVestingFunder) GetSigners() []sdk.AccAddress {
funder := sdk.MustAccAddressFromBech32(msg.FunderAddress)
return []sdk.AccAddress{funder}
}
// NewMsgConvertVestingAccount creates new instance of MsgConvertVestingAccount
func NewMsgConvertVestingAccount(vestingAcc sdk.AccAddress) *MsgConvertVestingAccount {
return &MsgConvertVestingAccount{
VestingAddress: vestingAcc.String(),
}
}
// Route returns the name of the module
func (msg MsgConvertVestingAccount) Route() string { return RouterKey }
// Type returns the action
func (msg MsgConvertVestingAccount) Type() string { return TypeMsgConvertVestingAccount }
// ValidateBasic runs stateless checks on the message
func (msg MsgConvertVestingAccount) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.GetVestingAddress()); err != nil {
return errorsmod.Wrapf(err, "invalid vesting address")
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg *MsgConvertVestingAccount) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(msg))
}
// GetSigners defines whose signature is required
func (msg MsgConvertVestingAccount) GetSigners() []sdk.AccAddress {
vesting := sdk.MustAccAddressFromBech32(msg.VestingAddress)
return []sdk.AccAddress{vesting}
}