Skip to content

Commit

Permalink
replace: mint with cudomint module
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaghettiOverload committed Aug 10, 2023
1 parent 64cb6e8 commit ecaca55
Show file tree
Hide file tree
Showing 19 changed files with 211 additions and 382 deletions.
4 changes: 1 addition & 3 deletions cmd/parse/gov/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/forbole/bdjuno/v4/database"
"github.com/forbole/bdjuno/v4/modules/distribution"
"github.com/forbole/bdjuno/v4/modules/gov"
"github.com/forbole/bdjuno/v4/modules/mint"
"github.com/forbole/bdjuno/v4/modules/slashing"
"github.com/forbole/bdjuno/v4/modules/staking"
"github.com/forbole/bdjuno/v4/utils"
Expand Down Expand Up @@ -52,12 +51,11 @@ func proposalCmd(parseConfig *parsecmdtypes.Config) *cobra.Command {

// Build expected modules of gov modules for handleParamChangeProposal
distrModule := distribution.NewModule(sources.DistrSource, parseCtx.EncodingConfig.Codec, db)
mintModule := mint.NewModule(sources.MintSource, parseCtx.EncodingConfig.Codec, db)
slashingModule := slashing.NewModule(sources.SlashingSource, parseCtx.EncodingConfig.Codec, db)
stakingModule := staking.NewModule(sources.StakingSource, parseCtx.EncodingConfig.Codec, db)

// Build the gov module
govModule := gov.NewModule(sources.GovSource, nil, distrModule, mintModule, slashingModule, stakingModule, parseCtx.EncodingConfig.Codec, db)
govModule := gov.NewModule(sources.GovSource, nil, distrModule, slashingModule, stakingModule, parseCtx.EncodingConfig.Codec, db)

err = refreshProposalDetails(parseCtx, proposalID, govModule)
if err != nil {
Expand Down
20 changes: 0 additions & 20 deletions cmd/parse/mint/cmd.go

This file was deleted.

45 changes: 0 additions & 45 deletions cmd/parse/mint/inflation.go

This file was deleted.

2 changes: 0 additions & 2 deletions cmd/parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
parsedistribution "github.com/forbole/bdjuno/v4/cmd/parse/distribution"
parsefeegrant "github.com/forbole/bdjuno/v4/cmd/parse/feegrant"
parsegov "github.com/forbole/bdjuno/v4/cmd/parse/gov"
parsemint "github.com/forbole/bdjuno/v4/cmd/parse/mint"
parsestaking "github.com/forbole/bdjuno/v4/cmd/parse/staking"
)

Expand All @@ -35,7 +34,6 @@ func NewParseCmd(parseCfg *parse.Config) *cobra.Command {
parsefeegrant.NewFeegrantCmd(parseCfg),
parsegenesis.NewGenesisCmd(parseCfg),
parsegov.NewGovCmd(parseCfg),
parsemint.NewMintCmd(parseCfg),
parsestaking.NewStakingCmd(parseCfg),
parsetransaction.NewTransactionsCmd(parseCfg),
)
Expand Down
47 changes: 32 additions & 15 deletions database/mint.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package database

import (
"encoding/json"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/forbole/bdjuno/v4/types"
)

// SaveInflation allows to store the inflation for the given block height as well as timestamp
Expand All @@ -27,24 +24,44 @@ WHERE inflation.height <= excluded.height`
return nil
}

// SaveMintParams allows to store the given params inside the database
func (db *Db) SaveMintParams(params *types.MintParams) error {
paramsBz, err := json.Marshal(&params.Params)
if err != nil {
return fmt.Errorf("error while marshaling mint params: %s", err)
func (db *Db) SaveAPR(apr sdk.Dec, height int64) error {
stmt := `
INSERT INTO apr (value, height)
VALUES ($1, $2)
ON CONFLICT (one_row_id) DO UPDATE
SET value = excluded.value,
height = excluded.height
WHERE apr.height <= excluded.height`

if _, err := db.SQL.Exec(stmt, apr.String(), height); err != nil {
return fmt.Errorf("error while storing APR: %s", err)
}

return nil
}

func (db *Db) SaveAPRHistory(apr sdk.Dec, height, timestamp int64) error {
stmt := `INSERT INTO apr_history (value, height, timestamp) VALUES ($1, $2, $3)`

if _, err := db.SQL.Exec(stmt, apr.String(), height, timestamp); err != nil {
return fmt.Errorf("error while storing APR history: %s", err)
}

return nil
}

func (db *Db) SaveAdjustedSupply(supply sdk.Dec, height int64) error {
stmt := `
INSERT INTO mint_params (params, height)
VALUES ($1, $2)
INSERT INTO adjusted_supply (value, height)
VALUES ($1, $2)
ON CONFLICT (one_row_id) DO UPDATE
SET params = excluded.params,
height = excluded.height
WHERE mint_params.height <= excluded.height`
SET value = excluded.value,
height = excluded.height
WHERE adjusted_supply.height <= excluded.height`

_, err = db.SQL.Exec(stmt, string(paramsBz), params.Height)
_, err := db.SQL.Exec(stmt, supply.String(), height)
if err != nil {
return fmt.Errorf("error while storing mint params: %s", err)
return fmt.Errorf("error while storing adjusted supply: %s", err)
}

return nil
Expand Down
29 changes: 0 additions & 29 deletions database/mint_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package database_test

import (
"encoding/json"

sdk "github.com/cosmos/cosmos-sdk/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"

"github.com/forbole/bdjuno/v4/types"

dbtypes "github.com/forbole/bdjuno/v4/database/types"
)
Expand Down Expand Up @@ -71,27 +66,3 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveInflation() {
expected = dbtypes.NewInflationRow(400.00, 110)
suite.Require().True(expected.Equal(rows[0]), "data should change with higher height")
}

func (suite *DbTestSuite) TestBigDipperDb_SaveMintParams() {
mintParams := minttypes.NewParams(
"udaric",
sdk.NewDecWithPrec(4, 1),
sdk.NewDecWithPrec(8, 1),
sdk.NewDecWithPrec(4, 1),
sdk.NewDecWithPrec(8, 1),
5006000,
)
err := suite.database.SaveMintParams(types.NewMintParams(mintParams, 10))
suite.Require().NoError(err)

var rows []dbtypes.MintParamsRow
err = suite.database.Sqlx.Select(&rows, `SELECT * FROM mint_params`)
suite.Require().NoError(err)
suite.Require().Len(rows, 1)

var storedParams minttypes.Params
err = json.Unmarshal([]byte(rows[0].Params), &storedParams)
suite.Require().NoError(err)
suite.Require().Equal(mintParams, storedParams)
suite.Require().Equal(int64(10), rows[0].Height)
}
87 changes: 87 additions & 0 deletions modules/cudomint/handle_periodic_operations.go
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"`
}
49 changes: 49 additions & 0 deletions modules/cudomint/module.go
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"
}
36 changes: 36 additions & 0 deletions modules/cudomint/rest/client.go
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
}
Loading

0 comments on commit ecaca55

Please sign in to comment.