Skip to content

Commit

Permalink
add periodic ops to history module
Browse files Browse the repository at this point in the history
  • Loading branch information
MonikaCat committed Mar 9, 2022
1 parent 34390af commit 29520fd
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 71 deletions.
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
54 changes: 54 additions & 0 deletions modules/history/handle_periodic_operations.go
Original file line number Diff line number Diff line change
@@ -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)

}
3 changes: 2 additions & 1 deletion modules/history/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 0 additions & 8 deletions modules/history/utils_prices.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 0 additions & 8 deletions modules/pricefeed/expected_modules.go

This file was deleted.

2 changes: 1 addition & 1 deletion modules/pricefeed/handle_additional_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
41 changes: 0 additions & 41 deletions modules/pricefeed/handle_periodic_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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

}
16 changes: 7 additions & 9 deletions modules/pricefeed/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down
2 changes: 1 addition & 1 deletion modules/registrar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down

0 comments on commit 29520fd

Please sign in to comment.