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 ff9a564
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 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
39 changes: 27 additions & 12 deletions app/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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),
Expand All @@ -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"}
Expand All @@ -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"`
}

0 comments on commit ff9a564

Please sign in to comment.