Skip to content

Commit

Permalink
update implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-zaremba committed Sep 15, 2023
1 parent 3df9da6 commit c39b114
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 88 deletions.
4 changes: 1 addition & 3 deletions proto/umee/oracle/v1/oracle.proto
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,7 @@ message AvgCounter {

// ExchangeRate stores exchange rate with timestamp
message ExchangeRate {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;
option (gogoproto.goproto_stringer) = false;
option (gogoproto.equal) = false;

string rate = 1 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
Expand Down
9 changes: 5 additions & 4 deletions x/leverage/keeper/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ func (k Keeper) TokenPrice(ctx sdk.Context, baseDenom string, mode types.PriceMo
mode = types.PriceModeSpot
}

var price, spotPrice, historicPrice sdk.Dec
var price, historicPrice sdk.Dec
var spotPrice oracletypes.ExchangeRate
if mode != types.PriceModeHistoric {
// spot price is required for modes other than historic
spotPrice, err = k.oracleKeeper.GetExchangeRate(ctx, t.SymbolDenom)
Expand All @@ -58,13 +59,13 @@ func (k Keeper) TokenPrice(ctx sdk.Context, baseDenom string, mode types.PriceMo

switch mode {
case types.PriceModeSpot:
price = spotPrice
price = spotPrice.Rate
case types.PriceModeHistoric:
price = historicPrice
case types.PriceModeHigh:
price = sdk.MaxDec(spotPrice, historicPrice)
price = sdk.MaxDec(spotPrice.Rate, historicPrice)
case types.PriceModeLow:
price = sdk.MinDec(spotPrice, historicPrice)
price = sdk.MinDec(spotPrice.Rate, historicPrice)
default:
return sdk.ZeroDec(), t.Exponent, types.ErrInvalidPriceMode.Wrapf("%d", mode)
}
Expand Down
4 changes: 3 additions & 1 deletion x/leverage/types/expected_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"

oracle "github.com/umee-network/umee/v6/x/oracle/types"
)

// AccountKeeper defines the expected account keeper used for leverage simulations (noalias)
Expand Down Expand Up @@ -31,6 +33,6 @@ type BankKeeper interface {

// OracleKeeper defines the expected x/oracle keeper interface.
type OracleKeeper interface {
GetExchangeRate(ctx sdk.Context, denom string) (sdk.Dec, error)
GetExchangeRate(ctx sdk.Context, denom string) (oracle.ExchangeRate, error)
MedianOfHistoricMedians(ctx sdk.Context, denom string, numStamps uint64) (sdk.Dec, uint32, error)
}
4 changes: 3 additions & 1 deletion x/oracle/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ func (q querier) ExchangeRates(

ctx := sdk.UnwrapSDKContext(goCtx)

// TODO: need to decide if we want to return DecCoins here or list of ExchangeRates with denoms (we
// need the latter for genesis anyway)
var exchangeRates sdk.DecCoins

if len(req.Denom) > 0 {
Expand All @@ -60,7 +62,7 @@ func (q querier) ExchangeRates(
return nil, err
}

exchangeRates = exchangeRates.Add(sdk.NewDecCoinFromDec(req.Denom, exchangeRate))
exchangeRates = exchangeRates.Add(sdk.NewDecCoinFromDec(req.Denom, exchangeRate.Rate))
} else {
q.IterateExchangeRates(ctx, func(denom string, rate sdk.Dec) (stop bool) {
exchangeRates = exchangeRates.Add(sdk.NewDecCoinFromDec(denom, rate))
Expand Down
11 changes: 6 additions & 5 deletions x/oracle/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package keeper

import (
"fmt"
"strings"

"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
Expand Down Expand Up @@ -84,6 +83,7 @@ func (k Keeper) GetExchangeRate(ctx sdk.Context, symbol string) (types.ExchangeR

// GetExchangeRateBase gets the consensus exchange rate of an asset
// in the base denom (e.g. ATOM -> uatom)
// TODO: needs to return timestamp as well
func (k Keeper) GetExchangeRateBase(ctx sdk.Context, denom string) (sdk.Dec, error) {
var symbol string
var exponent uint64
Expand All @@ -106,7 +106,7 @@ func (k Keeper) GetExchangeRateBase(ctx sdk.Context, denom string) (sdk.Dec, err
}

powerReduction := ten.Power(exponent)
return exchangeRate.Quo(powerReduction), nil
return exchangeRate.Rate.Quo(powerReduction), nil
}

// SetExchangeRate sets the consensus exchange rate of USD denominated in the
Expand All @@ -127,6 +127,7 @@ func (k Keeper) SetExchangeRateWithEvent(ctx sdk.Context, denom string, exchange
}

// IterateExchangeRates iterates over all USD rates in the store.
// TODO: handler should use ExchangeRate type rather than Dec
func (k Keeper) IterateExchangeRates(ctx sdk.Context, handler func(string, sdk.Dec) bool) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.KeyPrefixExchangeRate)
Expand All @@ -136,10 +137,10 @@ func (k Keeper) IterateExchangeRates(ctx sdk.Context, handler func(string, sdk.D
for ; iter.Valid(); iter.Next() {
key := iter.Key()
denom := string(key[prefixLen : len(key)-1]) // -1 to remove the null suffix
dp := sdk.DecProto{}
k.cdc.MustUnmarshal(iter.Value(), &dp)
var er types.ExchangeRate
k.cdc.MustUnmarshal(iter.Value(), &er)

if handler(denom, dp.Dec) {
if handler(denom, er.Rate) {
break
}
}
Expand Down
143 changes: 72 additions & 71 deletions x/oracle/types/oracle.pb.go

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

4 changes: 1 addition & 3 deletions x/oracle/types/vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"gopkg.in/yaml.v3"

"github.com/umee-network/umee/v6/x/leverage/types"
)

func NewAggregateExchangeRatePrevote(
Expand Down Expand Up @@ -83,7 +81,7 @@ func ParseExchangeRateTuples(tuplesStr string) (ExchangeRateTuples, error) {
return nil, err
}
if !decCoin.IsPositive() {
return nil, types.ErrInvalidOraclePrice
return nil, fmt.Errorf("exchange rate can't be negative: %s", tupleStr)
}

denom := strings.ToUpper(denomAmountStr[0])
Expand Down

0 comments on commit c39b114

Please sign in to comment.