-
Notifications
You must be signed in to change notification settings - Fork 197
/
message_register_host_zone.go
135 lines (121 loc) · 4.33 KB
/
message_register_host_zone.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
package types
import (
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
errorsmod "cosmossdk.io/errors"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
ibctransfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
"github.com/Stride-Labs/stride/v17/utils"
)
const TypeMsgRegisterHostZone = "register_host_zone"
var _ sdk.Msg = &MsgRegisterHostZone{}
func NewMsgRegisterHostZone(
creator string,
connectionId string,
bech32prefix string,
hostDenom string,
ibcDenom string,
transferChannelId string,
unbondingPeriod uint64,
minRedemptionRate sdk.Dec,
maxRedemptionRate sdk.Dec,
lsmLiquidStakeEnabled bool,
) *MsgRegisterHostZone {
return &MsgRegisterHostZone{
Creator: creator,
ConnectionId: connectionId,
Bech32Prefix: bech32prefix,
HostDenom: hostDenom,
IbcDenom: ibcDenom,
TransferChannelId: transferChannelId,
UnbondingPeriod: unbondingPeriod,
MinRedemptionRate: minRedemptionRate,
MaxRedemptionRate: maxRedemptionRate,
LsmLiquidStakeEnabled: lsmLiquidStakeEnabled,
}
}
func (msg *MsgRegisterHostZone) Route() string {
return RouterKey
}
func (msg *MsgRegisterHostZone) Type() string {
return TypeMsgRegisterHostZone
}
func (msg *MsgRegisterHostZone) GetSigners() []sdk.AccAddress {
creator, err := sdk.AccAddressFromBech32(msg.Creator)
if err != nil {
panic(err)
}
return []sdk.AccAddress{creator}
}
func (msg *MsgRegisterHostZone) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}
func (msg *MsgRegisterHostZone) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.Creator)
if err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid creator address (%s)", err)
}
if err := utils.ValidateAdminAddress(msg.Creator); err != nil {
return err
}
// VALIDATE DENOMS
// host denom cannot be empty
if msg.HostDenom == "" {
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "host denom cannot be empty")
}
// host denom must be a valid asset denom
if err := sdk.ValidateDenom(msg.HostDenom); err != nil {
return err
}
// ibc denom cannot be empty and must begin with "ibc"
if msg.IbcDenom == "" {
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "ibc denom cannot be empty")
}
if !strings.HasPrefix(msg.IbcDenom, "ibc") {
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "ibc denom must begin with 'ibc'")
}
// ibc denom must be valid
err = ibctransfertypes.ValidateIBCDenom(msg.IbcDenom)
if err != nil {
return err
}
// bech32 prefix must be non-empty (we validate it fully in msg_server)
if strings.TrimSpace(msg.Bech32Prefix) == "" {
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "bech32 prefix must be non-empty")
}
// connection id cannot be empty and must begin with "connection"
if msg.ConnectionId == "" {
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "connection id cannot be empty")
}
if !strings.HasPrefix(msg.ConnectionId, "connection") {
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "connection id must begin with 'connection'")
}
// transfer channel id cannot be empty
if msg.TransferChannelId == "" {
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "transfer channel id cannot be empty")
}
// transfer channel id must begin with "channel"
if !strings.HasPrefix(msg.TransferChannelId, "channel") {
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "transfer channel id must begin with 'channel'")
}
// unbonding frequency must be positive nonzero
if msg.UnbondingPeriod < 1 {
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "unbonding frequency must be greater than zero")
}
// min/max redemption rate check
if !msg.MinRedemptionRate.IsNil() && msg.MinRedemptionRate.IsNegative() {
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "min redemption rate should not be negative")
}
if !msg.MaxRedemptionRate.IsNil() && msg.MaxRedemptionRate.IsNegative() {
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "max redemption rate should not be negative")
}
if !msg.MinRedemptionRate.IsNil() &&
!msg.MaxRedemptionRate.IsNil() &&
!msg.MinRedemptionRate.IsZero() &&
!msg.MaxRedemptionRate.IsZero() &&
msg.MinRedemptionRate.GTE(msg.MaxRedemptionRate) {
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "min redemption rate should be lower than max redemption rate")
}
return nil
}