diff --git a/go.mod b/go.mod index 136115e22..d295f7781 100644 --- a/go.mod +++ b/go.mod @@ -13,11 +13,9 @@ require ( github.com/proullon/ramsql v0.0.0-20181213202341-817cee58a244 github.com/rs/zerolog v1.26.1 github.com/spf13/cobra v1.3.0 - github.com/spf13/viper v1.10.0 github.com/stretchr/testify v1.7.0 github.com/tendermint/tendermint v0.34.14 google.golang.org/grpc v1.42.0 - gopkg.in/yaml.v2 v2.4.0 gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b ) diff --git a/modules/history/handle_periodic_operations.go b/modules/history/handle_periodic_operations.go new file mode 100644 index 000000000..9d67e2ab0 --- /dev/null +++ b/modules/history/handle_periodic_operations.go @@ -0,0 +1,54 @@ +package history + +import ( + "fmt" + + "github.com/go-co-op/gocron" + "github.com/rs/zerolog/log" + + "github.com/forbole/bdjuno/v2/modules/pricefeed/coingecko" + "github.com/forbole/bdjuno/v2/modules/utils" +) + +// RegisterPeriodicOperations implements modules.PeriodicOperationsModule +func (m *Module) RegisterPeriodicOperations(scheduler *gocron.Scheduler) error { + log.Debug().Str("module", "history").Msg("setting up periodic tasks") + + // Fetch total supply of tokens every 1hr to store historical price data + if _, err := scheduler.Every(1).Hour().Do(func() { + utils.WatchMethod(m.updatePricesHistory) + }); err != nil { + return fmt.Errorf("error while setting up history period operations: %s", err) + } + + return nil +} + +// updatePricesHistory fetch total amount of coins in the system from RPC +// and store historical perice data inside the database +func (m *Module) updatePricesHistory() error { + log.Debug(). + Str("module", "history"). + Str("operation", "history"). + Msg("getting token price and market cap history") + + // Get the list of tokens price id + ids, err := m.db.GetTokensPriceID() + if err != nil { + return fmt.Errorf("error while getting tokens price id: %s", err) + } + + if len(ids) == 0 { + log.Debug().Str("module", "history").Msg("no traded tokens price id found") + return nil + } + + // Get the tokens prices + prices, err := coingecko.GetTokensPrices(ids) + if err != nil { + return fmt.Errorf("error while getting tokens prices history: %s", err) + } + + return m.UpdatePricesHistory(prices) + +} diff --git a/modules/history/module.go b/modules/history/module.go index c3b0ff37e..021dde559 100644 --- a/modules/history/module.go +++ b/modules/history/module.go @@ -14,7 +14,8 @@ const ( ) var ( - _ modules.Module = &Module{} + _ modules.Module = &Module{} + _ modules.PeriodicOperationsModule = &Module{} ) // Module represents the module that allows to store historic information diff --git a/modules/history/utils_prices.go b/modules/history/utils_prices.go index f942c0cc0..d6bbcc0f0 100644 --- a/modules/history/utils_prices.go +++ b/modules/history/utils_prices.go @@ -4,14 +4,6 @@ import ( "github.com/forbole/bdjuno/v2/types" ) -// IsHistoryModuleEnabled checks if history module is enabled inside config.yaml file -func (m *Module) IsHistoryModuleEnabled() bool { - if m.cfg.IsModuleEnabled(moduleName) { - return true - } - return false -} - // UpdatePricesHistory stores the given prices inside the price history table func (m *Module) UpdatePricesHistory(prices []types.TokenPrice) error { return m.db.SaveTokenPricesHistory(prices) diff --git a/modules/pricefeed/expected_modules.go b/modules/pricefeed/expected_modules.go deleted file mode 100644 index 3b2cdf857..000000000 --- a/modules/pricefeed/expected_modules.go +++ /dev/null @@ -1,8 +0,0 @@ -package pricefeed - -import "github.com/forbole/bdjuno/v2/types" - -type HistoryModule interface { - IsHistoryModuleEnabled() bool - UpdatePricesHistory([]types.TokenPrice) error -} diff --git a/modules/pricefeed/handle_additional_operations.go b/modules/pricefeed/handle_additional_operations.go index 3ed557792..e5bd62459 100644 --- a/modules/pricefeed/handle_additional_operations.go +++ b/modules/pricefeed/handle_additional_operations.go @@ -56,5 +56,5 @@ func (m *Module) storeTokens() error { return fmt.Errorf("error while storing token prices: %s", err) } - return m.historyModule.UpdatePricesHistory(prices) + return nil } diff --git a/modules/pricefeed/handle_periodic_operations.go b/modules/pricefeed/handle_periodic_operations.go index 9773811a4..508d00c0f 100644 --- a/modules/pricefeed/handle_periodic_operations.go +++ b/modules/pricefeed/handle_periodic_operations.go @@ -21,13 +21,6 @@ func (m *Module) RegisterPeriodicOperations(scheduler *gocron.Scheduler) error { return fmt.Errorf("error while setting up pricefeed period operations: %s", err) } - // Fetch total supply of tokens every 1hr to store historical price data - if _, err := scheduler.Every(1).Hour().Do(func() { - utils.WatchMethod(m.updatePricesHistory) - }); err != nil { - return fmt.Errorf("error while setting up historic pricefeed period operations: %s", err) - } - return nil } @@ -62,39 +55,5 @@ func (m *Module) updatePrice() error { } return nil -} - -// updatePricesHistory fetch total amount of coins in the system from RPC -// and store historical perice data inside the database -func (m *Module) updatePricesHistory() error { - historyEnabled := m.historyModule.IsHistoryModuleEnabled() - - if historyEnabled { - log.Debug(). - Str("module", "pricefeed"). - Str("operation", "pricefeed"). - Msg("getting hourly token price and market cap") - // Get the list of tokens price id - ids, err := m.db.GetTokensPriceID() - if err != nil { - return fmt.Errorf("error while getting tokens price id: %s", err) - } - - if len(ids) == 0 { - log.Debug().Str("module", "pricefeed").Msg("no traded tokens price id found") - return nil - } - - // Get the tokens prices - prices, err := coingecko.GetTokensPrices(ids) - if err != nil { - return fmt.Errorf("error while getting tokens prices: %s", err) - } - - return m.historyModule.UpdatePricesHistory(prices) - } - - return nil - } diff --git a/modules/pricefeed/module.go b/modules/pricefeed/module.go index d9bacd046..f327bd268 100644 --- a/modules/pricefeed/module.go +++ b/modules/pricefeed/module.go @@ -17,24 +17,22 @@ var ( // Module represents the module that allows to get the token prices type Module struct { - cfg *Config - cdc codec.Marshaler - db *database.Db - historyModule HistoryModule + cfg *Config + cdc codec.Marshaler + db *database.Db } // NewModule returns a new Module instance -func NewModule(cfg config.Config, historyModule HistoryModule, cdc codec.Marshaler, db *database.Db) *Module { +func NewModule(cfg config.Config, cdc codec.Marshaler, db *database.Db) *Module { pricefeedCfg, err := ParseConfig(cfg.GetBytes()) if err != nil { panic(err) } return &Module{ - cfg: pricefeedCfg, - cdc: cdc, - db: db, - historyModule: historyModule, + cfg: pricefeedCfg, + cdc: cdc, + db: db, } } diff --git a/modules/registrar.go b/modules/registrar.go index 7b896f45c..0d4131890 100644 --- a/modules/registrar.go +++ b/modules/registrar.go @@ -129,7 +129,7 @@ func (r *Registrar) BuildModules(ctx registrar.Context) jmodules.Modules { historyModule, mintModule, modules.NewModule(ctx.JunoConfig.Chain, db), - pricefeed.NewModule(ctx.JunoConfig, historyModule, cdc, db), + pricefeed.NewModule(ctx.JunoConfig, cdc, db), slashingModule, stakingModule, }