Skip to content

Commit

Permalink
renamed ExchangeRate to DenomExchangeRate on oracle genesis
Browse files Browse the repository at this point in the history
 and using ExchangeRate for return types on oracle
  • Loading branch information
gsk967 committed Sep 22, 2023
1 parent 39ef0bc commit ed56799
Show file tree
Hide file tree
Showing 16 changed files with 201 additions and 170 deletions.
2 changes: 1 addition & 1 deletion app/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func IntegrationTestNetworkConfig() network.Config {
// execute ballot voting and thus clear out previous exchange rates, since we
// are not running a price-feeder.
oracleGenState.Params.VotePeriod = 1000
oracleGenState.ExchangeRates = append(oracleGenState.ExchangeRates, oracletypes.NewExchangeRate(
oracleGenState.ExchangeRates = append(oracleGenState.ExchangeRates, oracletypes.NewDenomExchangeRate(
params.DisplayDenom, sdk.MustNewDecFromStr("34.21"), time.Now()))
// Set mock historic medians to satisfy leverage module's 24 median requirement
for i := 1; i <= 24; i++ {
Expand Down
2 changes: 1 addition & 1 deletion proto/umee/oracle/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ option (gogoproto.goproto_getters_all) = false;
message GenesisState {
Params params = 1 [(gogoproto.nullable) = false];
repeated FeederDelegation feeder_delegations = 2 [(gogoproto.nullable) = false];
repeated ExchangeRate exchange_rates = 3 [(gogoproto.nullable) = false];
repeated DenomExchangeRate exchange_rates = 3 [(gogoproto.nullable) = false];
repeated MissCounter miss_counters = 4 [(gogoproto.nullable) = false];
repeated AggregateExchangeRatePrevote aggregate_exchange_rate_prevotes = 5 [(gogoproto.nullable) = false];
repeated AggregateExchangeRateVote aggregate_exchange_rate_votes = 6 [(gogoproto.nullable) = false];
Expand Down
4 changes: 2 additions & 2 deletions proto/umee/oracle/v1/oracle.proto
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ message AvgCounter {
google.protobuf.Timestamp start = 3 [(gogoproto.nullable) = false, (gogoproto.stdtime) = true];
}

// ExchangeRate stores exchange rate with timestamp
message ExchangeRate {
// DenomExchangeRate stores exchange rate with timestamp
message DenomExchangeRate {
option (gogoproto.equal) = false;
option (gogoproto.goproto_stringer) = false;
string denom = 1 [(gogoproto.moretags) = "yaml:\"denom\""];
Expand Down
6 changes: 2 additions & 4 deletions x/oracle/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ func (s *IntegrationTestSuite) TestEndBlockerVoteThreshold() {
s.Require().NoError(err)
s.Require().Equal(types.ExchangeRate{
Rate: sdk.OneDec(),
Denom: denom.SymbolDenom,
Timestamp: ctx.BlockTime()},
rate)
}
Expand Down Expand Up @@ -202,8 +201,7 @@ func (s *IntegrationTestSuite) TestEndBlockerVoteThreshold() {
for _, denom := range app.OracleKeeper.AcceptList(ctx) {
rate, err := app.OracleKeeper.GetExchangeRate(ctx, denom.SymbolDenom)
s.Require().NoError(err)
s.Require().Equal(types.ExchangeRate{
Denom: denom.SymbolDenom, Rate: sdk.NewDecWithPrec(5, 1), Timestamp: ctx.BlockTime()}, rate)
s.Require().Equal(types.ExchangeRate{Rate: sdk.NewDecWithPrec(5, 1), Timestamp: ctx.BlockTime()}, rate)
}

// TODO: check reward distribution
Expand Down Expand Up @@ -240,7 +238,7 @@ func (s *IntegrationTestSuite) TestEndBlockerVoteThreshold() {

rate, err := app.OracleKeeper.GetExchangeRate(ctx, "umee")
s.Require().NoError(err)
s.Require().Equal(types.ExchangeRate{Denom: "UMEE", Rate: sdk.OneDec(), Timestamp: ctx.BlockTime()}, rate)
s.Require().Equal(types.ExchangeRate{Rate: sdk.OneDec(), Timestamp: ctx.BlockTime()}, rate)
rate, err = app.OracleKeeper.GetExchangeRate(ctx, "ATOM")
s.Require().ErrorIs(err, types.ErrUnknownDenom.Wrap("ATOM"))
s.Require().Equal(types.ExchangeRate{}, rate)
Expand Down
4 changes: 2 additions & 2 deletions x/oracle/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) *types.GenesisState {
return false
})

exchangeRates := []types.ExchangeRate{}
exchangeRates := []types.DenomExchangeRate{}
keeper.IterateExchangeRates(ctx, func(denom string, er sdk.Dec, t time.Time) (stop bool) {
exchangeRates = append(exchangeRates, types.NewExchangeRate(denom, er, t))
exchangeRates = append(exchangeRates, types.NewDenomExchangeRate(denom, er, t))
return false
})

Expand Down
4 changes: 2 additions & 2 deletions x/oracle/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (s *IntegrationTestSuite) TestGenesis_InitGenesis() {
"valid",
types.GenesisState{
Params: types.DefaultParams(),
ExchangeRates: []types.ExchangeRate{
ExchangeRates: []types.DenomExchangeRate{
{
Denom: denom,
Rate: exchangeRate,
Expand Down Expand Up @@ -153,7 +153,7 @@ func (s *IntegrationTestSuite) TestGenesis_ExportGenesis() {
ValidatorAddress: umeevaloperAddr,
},
}
exchangeRateTuples := []types.ExchangeRate{
exchangeRateTuples := []types.DenomExchangeRate{
{
Denom: upperDenom,
Rate: exchangeRate,
Expand Down
13 changes: 6 additions & 7 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"
"time"

"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -76,12 +75,12 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger {
// GetExchangeRate gets the consensus exchange rate of USD denominated in the
// denom asset from the store.
func (k Keeper) GetExchangeRate(ctx sdk.Context, symbol string) (types.ExchangeRate, error) {
v := store.GetValue[*types.DenomExchangeRate](ctx.KVStore(k.storeKey), types.KeyExchangeRate(symbol),
v := store.GetValue[*types.ExchangeRate](ctx.KVStore(k.storeKey), types.KeyExchangeRate(symbol),
"exchange_rate")
if v == nil {
return types.ExchangeRate{}, types.ErrUnknownDenom.Wrap(symbol)
}
return types.NewExchangeRate(strings.ToUpper(symbol), sdk.MustNewDecFromStr(v.Rate), v.Timestamp), nil
return *v, nil
}

// GetExchangeRateBase gets the consensus exchange rate of an asset
Expand Down Expand Up @@ -116,8 +115,8 @@ func (k Keeper) GetExchangeRateBase(ctx sdk.Context, denom string) (sdk.Dec, err
// denom asset to the store with timestamp but with passing timestamp to func
func (k Keeper) SetExchangeRateWithTimestamp(ctx sdk.Context, denom string, rate sdk.Dec, t time.Time) {
key := types.KeyExchangeRate(denom)
val := types.DenomExchangeRate{Rate: rate.String(), Timestamp: t}
err := store.SetValue[*types.DenomExchangeRate](ctx.KVStore(k.storeKey), key, &val, "exchange_rate")
val := types.ExchangeRate{Rate: rate, Timestamp: t}
err := store.SetValue[*types.ExchangeRate](ctx.KVStore(k.storeKey), key, &val, "exchange_rate")
util.Panic(err)
}

Expand Down Expand Up @@ -146,11 +145,11 @@ 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
var exgRate types.DenomExchangeRate
var exgRate types.ExchangeRate
err := exgRate.Unmarshal(iter.Value())
util.Panic(err)

if handler(denom, sdk.MustNewDecFromStr(exgRate.Rate), exgRate.Timestamp) {
if handler(denom, exgRate.Rate, exgRate.Timestamp) {
break
}
}
Expand Down
4 changes: 2 additions & 2 deletions x/oracle/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func (s *IntegrationTestSuite) TestSetExchangeRateWithEvent() {
s.app.OracleKeeper.SetExchangeRateWithEvent(s.ctx, displayDenom, v)
rate, err := s.app.OracleKeeper.GetExchangeRate(s.ctx, displayDenom)
s.Require().NoError(err)
s.Require().Equal(rate, types.ExchangeRate{Rate: v, Timestamp: s.ctx.BlockTime(), Denom: displayDenom})
s.Require().Equal(rate, types.ExchangeRate{Rate: v, Timestamp: s.ctx.BlockTime()})
}

func (s *IntegrationTestSuite) TestGetExchangeRate_InvalidDenom() {
Expand All @@ -228,7 +228,7 @@ func (s *IntegrationTestSuite) TestGetExchangeRate_NotSet() {

func (s *IntegrationTestSuite) TestGetExchangeRate_Valid() {
v := sdk.OneDec()
expected := types.ExchangeRate{Rate: v, Timestamp: s.ctx.BlockTime(), Denom: displayDenom}
expected := types.ExchangeRate{Rate: v, Timestamp: s.ctx.BlockTime()}
s.app.OracleKeeper.SetExchangeRate(s.ctx, displayDenom, v)
rate, err := s.app.OracleKeeper.GetExchangeRate(s.ctx, displayDenom)
s.Require().NoError(err)
Expand Down
28 changes: 19 additions & 9 deletions x/oracle/types/exchange_rates.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,43 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

// NewExchangeRate creates a ExchangeRate instance
func NewExchangeRate(denom string, exchangeRate sdk.Dec, t time.Time) ExchangeRate {
return ExchangeRate{
// NewDenomExchangeRate creates a DenomExchangeRate instance
func NewDenomExchangeRate(denom string, exchangeRate sdk.Dec, t time.Time) DenomExchangeRate {
return DenomExchangeRate{
Denom: denom,
Rate: exchangeRate,
Timestamp: t,
}
}

// DenomExchangeRate is type for storing rate and timestamp of denom into store without denom.
type DenomExchangeRate struct {
Rate string `json:"rate"`
func (v DenomExchangeRate) String() string {
bz, _ := json.Marshal(v)
return string(bz)
}

// ExchangeRate is type for storing rate and timestamp of denom into store without denom.
type ExchangeRate struct {
Rate sdk.Dec `json:"rate"`
Timestamp time.Time `json:"timestamp"`
}

// Marshal implements store.Marshalable.
func (d *DenomExchangeRate) Marshal() ([]byte, error) {
func (d *ExchangeRate) Marshal() ([]byte, error) {
return json.Marshal(d)
}

func (d *ExchangeRate) String() string {
out, _ := json.Marshal(d)
return string(out)
}

// MarshalTo implements store.Marshalable.
func (DenomExchangeRate) MarshalTo(_ []byte) (int, error) {
func (ExchangeRate) MarshalTo(_ []byte) (int, error) {
panic("unimplemented")
}

// Unmarshal implements store.Marshalable.
func (d *DenomExchangeRate) Unmarshal(data []byte) error {
func (d *ExchangeRate) Unmarshal(data []byte) error {
err := json.Unmarshal(data, d)
return err
}
23 changes: 23 additions & 0 deletions x/oracle/types/exchange_rates_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package types

import (
"testing"
time "time"

sdk "github.com/cosmos/cosmos-sdk/types"
"gotest.tools/v3/assert"
)

func TestExchangeRateMarshalAndUnmarshal(t *testing.T) {
der := ExchangeRate{Rate: sdk.NewDec(1), Timestamp: time.Now()}

// Marshal the exchange rate
md, err := der.Marshal()
assert.NilError(t, err)

// Unmarshal the exchange rate
var newExgRate ExchangeRate
err = newExgRate.Unmarshal(md)
assert.NilError(t, err)
assert.DeepEqual(t, der, newExgRate)
}
4 changes: 2 additions & 2 deletions x/oracle/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// NewGenesisState creates a new GenesisState object
func NewGenesisState(
params Params,
rates []ExchangeRate,
rates []DenomExchangeRate,
feederDelegations []FeederDelegation,
missCounters []MissCounter,
aggregateExchangeRatePrevotes []AggregateExchangeRatePrevote,
Expand Down Expand Up @@ -39,7 +39,7 @@ func NewGenesisState(
func DefaultGenesisState() *GenesisState {
return &GenesisState{
Params: DefaultParams(),
ExchangeRates: []ExchangeRate{},
ExchangeRates: []DenomExchangeRate{},
FeederDelegations: []FeederDelegation{},
MissCounters: []MissCounter{},
AggregateExchangeRatePrevotes: []AggregateExchangeRatePrevote{},
Expand Down
82 changes: 41 additions & 41 deletions x/oracle/types/genesis.pb.go

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

2 changes: 1 addition & 1 deletion x/oracle/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestGenesisValidation(t *testing.T) {
func TestGetGenesisStateFromAppState(t *testing.T) {
emptyGenesis := GenesisState{
Params: Params{},
ExchangeRates: []ExchangeRate{},
ExchangeRates: []DenomExchangeRate{},
FeederDelegations: []FeederDelegation{},
MissCounters: []MissCounter{},
AggregateExchangeRatePrevotes: []AggregateExchangeRatePrevote{},
Expand Down
Loading

0 comments on commit ed56799

Please sign in to comment.