Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support of the feemodel params. #4

Merged
merged 1 commit into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ This will:

2. Build and start
```bash
docker-compose up
docker-compose build && docker-compose up
```

* Open [hasura UI](http://localhost:8080/console) and check that it works correctly.
Expand Down
2 changes: 1 addition & 1 deletion cmd/parse/gov/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func proposalCmd(parseConfig *parsecmdtypes.Config) *cobra.Command {
db := database.Cast(parseCtx.Database)

// Build the gov module
govModule := gov.NewModule(sources.GovSource, nil, nil, nil, nil, nil, parseCtx.EncodingConfig.Marshaler, db)
govModule := gov.NewModule(sources.GovSource, nil, nil, nil, nil, nil, nil, parseCtx.EncodingConfig.Marshaler, db)

err = refreshProposalDetails(parseCtx, proposalID, govModule)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions config-sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ chain:
- staking
- distribution
- actions
- feemodel
node:
type: remote
config:
Expand Down
58 changes: 58 additions & 0 deletions database/feemodel_params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package database

import (
"encoding/json"
"fmt"

feemodeltypes "github.com/CoreumFoundation/coreum/x/feemodel/types"
dbtypes "github.com/forbole/bdjuno/v3/database/types"
"github.com/forbole/bdjuno/v3/types"
)

// SaveFeeModelParams allows to store the given params into the database.
func (db *Db) SaveFeeModelParams(params types.FeeModelParams) error {
paramsBz, err := json.Marshal(&params.Params)
if err != nil {
return fmt.Errorf("error while marshaling feemodel params: %s", err)
}

stmt := `
INSERT INTO feemodel_params (params, height)
VALUES ($1, $2)
ON CONFLICT (one_row_id) DO UPDATE
SET params = excluded.params,
height = excluded.height
WHERE feemodel_params.height <= excluded.height`

_, err = db.Sql.Exec(stmt, string(paramsBz), params.Height)
if err != nil {
return fmt.Errorf("error while storing feemodel params: %s", err)
}

return nil
}

// GetFeeModelParams returns the types.FeeModelParams instance containing the current params
func (db *Db) GetFeeModelParams() (*types.FeeModelParams, error) {
var rows []dbtypes.FeeModelParamsRow
stmt := `SELECT * FROM feemodel_params LIMIT 1`
err := db.Sqlx.Select(&rows, stmt)
if err != nil {
return nil, err
}

if len(rows) == 0 {
return nil, fmt.Errorf("no feemodel params found")
}

var feemodelParams feemodeltypes.Params
err = json.Unmarshal([]byte(rows[0].Params), &feemodelParams)
if err != nil {
return nil, err
}

return &types.FeeModelParams{
Params: feemodelParams,
Height: rows[0].Height,
}, nil
}
47 changes: 47 additions & 0 deletions database/feemodel_params_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package database_test

import (
"encoding/json"

feemodeltypes "github.com/CoreumFoundation/coreum/x/feemodel/types"
dbtypes "github.com/forbole/bdjuno/v3/database/types"
"github.com/forbole/bdjuno/v3/types"
)

func (suite *DbTestSuite) TestSaveFeeModelParams() {
feemodelParams := feemodeltypes.DefaultParams()
err := suite.database.SaveFeeModelParams(types.NewFeeModelParams(feemodelParams, 10))
suite.Require().NoError(err)

var rows []dbtypes.FeeModelParamsRow
err = suite.database.Sqlx.Select(&rows, `SELECT * FROM feemodel_params`)
suite.Require().NoError(err)

suite.Require().Len(rows, 1)

var stored feemodeltypes.Params
err = json.Unmarshal([]byte(rows[0].Params), &stored)
suite.Require().NoError(err)
suite.Require().Equal(feemodelParams, stored)
}

func (suite *DbTestSuite) TestGetFeeModelParams() {
feemodelParams := feemodeltypes.DefaultParams()

paramsBz, err := json.Marshal(&feemodelParams)
suite.Require().NoError(err)

_, err = suite.database.Sql.Exec(
`INSERT INTO feemodel_params (params, height) VALUES ($1, $2)`,
string(paramsBz), 10,
)
suite.Require().NoError(err)

params, err := suite.database.GetFeeModelParams()
suite.Require().NoError(err)

suite.Require().Equal(&types.FeeModelParams{
Params: feemodelParams,
Height: 10,
}, params)
}
10 changes: 10 additions & 0 deletions database/schema/12-feemodel.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* ---- PARAMS ---- */

CREATE TABLE feemodel_params
(
one_row_id BOOLEAN NOT NULL DEFAULT TRUE PRIMARY KEY,
params JSONB NOT NULL,
height BIGINT NOT NULL,
CHECK (one_row_id)
);
CREATE INDEX feemodel_params_height_index ON feemodel_params (height);
8 changes: 8 additions & 0 deletions database/types/feemodel_params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package types

// FeeModelParamsRow represents a single row inside the feemodel_params table
type FeeModelParamsRow struct {
OneRowID bool `db:"one_row_id"`
Params string `db:"params"`
Height int64 `db:"height"`
}
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/forbole/bdjuno/v3
go 1.18

require (
github.com/CoreumFoundation/coreum v0.0.1-dev
github.com/CoreumFoundation/coreum v0.0.1-dev-1.0.20221221080541-af88eb7eecde
github.com/cosmos/cosmos-sdk v0.45.9
github.com/forbole/juno/v3 v3.4.1-0.20220912104021-a2cfbab251bc
github.com/go-co-op/gocron v1.13.0
Expand Down Expand Up @@ -58,6 +58,7 @@ require (
github.com/dvsekhvalnov/jose2go v1.5.0 // indirect
github.com/felixge/httpsnoop v1.0.2 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/gin-gonic/gin v1.7.0 // indirect
github.com/go-kit/kit v0.12.0 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
Expand Down Expand Up @@ -120,9 +121,7 @@ require (
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect
github.com/tendermint/btcd v0.1.1 // indirect
github.com/tendermint/crypto v0.0.0-20191022145703-50d29ede1e15 // indirect
github.com/tendermint/fundraising v0.3.0 // indirect
github.com/tendermint/go-amino v0.16.0 // indirect
github.com/tendermint/spn v0.2.1-0.20220610090138-44b136f042c4 // indirect
github.com/tendermint/tm-db v0.6.7 // indirect
github.com/zondax/hid v0.9.0 // indirect
go.etcd.io/bbolt v1.3.6 // indirect
Expand Down
Loading