-
Notifications
You must be signed in to change notification settings - Fork 257
/
Copy pathhandle_periodic_operations.go
45 lines (36 loc) · 1.11 KB
/
handle_periodic_operations.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package mint
import (
"fmt"
"github.com/forbole/bdjuno/v4/modules/utils"
"github.com/go-co-op/gocron"
"github.com/rs/zerolog/log"
)
// RegisterPeriodicOperations implements modules.PeriodicOperationsModule
func (m *Module) RegisterPeriodicOperations(scheduler *gocron.Scheduler) error {
log.Debug().Str("module", "mint").Msg("setting up periodic tasks")
// Setup a cron job to run every midnight
if _, err := scheduler.Every(1).Day().At("00:00").Do(func() {
utils.WatchMethod(m.UpdateInflation)
}); err != nil {
return err
}
return nil
}
// updateInflation fetches from the REST APIs the latest value for the
// inflation, and saves it inside the database.
func (m *Module) UpdateInflation() error {
log.Debug().
Str("module", "mint").
Str("operation", "inflation").
Msg("getting inflation data")
block, err := m.db.GetLastBlockHeightAndTimestamp()
if err != nil {
return err
}
// Get the inflation
inflation, err := m.source.GetInflation(block.Height)
if err != nil {
return fmt.Errorf("error while querying API for inflation value: %s", err)
}
return m.db.SaveInflation(inflation, block.Height)
}