Skip to content

Commit

Permalink
Feat/oracle query proto wrap (#180)
Browse files Browse the repository at this point in the history
* feat: add query proto wrap

* feat: add wasm bindings

* feat: support PriceAll for CW querier

* feat: remove total field

* feat: call of price all from custom querier

* fix: remove uneccessary code
  • Loading branch information
cosmic-vagabond authored Sep 1, 2023
1 parent d132628 commit 98685e5
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 2 deletions.
3 changes: 3 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,9 @@ func NewElysApp(
// The last arguments can contain custom message handlers, and custom query handlers,
// if we want to allow any custom callbacks
availableCapabilities := strings.Join(AllCapabilities(), ",")

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

app.WasmKeeper = wasm.NewKeeper(
appCodec,
keys[wasm.StoreKey],
Expand Down
96 changes: 96 additions & 0 deletions app/wasm.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
package app

import (
"encoding/json"

errorsmod "cosmossdk.io/errors"
"github.com/CosmWasm/wasmd/x/wasm"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
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"
)

// AllCapabilities returns all capabilities available with the current wasmvm
// See https://github.com/CosmWasm/cosmwasm/blob/main/docs/CAPABILITIES-BUILT-IN.md
// This functionality is going to be moved upstream: https://github.com/CosmWasm/wasmvm/issues/425
Expand All @@ -12,3 +25,86 @@ func AllCapabilities() []string {
"cosmwasm_1_2",
}
}

type QueryPlugin struct {
oracleKeeper *oraclekeeper.Keeper
}

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

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

queryPluginOpt := wasmkeeper.WithQueryPlugins(&wasmkeeper.QueryPlugins{
Custom: CustomQuerier(wasmQueryPlugin),
})

return []wasm.Option{
queryPluginOpt,
}
}

// CustomQuerier dispatches custom CosmWasm bindings queries.
func CustomQuerier(qp *QueryPlugin) func(ctx sdk.Context, request json.RawMessage) ([]byte, error) {
return func(ctx sdk.Context, request json.RawMessage) ([]byte, error) {
var contractQuery ElysQuery
if err := json.Unmarshal(request, &contractQuery); err != nil {
return nil, errorsmod.Wrap(err, "elys query")
}

switch {
case contractQuery.PriceAll != nil:
pagination := contractQuery.PriceAll.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: price,
Pagination: &query.PageResponse{
NextKey: priceResponse.Pagination.NextKey,
},
}

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

return responseBytes, nil

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

type ElysQuery struct {
PriceAll *PriceAll `json:"price_all,omitempty"`
}

type PriceAll struct {
Pagination *query.PageRequest `protobuf:"bytes,1,opt,name=pagination,proto3" json:"pagination,omitempty"`
}

type AllPriceResponse struct {
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"`
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
cosmossdk.io/math v1.0.1
cosmossdk.io/simapp v0.0.0-20230323161446-0af178d721ff
github.com/CosmWasm/wasmd v0.41.0
github.com/CosmWasm/wasmvm v1.3.0
github.com/bandprotocol/bandchain-packet v0.0.2
github.com/cometbft/cometbft v0.37.2
github.com/cometbft/cometbft-db v0.8.0
Expand All @@ -33,7 +34,6 @@ require (
cosmossdk.io/depinject v1.0.0-alpha.3 // indirect
cosmossdk.io/log v1.1.1-0.20230704160919-88f2c830b0ca // indirect
cosmossdk.io/tools/rosetta v0.2.1 // indirect
github.com/CosmWasm/wasmvm v1.3.0 // indirect
github.com/cosmos/gogogateway v1.2.0 // indirect
github.com/cosmos/ics23/go v0.10.0 // indirect
github.com/cosmos/rosetta-sdk-go v0.10.0 // indirect
Expand Down
19 changes: 18 additions & 1 deletion x/amm/types/query.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 98685e5

Please sign in to comment.