Skip to content

Commit

Permalink
feat: call of price all from custom querier
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmic-vagabond committed Sep 1, 2023
1 parent 389269b commit ccec669
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ func NewElysApp(
// if we want to allow any custom callbacks
availableCapabilities := strings.Join(AllCapabilities(), ",")

wasmOpts = append(RegisterCustomPlugins(), wasmOpts...)
wasmOpts = append(RegisterCustomPlugins(&app.OracleKeeper), wasmOpts...)

app.WasmKeeper = wasm.NewKeeper(
appCodec,
Expand Down
35 changes: 21 additions & 14 deletions app/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
wasmvmtypes "github.com/CosmWasm/wasmvm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
query "github.com/cosmos/cosmos-sdk/types/query"
oraclekeeper "github.com/elys-network/elys/x/oracle/keeper"
"github.com/elys-network/elys/x/oracle/types"
oracletypes "github.com/elys-network/elys/x/oracle/types"
)

Expand All @@ -26,15 +28,22 @@ func AllCapabilities() []string {
}

type QueryPlugin struct {
oracleKeeper *oraclekeeper.Keeper
}

// NewQueryPlugin returns a reference to a new QueryPlugin.
func NewQueryPlugin() *QueryPlugin {
return &QueryPlugin{}
func NewQueryPlugin(
oracle *oraclekeeper.Keeper,
) *QueryPlugin {
return &QueryPlugin{
oracleKeeper: oracle,
}
}

func RegisterCustomPlugins() []wasmkeeper.Option {
wasmQueryPlugin := NewQueryPlugin()
func RegisterCustomPlugins(
oracle *oraclekeeper.Keeper,
) []wasmkeeper.Option {
wasmQueryPlugin := NewQueryPlugin(oracle)

queryPluginOpt := wasmkeeper.WithQueryPlugins(&wasmkeeper.QueryPlugins{
Custom: CustomQuerier(wasmQueryPlugin),
Expand All @@ -57,21 +66,19 @@ func CustomQuerier(qp *QueryPlugin) func(ctx sdk.Context, request json.RawMessag
case contractQuery.PriceAll != nil:
pagination := contractQuery.PriceAll.Pagination

_ = pagination

res := AllPriceResponse{
Price: []oracletypes.Price{},
Pagination: &query.PageResponse{
NextKey: nil,
},
// Calling the PriceAll function and handling its response
priceResponse, err := qp.oracleKeeper.PriceAll(ctx, &types.QueryAllPriceRequest{Pagination: pagination})
if err != nil {
return nil, errorsmod.Wrap(err, "failed to get all prices")
}

bz, err := json.Marshal(res)
// Serializing the response to a JSON byte array
responseBytes, err := json.Marshal(priceResponse)
if err != nil {
return nil, errorsmod.Wrap(err, "elys price all query response")
return nil, errorsmod.Wrap(err, "failed to serialize price response")
}

return bz, nil
return responseBytes, nil

default:
return nil, wasmvmtypes.UnsupportedRequest{Kind: "unknown elys query variant"}
Expand Down

0 comments on commit ccec669

Please sign in to comment.