-
Notifications
You must be signed in to change notification settings - Fork 857
/
msg.go
115 lines (97 loc) · 3.5 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
package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
ibctransfertypes "github.com/cosmos/ibc-go/v3/modules/apps/transfer/types"
"github.com/ethereum/go-ethereum/common"
)
var (
_ sdk.Msg = &MsgConvertCoin{}
_ sdk.Msg = &MsgConvertERC20{}
)
const (
TypeMsgConvertCoin = "convert_coin"
TypeMsgConvertERC20 = "convert_ERC20"
)
// NewMsgConvertCoin creates a new instance of MsgConvertCoin
func NewMsgConvertCoin(coin sdk.Coin, receiver common.Address, sender sdk.AccAddress) *MsgConvertCoin { // nolint: interfacer
return &MsgConvertCoin{
Coin: coin,
Receiver: receiver.Hex(),
Sender: sender.String(),
}
}
// 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 err := ValidateErc20Denom(msg.Coin.Denom); err != nil {
if err := ibctransfertypes.ValidateIBCDenom(msg.Coin.Denom); err != nil {
return err
}
}
if !msg.Coin.Amount.IsPositive() {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "cannot mint a non-positive amount")
}
_, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
return sdkerrors.Wrap(err, "invalid sender address")
}
if !common.IsHexAddress(msg.Receiver) {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid receiver hex address %s", msg.Receiver)
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg *MsgConvertCoin) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
}
// GetSigners defines whose signature is required
func (msg MsgConvertCoin) GetSigners() []sdk.AccAddress {
addr, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
return nil
}
return []sdk.AccAddress{addr}
}
// NewMsgConvertERC20 creates a new instance of MsgConvertERC20
func NewMsgConvertERC20(amount sdk.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 sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid contract hex address '%s'", msg.ContractAddress)
}
if !msg.Amount.IsPositive() {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "cannot mint a non-positive amount")
}
_, err := sdk.AccAddressFromBech32(msg.Receiver)
if err != nil {
return sdkerrors.Wrap(err, "invalid reciver address")
}
if !common.IsHexAddress(msg.Sender) {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid sender hex address %s", msg.Sender)
}
return nil
}
// GetSignBytes encodes the message for signing
func (msg *MsgConvertERC20) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
}
// GetSigners defines whose signature is required
func (msg MsgConvertERC20) GetSigners() []sdk.AccAddress {
addr := common.HexToAddress(msg.Sender)
return []sdk.AccAddress{addr.Bytes()}
}