-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmsg.go
138 lines (116 loc) · 4.33 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
package types
import (
"github.com/ethereum/go-ethereum/common"
protov2 "google.golang.org/protobuf/proto"
erc20api "github.com/cosmos/evm/api/cosmos/evm/erc20/v1"
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/math"
txsigning "cosmossdk.io/x/tx/signing"
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
)
var (
_ sdk.Msg = &MsgConvertERC20{}
_ sdk.Msg = &MsgUpdateParams{}
_ sdk.Msg = &MsgRegisterERC20{}
_ sdk.Msg = &MsgToggleConversion{}
_ sdk.HasValidateBasic = &MsgConvertERC20{}
_ sdk.HasValidateBasic = &MsgConvertCoin{}
_ sdk.HasValidateBasic = &MsgUpdateParams{}
_ sdk.HasValidateBasic = &MsgRegisterERC20{}
_ sdk.HasValidateBasic = &MsgToggleConversion{}
)
const (
TypeMsgConvertERC20 = "convert_ERC20"
TypeMsgConvertCoin = "convert_coin"
)
var MsgConvertERC20CustomGetSigner = txsigning.CustomGetSigner{
MsgType: protov2.MessageName(&erc20api.MsgConvertERC20{}),
Fn: erc20api.GetSigners,
}
// NewMsgConvertERC20 creates a new instance of MsgConvertERC20
func NewMsgConvertERC20(amount math.Int, receiver sdk.AccAddress, contract, sender common.Address) *MsgConvertERC20 { //nolint: interfacer
return &MsgConvertERC20{
ContractAddress: contract.String(),
Amount: amount,
Receiver: receiver.String(),
Sender: sender.Hex(),
}
}
// Route should return the name of the module
func (msg MsgConvertERC20) Route() string { return RouterKey }
// Type should return the action
func (msg MsgConvertERC20) Type() string { return TypeMsgConvertERC20 }
// ValidateBasic runs stateless checks on the message
func (msg MsgConvertERC20) ValidateBasic() error {
if !common.IsHexAddress(msg.ContractAddress) {
return errorsmod.Wrapf(errortypes.ErrInvalidAddress, "invalid contract hex address '%s'", msg.ContractAddress)
}
if !msg.Amount.IsPositive() {
return errorsmod.Wrapf(errortypes.ErrInvalidCoins, "cannot mint a non-positive amount")
}
_, err := sdk.AccAddressFromBech32(msg.Receiver)
if err != nil {
return errorsmod.Wrap(err, "invalid receiver address")
}
if !common.IsHexAddress(msg.Sender) {
return errorsmod.Wrapf(errortypes.ErrInvalidAddress, "invalid sender hex address %s", msg.Sender)
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg MsgConvertERC20) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(&msg))
}
// ValidateBasic does a sanity check of the provided data
func (m *MsgUpdateParams) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(m.Authority); err != nil {
return errorsmod.Wrap(err, "Invalid authority address")
}
return m.Params.Validate()
}
// GetSignBytes implements the LegacyMsg interface.
func (m MsgUpdateParams) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(&m))
}
// ValidateBasic does a sanity check of the provided data
func (m *MsgRegisterERC20) ValidateBasic() error {
for _, addr := range m.Erc20Addresses {
if !common.IsHexAddress(addr) {
return errortypes.ErrInvalidAddress.Wrapf("invalid ERC20 contract address: %s", addr)
}
}
return nil
}
// ValidateBasic does a sanity check of the provided data
func (m *MsgToggleConversion) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(m.Authority); err != nil {
return errorsmod.Wrap(err, "Invalid authority address")
}
return nil
}
// Route should return the name of the module
func (msg MsgConvertCoin) Route() string { return RouterKey }
// Type should return the action
func (msg MsgConvertCoin) Type() string { return TypeMsgConvertCoin }
// ValidateBasic runs stateless checks on the message
func (msg MsgConvertCoin) ValidateBasic() error {
if len(msg.Coin.Denom) == 0 {
return errorsmod.Wrapf(errortypes.ErrInvalidCoins, "denom cannot be empty")
}
if !msg.Coin.Amount.IsPositive() {
return errorsmod.Wrapf(errortypes.ErrInvalidCoins, "cannot mint a non-positive amount")
}
_, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
return errorsmod.Wrap(err, "invalid sender address")
}
if !common.IsHexAddress(msg.Receiver) {
return errorsmod.Wrapf(errortypes.ErrInvalidAddress, "invalid receiver hex address %s", msg.Receiver)
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg MsgConvertCoin) GetSignBytes() []byte {
return sdk.MustSortJSON(AminoCdc.MustMarshalJSON(&msg))
}