-
Notifications
You must be signed in to change notification settings - Fork 845
/
msg.go
146 lines (122 loc) · 4.49 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
package types
import (
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
sdkvesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
)
var (
_ sdk.Msg = &MsgCreateClawbackVestingAccount{}
_ sdk.Msg = &MsgClawback{}
)
const (
TypeMsgCreateClawbackVestingAccount = "create_clawback_vesting_account"
TypeMsgClawback = "clawback"
)
// 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 sdkerrors.Wrapf(err, "invalid from address")
}
if _, err := sdk.AccAddressFromBech32(msg.ToAddress); err != nil {
return sdkerrors.Wrapf(err, "invalid to address")
}
lockupCoins := sdk.NewCoins()
for i, period := range msg.LockupPeriods {
if period.Length < 1 {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid period length of %d in period %d, length must be greater than 0", period.Length, i)
}
lockupCoins = lockupCoins.Add(period.Amount...)
}
vestingCoins := sdk.NewCoins()
for i, period := range msg.VestingPeriods {
if period.Length < 1 {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid period length of %d in period %d, length must be greater than 0", period.Length, i)
}
vestingCoins = vestingCoins.Add(period.Amount...)
}
// 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 sdkerrors.Wrapf(sdkerrors.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(ModuleCdc.MustMarshalJSON(msg))
}
// GetSigners defines whose signature is required
func (msg MsgCreateClawbackVestingAccount) GetSigners() []sdk.AccAddress {
from, err := sdk.AccAddressFromBech32(msg.FromAddress)
if err != nil {
return nil
}
return []sdk.AccAddress{from}
}
// NewMsgClawbackcreates 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 sdkerrors.Wrapf(err, "invalid funder address")
}
if _, err := sdk.AccAddressFromBech32(msg.GetAccountAddress()); err != nil {
return sdkerrors.Wrapf(err, "invalid addr address")
}
if msg.GetDestAddress() != "" {
if _, err := sdk.AccAddressFromBech32(msg.GetDestAddress()); err != nil {
return sdkerrors.Wrapf(err, "invalid dest address")
}
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg *MsgClawback) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
}
// GetSigners defines whose signature is required
func (msg MsgClawback) GetSigners() []sdk.AccAddress {
funder, err := sdk.AccAddressFromBech32(msg.FunderAddress)
if err != nil {
return nil
}
return []sdk.AccAddress{funder}
}