From 80c17ad0aae7b40da9193921749b9a694e74d1d8 Mon Sep 17 00:00:00 2001 From: rbajollari Date: Mon, 30 Oct 2023 20:51:42 -0400 Subject: [PATCH] pr comments --- cmd/price-feeder.go | 23 ++++++------- oracle/oracle.go | 61 ++------------------------------- oracle/oracle_test.go | 2 +- oracle/util.go | 58 +++++++++++++++++++++++++++++++ tests/integration/price_test.go | 3 +- 5 files changed, 72 insertions(+), 75 deletions(-) diff --git a/cmd/price-feeder.go b/cmd/price-feeder.go index bb74c8d1..ff07b9e8 100644 --- a/cmd/price-feeder.go +++ b/cmd/price-feeder.go @@ -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" ) @@ -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()) @@ -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 } @@ -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 diff --git a/oracle/oracle.go b/oracle/oracle.go index 151baa79..e6f10798 100644 --- a/oracle/oracle.go +++ b/oracle/oracle.go @@ -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(), @@ -97,7 +97,7 @@ func New( paramCache: &ParamCache{params: nil}, chainConfig: chainConfig, endpoints: endpoints, - }, nil + } } // LoadProviderPairsAndDeviations loads the on chain pair providers and @@ -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") diff --git a/oracle/oracle_test.go b/oracle/oracle_test.go index 600d4cfb..b4633476 100644 --- a/oracle/oracle_test.go +++ b/oracle/oracle_test.go @@ -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{ diff --git a/oracle/util.go b/oracle/util.go index 2360e572..1c7b9d78 100644 --- a/oracle/util.go +++ b/oracle/util.go @@ -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" @@ -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 +} diff --git a/tests/integration/price_test.go b/tests/integration/price_test.go index 858b1009..7974e16a 100644 --- a/tests/integration/price_test.go +++ b/tests/integration/price_test.go @@ -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(), @@ -55,7 +55,6 @@ func TestPriceAccuracy(t *testing.T) { cfg.ProviderEndpointsMap(), false, ) - require.NoError(t, err) symbols := cfg.ExpectedSymbols()