Skip to content

Commit

Permalink
feat(wasm): implements CustomQuerier with logic module
Browse files Browse the repository at this point in the history
The CustomQuerier allow here to call the logic Ask query from a wasm
contract.
  • Loading branch information
amimart committed Feb 7, 2023
1 parent 383a0e7 commit 3c68496
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions app/wasm/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package wasm

import (
"encoding/json"

wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
logickeeper "github.com/okp4/okp4d/x/logic/keeper"
logicwasm "github.com/okp4/okp4d/x/logic/wasm"
)

// customQuery represents the wasm custom query structure, it is intended to allow wasm contracts to execute queries
// against the logic modules.
type customQuery struct {
Ask *logicwasm.AskQuery `json:"ask,omitempty"`
}

// CustomQueryPlugins creates a wasm QueryPlugins containing the custom querier managing wasm contracts queries to the
// logic module.
func CustomQueryPlugins(logicKeeper logickeeper.Keeper) *wasmkeeper.QueryPlugins {
return &wasmkeeper.QueryPlugins{
Custom: makeCustomQuerier(
logicwasm.MakeLogicQuerier(logicKeeper),
),
}
}

func makeCustomQuerier(logicQuerier logicwasm.LogicQuerier) wasmkeeper.CustomQuerier {
return func(ctx sdk.Context, request json.RawMessage) ([]byte, error) {
var query customQuery
if err := json.Unmarshal(request, &query); err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}

if query.Ask != nil {
return logicQuerier.Ask(ctx, query.Ask)
}

return nil, sdkerrors.Wrap(wasmtypes.ErrInvalidMsg, "Unknown Custom query variant")
}
}

0 comments on commit 3c68496

Please sign in to comment.