-
Notifications
You must be signed in to change notification settings - Fork 19
/
msg.go
115 lines (99 loc) · 3.01 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"
"github.com/ethereum/go-ethereum/common"
)
// ensure Msg interface compliance at compile time
var (
_ sdk.Msg = &MsgBridgeEthereumToKava{}
_ sdk.Msg = &MsgConvertCoinToERC20{}
)
// NewMsgBridgeEthereumToKava returns a new MsgBridgeEthereumToKava
func NewMsgBridgeEthereumToKava(
relayer string,
ethereumERC20Address string,
amount sdk.Int,
receiver string,
sequence sdk.Int,
) MsgBridgeEthereumToKava {
return MsgBridgeEthereumToKava{
Relayer: relayer,
EthereumERC20Address: ethereumERC20Address,
Amount: amount,
Receiver: receiver,
Sequence: sequence,
}
}
// GetSigners returns the addresses of signers that must sign.
func (msg MsgBridgeEthereumToKava) GetSigners() []sdk.AccAddress {
sender, err := sdk.AccAddressFromBech32(msg.Relayer)
if err != nil {
panic(err)
}
return []sdk.AccAddress{sender}
}
// ValidateBasic does a simple validation check that doesn't require access to any other information.
func (msg MsgBridgeEthereumToKava) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.Relayer)
if err != nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, err.Error())
}
if !common.IsHexAddress(msg.EthereumERC20Address) {
return sdkerrors.Wrap(
sdkerrors.ErrInvalidAddress,
"ethereum ERC20 address is not a valid hex address",
)
}
if msg.Amount.IsZero() || msg.Amount.IsNegative() {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "amount must be positive non-zero %s", msg.Amount)
}
if !common.IsHexAddress(msg.Receiver) {
return sdkerrors.Wrap(
sdkerrors.ErrInvalidAddress,
"receiver address is not a valid hex address",
)
}
if msg.Sequence.IsNegative() {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidSequence, "sequence is negative %s", msg.Sequence)
}
return nil
}
// NewMsgConvertCoinToERC20 returns a new MsgConvertCoinToERC20
func NewMsgConvertCoinToERC20(
initiator string,
receiver string,
amount sdk.Coin,
) MsgConvertCoinToERC20 {
return MsgConvertCoinToERC20{
Initiator: initiator,
Receiver: receiver,
Amount: &amount,
}
}
// GetSigners returns the addresses of signers that must sign.
func (msg MsgConvertCoinToERC20) GetSigners() []sdk.AccAddress {
sender, err := sdk.AccAddressFromBech32(msg.Initiator)
if err != nil {
panic(err)
}
return []sdk.AccAddress{sender}
}
// ValidateBasic does a simple validation check that doesn't require access to any other information.
func (msg MsgConvertCoinToERC20) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.Initiator)
if err != nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, err.Error())
}
if !common.IsHexAddress(msg.Receiver) {
return sdkerrors.Wrap(
sdkerrors.ErrInvalidAddress,
"Receiver is not a valid hex address",
)
}
if msg.Amount.IsZero() {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "amount cannot be zero")
}
// Checks for negative
return msg.Amount.Validate()
}