From 3c684960e4f2cfae8d278bb64db1e5ff2a2d4dbe Mon Sep 17 00:00:00 2001 From: Arnaud Mimart <33665250+amimart@users.noreply.github.com> Date: Sat, 4 Feb 2023 23:03:16 +0100 Subject: [PATCH] feat(wasm): implements CustomQuerier with logic module The CustomQuerier allow here to call the logic Ask query from a wasm contract. --- app/wasm/query.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 app/wasm/query.go diff --git a/app/wasm/query.go b/app/wasm/query.go new file mode 100644 index 00000000..a157a6f0 --- /dev/null +++ b/app/wasm/query.go @@ -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") + } +}