From ff9a56450dc1b2ab2703b2a2969a9a1b4472d957 Mon Sep 17 00:00:00 2001 From: Cosmic Vagabond <121588426+cosmic-vagabond@users.noreply.github.com> Date: Fri, 1 Sep 2023 09:37:27 +0200 Subject: [PATCH] feat: call of price all from custom querier --- app/app.go | 2 +- app/wasm.go | 39 +++++++++++++++++++++++++++------------ 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/app/app.go b/app/app.go index 2eb1e3c2d..137c51458 100644 --- a/app/app.go +++ b/app/app.go @@ -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, diff --git a/app/wasm.go b/app/wasm.go index 2e8e21f47..a7a25343c 100644 --- a/app/wasm.go +++ b/app/wasm.go @@ -9,6 +9,7 @@ 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" oracletypes "github.com/elys-network/elys/x/oracle/types" ) @@ -26,15 +27,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), @@ -57,21 +65,30 @@ func CustomQuerier(qp *QueryPlugin) func(ctx sdk.Context, request json.RawMessag case contractQuery.PriceAll != nil: pagination := contractQuery.PriceAll.Pagination - _ = pagination + // Calling the PriceAll function and handling its response + priceResponse, err := qp.oracleKeeper.PriceAll(ctx, &oracletypes.QueryAllPriceRequest{Pagination: pagination}) + if err != nil { + return nil, errorsmod.Wrap(err, "failed to get all prices") + } + + // copy array priceResponse.Price + price := make([]oracletypes.Price, len(priceResponse.Price)) + copy(price, priceResponse.Price) res := AllPriceResponse{ - Price: []oracletypes.Price{}, + Price: price, Pagination: &query.PageResponse{ - NextKey: nil, + NextKey: priceResponse.Pagination.NextKey, }, } - bz, err := json.Marshal(res) + // Serializing the response to a JSON byte array + responseBytes, err := json.Marshal(res) 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"} @@ -84,12 +101,10 @@ type ElysQuery struct { } type PriceAll struct { - // oracletypes.QueryAllPriceRequest Pagination *query.PageRequest `protobuf:"bytes,1,opt,name=pagination,proto3" json:"pagination,omitempty"` } type AllPriceResponse struct { - // oracletypes.QueryAllPriceResponse Price []oracletypes.Price `protobuf:"bytes,1,rep,name=price,proto3" json:"price"` Pagination *query.PageResponse `protobuf:"bytes,2,opt,name=pagination,proto3" json:"pagination,omitempty"` }