/
msg_parser.go
48 lines (41 loc) · 1.74 KB
/
msg_parser.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 wasm
import (
"encoding/json"
"github.com/CosmWasm/wasmd/x/wasm"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/magewar/mage/cosmwasm"
"github.com/magewar/mage/x/commons"
"github.com/magewar/mage/x/reactions/types"
)
var _ cosmwasm.MsgParserInterface = MsgsParser{}
type MsgsParser struct {
cdc codec.Codec
}
func NewWasmMsgParser(cdc codec.Codec) MsgsParser {
return MsgsParser{cdc: cdc}
}
func (parser MsgsParser) ParseCustomMsgs(contractAddr sdk.AccAddress, data json.RawMessage) ([]sdk.Msg, error) {
var msg types.ReactionsMsg
err := json.Unmarshal(data, &msg)
if err != nil {
return nil, sdkerrors.Wrapf(err, "failed to parse x/reactions message from contract %s", contractAddr.String())
}
switch {
case msg.AddReaction != nil:
return commons.HandleWasmMsg(parser.cdc, *msg.AddReaction, &types.MsgAddReaction{})
case msg.RemoveReaction != nil:
return commons.HandleWasmMsg(parser.cdc, *msg.RemoveReaction, &types.MsgRemoveReaction{})
case msg.AddRegisteredReaction != nil:
return commons.HandleWasmMsg(parser.cdc, *msg.AddRegisteredReaction, &types.MsgAddRegisteredReaction{})
case msg.EditRegisteredReaction != nil:
return commons.HandleWasmMsg(parser.cdc, *msg.EditRegisteredReaction, &types.MsgEditRegisteredReaction{})
case msg.RemoveRegisteredReaction != nil:
return commons.HandleWasmMsg(parser.cdc, *msg.RemoveRegisteredReaction, &types.MsgRemoveRegisteredReaction{})
case msg.SetReactionsParams != nil:
return commons.HandleWasmMsg(parser.cdc, *msg.SetReactionsParams, &types.MsgSetReactionsParams{})
default:
return nil, sdkerrors.Wrap(wasm.ErrInvalidMsg, "cosmwasm-reactions-msg-parser: message not supported")
}
}