-
Notifications
You must be signed in to change notification settings - Fork 44
/
msg_parser.go
85 lines (75 loc) · 2.7 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
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
package cosmwasm
import (
"encoding/json"
"cosmossdk.io/errors"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
poststypes "github.com/desmos-labs/desmos/v7/x/posts/types"
profilestypes "github.com/desmos-labs/desmos/v7/x/profiles/types"
reactionstypes "github.com/desmos-labs/desmos/v7/x/reactions/types"
relationshipstypes "github.com/desmos-labs/desmos/v7/x/relationships/types"
reportstypes "github.com/desmos-labs/desmos/v7/x/reports/types"
subspacestypes "github.com/desmos-labs/desmos/v7/x/subspaces/types"
)
const (
WasmMsgParserRouteProfiles = profilestypes.ModuleName
WasmMsgParserRouteSubspaces = subspacestypes.ModuleName
WasmMsgParserRouteRelationships = relationshipstypes.ModuleName
WasmMsgParserRoutePosts = poststypes.ModuleName
WasmMsgParserRouteReports = reportstypes.ModuleName
WasmMsgParserRouteReactions = reactionstypes.ModuleName
)
type MsgParserInterface interface {
ParseCustomMsgs(contractAddr sdk.AccAddress, data json.RawMessage) ([]sdk.Msg, error)
}
type ParserRouter struct {
Parsers map[string]MsgParserInterface
}
func NewParserRouter() ParserRouter {
return ParserRouter{
Parsers: make(map[string]MsgParserInterface),
}
}
type CustomMsg struct {
Profiles *json.RawMessage `json:"profiles"`
Subspaces *json.RawMessage `json:"subspaces"`
Relationships *json.RawMessage `json:"relationships"`
Posts *json.RawMessage `json:"posts"`
Reports *json.RawMessage `json:"reports"`
Reactions *json.RawMessage `json:"reactions"`
}
func (router ParserRouter) ParseCustom(contractAddr sdk.AccAddress, data json.RawMessage) ([]sdk.Msg, error) {
var customMsg CustomMsg
err := json.Unmarshal(data, &customMsg)
if err != nil {
return nil, errors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
// get route and msg from data
var route string
var msg json.RawMessage
switch {
case customMsg.Profiles != nil:
route = WasmMsgParserRouteProfiles
msg = *customMsg.Profiles
case customMsg.Subspaces != nil:
route = WasmMsgParserRouteSubspaces
msg = *customMsg.Subspaces
case customMsg.Relationships != nil:
route = WasmMsgParserRouteRelationships
msg = *customMsg.Relationships
case customMsg.Posts != nil:
route = WasmMsgParserRoutePosts
msg = *customMsg.Posts
case customMsg.Reports != nil:
route = WasmMsgParserRouteReports
msg = *customMsg.Reports
case customMsg.Reactions != nil:
route = WasmMsgParserRouteReactions
msg = *customMsg.Reactions
}
if parser, ok := router.Parsers[route]; ok {
return parser.ParseCustomMsgs(contractAddr, msg)
}
return nil, errors.Wrap(wasmtypes.ErrInvalidMsg, "unimplemented route")
}