Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update wasm parser for v1.0.0 cosmwasm std #946

Merged
merged 4 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions cosmwasm/msg_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,46 @@ func NewParserRouter() ParserRouter {
}

type CustomMsg struct {
Route string `json:"route"`
MsgData json.RawMessage `json:"msg_data"`
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, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}

if parser, ok := router.Parsers[customMsg.Route]; ok {
return parser.ParseCustomMsgs(contractAddr, customMsg.MsgData)
// 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
}

return nil, sdkerrors.Wrap(wasm.ErrInvalidMsg, customMsg.Route)
if parser, ok := router.Parsers[route]; ok {
return parser.ParseCustomMsgs(contractAddr, msg)
}
return nil, sdkerrors.Wrap(wasm.ErrInvalidMsg, "unimplemented route")
}
39 changes: 32 additions & 7 deletions cosmwasm/query_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ func NewQuerier(queriers map[string]Querier) QuerierRouter {
}

type CustomQuery struct {
Route string `json:"route"`
QueryData json.RawMessage `json:"query_data"`
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"`
}

const (
Expand All @@ -46,14 +50,35 @@ const (
func (q QuerierRouter) QueryCustom(ctx sdk.Context, data json.RawMessage) ([]byte, error) {
var customQuery CustomQuery
err := json.Unmarshal(data, &customQuery)

if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}

if querier, ok := q.Queriers[customQuery.Route]; ok {
return querier.QueryCustom(ctx, customQuery.QueryData)
// get route and query from data
var route string
var query json.RawMessage
switch {
case customQuery.Profiles != nil:
route = QueryRouteProfiles
query = *customQuery.Profiles
case customQuery.Subspaces != nil:
route = QueryRouteSubspaces
query = *customQuery.Subspaces
case customQuery.Relationships != nil:
route = QueryRouteRelationships
query = *customQuery.Relationships
case customQuery.Posts != nil:
route = QueryRoutePosts
query = *customQuery.Posts
case customQuery.Reports != nil:
route = QueryRouteReports
query = *customQuery.Reports
case customQuery.Reactions != nil:
route = QueryRouteReactions
query = *customQuery.Reactions
}

return nil, sdkerrors.Wrap(wasm.ErrQueryFailed, customQuery.Route)
if querier, ok := q.Queriers[route]; ok {
return querier.QueryCustom(ctx, query)
}
return nil, sdkerrors.Wrap(wasm.ErrQueryFailed, "unimplemented route")
}