forked from forbole/callisto
/
periodic_operations.go
54 lines (43 loc) · 1.43 KB
/
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
46
47
48
49
50
51
52
53
54
package mint
import (
"context"
"github.com/go-co-op/gocron"
"github.com/rs/zerolog/log"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
"github.com/faddat/bdjuno/database"
"github.com/faddat/bdjuno/x/utils"
)
// RegisterPeriodicOps returns the AdditionalOperation that periodically runs fetches from
// the LCD to make sure that constantly changing data are synced properly.
func RegisterPeriodicOps(scheduler *gocron.Scheduler, minClient minttypes.QueryClient, db *database.BigDipperDb) 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").StartImmediately().Do(func() {
utils.WatchMethod(func() error { return updateInflation(minClient, db) })
}); 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 updateInflation(mintClient minttypes.QueryClient, db *database.BigDipperDb) error {
log.Debug().
Str("module", "mint").
Str("operation", "inflation").
Msg("getting inflation data")
height, err := db.GetLastBlockHeight()
if err != nil {
return err
}
// Get the inflation
res, err := mintClient.Inflation(
context.Background(),
&minttypes.QueryInflationRequest{},
utils.GetHeightRequestHeader(height),
)
if err != nil {
return err
}
return db.SaveInflation(res.Inflation, height)
}