-
Notifications
You must be signed in to change notification settings - Fork 0
/
market_prices.go
77 lines (64 loc) · 2.02 KB
/
market_prices.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
package process
import (
"reflect"
"github.com/jinxprotocol/v4-chain/protocol/x/prices/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
var (
msgUpdateMarketPricesType = reflect.TypeOf(types.MsgUpdateMarketPrices{})
)
// UpdateMarketPricesTx represents `MsgUpdateMarketPrices` tx that can be validated.
type UpdateMarketPricesTx struct {
ctx sdk.Context
pricesKeeper ProcessPricesKeeper
msg *types.MsgUpdateMarketPrices
}
// DecodeAddPremiumVotesTx returns a new `UpdateMarketPricesTx` after validating the following:
// - decodes the given tx bytes
// - checks the num of msgs in the tx matches expectations
// - checks the msg is of expected type
//
// If error occurs during any of the checks, returns error.
func DecodeUpdateMarketPricesTx(
ctx sdk.Context,
pricesKeeper ProcessPricesKeeper,
decoder sdk.TxDecoder,
txBytes []byte,
) (*UpdateMarketPricesTx, error) {
// Decode.
tx, err := decoder(txBytes)
if err != nil {
return nil, getDecodingError(msgUpdateMarketPricesType, err)
}
// Check msg length.
msgs := tx.GetMsgs()
if len(msgs) != 1 {
return nil, getUnexpectedNumMsgsError(msgUpdateMarketPricesType, 1, len(msgs))
}
// Check msg type.
updateMarketPrices, ok := msgs[0].(*types.MsgUpdateMarketPrices)
if !ok {
return nil, getUnexpectedMsgTypeError(msgUpdateMarketPricesType, msgs[0])
}
return &UpdateMarketPricesTx{
ctx: ctx,
pricesKeeper: pricesKeeper,
msg: updateMarketPrices,
}, nil
}
// Validate returns an error if:
// - the underlying msg fails `ValidateBasic`
// - the underlying msg values are not "valid" according to the index price.
func (umpt *UpdateMarketPricesTx) Validate() error {
if err := umpt.msg.ValidateBasic(); err != nil {
return getValidateBasicError(umpt.msg, err)
}
if err := umpt.pricesKeeper.PerformStatefulPriceUpdateValidation(umpt.ctx, umpt.msg, true); err != nil {
return err
}
return nil
}
// GetMsg returns the underlying `MsgUpdateMarketPrices`.
func (umpt *UpdateMarketPricesTx) GetMsg() sdk.Msg {
return umpt.msg
}