diff --git a/x/oracle/abci_test.go b/x/oracle/abci_test.go index 8111eca94b..12442d3137 100644 --- a/x/oracle/abci_test.go +++ b/x/oracle/abci_test.go @@ -304,7 +304,7 @@ func (s *IntegrationTestSuite) TestEndblockerHistoracle() { s.Require().NoError(err) medians := app.OracleKeeper.AllMedianPrices(ctx) - medians = *medians.FilterByBlock(uint64(blockHeight)).FilterByDenom(denom) + medians = medians.FilterByBlock(uint64(blockHeight)).FilterByDenom(denom) actualMedian := medians[0].ExchangeRateTuple.ExchangeRate s.Require().Equal(expectedMedian, actualMedian) @@ -313,7 +313,7 @@ func (s *IntegrationTestSuite) TestEndblockerHistoracle() { s.Require().NoError(err) medianDeviations := app.OracleKeeper.AllMedianDeviationPrices(ctx) - medianDeviations = *medianDeviations.FilterByBlock(uint64(blockHeight)).FilterByDenom(denom) + medianDeviations = medianDeviations.FilterByBlock(uint64(blockHeight)).FilterByDenom(denom) actualMedianDeviation := medianDeviations[0].ExchangeRateTuple.ExchangeRate s.Require().Equal(expectedMedianDeviation, actualMedianDeviation) } diff --git a/x/oracle/keeper/grpc_query.go b/x/oracle/keeper/grpc_query.go index 4f3d292503..ae46e5cb47 100644 --- a/x/oracle/keeper/grpc_query.go +++ b/x/oracle/keeper/grpc_query.go @@ -263,8 +263,7 @@ func (q querier) Medians( } ctx := sdk.UnwrapSDKContext(goCtx) - - medians := types.Prices{} + var medians types.Prices if len(req.Denom) > 0 { if req.NumStamps == 0 { @@ -280,7 +279,7 @@ func (q querier) Medians( medians = q.AllMedianPrices(ctx) } - return &types.QueryMediansResponse{Medians: *medians.Sort()}, nil + return &types.QueryMediansResponse{Medians: medians.Sort()}, nil } // MedianDeviations queries median deviations of all denoms, or, if specified, returns @@ -302,12 +301,12 @@ func (q querier) MedianDeviations( if err != nil { return nil, err } - medianDeviations = append(medianDeviations, *price) + medianDeviations = append(medianDeviations, price) } else { medianDeviations = q.AllMedianDeviationPrices(ctx) } - return &types.QueryMedianDeviationsResponse{MedianDeviations: *medianDeviations.Sort()}, nil + return &types.QueryMedianDeviationsResponse{MedianDeviations: medianDeviations.Sort()}, nil } // AvgPrice queries historic avg price for requested denom. diff --git a/x/oracle/keeper/grpc_query_test.go b/x/oracle/keeper/grpc_query_test.go index f3e9cd473a..37d55d870b 100644 --- a/x/oracle/keeper/grpc_query_test.go +++ b/x/oracle/keeper/grpc_query_test.go @@ -218,8 +218,8 @@ func (s *IntegrationTestSuite) TestQuerier_Medians() { s.Require().NoError(err) expected := []types.Price{ - *types.NewPrice(atomMedian0.Amount, "atom", blockHeight0), - *types.NewPrice(umeeMedian0.Amount, "umee", blockHeight0), + types.NewPrice(atomMedian0.Amount, "atom", blockHeight0), + types.NewPrice(umeeMedian0.Amount, "umee", blockHeight0), } s.Require().Equal(res.Medians, expected) @@ -227,7 +227,7 @@ func (s *IntegrationTestSuite) TestQuerier_Medians() { s.Require().NoError(err) expected = []types.Price{ - *types.NewPrice(atomMedian0.Amount, "atom", blockHeight0), + types.NewPrice(atomMedian0.Amount, "atom", blockHeight0), } s.Require().Equal(res.Medians, expected) @@ -239,10 +239,10 @@ func (s *IntegrationTestSuite) TestQuerier_Medians() { s.Require().NoError(err) expected = []types.Price{ - *types.NewPrice(atomMedian0.Amount, "atom", blockHeight0), - *types.NewPrice(umeeMedian0.Amount, "umee", blockHeight0), - *types.NewPrice(atomMedian1.Amount, "atom", blockHeight1), - *types.NewPrice(umeeMedian1.Amount, "umee", blockHeight1), + types.NewPrice(atomMedian0.Amount, "atom", blockHeight0), + types.NewPrice(umeeMedian0.Amount, "umee", blockHeight0), + types.NewPrice(atomMedian1.Amount, "atom", blockHeight1), + types.NewPrice(umeeMedian1.Amount, "umee", blockHeight1), } s.Require().Equal(res.Medians, expected) @@ -250,8 +250,8 @@ func (s *IntegrationTestSuite) TestQuerier_Medians() { s.Require().NoError(err) expected = []types.Price{ - *types.NewPrice(atomMedian0.Amount, "atom", blockHeight0), - *types.NewPrice(atomMedian1.Amount, "atom", blockHeight1), + types.NewPrice(atomMedian0.Amount, "atom", blockHeight0), + types.NewPrice(atomMedian1.Amount, "atom", blockHeight1), } s.Require().Equal(res.Medians, expected) @@ -275,8 +275,8 @@ func (s *IntegrationTestSuite) TestQuerier_MedianDeviations() { s.Require().NoError(err) expected := []types.Price{ - *types.NewPrice(atomMedianDeviation.Amount, "atom", blockHeight), - *types.NewPrice(umeeMedianDeviation.Amount, "umee", blockHeight), + types.NewPrice(atomMedianDeviation.Amount, "atom", blockHeight), + types.NewPrice(umeeMedianDeviation.Amount, "umee", blockHeight), } s.Require().Equal(res.MedianDeviations, expected) @@ -284,7 +284,7 @@ func (s *IntegrationTestSuite) TestQuerier_MedianDeviations() { s.Require().NoError(err) expected = []types.Price{ - *types.NewPrice(atomMedianDeviation.Amount, "atom", blockHeight), + types.NewPrice(atomMedianDeviation.Amount, "atom", blockHeight), } s.Require().Equal(res.MedianDeviations, expected) } diff --git a/x/oracle/keeper/historic_price.go b/x/oracle/keeper/historic_price.go index 15ac2adf0e..4b265f5b7f 100644 --- a/x/oracle/keeper/historic_price.go +++ b/x/oracle/keeper/historic_price.go @@ -60,20 +60,18 @@ func (k Keeper) SetHistoricMedian( func (k Keeper) HistoricMedianDeviation( ctx sdk.Context, denom string, -) (*types.Price, error) { +) (types.Price, error) { store := ctx.KVStore(k.storeKey) blockDiff := uint64(ctx.BlockHeight())%k.MedianStampPeriod(ctx) + 1 blockNum := uint64(ctx.BlockHeight()) - blockDiff bz := store.Get(types.KeyMedianDeviation(denom, blockNum)) if bz == nil { - return &types.Price{}, types.ErrNoMedianDeviation.Wrap("denom: " + denom) + return types.Price{}, types.ErrNoMedianDeviation.Wrap("denom: " + denom) } decProto := sdk.DecProto{} k.cdc.MustUnmarshal(bz, &decProto) - price := types.NewPrice(decProto.Dec, denom, blockNum) - - return price, nil + return types.NewPrice(decProto.Dec, denom, blockNum), nil } // WithinHistoricMedianDeviation returns whether or not the current price of a @@ -276,8 +274,7 @@ func (k Keeper) IterateHistoricMedians( denom, block := types.ParseDenomAndBlockFromKey(iter.Key(), types.KeyPrefixMedian) decProto := sdk.DecProto{} k.cdc.MustUnmarshal(iter.Value(), &decProto) - price := types.NewPrice(decProto.Dec, denom, block) - if handler(*price) { + if handler(types.NewPrice(decProto.Dec, denom, block)) { break } } diff --git a/x/oracle/keeper/historic_price_test.go b/x/oracle/keeper/historic_price_test.go index c147c1d1ef..ec07e08b3b 100644 --- a/x/oracle/keeper/historic_price_test.go +++ b/x/oracle/keeper/historic_price_test.go @@ -36,9 +36,9 @@ func (s *IntegrationTestSuite) TestSetHistoraclePricing() { // check medians, num of available medians, and median standard deviation medians := app.OracleKeeper.HistoricMedians(ctx, displayDenom, 3) s.Require().Equal(len(medians), 3) - s.Require().Equal(medians[0], *types.NewPrice(sdk.MustNewDecFromStr("1.2"), displayDenom, 17)) - s.Require().Equal(medians[1], *types.NewPrice(sdk.MustNewDecFromStr("1.125"), displayDenom, 14)) - s.Require().Equal(medians[2], *types.NewPrice(sdk.MustNewDecFromStr("1.1"), displayDenom, 11)) + s.Require().Equal(medians[0], types.NewPrice(sdk.MustNewDecFromStr("1.2"), displayDenom, 17)) + s.Require().Equal(medians[1], types.NewPrice(sdk.MustNewDecFromStr("1.125"), displayDenom, 14)) + s.Require().Equal(medians[2], types.NewPrice(sdk.MustNewDecFromStr("1.1"), displayDenom, 11)) medianDeviation, err := app.OracleKeeper.HistoricMedianDeviation(ctx, displayDenom) s.Require().NoError(err) @@ -94,6 +94,6 @@ func (s *IntegrationTestSuite) TestSetHistoraclePricing() { medians = app.OracleKeeper.HistoricMedians(ctx, displayDenom, 3) s.Require().Equal(len(medians), 2) - s.Require().Equal(medians[0], *types.NewPrice(sdk.MustNewDecFromStr("1.2"), displayDenom, 17)) - s.Require().Equal(medians[1], *types.NewPrice(sdk.MustNewDecFromStr("1.125"), displayDenom, 14)) + s.Require().Equal(medians[0], types.NewPrice(sdk.MustNewDecFromStr("1.2"), displayDenom, 17)) + s.Require().Equal(medians[1], types.NewPrice(sdk.MustNewDecFromStr("1.125"), displayDenom, 14)) } diff --git a/x/oracle/types/price.go b/x/oracle/types/price.go index e8abbeed94..812716149c 100644 --- a/x/oracle/types/price.go +++ b/x/oracle/types/price.go @@ -8,8 +8,8 @@ import ( type Prices []Price -func NewPrice(exchangeRate sdk.Dec, denom string, blockNum uint64) *Price { - return &Price{ +func NewPrice(exchangeRate sdk.Dec, denom string, blockNum uint64) Price { + return Price{ ExchangeRateTuple: ExchangeRateTuple{ ExchangeRate: exchangeRate, Denom: denom, @@ -18,44 +18,43 @@ func NewPrice(exchangeRate sdk.Dec, denom string, blockNum uint64) *Price { } } -func (p *Prices) Decs() []sdk.Dec { +func (p Prices) Decs() []sdk.Dec { decs := []sdk.Dec{} - for _, price := range *p { + for _, price := range p { decs = append(decs, price.ExchangeRateTuple.ExchangeRate) } return decs } -func (p *Prices) FilterByBlock(blockNum uint64) *Prices { +func (p Prices) FilterByBlock(blockNum uint64) Prices { prices := Prices{} - for _, price := range *p { + for _, price := range p { if price.BlockNum == blockNum { prices = append(prices, price) } } - return &prices + return prices } -func (p *Prices) FilterByDenom(denom string) *Prices { +func (p Prices) FilterByDenom(denom string) Prices { prices := Prices{} - for _, price := range *p { + for _, price := range p { if price.ExchangeRateTuple.Denom == denom { prices = append(prices, price) } } - return &prices + return prices } -func (p *Prices) Sort() *Prices { - prices := *p +func (p Prices) Sort() Prices { sort.Slice( - prices, + p, func(i, j int) bool { - if prices[i].BlockNum == prices[j].BlockNum { - return prices[i].ExchangeRateTuple.Denom < prices[j].ExchangeRateTuple.Denom + if p[i].BlockNum == p[j].BlockNum { + return p[i].ExchangeRateTuple.Denom < p[j].ExchangeRateTuple.Denom } - return prices[i].BlockNum < prices[j].BlockNum + return p[i].BlockNum < p[j].BlockNum }, ) - return &prices + return p } diff --git a/x/oracle/types/price_test.go b/x/oracle/types/price_test.go new file mode 100644 index 0000000000..80e03cb388 --- /dev/null +++ b/x/oracle/types/price_test.go @@ -0,0 +1,17 @@ +package types + +import ( + "testing" + + sdk "github.com/cosmos/cosmos-sdk/types" + "gotest.tools/v3/assert" +) + +func TestPrices(t *testing.T) { + zero := sdk.ZeroDec() + p1 := NewPrice(zero, "atom", 4) + p2 := NewPrice(zero, "atom", 3) + prices := Prices{p1, p2} + prices.Sort() + assert.DeepEqual(t, Prices{p2, p1}, prices) +}