Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v2 spot price queries #3335

Merged
merged 7 commits into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions proto/osmosis/gamm/v2/query.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
syntax = "proto3";
package osmosis.gamm.v2;

import "gogoproto/gogo.proto";
import "osmosis/gamm/v1beta1/tx.proto";

import "cosmos/base/v1beta1/coin.proto";
import "cosmos/base/query/v1beta1/pagination.proto";
import "google/api/annotations.proto";
import "google/protobuf/any.proto";
import "cosmos_proto/cosmos.proto";

option go_package = "github.com/osmosis-labs/osmosis/v12/x/gamm/v2types";

service Query {
// SpotPrice defines a gRPC query handler that returns the spot price given
// a base denomination and a quote denomination.
rpc SpotPrice(QuerySpotPriceRequest) returns (QuerySpotPriceResponse) {
option (google.api.http).get = "/osmosis/gamm/v2/pools/{pool_id}/prices";
}
}

// QuerySpotPriceRequest defines the gRPC request structure for a SpotPrice
// query.
message QuerySpotPriceRequest {
uint64 pool_id = 1 [ (gogoproto.moretags) = "yaml:\"pool_id\"" ];
string base_asset_denom = 2
[ (gogoproto.moretags) = "yaml:\"base_asset_denom\"" ];
string quote_asset_denom = 3
[ (gogoproto.moretags) = "yaml:\"quote_asset_denom\"" ];
}

// QuerySpotPriceResponse defines the gRPC response structure for a SpotPrice
// query.
message QuerySpotPriceResponse {
// String of the Dec. Ex) 10.203uatom
string spot_price = 1 [ (gogoproto.moretags) = "yaml:\"spot_price\"" ];
}
66 changes: 66 additions & 0 deletions proto/osmosis/twap/v2/query.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
syntax = "proto3";
package osmosis.twap.v2;

import "gogoproto/gogo.proto";
import "osmosis/twap/v1beta1/twap_record.proto";
import "osmosis/twap/v1beta1/genesis.proto";

import "cosmos/base/v1beta1/coin.proto";
import "cosmos/base/query/v1beta1/pagination.proto";
import "google/api/annotations.proto";
import "google/protobuf/any.proto";
import "cosmos_proto/cosmos.proto";
import "google/protobuf/timestamp.proto";

option go_package = "github.com/osmosis-labs/osmosis/v12/x/twap/client/v2queryproto";

service Query {
rpc ArithmeticTwap(ArithmeticTwapRequest) returns (ArithmeticTwapResponse) {
option (google.api.http).get = "/osmosis/twap/v2/ArithmeticTwap";
}
rpc ArithmeticTwapToNow(ArithmeticTwapToNowRequest)
returns (ArithmeticTwapToNowResponse) {
option (google.api.http).get = "/osmosis/twap/v2/ArithmeticTwapToNow";
}
}

message ArithmeticTwapRequest {
uint64 pool_id = 1;
string base_asset = 2;
string quote_asset = 3;
google.protobuf.Timestamp start_time = 4 [
(gogoproto.nullable) = false,
(gogoproto.stdtime) = true,
(gogoproto.moretags) = "yaml:\"start_time\""
];
google.protobuf.Timestamp end_time = 5 [
(gogoproto.nullable) = true,
(gogoproto.stdtime) = true,
(gogoproto.moretags) = "yaml:\"end_time\""
];
}
message ArithmeticTwapResponse {
string arithmetic_twap = 1 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.moretags) = "yaml:\"arithmetic_twap\"",
(gogoproto.nullable) = false
];
}

message ArithmeticTwapToNowRequest {
uint64 pool_id = 1;
string base_asset = 2;
string quote_asset = 3;
google.protobuf.Timestamp start_time = 4 [
(gogoproto.nullable) = false,
(gogoproto.stdtime) = true,
(gogoproto.moretags) = "yaml:\"start_time\""
];
}
message ArithmeticTwapToNowResponse {
string arithmetic_twap = 1 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.moretags) = "yaml:\"arithmetic_twap\"",
(gogoproto.nullable) = false
];
}
7 changes: 5 additions & 2 deletions wasmbinding/stargate_whitelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ import (

epochtypes "github.com/osmosis-labs/osmosis/v12/x/epochs/types"
gammtypes "github.com/osmosis-labs/osmosis/v12/x/gamm/types"
gammv2types "github.com/osmosis-labs/osmosis/v12/x/gamm/v2types"
incentivestypes "github.com/osmosis-labs/osmosis/v12/x/incentives/types"
lockuptypes "github.com/osmosis-labs/osmosis/v12/x/lockup/types"
minttypes "github.com/osmosis-labs/osmosis/v12/x/mint/types"
poolincentivestypes "github.com/osmosis-labs/osmosis/v12/x/pool-incentives/types"
superfluidtypes "github.com/osmosis-labs/osmosis/v12/x/superfluid/types"
tokenfactorytypes "github.com/osmosis-labs/osmosis/v12/x/tokenfactory/types"
twapquerytypes "github.com/osmosis-labs/osmosis/v12/x/twap/client/queryproto"
twapv2querytypes "github.com/osmosis-labs/osmosis/v12/x/twap/client/v2queryproto"
txfeestypes "github.com/osmosis-labs/osmosis/v12/x/txfees/types"
)

