-
Notifications
You must be signed in to change notification settings - Fork 579
/
msgs.go
104 lines (91 loc) · 3.07 KB
/
msgs.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
package types
import (
"strings"
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
host "github.com/cosmos/ibc-go/v8/modules/core/24-host"
ibcerrors "github.com/cosmos/ibc-go/v8/modules/core/errors"
)
var (
_ sdk.Msg = (*MsgUpdateParams)(nil)
_ sdk.Msg = (*MsgTransfer)(nil)
_ sdk.HasValidateBasic = (*MsgUpdateParams)(nil)
_ sdk.HasValidateBasic = (*MsgTransfer)(nil)
)
// NewMsgUpdateParams creates a new MsgUpdateParams instance
func NewMsgUpdateParams(signer string, params Params) *MsgUpdateParams {
return &MsgUpdateParams{
Signer: signer,
Params: params,
}
}
// ValidateBasic implements sdk.Msg
func (msg MsgUpdateParams) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
return errorsmod.Wrapf(ibcerrors.ErrInvalidAddress, "string could not be parsed as address: %v", err)
}
return nil
}
// GetSigners implements sdk.Msg
func (msg MsgUpdateParams) GetSigners() []sdk.AccAddress {
accAddr, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
panic(err)
}
return []sdk.AccAddress{accAddr}
}
// NewMsgTransfer creates a new MsgTransfer instance
func NewMsgTransfer(
sourcePort, sourceChannel string,
token sdk.Coin, sender, receiver string,
timeoutHeight clienttypes.Height, timeoutTimestamp uint64,
memo string,
) *MsgTransfer {
return &MsgTransfer{
SourcePort: sourcePort,
SourceChannel: sourceChannel,
Token: token,
Sender: sender,
Receiver: receiver,
TimeoutHeight: timeoutHeight,
TimeoutTimestamp: timeoutTimestamp,
Memo: memo,
}
}
// ValidateBasic performs a basic check of the MsgTransfer fields.
// NOTE: timeout height or timestamp values can be 0 to disable the timeout.
// NOTE: The recipient addresses format is not validated as the format defined by
// the chain is not known to IBC.
func (msg MsgTransfer) ValidateBasic() error {
if err := host.PortIdentifierValidator(msg.SourcePort); err != nil {
return errorsmod.Wrap(err, "invalid source port ID")
}
if err := host.ChannelIdentifierValidator(msg.SourceChannel); err != nil {
return errorsmod.Wrap(err, "invalid source channel ID")
}
if !msg.Token.IsValid() {
return errorsmod.Wrap(ibcerrors.ErrInvalidCoins, msg.Token.String())
}
if !msg.Token.IsPositive() {
return errorsmod.Wrap(ibcerrors.ErrInsufficientFunds, msg.Token.String())
}
// NOTE: sender format must be validated as it is required by the GetSigners function.
_, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
return errorsmod.Wrapf(ibcerrors.ErrInvalidAddress, "string could not be parsed as address: %v", err)
}
if strings.TrimSpace(msg.Receiver) == "" {
return errorsmod.Wrap(ibcerrors.ErrInvalidAddress, "missing recipient address")
}
return ValidateIBCDenom(msg.Token.Denom)
}
// GetSigners implements sdk.Msg
func (msg MsgTransfer) GetSigners() []sdk.AccAddress {
signer, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
panic(err)
}
return []sdk.AccAddress{signer}
}