-
Notifications
You must be signed in to change notification settings - Fork 39
/
msgs.go
191 lines (162 loc) · 5.85 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
181
182
183
184
185
186
187
188
189
190
191
package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
const RouterKey = ModuleName
// oracle message types
const (
TypeMsgRelayPriceFeedPrice = "relayPriceFeedPrice"
TypeMsgRelayBandRates = "relayBandRates"
TypeMsgRelayCoinbaseMessages = "relayCoinbaseMessages"
TypeMsgRequestBandIBCRates = "requestBandIBCRates"
)
var (
_ sdk.Msg = &MsgRelayPriceFeedPrice{}
_ sdk.Msg = &MsgRelayBandRates{}
_ sdk.Msg = &MsgRelayCoinbaseMessages{}
_ sdk.Msg = &MsgRequestBandIBCRates{}
)
// Route implements the sdk.Msg interface. It should return the name of the module
func (msg MsgRelayPriceFeedPrice) Route() string { return RouterKey }
// Type implements the sdk.Msg interface. It should return the action.
func (msg MsgRelayPriceFeedPrice) Type() string { return TypeMsgRelayPriceFeedPrice }
// ValidateBasic implements the sdk.Msg interface. It runs stateless checks on the message
func (msg MsgRelayPriceFeedPrice) ValidateBasic() error {
if msg.Sender == "" {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Sender)
}
if _, err := sdk.AccAddressFromBech32(msg.Sender); err != nil {
return err
}
priceCount := len(msg.Price)
if len(msg.Base) != priceCount {
return ErrBadPriceFeedBaseCount
}
if len(msg.Quote) != priceCount {
return ErrBadPriceFeedQuoteCount
}
for _, price := range msg.Price {
if !price.IsPositive() {
return ErrBadPrice
}
if price.GT(LargestDecPrice) {
return ErrPriceTooLarge
}
}
return nil
}
// GetSignBytes implements the sdk.Msg interface. It encodes the message for signing
func (msg *MsgRelayPriceFeedPrice) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
}
// GetSigners implements the sdk.Msg interface. It defines whose signature is required
func (msg MsgRelayPriceFeedPrice) GetSigners() []sdk.AccAddress {
sender, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
panic(err)
}
return []sdk.AccAddress{sender}
}
// Route implements the sdk.Msg interface. It should return the name of the module
func (msg MsgRelayBandRates) Route() string { return RouterKey }
// Type implements the sdk.Msg interface. It should return the action.
func (msg MsgRelayBandRates) Type() string { return TypeMsgRelayBandRates }
// ValidateBasic implements the sdk.Msg interface for MsgRelay.
func (msg MsgRelayBandRates) ValidateBasic() error {
if msg.Relayer == "" {
return ErrEmptyRelayerAddr
}
// check that the sizes of symbols,rates,resolveTimes,requestIDs are equal
symbolsCount := len(msg.Symbols)
if len(msg.Rates) != symbolsCount {
return ErrBadRatesCount
}
if len(msg.ResolveTimes) != symbolsCount {
return ErrBadResolveTimesCount
}
if len(msg.RequestIDs) != symbolsCount {
return ErrBadRequestIDsCount
}
return nil
}
// GetSignBytes implements the sdk.Msg interface. It encodes the message for signing
func (msg *MsgRelayBandRates) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
}
// GetSigners implements the sdk.Msg interface. It defines whose signature is required
func (msg MsgRelayBandRates) GetSigners() []sdk.AccAddress {
sender, err := sdk.AccAddressFromBech32(msg.Relayer)
if err != nil {
panic(err)
}
return []sdk.AccAddress{sender}
}
// Route implements the sdk.Msg interface. It should return the name of the module
func (msg MsgRelayCoinbaseMessages) Route() string { return RouterKey }
// Type implements the sdk.Msg interface. It should return the action.
func (msg MsgRelayCoinbaseMessages) Type() string { return TypeMsgRelayCoinbaseMessages }
// ValidateBasic implements the sdk.Msg interface for MsgRelay.
func (msg MsgRelayCoinbaseMessages) ValidateBasic() error {
if msg.Sender == "" {
return ErrEmptyRelayerAddr
}
// check that the sizes of messages and signatures are equal
if len(msg.Signatures) != len(msg.Messages) || len(msg.Messages) == 0 {
return ErrBadMessagesCount
}
return nil
}
// GetSignBytes implements the sdk.Msg interface. It encodes the message for signing
func (msg *MsgRelayCoinbaseMessages) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
}
// GetSigners implements the sdk.Msg interface. It defines whose signature is required
func (msg MsgRelayCoinbaseMessages) GetSigners() []sdk.AccAddress {
sender, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
panic(err)
}
return []sdk.AccAddress{sender}
}
// NewMsgRequestBandIBCRates creates a new MsgRequestBandIBCRates instance.
func NewMsgRequestBandIBCRates(
sender sdk.AccAddress,
requestID uint64,
) *MsgRequestBandIBCRates {
return &MsgRequestBandIBCRates{
Sender: sender.String(),
RequestId: requestID,
}
}
// Route implements the sdk.Msg interface for MsgRequestData.
func (msg MsgRequestBandIBCRates) Route() string { return RouterKey }
// Type implements the sdk.Msg interface for MsgRequestData.
func (msg MsgRequestBandIBCRates) Type() string { return TypeMsgRequestBandIBCRates }
// ValidateBasic implements the sdk.Msg interface for MsgRequestData.
func (msg MsgRequestBandIBCRates) ValidateBasic() error {
sender, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
return err
}
if sender.Empty() {
return sdkerrors.Wrapf(ErrInvalidBandIBCRequest, "MsgRequestBandIBCRates: Sender address must not be empty.")
}
if msg.RequestId == 0 {
return sdkerrors.Wrapf(ErrInvalidBandIBCRequest, "MsgRequestBandIBCRates: requestID should be greater than zero")
}
return nil
}
// GetSigners implements the sdk.Msg interface for MsgRequestData.
func (msg MsgRequestBandIBCRates) GetSigners() []sdk.AccAddress {
sender, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
panic(err)
}
return []sdk.AccAddress{sender}
}
// GetSignBytes implements the sdk.Msg interface for MsgRequestData.
func (msg MsgRequestBandIBCRates) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}