-
Notifications
You must be signed in to change notification settings - Fork 4
/
msgs.go
48 lines (41 loc) · 1.46 KB
/
msgs.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
package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// RouterKey is the name of the coinswap module
const RouterKey = ModuleName
// Route returns the route of MsgExchange - "coinswap" (sdk.Msg interface).
func (msg MsgExchange) Route() string { return RouterKey }
// Type returns the message type of MsgExchange (sdk.Msg interface).
func (msg MsgExchange) Type() string { return "exchange" }
// ValidateBasic checks whether the given MsgExchange instance (sdk.Msg interface).
func (msg MsgExchange) ValidateBasic() error {
requester, err := sdk.AccAddressFromBech32(msg.Requester)
if err != nil {
return err
}
if err := sdk.VerifyAddressFormat(requester); err != nil {
return sdkerrors.Wrapf(err, "requester: %s", requester)
}
if ok := msg.Amount.IsValid(); !ok {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidCoins, "amount: %s", msg.Amount)
}
if msg.To == "" || msg.From == "" {
return sdkerrors.Wrapf(ErrInvalidExchangeDenom, "denominations: %s:%s", msg.From, msg.To)
}
if msg.From != msg.Amount.Denom {
return sdkerrors.Wrapf(ErrExchangeDenomMissmatch, "denominations: %s:%s", msg.From, msg.To)
}
return nil
}
func (msg MsgExchange) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}
func (msg MsgExchange) GetSigners() []sdk.AccAddress {
addr, err := sdk.AccAddressFromBech32(msg.Requester)
if err != nil {
panic(err)
}
return []sdk.AccAddress{addr}
}