/
msg.go
67 lines (60 loc) · 1.89 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
package types
import (
sdkerrors "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
)
var (
_ sdk.Msg = (*MsgSwapRequest)(nil)
)
func NewMsgSwapRequest(from sdk.AccAddress, txHash EthereumHash, receiver sdk.AccAddress, amount sdkmath.Int) *MsgSwapRequest {
return &MsgSwapRequest{
From: from.String(),
TxHash: txHash.Bytes(),
Receiver: receiver.String(),
Amount: amount,
}
}
func (m *MsgSwapRequest) ValidateBasic() error {
if m.From == "" {
return sdkerrors.Wrap(ErrorInvalidFrom, "from cannot be empty")
}
if _, err := sdk.AccAddressFromBech32(m.From); err != nil {
return sdkerrors.Wrapf(ErrorInvalidFrom, "%s", err)
}
if m.Receiver == "" {
return sdkerrors.Wrap(ErrorInvalidReceiver, "receiver cannot be empty")
}
if _, err := sdk.AccAddressFromBech32(m.Receiver); err != nil {
return sdkerrors.Wrapf(ErrorInvalidReceiver, "%s", err)
}
if m.TxHash == nil {
return sdkerrors.Wrap(ErrorInvalidTxHash, "tx_hash cannot be nil")
}
if len(m.TxHash) == 0 {
return sdkerrors.Wrap(ErrorInvalidTxHash, "tx_hash cannot be empty")
}
if len(m.TxHash) < EthereumHashLength {
return sdkerrors.Wrapf(ErrorInvalidTxHash, "tx_hash length cannot be less than %d", EthereumHashLength)
}
if len(m.TxHash) > EthereumHashLength {
return sdkerrors.Wrapf(ErrorInvalidTxHash, "tx_hash length cannot be greater than %d", EthereumHashLength)
}
if m.Amount.IsNegative() {
return sdkerrors.Wrap(ErrorInvalidAmount, "amount cannot be negative")
}
if m.Amount.IsZero() {
return sdkerrors.Wrap(ErrorInvalidAmount, "amount cannot be zero")
}
if m.Amount.LT(PrecisionLoss) {
return sdkerrors.Wrapf(ErrorInvalidAmount, "amount cannot be less than %s", PrecisionLoss)
}
return nil
}
func (m *MsgSwapRequest) GetSigners() []sdk.AccAddress {
from, err := sdk.AccAddressFromBech32(m.From)
if err != nil {
panic(err)
}
return []sdk.AccAddress{from}
}