/
message_withdraw.go
76 lines (62 loc) · 1.93 KB
/
message_withdraw.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
package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/furya-network/furya/utils"
)
const typeMsgWithdraw = "withdraw"
var _ sdk.Msg = &MsgWithdraw{}
// NewMsgWithdraw creates the new input for withdrawal of a deposit
func NewMsgWithdraw(creator string, marketUID string, amount sdk.Int, participationIndex uint64, mode WithdrawalMode) *MsgWithdraw {
return &MsgWithdraw{
Creator: creator,
MarketUID: marketUID,
ParticipationIndex: participationIndex,
Mode: mode,
Amount: amount,
}
}
// Route return the message route for slashing
func (msg *MsgWithdraw) Route() string {
return RouterKey
}
// Type returns the msg add market type
func (msg *MsgWithdraw) Type() string {
return typeMsgWithdraw
}
// GetSigners return the creators address
func (msg *MsgWithdraw) 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 *MsgWithdraw) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}
// ValidateBasic validates the input creation market
func (msg *MsgWithdraw) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.Creator)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid creator address (%s)", err)
}
if msg.Mode != WithdrawalMode_WITHDRAWAL_MODE_FULL &&
msg.Mode != WithdrawalMode_WITHDRAWAL_MODE_PARTIAL {
return ErrInvalidWithdrawMode
}
if !utils.IsValidUID(msg.MarketUID) {
return ErrInvalidMarketUID
}
if msg.ParticipationIndex < 1 {
return ErrInvalidIndex
}
if msg.Mode == WithdrawalMode_WITHDRAWAL_MODE_PARTIAL {
if !msg.Amount.IsPositive() {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "invalid withdrawal amount")
}
}
return nil
}