generated from okp4/template-oss
-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(wasm): implements CustomQuerier with logic module
The CustomQuerier allow here to call the logic Ask query from a wasm contract.
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |