/
msg_parser.go
46 lines (39 loc) · 1.54 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
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/gridiron-zone/huddle/cosmwasm"
"github.com/gridiron-zone/huddle/x/commons"
"github.com/gridiron-zone/huddle/x/reports/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.ReportsMsg
err := json.Unmarshal(data, &msg)
if err != nil {
return nil, sdkerrors.Wrapf(err, "failed to parse x/reports message from contract %s", contractAddr.String())
}
switch {
case msg.CreateReport != nil:
return commons.HandleWasmMsg(parser.cdc, *msg.CreateReport, &types.MsgCreateReport{})
case msg.DeleteReport != nil:
return commons.HandleWasmMsg(parser.cdc, *msg.DeleteReport, &types.MsgDeleteReport{})
case msg.SupportStandardReason != nil:
return commons.HandleWasmMsg(parser.cdc, *msg.SupportStandardReason, &types.MsgSupportStandardReason{})
case msg.AddReason != nil:
return commons.HandleWasmMsg(parser.cdc, *msg.AddReason, &types.MsgAddReason{})
case msg.RemoveReason != nil:
return commons.HandleWasmMsg(parser.cdc, *msg.RemoveReason, &types.MsgRemoveReason{})
default:
return nil, sdkerrors.Wrap(wasm.ErrInvalidMsg, "cosmwasm-reports-msg-parser: message not supported")
}
}