Skip to content

Commit

Permalink
feat: added metoken queries to cosmwasm and stargate queries (#2177)
Browse files Browse the repository at this point in the history
* Add metoken queries to cosmwasm and stargate queries

* added tests for stargate and cosmwasm metoken queries

* fix the lint

* address the pr comments
  • Loading branch information
gsk967 authored Aug 2, 2023
1 parent 1db033a commit 30c1c66
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 32 deletions.
3 changes: 2 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,8 @@ func New(
availableCapabilities := "iterator,staking,stargate,cosmwasm_1_1,cosmwasm_1_2,umee"

// Register umee custom plugin to wasm
wasmOpts = append(uwasm.RegisterCustomPlugins(app.LeverageKeeper, app.OracleKeeper, app.IncentiveKeeper), wasmOpts...)
wasmOpts = append(uwasm.RegisterCustomPlugins(app.LeverageKeeper, app.OracleKeeper, app.IncentiveKeeper,
app.MetokenKeeperB), wasmOpts...)
// Register stargate queries
wasmOpts = append(wasmOpts, uwasm.RegisterStargateQueries(*bApp.GRPCQueryRouter(), appCodec)...)

Expand Down
4 changes: 3 additions & 1 deletion app/wasm/custom_plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/umee-network/umee/v5/app/wasm/query"
inckeeper "github.com/umee-network/umee/v5/x/incentive/keeper"
leveragekeeper "github.com/umee-network/umee/v5/x/leverage/keeper"
metokenkeeper "github.com/umee-network/umee/v5/x/metoken/keeper"
oraclekeeper "github.com/umee-network/umee/v5/x/oracle/keeper"
)

Expand All @@ -18,8 +19,9 @@ func RegisterCustomPlugins(
leverageKeeper leveragekeeper.Keeper,
oracleKeeper oraclekeeper.Keeper,
incentiveKeeper inckeeper.Keeper,
metokenBuilder metokenkeeper.Builder,
) []wasmkeeper.Option {
wasmQueryPlugin := query.NewQueryPlugin(leverageKeeper, oracleKeeper, incentiveKeeper)
wasmQueryPlugin := query.NewQueryPlugin(leverageKeeper, oracleKeeper, incentiveKeeper, metokenBuilder)
queryPluginOpt := wasmkeeper.WithQueryPlugins(&wasmkeeper.QueryPlugins{
Custom: wasmQueryPlugin.CustomQuerier(),
})
Expand Down
61 changes: 61 additions & 0 deletions app/wasm/query/handle_metoken.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package query

import (
"context"

"github.com/gogo/protobuf/proto"
"github.com/umee-network/umee/v5/x/metoken"
)

// HandleMeTokenParams handles the get for x/metoken module's parameters.
func (q UmeeQuery) HandleMeTokenParams(
ctx context.Context,
qs metoken.QueryServer,
) (proto.Message, error) {
return qs.Params(ctx, &metoken.QueryParams{})
}

// HandleMeTokenIndexes handles the get for x/metoken indexes.
func (q UmeeQuery) HandleMeTokenIndexes(
ctx context.Context,
qs metoken.QueryServer,
) (proto.Message, error) {
req := metoken.QueryIndexes{MetokenDenom: q.Indexes.MetokenDenom}
return qs.Indexes(ctx, &req)
}

// HandleMeTokenSwapFee handles the get for x/metoken swap fee.
func (q UmeeQuery) HandleMeTokenSwapFee(
ctx context.Context,
qs metoken.QueryServer,
) (proto.Message, error) {
req := metoken.QuerySwapFee{Asset: q.SwapFee.Asset, MetokenDenom: q.SwapFee.MetokenDenom}
return qs.SwapFee(ctx, &req)
}

// HandleMeTokenRedeemFee handles the get for x/metoken redeem fee.
func (q UmeeQuery) HandleMeTokenRedeemFee(
ctx context.Context,
qs metoken.QueryServer,
) (proto.Message, error) {
req := metoken.QueryRedeemFee{AssetDenom: q.RedeemFee.AssetDenom, Metoken: q.RedeemFee.Metoken}
return qs.RedeemFee(ctx, &req)
}

// HandleMeTokenIndexBalances handles the get for x/metoken indexes balances.
func (q UmeeQuery) HandleMeTokenIndexBalances(
ctx context.Context,
qs metoken.QueryServer,
) (proto.Message, error) {
req := metoken.QueryIndexBalances{MetokenDenom: q.IndexBalances.MetokenDenom}
return qs.IndexBalances(ctx, &req)
}

// HandleMeTokenIndexPrice handles the get for x/metoken indexe price.
func (q UmeeQuery) HandleMeTokenIndexPrice(
ctx context.Context,
qs metoken.QueryServer,
) (proto.Message, error) {
req := metoken.QueryIndexPrice{MetokenDenom: q.IndexPrice.MetokenDenom}
return qs.IndexPrice(ctx, &req)
}
31 changes: 25 additions & 6 deletions app/wasm/query/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,32 @@ import (
inckeeper "github.com/umee-network/umee/v5/x/incentive/keeper"
lvkeeper "github.com/umee-network/umee/v5/x/leverage/keeper"
lvtypes "github.com/umee-network/umee/v5/x/leverage/types"
"github.com/umee-network/umee/v5/x/metoken"
metokenkeeper "github.com/umee-network/umee/v5/x/metoken/keeper"
ockeeper "github.com/umee-network/umee/v5/x/oracle/keeper"
ocpes "github.com/umee-network/umee/v5/x/oracle/types"
)

// Plugin wraps the query plugin with queriers.
type Plugin struct {
lvQueryServer lvtypes.QueryServer
ocQueryServer ocpes.QueryServer
incQueryServer incentive.QueryServer
lvQueryServer lvtypes.QueryServer
ocQueryServer ocpes.QueryServer
incQueryServer incentive.QueryServer
metokenQueryServer metoken.QueryServer
}

// NewQueryPlugin creates a plugin to query native modules.
func NewQueryPlugin(
leverageKeeper lvkeeper.Keeper,
oracleKeeper ockeeper.Keeper,
incentiveKeeper inckeeper.Keeper,
metokenBuilder metokenkeeper.Builder,
) *Plugin {
return &Plugin{
lvQueryServer: lvkeeper.NewQuerier(leverageKeeper),
ocQueryServer: ockeeper.NewQuerier(oracleKeeper),
incQueryServer: inckeeper.NewQuerier(incentiveKeeper),
lvQueryServer: lvkeeper.NewQuerier(leverageKeeper),
ocQueryServer: ockeeper.NewQuerier(oracleKeeper),
incQueryServer: inckeeper.NewQuerier(incentiveKeeper),
metokenQueryServer: metokenkeeper.NewQuerier(metokenBuilder),
}
}

Expand Down Expand Up @@ -119,6 +124,20 @@ func (plugin *Plugin) CustomQuerier() func(ctx sdk.Context, request json.RawMess
case smartcontractQuery.LastRewardTime != nil:
resp, err = smartcontractQuery.HandleLastRewardTime(ctx, plugin.incQueryServer)

// metoken
case smartcontractQuery.MeTokenParameters != nil:
resp, err = smartcontractQuery.HandleMeTokenParams(ctx, plugin.metokenQueryServer)
case smartcontractQuery.Indexes != nil:
resp, err = smartcontractQuery.HandleMeTokenIndexes(ctx, plugin.metokenQueryServer)
case smartcontractQuery.SwapFee != nil:
resp, err = smartcontractQuery.HandleMeTokenSwapFee(ctx, plugin.metokenQueryServer)
case smartcontractQuery.RedeemFee != nil:
resp, err = smartcontractQuery.HandleMeTokenRedeemFee(ctx, plugin.metokenQueryServer)
case smartcontractQuery.IndexBalances != nil:
resp, err = smartcontractQuery.HandleMeTokenIndexBalances(ctx, plugin.metokenQueryServer)
case smartcontractQuery.IndexPrice != nil:
resp, err = smartcontractQuery.HandleMeTokenIndexPrice(ctx, plugin.metokenQueryServer)

default:
return nil, wasmvmtypes.UnsupportedRequest{Kind: "invalid umee query"}
}
Expand Down
9 changes: 9 additions & 0 deletions app/wasm/query/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
wasmvmtypes "github.com/CosmWasm/wasmvm/types"
"github.com/umee-network/umee/v5/x/incentive"
lvtypes "github.com/umee-network/umee/v5/x/leverage/types"
"github.com/umee-network/umee/v5/x/metoken"
octypes "github.com/umee-network/umee/v5/x/oracle/types"
)

Expand Down Expand Up @@ -90,6 +91,14 @@ type UmeeQuery struct {
ActualRates *incentive.QueryActualRates `json:"actual_rates,omitempty"`
// LastRewardTime queries the last block time at which incentive rewards were calculated.
LastRewardTime *incentive.QueryLastRewardTime `json:"last_reward_time,omitempty"`

// metoken queries
MeTokenParameters *metoken.QueryParams `json:"metoken_parameters,omitempty"`
Indexes *metoken.QueryIndexes `json:"metoken_indexes,omitempty"`
SwapFee *metoken.QuerySwapFee `json:"metoken_swapfee,omitempty"`
RedeemFee *metoken.QueryRedeemFee `json:"metoken_redeemfee,omitempty"`
IndexBalances *metoken.QueryIndexBalances `json:"metoken_indexbalances,omitempty"`
IndexPrice *metoken.QueryIndexPrice `json:"metoken_indexprice,omitempty"`
}

// MarshalResponse marshals any response.
Expand Down
10 changes: 10 additions & 0 deletions app/wasm/query/whitelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/umee-network/umee/v5/x/incentive"
ltypes "github.com/umee-network/umee/v5/x/leverage/types"
"github.com/umee-network/umee/v5/x/metoken"
otypes "github.com/umee-network/umee/v5/x/oracle/types"
ugovtypes "github.com/umee-network/umee/v5/x/ugov"
uibctypes "github.com/umee-network/umee/v5/x/uibc"
Expand Down Expand Up @@ -43,6 +44,7 @@ const (
oracleBaseQueryPath = "/umee.oracle.v1.Query/"
uibcBaseQueryPath = "/umee.uibc.v1.Query/"
incentiveBaseQueryPath = "/umee.incentive.v1.Query/"
metokenBaseQueryPath = "/umee.metoken.v1.Query/" // #nosec G101
)

func init() {
Expand Down Expand Up @@ -134,6 +136,14 @@ func init() {
setWhitelistedQuery(incentiveBaseQueryPath+"CurrentRates", &incentive.QueryCurrentRatesResponse{})
setWhitelistedQuery(incentiveBaseQueryPath+"ActualRates", &incentive.QueryActualRates{})
setWhitelistedQuery(incentiveBaseQueryPath+"LastRewardTime", &incentive.QueryLastRewardTimeResponse{})

// metoken
setWhitelistedQuery(metokenBaseQueryPath+"Params", &metoken.QueryParamsResponse{})
setWhitelistedQuery(metokenBaseQueryPath+"Indexes", &metoken.QueryIndexesResponse{})
setWhitelistedQuery(metokenBaseQueryPath+"SwapFee", &metoken.QuerySwapFeeResponse{})
setWhitelistedQuery(metokenBaseQueryPath+"RedeemFee", &metoken.QueryRedeemFeeResponse{})
setWhitelistedQuery(metokenBaseQueryPath+"IndexBalances", &metoken.QueryIndexBalancesResponse{})
setWhitelistedQuery(metokenBaseQueryPath+"IndexPrice", &metoken.QueryIndexPriceResponse{})
}

// GetWhitelistedQuery returns the whitelisted query at the provided path.
Expand Down
4 changes: 4 additions & 0 deletions app/wasm/test/cosmwasm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ func TestCosmwasm(t *testing.T) {
its.TestOracleQueries()
its.TestLeverageTxs()
its.TestIncentiveQueries()
its.TestMetokenQueries()

// stargate queries
its.TestStargateQueries()
}
135 changes: 111 additions & 24 deletions app/wasm/test/umee_cw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
wq "github.com/umee-network/umee/v5/app/wasm/query"
"github.com/umee-network/umee/v5/x/incentive"
lvtypes "github.com/umee-network/umee/v5/x/leverage/types"
"github.com/umee-network/umee/v5/x/metoken"
"github.com/umee-network/umee/v5/x/oracle/types"
)

Expand Down Expand Up @@ -133,30 +134,6 @@ func (s *IntegrationTestSuite) TestLeverageQueries() {
}
}

