forked from umee-network/umee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msgs.go
180 lines (147 loc) · 5.44 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
"github.com/tendermint/tendermint/crypto/tmhash"
"github.com/mokitanetwork/katana/util/checkers"
)
var (
_ legacytx.LegacyMsg = &MsgDelegateFeedConsent{}
_ legacytx.LegacyMsg = &MsgAggregateExchangeRatePrevote{}
_ legacytx.LegacyMsg = &MsgAggregateExchangeRateVote{}
)
func NewMsgAggregateExchangeRatePrevote(
hash AggregateVoteHash,
feeder sdk.AccAddress,
validator sdk.ValAddress,
) *MsgAggregateExchangeRatePrevote {
return &MsgAggregateExchangeRatePrevote{
Hash: hash.String(),
Feeder: feeder.String(),
Validator: validator.String(),
}
}
// Route implements LegacyMsg interface
func (msg MsgAggregateExchangeRatePrevote) Route() string { return RouterKey }
// Type implements LegacyMsg interface
func (msg MsgAggregateExchangeRatePrevote) Type() string { return sdk.MsgTypeURL(&msg) }
// GetSignBytes implements sdk.Msg
func (msg MsgAggregateExchangeRatePrevote) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}
// GetSigners implements sdk.Msg
func (msg MsgAggregateExchangeRatePrevote) GetSigners() []sdk.AccAddress {
return checkers.Signers(msg.Feeder)
}
// ValidateBasic Implements sdk.Msg
func (msg MsgAggregateExchangeRatePrevote) ValidateBasic() error {
_, err := AggregateVoteHashFromHex(msg.Hash)
if err != nil {
return sdkerrors.Wrapf(ErrInvalidHash, "invalid vote hash (%s)", err)
}
// HEX encoding doubles the hash length
if len(msg.Hash) != tmhash.TruncatedSize*2 {
return ErrInvalidHashLength
}
_, err = sdk.AccAddressFromBech32(msg.Feeder)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid feeder address (%s)", err)
}
_, err = sdk.ValAddressFromBech32(msg.Validator)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid operator address (%s)", err)
}
return nil
}
func NewMsgAggregateExchangeRateVote(
salt string,
exchangeRates string,
feeder sdk.AccAddress,
validator sdk.ValAddress,
) *MsgAggregateExchangeRateVote {
return &MsgAggregateExchangeRateVote{
Salt: salt,
ExchangeRates: exchangeRates,
Feeder: feeder.String(),
Validator: validator.String(),
}
}
// Route implements LegacyMsg interface
func (msg MsgAggregateExchangeRateVote) Route() string { return RouterKey }
// Type implements LegacyMsg interface
func (msg MsgAggregateExchangeRateVote) Type() string { return sdk.MsgTypeURL(&msg) }
// GetSignBytes implements sdk.Msg
func (msg MsgAggregateExchangeRateVote) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}
// GetSigners implements sdk.Msg
func (msg MsgAggregateExchangeRateVote) GetSigners() []sdk.AccAddress {
return checkers.Signers(msg.Feeder)
}
// ValidateBasic implements sdk.Msg
func (msg MsgAggregateExchangeRateVote) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.Feeder)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid feeder address (%s)", err)
}
_, err = sdk.ValAddressFromBech32(msg.Validator)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid operator address (%s)", err)
}
if l := len(msg.ExchangeRates); l == 0 {
return sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "must provide at least one oracle exchange rate")
} else if l > 4096 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "exchange rates string can not exceed 4096 characters")
}
exchangeRates, err := ParseExchangeRateTuples(msg.ExchangeRates)
if err != nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, "failed to parse exchange rates string cause: "+err.Error())
}
for _, exchangeRate := range exchangeRates {
// check overflow bit length
if exchangeRate.ExchangeRate.BigInt().BitLen() > 255+sdk.DecimalPrecisionBits {
return sdkerrors.Wrap(ErrInvalidExchangeRate, "overflow")
}
}
if len(msg.Salt) != 64 {
return ErrInvalidSaltLength
}
_, err = AggregateVoteHashFromHex(msg.Salt)
if err != nil {
return sdkerrors.Wrap(ErrInvalidSaltFormat, "salt must be a valid hex string")
}
return nil
}
// NewMsgDelegateFeedConsent creates a MsgDelegateFeedConsent instance
func NewMsgDelegateFeedConsent(operatorAddress sdk.ValAddress, feederAddress sdk.AccAddress) *MsgDelegateFeedConsent {
return &MsgDelegateFeedConsent{
Operator: operatorAddress.String(),
Delegate: feederAddress.String(),
}
}
// Route implements LegacyMsg interface
func (msg MsgDelegateFeedConsent) Route() string { return RouterKey }
// Type implements LegacyMsg interface
func (msg MsgDelegateFeedConsent) Type() string { return sdk.MsgTypeURL(&msg) }
// GetSignBytes implements sdk.Msg
func (msg MsgDelegateFeedConsent) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}
// GetSigners implements sdk.Msg
func (msg MsgDelegateFeedConsent) GetSigners() []sdk.AccAddress {
operator, _ := sdk.ValAddressFromBech32(msg.Operator)
return []sdk.AccAddress{sdk.AccAddress(operator)}
}
// ValidateBasic implements sdk.Msg
func (msg MsgDelegateFeedConsent) ValidateBasic() error {
_, err := sdk.ValAddressFromBech32(msg.Operator)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid operator address (%s)", err)
}
_, err = sdk.AccAddressFromBech32(msg.Delegate)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid delegate address (%s)", err)
}
return nil
}