-
Notifications
You must be signed in to change notification settings - Fork 193
/
message_create_trade_route.go
139 lines (118 loc) · 4.46 KB
/
message_create_trade_route.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
139
package types
import (
"regexp"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
errorsmod "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
)
const TypeMsgCreateTradeRoute = "create_trade_route"
const (
ConnectionIdRegex = `^connection-\d+$`
ChannelIdRegex = `^channel-\d+$`
IBCPrefix = "ibc/"
)
var (
_ sdk.Msg = &MsgCreateTradeRoute{}
_ legacytx.LegacyMsg = &MsgCreateTradeRoute{}
)
func (msg *MsgCreateTradeRoute) Type() string {
return TypeMsgCreateTradeRoute
}
func (msg *MsgCreateTradeRoute) Route() string {
return RouterKey
}
func (msg *MsgCreateTradeRoute) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}
func (msg *MsgCreateTradeRoute) GetSigners() []sdk.AccAddress {
addr, _ := sdk.AccAddressFromBech32(msg.Authority)
return []sdk.AccAddress{addr}
}
func (msg *MsgCreateTradeRoute) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(msg.Authority); err != nil {
return errorsmod.Wrap(err, "invalid authority address")
}
if msg.HostChainId == "" {
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "host chain ID cannot be empty")
}
if err := ValidateConnectionId(msg.StrideToRewardConnectionId); err != nil {
return errorsmod.Wrap(err, "invalid stride to reward connection ID")
}
if err := ValidateConnectionId(msg.StrideToTradeConnectionId); err != nil {
return errorsmod.Wrap(err, "invalid stride to trade connection ID")
}
if err := ValidateChannelId(msg.HostToRewardTransferChannelId); err != nil {
return errorsmod.Wrap(err, "invalid host to reward channel ID")
}
if err := ValidateChannelId(msg.RewardToTradeTransferChannelId); err != nil {
return errorsmod.Wrap(err, "invalid reward to trade channel ID")
}
if err := ValidateChannelId(msg.TradeToHostTransferChannelId); err != nil {
return errorsmod.Wrap(err, "invalid trade to host channel ID")
}
if err := ValidateDenom(msg.RewardDenomOnHost, true); err != nil {
return errorsmod.Wrap(err, "invalid reward denom on host")
}
if err := ValidateDenom(msg.RewardDenomOnReward, false); err != nil {
return errorsmod.Wrap(err, "invalid reward denom on reward")
}
if err := ValidateDenom(msg.RewardDenomOnTrade, true); err != nil {
return errorsmod.Wrap(err, "invalid reward denom on trade")
}
if err := ValidateDenom(msg.HostDenomOnTrade, true); err != nil {
return errorsmod.Wrap(err, "invalid host denom on trade")
}
if err := ValidateDenom(msg.HostDenomOnHost, false); err != nil {
return errorsmod.Wrap(err, "invalid host denom on host")
}
if msg.PoolId < 1 {
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "invalid pool id")
}
if msg.MaxSwapAmount.GT(sdkmath.ZeroInt()) && msg.MinSwapAmount.GT(msg.MaxSwapAmount) {
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "min swap amount cannot be greater than max swap amount")
}
maxAllowedSwapLossRate, err := sdk.NewDecFromStr(msg.MaxAllowedSwapLossRate)
if err != nil {
return errorsmod.Wrapf(err, "unable to cast max allowed swap loss rate to a decimal")
}
if maxAllowedSwapLossRate.LT(sdk.ZeroDec()) || maxAllowedSwapLossRate.GT(sdk.OneDec()) {
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "max allowed swap loss rate must be between 0 and 1")
}
return nil
}
// Helper function to validate a connection Id
func ValidateConnectionId(connectionId string) error {
matched, err := regexp.MatchString(ConnectionIdRegex, connectionId)
if err != nil {
return errorsmod.Wrapf(err, "unable to verify connnection-id (%s)", connectionId)
}
if !matched {
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "invalid connection-id (%s), must be of the format 'connection-{N}'", connectionId)
}
return nil
}
// Helper function to validate a channel Id
func ValidateChannelId(channelId string) error {
matched, err := regexp.MatchString(ChannelIdRegex, channelId)
if err != nil {
return errorsmod.Wrapf(err, "unable to verify channel-id (%s)", channelId)
}
if !matched {
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "invalid channel-id (%s), must be of the format 'channel-{N}'", channelId)
}
return nil
}
// Helper function to validate a denom
func ValidateDenom(denom string, ibc bool) error {
if denom == "" {
return errorsmod.Wrap(ErrInvalidDenom, "denom is empty")
}
if ibc && !strings.HasPrefix(denom, IBCPrefix) {
return errorsmod.Wrapf(ErrInvalidDenom, "denom (%s) should have ibc prefix", denom)
}
return nil
}