-
Notifications
You must be signed in to change notification settings - Fork 0
/
msg.go
124 lines (101 loc) · 3.19 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
package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/legacy/legacytx"
)
var (
_ sdk.Msg = &MsgDeposit{}
_ sdk.Msg = &MsgWithdraw{}
_ legacytx.LegacyMsg = &MsgDeposit{}
_ legacytx.LegacyMsg = &MsgWithdraw{}
)
// legacy message types
const (
TypeMsgDeposit = "earn_msg_deposit"
TypeMsgWithdraw = "earn_msg_withdraw"
)
// NewMsgDeposit returns a new MsgDeposit.
func NewMsgDeposit(depositor string, amount sdk.Coin, strategy StrategyType) *MsgDeposit {
return &MsgDeposit{
Depositor: depositor,
Amount: amount,
Strategy: strategy,
}
}
// ValidateBasic does a simple validation check that doesn't require access to any other information.
func (msg MsgDeposit) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.Depositor); err != nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, err.Error())
}
if err := msg.Amount.Validate(); err != nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, err.Error())
}
if err := msg.Strategy.Validate(); err != nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
}
return nil
}
// GetSignBytes gets the canonical byte representation of the Msg.
func (msg MsgDeposit) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}
// GetSigners returns the addresses of signers that must sign.
func (msg MsgDeposit) GetSigners() []sdk.AccAddress {
depositor, err := sdk.AccAddressFromBech32(msg.Depositor)
if err != nil {
panic(err)
}
return []sdk.AccAddress{depositor}
}
// Route implements the LegacyMsg.Route method.
func (msg MsgDeposit) Route() string {
return RouterKey
}
// Type implements the LegacyMsg.Type method.
func (msg MsgDeposit) Type() string {
return TypeMsgDeposit
}
// NewMsgWithdraw returns a new MsgWithdraw.
func NewMsgWithdraw(from string, amount sdk.Coin, strategy StrategyType) *MsgWithdraw {
return &MsgWithdraw{
From: from,
Amount: amount,
Strategy: strategy,
}
}
// ValidateBasic does a simple validation check that doesn't require access to any other information.
func (msg MsgWithdraw) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.From); err != nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, err.Error())
}
if err := msg.Amount.Validate(); err != nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, err.Error())
}
if err := msg.Strategy.Validate(); err != nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
}
return nil
}
// GetSignBytes gets the canonical byte representation of the Msg.
func (msg MsgWithdraw) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}
// GetSigners returns the addresses of signers that must sign.
func (msg MsgWithdraw) GetSigners() []sdk.AccAddress {
depositor, err := sdk.AccAddressFromBech32(msg.From)
if err != nil {
panic(err)
}
return []sdk.AccAddress{depositor}
}
// Route implements the LegacyMsg.Route method.
func (msg MsgWithdraw) Route() string {
return RouterKey
}
// Type implements the LegacyMsg.Type method.
func (msg MsgWithdraw) Type() string {
return TypeMsgWithdraw
}