-
Notifications
You must be signed in to change notification settings - Fork 16
/
message_beam_open.go
78 lines (66 loc) · 2.38 KB
/
message_beam_open.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
package types
import (
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
var _ sdk.Msg = &MsgOpenBeam{}
// NewMsgOpenBeam Build a open beam message based on parameters
func NewMsgOpenBeam(id string, creator string, owner string, amount *sdk.Coin, secret string, schema string, data *BeamData, closesAtBlock int32, claimExpiresAtBlock int32) *MsgOpenBeam {
return &MsgOpenBeam{
Id: id,
CreatorAddress: creator,
Amount: amount,
Secret: secret,
Schema: schema,
Data: data,
ClaimAddress: owner,
ClaimExpiresAtBlock: claimExpiresAtBlock,
ClosesAtBlock: closesAtBlock,
}
}
// Route dunno
func (msg MsgOpenBeam) Route() string {
return RouterKey
}
// Type Return the message type
func (msg MsgOpenBeam) Type() string {
return "OpenBeam"
}
// GetSigners Return the list of signers for the given message
func (msg *MsgOpenBeam) GetSigners() []sdk.AccAddress {
creator, err := sdk.AccAddressFromBech32(msg.GetCreatorAddress())
if err != nil {
panic(err)
}
return []sdk.AccAddress{creator}
}
// GetSignBytes Return the generated bytes from the signature
func (msg *MsgOpenBeam) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}
// ValidateBasic Validate the message payload before dispatching to the local kv store
func (msg *MsgOpenBeam) ValidateBasic() error {
if len(msg.Id) <= 0 {
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "Invalid id supplied (%d)", len(msg.Id))
}
// Ensure the address is correct and that we are able to acquire it
_, err := sdk.AccAddressFromBech32(msg.GetCreatorAddress())
if err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "Invalid creator address (%s)", err)
}
// Validate the secret
if len(msg.Secret) <= 0 {
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "Invalid secret supplied")
}
// Validate the schema
if msg.GetSchema() != BEAM_SCHEMA_REVIEW && msg.GetSchema() != BEAM_SCHEMA_REWARD {
return errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "Invalid schema must be review or reward")
}
// If we have an amount, make sure it is not negative nor zero
if msg.Amount != nil && msg.Amount.IsNegative() {
return errorsmod.Wrapf(sdkerrors.ErrInvalidCoins, "Invalid amount: must be greater or equal 0")
}
return nil
}