-
Notifications
You must be signed in to change notification settings - Fork 365
/
msgs.go
79 lines (67 loc) · 2.19 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
package types
import (
"errors"
"fmt"
"strings"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
const (
// TypeMsgPostPrice type of PostPrice msg
TypeMsgPostPrice = "post_price"
// MaxExpiry defines the max expiry time defined as UNIX time (9999-12-31 23:59:59 +0000 UTC)
MaxExpiry = 253402300799
)
// ensure Msg interface compliance at compile time
var _ sdk.Msg = &MsgPostPrice{}
// MsgPostPrice struct representing a posted price message.
// Used by oracles to input prices to the pricefeed
type MsgPostPrice struct {
From sdk.AccAddress `json:"from" yaml:"from"` // client that sent in this address
MarketID string `json:"market_id" yaml:"market_id"` // asset code used by exchanges/api
Price sdk.Dec `json:"price" yaml:"price"` // price in decimal (max precision 18)
Expiry time.Time `json:"expiry" yaml:"expiry"` // expiry time
}
// NewMsgPostPrice creates a new post price msg
func NewMsgPostPrice(
from sdk.AccAddress,
assetCode string,
price sdk.Dec,
expiry time.Time) MsgPostPrice {
return MsgPostPrice{
From: from,
MarketID: assetCode,
Price: price,
Expiry: expiry,
}
}
// Route Implements Msg.
func (msg MsgPostPrice) Route() string { return RouterKey }
// Type Implements Msg
func (msg MsgPostPrice) Type() string { return TypeMsgPostPrice }
// GetSignBytes Implements Msg.
func (msg MsgPostPrice) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}
// GetSigners Implements Msg.
func (msg MsgPostPrice) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.From}
}
// ValidateBasic does a simple validation check that doesn't require access to any other information.
func (msg MsgPostPrice) ValidateBasic() error {
if msg.From.Empty() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "sender address cannot be empty")
}
if strings.TrimSpace(msg.MarketID) == "" {
return errors.New("market id cannot be blank")
}
if msg.Price.IsNegative() {
return fmt.Errorf("price cannot be negative: %s", msg.Price.String())
}
if msg.Expiry.Unix() <= 0 {
return errors.New("must set an expiration time")
}
return nil
}