func (s *IntegrationTestSuite) TestStargateQueries() {
data := lvtypes.QueryMarketSummary{
Denom: appparams.BondDenom,
}
d, err := data.Marshal()
assert.NilError(s.T, err)
sq := StargateQuery{}
sq.Chain.Stargate = wasmvmtypes.StargateQuery{
Path: "/umee.leverage.v1.Query/MarketSummary",
Data: d,
}

cq, err := json.Marshal(sq)
assert.NilError(s.T, err)
resp, err := s.wasmQueryClient.SmartContractState(sdk.WrapSDKContext(s.ctx), &wasmtypes.QuerySmartContractStateRequest{
Address: s.contractAddr, QueryData: cq,
})
assert.NilError(s.T, err)
var rr lvtypes.QueryMarketSummaryResponse
err = s.encfg.Codec.UnmarshalJSON(resp.Data, &rr)
assert.NilError(s.T, err)
assert.Equal(s.T, "UMEE", rr.SymbolDenom)
}

func (s *IntegrationTestSuite) TestOracleQueries() {
tests := []struct {
Name string
Expand Down Expand Up @@ -196,6 +173,88 @@ func (s *IntegrationTestSuite) TestOracleQueries() {
}
}

func (s *IntegrationTestSuite) TestStargateQueries() {
tests := []struct {
name string
sq func() StargateQuery
resp func(resp wasmtypes.QuerySmartContractStateResponse)
}{
{
name: "stargate: leverage params ",
sq: func() StargateQuery {
data := lvtypes.QueryParams{}
d, err := data.Marshal()
assert.NilError(s.T, err)
sq := StargateQuery{}
sq.Chain.Stargate = wasmvmtypes.StargateQuery{
Path: "/umee.leverage.v1.Query/Params",
Data: d,
}
return sq
},
resp: func(resp wasmtypes.QuerySmartContractStateResponse) {
var rr lvtypes.QueryParamsResponse
err := s.encfg.Codec.UnmarshalJSON(resp.Data, &rr)
assert.NilError(s.T, err)
assert.DeepEqual(s.T, lvtypes.DefaultParams(), rr.Params)
},
},
{
name: "stargate: metoken queries ",
sq: func() StargateQuery {
data := metoken.QueryParams{}
d, err := data.Marshal()
assert.NilError(s.T, err)
sq := StargateQuery{}
sq.Chain.Stargate = wasmvmtypes.StargateQuery{
Path: "/umee.metoken.v1.Query/Params",
Data: d,
}
return sq
},
resp: func(resp wasmtypes.QuerySmartContractStateResponse) {
var rr metoken.QueryParamsResponse
err := s.encfg.Codec.UnmarshalJSON(resp.Data, &rr)
assert.NilError(s.T, err)
assert.DeepEqual(s.T, metoken.DefaultParams(), rr.Params)
},
}, {
name: "stargate: leverage market summary",
sq: func() StargateQuery {
data := lvtypes.QueryMarketSummary{
Denom: appparams.BondDenom,
}
d, err := data.Marshal()
assert.NilError(s.T, err)
sq := StargateQuery{}
sq.Chain.Stargate = wasmvmtypes.StargateQuery{
Path: "/umee.leverage.v1.Query/MarketSummary",
Data: d,
}
return sq
},
resp: func(resp wasmtypes.QuerySmartContractStateResponse) {
var rr lvtypes.QueryMarketSummaryResponse
err := s.encfg.Codec.UnmarshalJSON(resp.Data, &rr)
assert.NilError(s.T, err)
assert.Equal(s.T, "UMEE", rr.SymbolDenom)
},
},
}

for _, test := range tests {
s.T.Run(test.name, func(t *testing.T) {
cq, err := json.Marshal(test.sq())
assert.NilError(s.T, err)
resp, err := s.wasmQueryClient.SmartContractState(sdk.WrapSDKContext(s.ctx), &wasmtypes.QuerySmartContractStateRequest{
Address: s.contractAddr, QueryData: cq,
})
assert.NilError(s.T, err)
test.resp(*resp)
})
}
}

func (s *IntegrationTestSuite) TestLeverageTxs() {
accAddr := sdk.MustAccAddressFromBech32(s.contractAddr)
err := s.app.BankKeeper.SendCoinsFromModuleToAccount(s.ctx, minttypes.ModuleName, accAddr, sdk.NewCoins(sdk.NewCoin(appparams.BondDenom, sdk.NewInt(100000))))
Expand Down Expand Up @@ -278,3 +337,31 @@ func (s *IntegrationTestSuite) TestIncentiveQueries() {
})
}
}

func (s *IntegrationTestSuite) TestMetokenQueries() {
tests := []struct {
Name string
CQ []byte
ResponseCheck func(data []byte)
}{
{
Name: "metoken query params",
CQ: s.genCustomQuery(wq.UmeeQuery{
MeTokenParameters: &metoken.QueryParams{},
}),
ResponseCheck: func(data []byte) {
var rr metoken.QueryParamsResponse
err := json.Unmarshal(data, &rr)
assert.NilError(s.T, err)
assert.DeepEqual(s.T, rr.Params, metoken.DefaultParams())
},
},
}

for _, tc := range tests {
s.T.Run(tc.Name, func(t *testing.T) {
resp := s.queryContract(tc.CQ)
tc.ResponseCheck(resp.Data)
})
}
}
Binary file modified tests/artifacts/umee_cosmwasm-aarch64.wasm
Binary file not shown.

0 comments on commit 30c1c66

Please sign in to comment.