Skip to content

Commit

Permalink
pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rbajollari committed Oct 31, 2023
1 parent 6da48b9 commit 80c17ad
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 75 deletions.
23 changes: 10 additions & 13 deletions cmd/price-feeder.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ const (
logLevelJSON = "json"
logLevelText = "text"

flagLogLevel = "log-level"
flagLogFormat = "log-format"
flagSkipProviderCheck = "skip-provider-check"
flagChainConfig = "chain-config"
flagLogLevel = "log-level"
flagLogFormat = "log-format"
flagSkipProviderCheck = "skip-provider-check"
flagConfigCurrencyProviders = "config-currency-providers"

envVariablePass = "PRICE_FEEDER_PASS"
)
Expand All @@ -63,9 +63,9 @@ func init() {
rootCmd.PersistentFlags().String(flagLogFormat, logLevelText, "logging format; must be either json or text")
rootCmd.PersistentFlags().Bool(flagSkipProviderCheck, false, "skip the coingecko API provider check")
rootCmd.PersistentFlags().Bool(
flagChainConfig,
flagConfigCurrencyProviders,
false,
"use on chain values for currency pair providers and deviations instead of config values",
"use config file for currency pair providers and deviations instead of on chain values",
)

rootCmd.AddCommand(getVersionCmd())
Expand Down Expand Up @@ -101,7 +101,7 @@ func priceFeederCmdHandler(cmd *cobra.Command, args []string) error {
return err
}

chainConfig, err := cmd.Flags().GetBool(flagChainConfig)
configCurrencyProviders, err := cmd.Flags().GetBool(flagConfigCurrencyProviders)
if err != nil {
return err
}
Expand Down Expand Up @@ -177,20 +177,17 @@ func priceFeederCmdHandler(cmd *cobra.Command, args []string) error {
return err
}

oracle, err := oracle.New(
oracle := oracle.New(
logger,
oracleClient,
cfg.ProviderPairs(),
providerTimeout,
deviations,
cfg.ProviderEndpointsMap(),
chainConfig,
!configCurrencyProviders,
)
if err != nil {
return err
}

if chainConfig {
if !configCurrencyProviders {
err := oracle.LoadProviderPairsAndDeviations(ctx)
if err != nil {
return err
Expand Down
61 changes: 2 additions & 59 deletions oracle/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func New(
deviations map[string]sdk.Dec,
endpoints map[types.ProviderName]provider.Endpoint,
chainConfig bool,
) (*Oracle, error) {
) *Oracle {
return &Oracle{
logger: logger.With().Str("module", "oracle").Logger(),
closer: pfsync.NewCloser(),
Expand All @@ -97,7 +97,7 @@ func New(
paramCache: &ParamCache{params: nil},
chainConfig: chainConfig,
endpoints: endpoints,
}, nil
}
}

// LoadProviderPairsAndDeviations loads the on chain pair providers and
Expand Down Expand Up @@ -557,63 +557,6 @@ func (o *Oracle) checkCurrencyPairAndDeviations(currentParams, newParams oraclet
return nil
}

// createPairProvidersFromCurrencyPairProvidersList will create the pair providers
// map used by the price feeder Oracle from a CurrencyPairProvidersList defined by
// Ojo's oracle module.
func createPairProvidersFromCurrencyPairProvidersList(
currencyPairs oracletypes.CurrencyPairProvidersList,
) map[types.ProviderName][]types.CurrencyPair {
providerPairs := make(map[types.ProviderName][]types.CurrencyPair)

for _, pair := range currencyPairs {
for _, provider := range pair.Providers {
if len(pair.PairAddress) > 0 {
for _, uniPair := range pair.PairAddress {
if (uniPair.AddressProvider == provider) && (uniPair.Address != "") {
providerPairs[types.ProviderName(uniPair.AddressProvider)] = append(
providerPairs[types.ProviderName(uniPair.AddressProvider)],
types.CurrencyPair{
Base: pair.BaseDenom,
Quote: pair.QuoteDenom,
Address: uniPair.Address,
},
)
}
}
} else {
providerPairs[types.ProviderName(provider)] = append(
providerPairs[types.ProviderName(provider)],
types.CurrencyPair{
Base: pair.BaseDenom,
Quote: pair.QuoteDenom,
},
)
}
}
}

return providerPairs
}

// createDeviationsFromCurrencyDeviationThresholdList will create the deviations
// map used by the price feeder Oracle from a CurrencyDeviationThresholdList defined by
// Ojo's oracle module.
func createDeviationsFromCurrencyDeviationThresholdList(
deviationList oracletypes.CurrencyDeviationThresholdList,
) (map[string]sdk.Dec, error) {
deviations := make(map[string]sdk.Dec, len(deviationList))

for _, deviation := range deviationList {
threshold, err := sdk.NewDecFromStr(deviation.Threshold)
if err != nil {
return nil, err
}
deviations[deviation.BaseDenom] = threshold
}

return deviations, nil
}

func (o *Oracle) tick(ctx context.Context) error {
o.logger.Debug().Msg("executing oracle tick")

Expand Down
2 changes: 1 addition & 1 deletion oracle/oracle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ type OracleTestSuite struct {

// SetupSuite executes once before the suite's tests are executed.
func (ots *OracleTestSuite) SetupSuite() {
ots.oracle, _ = New(
ots.oracle = New(
zerolog.Nop(),
client.OracleClient{},
map[types.ProviderName][]types.CurrencyPair{
Expand Down
58 changes: 58 additions & 0 deletions oracle/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
oracletypes "github.com/ojo-network/ojo/x/oracle/types"

"github.com/ojo-network/price-feeder/oracle/provider"
"github.com/ojo-network/price-feeder/oracle/types"
Expand Down Expand Up @@ -222,3 +223,60 @@ func ComputeVwapsByProvider(prices types.AggregatedProviderPrices) types.Currenc
}
return vwaps
}

// createPairProvidersFromCurrencyPairProvidersList will create the pair providers
// map used by the price feeder Oracle from a CurrencyPairProvidersList defined by
// Ojo's oracle module.
func createPairProvidersFromCurrencyPairProvidersList(
currencyPairs oracletypes.CurrencyPairProvidersList,
) map[types.ProviderName][]types.CurrencyPair {
providerPairs := make(map[types.ProviderName][]types.CurrencyPair)

for _, pair := range currencyPairs {
for _, provider := range pair.Providers {
if len(pair.PairAddress) > 0 {
for _, uniPair := range pair.PairAddress {
if (uniPair.AddressProvider == provider) && (uniPair.Address != "") {
providerPairs[types.ProviderName(uniPair.AddressProvider)] = append(
providerPairs[types.ProviderName(uniPair.AddressProvider)],
types.CurrencyPair{
Base: pair.BaseDenom,
Quote: pair.QuoteDenom,
Address: uniPair.Address,
},
)
}
}
} else {
providerPairs[types.ProviderName(provider)] = append(
providerPairs[types.ProviderName(provider)],
types.CurrencyPair{
Base: pair.BaseDenom,
Quote: pair.QuoteDenom,
},
)
}
}
}

return providerPairs
}

// createDeviationsFromCurrencyDeviationThresholdList will create the deviations
// map used by the price feeder Oracle from a CurrencyDeviationThresholdList defined by
// Ojo's oracle module.
func createDeviationsFromCurrencyDeviationThresholdList(
deviationList oracletypes.CurrencyDeviationThresholdList,
) (map[string]sdk.Dec, error) {
deviations := make(map[string]sdk.Dec, len(deviationList))

for _, deviation := range deviationList {
threshold, err := sdk.NewDecFromStr(deviation.Threshold)
if err != nil {
return nil, err
}
deviations[deviation.BaseDenom] = threshold
}

return deviations, nil
}
3 changes: 1 addition & 2 deletions tests/integration/price_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestPriceAccuracy(t *testing.T) {
deviations, err := cfg.DeviationsMap()
require.NoError(t, err)

oracle, err := oracle.New(
oracle := oracle.New(
logger,
client.OracleClient{},
cfg.ProviderPairs(),
Expand All @@ -55,7 +55,6 @@ func TestPriceAccuracy(t *testing.T) {
cfg.ProviderEndpointsMap(),
false,
)
require.NoError(t, err)

symbols := cfg.ExpectedSymbols()

Expand Down

0 comments on commit 80c17ad

Please sign in to comment.