Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into robert/basis-points
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-zaremba committed Jul 19, 2023
2 parents 679ba0a + 9ac93ee commit d907444
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 48 deletions.
4 changes: 2 additions & 2 deletions x/oracle/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
}
Expand Down
9 changes: 4 additions & 5 deletions x/oracle/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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.
Expand Down
24 changes: 12 additions & 12 deletions x/oracle/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,16 +218,16 @@ 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)

res, err = s.queryClient.Medians(ctx.Context(), &types.QueryMedians{Denom: atomMedian0.Denom, NumStamps: 1})
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)

Expand All @@ -239,19 +239,19 @@ 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)

res, err = s.queryClient.Medians(ctx.Context(), &types.QueryMedians{Denom: atomMedian1.Denom, NumStamps: 2})
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)

Expand All @@ -275,16 +275,16 @@ 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)

res, err = s.queryClient.MedianDeviations(ctx.Context(), &types.QueryMedianDeviations{Denom: atomMedianDeviation.Denom})
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)
}
Expand Down
11 changes: 4 additions & 7 deletions x/oracle/keeper/historic_price.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
Expand Down
10 changes: 5 additions & 5 deletions x/oracle/keeper/historic_price_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
}
33 changes: 16 additions & 17 deletions x/oracle/types/price.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}
17 changes: 17 additions & 0 deletions x/oracle/types/price_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit d907444

Please sign in to comment.