-
Notifications
You must be signed in to change notification settings - Fork 202
/
message_buy.go
67 lines (56 loc) · 1.74 KB
/
message_buy.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
package types
import (
"strings"
sdkerrors "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
legacyerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
const TypeMsgBuy = "buy"
var _ sdk.Msg = &MsgBuy{}
func NewMsgBuy(creator, consumer, index string, duration uint64, autoRenewal, advancePurchase bool) *MsgBuy {
return &MsgBuy{
Creator: creator,
Consumer: consumer,
Index: index,
Duration: duration,
AutoRenewal: autoRenewal,
AdvancePurchase: advancePurchase,
}
}
func (msg *MsgBuy) Route() string {
return RouterKey
}
func (msg *MsgBuy) Type() string {
return TypeMsgBuy
}
func (msg *MsgBuy) GetSigners() []sdk.AccAddress {
creator, err := sdk.AccAddressFromBech32(msg.Creator)
if err != nil {
panic(err)
}
return []sdk.AccAddress{creator}
}
func (msg *MsgBuy) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}
func (msg *MsgBuy) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.Creator)
if err != nil {
return sdkerrors.Wrapf(legacyerrors.ErrInvalidAddress, "invalid creator address (%s)", err)
}
_, err = sdk.AccAddressFromBech32(msg.Consumer)
if err != nil {
return sdkerrors.Wrapf(legacyerrors.ErrInvalidAddress, "invalid consumer address (%s)", err)
}
if strings.TrimSpace(msg.Index) == "" {
return sdkerrors.Wrapf(ErrBlankParameter, "invalid plan index (%s)", msg.Index)
}
if msg.Duration == 0 || msg.Duration > MAX_SUBSCRIPTION_DURATION {
return sdkerrors.Wrapf(ErrInvalidParameter, "invalid subscription duration (%s)", msg.Index)
}
if msg.AutoRenewal && msg.AdvancePurchase {
return sdkerrors.Wrapf(ErrInvalidParameter, "can't use enable-auto-renewal and advance-purchase flags together")
}
return nil
}