Skip to content

Commit

Permalink
Use slice type aliases, convert to response types
Browse files Browse the repository at this point in the history
  • Loading branch information
drklee3 committed Nov 10, 2021
1 parent cf8441e commit 0cf3384
Show file tree
Hide file tree
Showing 14 changed files with 687 additions and 147 deletions.
22 changes: 21 additions & 1 deletion docs/core/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@

- [kava/pricefeed/v1beta1/query.proto](#kava/pricefeed/v1beta1/query.proto)
- [CurrentPriceResponse](#kava.pricefeed.v1beta1.CurrentPriceResponse)
- [MarketResponse](#kava.pricefeed.v1beta1.MarketResponse)
- [PostedPriceResponse](#kava.pricefeed.v1beta1.PostedPriceResponse)
- [QueryMarketsRequest](#kava.pricefeed.v1beta1.QueryMarketsRequest)
- [QueryMarketsResponse](#kava.pricefeed.v1beta1.QueryMarketsResponse)
Expand Down Expand Up @@ -805,6 +806,25 @@ module.



<a name="kava.pricefeed.v1beta1.MarketResponse"></a>

### MarketResponse
MarketResponse defines an asset in the pricefeed.


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `market_id` | [string](#string) | | |
| `base_asset` | [string](#string) | | |
| `quote_asset` | [string](#string) | | |
| `oracles` | [string](#string) | repeated | |
| `active` | [bool](#bool) | | |






<a name="kava.pricefeed.v1beta1.PostedPriceResponse"></a>

### PostedPriceResponse
Expand Down Expand Up @@ -841,7 +861,7 @@ QueryMarketsResponse is the response type for the Query/Markets RPC method.

| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `markets` | [Market](#kava.pricefeed.v1beta1.Market) | repeated | List of markets |
| `markets` | [MarketResponse](#kava.pricefeed.v1beta1.MarketResponse) | repeated | List of markets |



Expand Down
2 changes: 1 addition & 1 deletion proto/kava/pricefeed/v1beta1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ message GenesisState {
// params defines all the paramaters of the module.
Params params = 1 [(gogoproto.nullable) = false];

repeated PostedPrice posted_prices = 2 [(gogoproto.nullable) = false];
repeated PostedPrice posted_prices = 2 [(gogoproto.castrepeated) = "PostedPrices", (gogoproto.nullable) = false];
}
17 changes: 14 additions & 3 deletions proto/kava/pricefeed/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ message QueryPricesRequest {}
message QueryPricesResponse {
option (gogoproto.goproto_getters) = false;

repeated CurrentPriceResponse prices = 1 [(gogoproto.nullable) = false];
repeated CurrentPriceResponse prices = 1
[(gogoproto.castrepeated) = "CurrentPriceResponses", (gogoproto.nullable) = false];
}

// QueryRawPricesRequest is the request type for the Query/RawPrices RPC method.
Expand All @@ -91,7 +92,8 @@ message QueryRawPricesRequest {
message QueryRawPricesResponse {
option (gogoproto.goproto_getters) = false;

repeated PostedPriceResponse raw_prices = 1 [(gogoproto.nullable) = false];
repeated PostedPriceResponse raw_prices = 1
[(gogoproto.castrepeated) = "PostedPriceResponses", (gogoproto.nullable) = false];
}

// QueryOraclesRequest is the request type for the Query/Oracles RPC method.
Expand All @@ -117,7 +119,7 @@ message QueryMarketsResponse {
option (gogoproto.goproto_getters) = false;

// List of markets
repeated Market markets = 1 [(gogoproto.nullable) = false];
repeated MarketResponse markets = 1 [(gogoproto.castrepeated) = "MarketResponses", (gogoproto.nullable) = false];
}

// PostedPriceResponse defines a price for market posted by a specific oracle.
Expand All @@ -134,3 +136,12 @@ message CurrentPriceResponse {
string market_id = 1 [(gogoproto.customname) = "MarketID"];
string price = 2 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
}

// MarketResponse defines an asset in the pricefeed.
message MarketResponse {
string market_id = 1 [(gogoproto.customname) = "MarketID"];
string base_asset = 2;
string quote_asset = 3;
repeated string oracles = 4;
bool active = 5;
}
5 changes: 3 additions & 2 deletions proto/kava/pricefeed/v1beta1/store.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ option (gogoproto.verbose_equal_all) = true;

// Params defines the parameters for the pricefeed module.
message Params {
option (gogoproto.goproto_stringer) = false; // false here because we define Stringer method in params.go
repeated Market markets = 1 [(gogoproto.nullable) = false];
option (gogoproto.goproto_stringer) = false;

repeated Market markets = 1 [(gogoproto.castrepeated) = "Markets", (gogoproto.nullable) = false];
}

// Market defines an asset in the pricefeed.
Expand Down
12 changes: 8 additions & 4 deletions x/pricefeed/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func (k QueryServer) Params(c context.Context, req *types.QueryParamsRequest) (*
return &types.QueryParamsResponse{Params: params}, nil
}

// Price implements the gRPC service handler for querying x/pricefeed price.
func (k QueryServer) Price(c context.Context, req *types.QueryPriceRequest) (*types.QueryPriceResponse, error) {
ctx := sdk.UnwrapSDKContext(c)

Expand All @@ -50,12 +51,12 @@ func (k QueryServer) Price(c context.Context, req *types.QueryPriceRequest) (*ty

return &types.QueryPriceResponse{
Price: types.CurrentPriceResponse(currentPrice)}, nil

}

func (k QueryServer) Prices(c context.Context, req *types.QueryPricesRequest) (*types.QueryPricesResponse, error) {
ctx := sdk.UnwrapSDKContext(c)

var currentPrices []types.CurrentPriceResponse
var currentPrices types.CurrentPriceResponses

for _, cp := range k.keeper.GetCurrentPrices(ctx) {
currentPrices = append(currentPrices, types.CurrentPriceResponse(cp))
Expand All @@ -74,7 +75,7 @@ func (k QueryServer) RawPrices(c context.Context, req *types.QueryRawPricesReque
return nil, status.Error(codes.NotFound, "invalid market ID")
}

var prices []types.PostedPriceResponse
var prices types.PostedPriceResponses
for _, rp := range k.keeper.GetRawPrices(ctx, req.MarketId) {
prices = append(prices, types.PostedPriceResponse{
MarketID: rp.MarketID,
Expand Down Expand Up @@ -110,7 +111,10 @@ func (k QueryServer) Oracles(c context.Context, req *types.QueryOraclesRequest)
func (k QueryServer) Markets(c context.Context, req *types.QueryMarketsRequest) (*types.QueryMarketsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)

markets := k.keeper.GetMarkets(ctx)
var markets types.MarketResponses
for _, market := range k.keeper.GetMarkets(ctx) {
markets = append(markets, market.ToMarketResponse())
}

return &types.QueryMarketsResponse{
Markets: markets,
Expand Down
6 changes: 4 additions & 2 deletions x/pricefeed/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,16 @@ func (suite *grpcQueryTestSuite) TestGrpcOracles_InvalidMarket() {
func (suite *grpcQueryTestSuite) TestGrpcMarkets() {
params := types.NewParams([]types.Market{
{MarketID: "tstusd", BaseAsset: "tst", QuoteAsset: "usd", Oracles: []sdk.AccAddress{}, Active: true},
{MarketID: "btcusd", BaseAsset: "btc", QuoteAsset: "usd", Oracles: []sdk.AccAddress{}, Active: true},
})
suite.keeper.SetParams(suite.ctx, params)

res, err := suite.queryServer.Markets(sdk.WrapSDKContext(suite.ctx), &types.QueryMarketsRequest{})
suite.NoError(err)
suite.Len(res.Markets, 1)
suite.Len(res.Markets, 2)
suite.Equal(len(res.Markets), len(params.Markets))
suite.NoError(res.Markets[0].VerboseEqual(params.Markets[0]))
suite.NoError(res.Markets[0].VerboseEqual(params.Markets[0].ToMarketResponse()))
suite.NoError(res.Markets[1].VerboseEqual(params.Markets[1].ToMarketResponse()))
}

func (suite *grpcQueryTestSuite) setTstPrice() {
Expand Down
19 changes: 9 additions & 10 deletions x/pricefeed/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (k Keeper) SetCurrentPrices(ctx sdk.Context, marketID string) error {
return types.ErrNoValidPrice
}

medianPrice := k.CalculateMedianPrice(ctx, notExpiredPrices)
medianPrice := k.CalculateMedianPrice(notExpiredPrices)

// check case that market price was not set in genesis
if validPrevPrice && !medianPrice.Equal(prevPrice.Price) {
Expand All @@ -135,7 +135,7 @@ func (k Keeper) setCurrentPrice(ctx sdk.Context, marketID string, currentPrice t
}

// CalculateMedianPrice calculates the median prices for the input prices.
func (k Keeper) CalculateMedianPrice(ctx sdk.Context, prices []types.CurrentPrice) sdk.Dec {
func (k Keeper) CalculateMedianPrice(prices []types.CurrentPrice) sdk.Dec {
l := len(prices)

if l == 1 {
Expand All @@ -148,16 +148,15 @@ func (k Keeper) CalculateMedianPrice(ctx sdk.Context, prices []types.CurrentPric
})
// for even numbers of prices, the median is calculated as the mean of the two middle prices
if l%2 == 0 {
median := k.calculateMeanPrice(ctx, prices[l/2-1:l/2+1])
median := k.calculateMeanPrice(prices[l/2-1], prices[l/2])
return median
}
// for odd numbers of prices, return the middle element
return prices[l/2].Price

}

func (k Keeper) calculateMeanPrice(ctx sdk.Context, prices []types.CurrentPrice) sdk.Dec {
sum := prices[0].Price.Add(prices[1].Price)
func (k Keeper) calculateMeanPrice(priceA, priceB types.CurrentPrice) sdk.Dec {
sum := priceA.Price.Add(priceB.Price)
mean := sum.Quo(sdk.NewDec(2))
return mean
}
Expand Down Expand Up @@ -195,8 +194,8 @@ func (k Keeper) IterateCurrentPrices(ctx sdk.Context, cb func(cp types.CurrentPr
}

// GetCurrentPrices returns all current price objects from the store
func (k Keeper) GetCurrentPrices(ctx sdk.Context) []types.CurrentPrice {
var cps []types.CurrentPrice
func (k Keeper) GetCurrentPrices(ctx sdk.Context) types.CurrentPrices {
var cps types.CurrentPrices
k.IterateCurrentPrices(ctx, func(cp types.CurrentPrice) (stop bool) {
cps = append(cps, cp)
return false
Expand All @@ -205,8 +204,8 @@ func (k Keeper) GetCurrentPrices(ctx sdk.Context) []types.CurrentPrice {
}

// GetRawPrices fetches the set of all prices posted by oracles for an asset
func (k Keeper) GetRawPrices(ctx sdk.Context, marketId string) []types.PostedPrice {
var pps []types.PostedPrice
func (k Keeper) GetRawPrices(ctx sdk.Context, marketId string) types.PostedPrices {
var pps types.PostedPrices
k.IterateRawPricesByMarket(ctx, marketId, func(pp types.PostedPrice) (stop bool) {
pps = append(pps, pp)
return false
Expand Down
2 changes: 1 addition & 1 deletion x/pricefeed/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
}

// GetMarkets returns the markets from params
func (k Keeper) GetMarkets(ctx sdk.Context) []types.Market {
func (k Keeper) GetMarkets(ctx sdk.Context) types.Markets {
return k.GetParams(ctx).Markets
}

Expand Down
2 changes: 1 addition & 1 deletion x/pricefeed/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ func (gs GenesisState) Validate() error {
return err
}

return ValidatePostedPrices(gs.PostedPrices)
return gs.PostedPrices.Validate()
}
27 changes: 14 additions & 13 deletions x/pricefeed/types/genesis.pb.go

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

Loading

0 comments on commit 0cf3384

Please sign in to comment.