-
Notifications
You must be signed in to change notification settings - Fork 28
/
message_deposit.go
95 lines (78 loc) · 2.57 KB
/
message_deposit.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
package types
import (
"strings"
"github.com/sge-network/sge/utils"
"github.com/spf13/cast"
sdkerrors "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrtypes "github.com/cosmos/cosmos-sdk/types/errors"
)
const typeMsgDeposit = "house_deposit"
var _ sdk.Msg = &MsgDeposit{}
// NewMsgDeposit creates the new input for adding deposit to blockchain
func NewMsgDeposit(creator, marketUID string, amount sdkmath.Int, ticket string) *MsgDeposit {
return &MsgDeposit{
Creator: creator,
MarketUID: marketUID,
Amount: amount,
Ticket: ticket,
}
}
// Route return the message route for slashing
func (*MsgDeposit) Route() string { return RouterKey }
// Type returns the msg add market type
func (*MsgDeposit) Type() string { return typeMsgDeposit }
// GetSigners return the creators address
func (msg *MsgDeposit) GetSigners() []sdk.AccAddress {
creator, err := sdk.AccAddressFromBech32(msg.Creator)
if err != nil {
panic(err)
}
return []sdk.AccAddress{creator}
}
// GetSignBytes return the marshalled bytes of the msg
func (msg *MsgDeposit) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}
// ValidateBasic validates the input creation market
func (msg *MsgDeposit) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.Creator)
if err != nil {
return sdkerrors.Wrapf(sdkerrtypes.ErrInvalidAddress, "invalid creator address (%s)", err)
}
if !utils.IsValidUID(msg.MarketUID) {
return ErrInvalidMarketUID
}
if !msg.Amount.IsPositive() {
return sdkerrors.Wrap(
sdkerrtypes.ErrInvalidRequest,
"invalid deposit amount",
)
}
return nil
}
// ValidateSanity validates deposit acceptability
func (msg MsgDeposit) ValidateSanity(_ sdk.Context, p *Params) error {
if msg.Amount.LT(p.MinDeposit) {
return sdkerrors.Wrapf(
ErrDepositTooSmall, ": got %s, expected greater or equal to %d",
msg.Amount.String(), p.MinDeposit,
)
}
return nil
}
// EmitEvent emits the event for the message success.
func (msg *MsgDeposit) EmitEvent(ctx *sdk.Context, depositor string, participationIndex uint64, feeAmount sdkmath.Int) {
emitter := utils.NewEventEmitter(ctx, attributeValueCategory)
emitter.AddMsg(typeMsgDeposit, msg.Creator,
sdk.NewAttribute(attributeKeyCreator, msg.Creator),
sdk.NewAttribute(attributeKeyDepositor, depositor),
sdk.NewAttribute(attributeKeyDepositFee, feeAmount.String()),
sdk.NewAttribute(attributeKeyDepositMarketUIDParticipantIndex,
strings.Join([]string{msg.MarketUID, cast.ToString(participationIndex)}, "#"),
),
)
emitter.Emit()
}