forked from forbole/callisto
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
64cb6e8
commit ecaca55
Showing
19 changed files
with
211 additions
and
382 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package cudomint | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"time" | ||
|
||
"github.com/forbole/bdjuno/v4/modules/utils" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"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", "cudomint").Msg("setting up periodic tasks") | ||
|
||
// 00:10 because stats service executes at 00:00 | ||
if _, err := scheduler.Every(1).Day().At("00:10").Do(func() { | ||
utils.WatchMethod(m.fetchStats) | ||
}); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (m *Module) fetchStats() error { | ||
ctx, cancelFunc := context.WithTimeout(context.Background(), time.Second*10) | ||
defer cancelFunc() | ||
response, err := m.client.GET(ctx, "/stats") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var stats statsResponse | ||
if err := json.Unmarshal([]byte(response), &stats); err != nil { | ||
return err | ||
} | ||
|
||
apr, err := sdk.NewDecFromStr(stats.APR.Value) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := m.db.SaveAPR(apr, stats.APR.Height); err != nil { | ||
return err | ||
} | ||
|
||
if err := m.db.SaveAPRHistory(apr, stats.APR.Height, time.Now().UnixNano()); err != nil { | ||
return err | ||
} | ||
|
||
inflation, err := sdk.NewDecFromStr(stats.Inflation.Value) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := m.db.SaveInflation(inflation, stats.Inflation.Height); err != nil { | ||
return err | ||
} | ||
|
||
supply, err := sdk.NewDecFromStr(stats.Supply.Value) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
supply = supply.MulInt64(1000000000000000000) | ||
|
||
if err := m.db.SaveAdjustedSupply(supply, stats.Supply.Height); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
type statsResponse struct { | ||
Inflation valueAtHeight `json:"inflation"` | ||
APR valueAtHeight `json:"apr"` | ||
Supply valueAtHeight `json:"supply"` | ||
} | ||
|
||
type valueAtHeight struct { | ||
Value string `json:"value"` | ||
Height int64 `json:"height"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package cudomint | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/cosmos/cosmos-sdk/codec" | ||
"github.com/forbole/juno/v5/modules" | ||
"gopkg.in/yaml.v3" | ||
|
||
"github.com/forbole/bdjuno/v4/database" | ||
"github.com/forbole/bdjuno/v4/modules/cudomint/rest" | ||
) | ||
|
||
var ( | ||
_ modules.Module = &Module{} | ||
_ modules.PeriodicOperationsModule = &Module{} | ||
) | ||
|
||
// Module represent database/mint module | ||
type Module struct { | ||
cdc codec.Codec | ||
db *database.Db | ||
client *rest.Client | ||
} | ||
|
||
type config struct { | ||
Config struct { | ||
StatsServiceURL string `yaml:"stats_service_url"` | ||
} `yaml:"cudomint"` | ||
} | ||
|
||
// NewModule returns a new Module instance | ||
func NewModule(cdc codec.Codec, db *database.Db, configBytes []byte) *Module { | ||
var config config | ||
if err := yaml.Unmarshal(configBytes, &config); err != nil { | ||
panic(fmt.Errorf("failed to parse cudomint config: %s", err)) | ||
} | ||
|
||
return &Module{ | ||
cdc: cdc, | ||
db: db, | ||
client: rest.NewClient(config.Config.StatsServiceURL), | ||
} | ||
} | ||
|
||
// Name implements modules.Module | ||
func (m *Module) Name() string { | ||
return "cudomint" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package rest | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
) | ||
|
||
type Client struct { | ||
URL string | ||
} | ||
|
||
func NewClient(url string) *Client { | ||
return &Client{URL: url} | ||
} | ||
|
||
func (sc Client) GET(ctx context.Context, uri string) (string, error) { | ||
getReq, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s%s", sc.URL, uri), nil) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
resp, err := http.DefaultClient.Do(getReq) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
defer resp.Body.Close() | ||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return string(body), nil | ||
} |
Oops, something went wrong.