Expand Down Expand Up @@ -78,6 +80,7 @@ func init() {
setWhitelistedQuery("/osmosis.gamm.v1beta1.Query/TotalPoolLiquidity", &gammtypes.QueryTotalPoolLiquidityResponse{})
setWhitelistedQuery("/osmosis.gamm.v1beta1.Query/TotalShares", &gammtypes.QueryTotalSharesResponse{})
setWhitelistedQuery("/osmosis.gamm.v1beta1.Query/SpotPrice", &gammtypes.QuerySpotPriceResponse{})
setWhitelistedQuery("/osmosis.gamm.v2.Query/SpotPrice", &gammv2types.QuerySpotPriceResponse{})

// incentives
setWhitelistedQuery("/osmosis.incentives.Query/ModuleToDistributeCoins", &incentivestypes.ModuleToDistributeCoinsResponse{})
Expand Down Expand Up @@ -115,8 +118,8 @@ func init() {
// Does not include denoms_from_creator, TBD if this is the index we want contracts to use instead of admin

// twap
setWhitelistedQuery("/osmosis.twap.v1beta1.Query/ArithmeticTwap", &twapquerytypes.ArithmeticTwapResponse{})
setWhitelistedQuery("/osmosis.twap.v1beta1.Query/ArithmeticTwapToNow", &twapquerytypes.ArithmeticTwapToNowResponse{})
setWhitelistedQuery("/osmosis.twap.v2.Query/ArithmeticTwap", &twapv2querytypes.ArithmeticTwapResponse{})
setWhitelistedQuery("/osmosis.twap.v2.Query/ArithmeticTwapToNow", &twapv2querytypes.ArithmeticTwapToNowResponse{})
setWhitelistedQuery("/osmosis.twap.v1beta1.Query/Params", &twapquerytypes.ParamsResponse{})
}

Expand Down
36 changes: 36 additions & 0 deletions x/gamm/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/osmosis-labs/osmosis/v12/x/gamm/pool-models/balancer"
"github.com/osmosis-labs/osmosis/v12/x/gamm/types"
"github.com/osmosis-labs/osmosis/v12/x/gamm/v2types"
)

var sdkIntMaxValue = sdk.NewInt(0)
Expand Down Expand Up @@ -44,6 +45,16 @@ func NewQuerier(k Keeper) Querier {
return Querier{Keeper: k}
}

// QuerierV2 defines a wrapper around the x/gamm keeper providing gRPC method
// handlers for v2 queries.
type QuerierV2 struct {
Keeper
}

func NewV2Querier(k Keeper) QuerierV2 {
return QuerierV2{Keeper: k}
}

// Pool checks if a pool exists and their respective poolWeights.
func (q Querier) Pool(
ctx context.Context,
Expand Down Expand Up @@ -377,6 +388,31 @@ func (q Querier) SpotPrice(ctx context.Context, req *types.QuerySpotPriceRequest
}, nil
}

func (q QuerierV2) SpotPrice(ctx context.Context, req *v2types.QuerySpotPriceRequest) (*v2types.QuerySpotPriceResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}

if req.BaseAssetDenom == "" {
return nil, status.Error(codes.InvalidArgument, "invalid base asset denom")
}

if req.QuoteAssetDenom == "" {
return nil, status.Error(codes.InvalidArgument, "invalid quote asset denom")
}

sdkCtx := sdk.UnwrapSDKContext(ctx)

sp, err := q.Keeper.CalculateSpotPrice(sdkCtx, req.PoolId, req.QuoteAssetDenom, req.BaseAssetDenom)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

return &v2types.QuerySpotPriceResponse{
SpotPrice: sp.String(),
}, nil
}

// TotalLiquidity returns total liquidity across all pools.
func (q Querier) TotalLiquidity(ctx context.Context, _ *types.QueryTotalLiquidityRequest) (*types.QueryTotalLiquidityResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
Expand Down
5 changes: 4 additions & 1 deletion x/gamm/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/osmosis-labs/osmosis/v12/x/gamm/pool-models/stableswap"
simulation "github.com/osmosis-labs/osmosis/v12/x/gamm/simulation"
"github.com/osmosis-labs/osmosis/v12/x/gamm/types"
"github.com/osmosis-labs/osmosis/v12/x/gamm/v2types"
)

var (
Expand Down Expand Up @@ -74,7 +75,8 @@ func (b AppModuleBasic) RegisterRESTRoutes(ctx client.Context, r *mux.Router) {
}

func (b AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) {
types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx)) //nolint:errcheck
types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx)) //nolint:errcheck
v2types.RegisterQueryHandlerClient(context.Background(), mux, v2types.NewQueryClient(clientCtx)) //nolint:errcheck
}

func (b AppModuleBasic) GetTxCmd() *cobra.Command {
Expand Down Expand Up @@ -105,6 +107,7 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
balancer.RegisterMsgServer(cfg.MsgServer(), keeper.NewBalancerMsgServerImpl(&am.keeper))
stableswap.RegisterMsgServer(cfg.MsgServer(), keeper.NewStableswapMsgServerImpl(&am.keeper))
types.RegisterQueryServer(cfg.QueryServer(), keeper.NewQuerier(am.keeper))
v2types.RegisterQueryServer(cfg.QueryServer(), keeper.NewV2Querier(am.keeper))
}

func NewAppModule(cdc codec.Codec, keeper keeper.Keeper,
Expand Down
Loading