Skip to content

Commit

Permalink
feat(mint): migrate mint module
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Jan 17, 2024
1 parent 524f2f7 commit ca000b2
Show file tree
Hide file tree
Showing 27 changed files with 295 additions and 233 deletions.
20 changes: 12 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,25 @@ go 1.21

require (
cosmossdk.io/api v0.7.2
cosmossdk.io/client/v2 v2.0.0-beta.1
cosmossdk.io/collections v0.4.0
cosmossdk.io/core v0.11.0
cosmossdk.io/depinject v1.0.0-alpha.4
cosmossdk.io/errors v1.0.0
cosmossdk.io/log v1.2.1
cosmossdk.io/math v1.2.0
cosmossdk.io/store v1.0.0
cosmossdk.io/tools/confix v0.1.0
cosmossdk.io/x/circuit v0.1.0
cosmossdk.io/x/evidence v0.1.0
cosmossdk.io/x/feegrant v0.1.0
cosmossdk.io/x/tx v0.12.0
cosmossdk.io/x/upgrade v0.1.0
github.com/CosmWasm/wasmd v0.50.0
github.com/Masterminds/sprig/v3 v3.2.3
github.com/armon/go-metrics v0.4.1
github.com/cometbft/cometbft v0.38.1
github.com/cometbft/cometbft-db v0.8.0
github.com/cosmos/cosmos-db v1.0.0
github.com/cosmos/cosmos-proto v1.0.0-beta.3
github.com/cosmos/cosmos-sdk v0.50.1
github.com/cosmos/gogoproto v1.4.11
Expand All @@ -25,6 +32,7 @@ require (
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.5.3
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/hashicorp/go-metrics v0.5.1
github.com/ichiban/prolog v1.1.0
github.com/ignite/cli v0.27.1
github.com/nuts-foundation/go-did v0.4.0
Expand All @@ -35,6 +43,7 @@ require (
github.com/spf13/cast v1.5.1
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.17.0
github.com/stretchr/testify v1.8.4
golang.org/x/net v0.19.0
google.golang.org/genproto/googleapis/api v0.0.0-20231012201019-e917dd12ba7a
Expand Down Expand Up @@ -62,10 +71,6 @@ require (
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v1.1.3 // indirect
cloud.google.com/go/storage v1.30.1 // indirect
cosmossdk.io/collections v0.4.0 // indirect
cosmossdk.io/core v0.11.0 // indirect
cosmossdk.io/depinject v1.0.0-alpha.4 // indirect
cosmossdk.io/x/tx v0.12.0 // indirect
filippo.io/edwards25519 v1.0.0 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
github.com/99designs/keyring v1.2.1 // indirect
Expand Down Expand Up @@ -96,13 +101,14 @@ require (
github.com/cockroachdb/redact v1.1.5 // indirect
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 // indirect
github.com/cosmos/btcutil v1.0.5 // indirect
github.com/cosmos/cosmos-db v1.0.0 // indirect
github.com/cosmos/go-bip39 v1.0.0 // indirect
github.com/cosmos/gogogateway v1.2.0 // indirect
github.com/cosmos/iavl v1.0.0 // indirect
github.com/cosmos/ics23/go v0.10.0 // indirect
github.com/cosmos/ledger-cosmos-go v0.13.3 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
github.com/creachadair/atomicfile v0.3.1 // indirect
github.com/creachadair/tomledit v0.0.24 // indirect
github.com/danieljoos/wincred v1.1.2 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
Expand Down Expand Up @@ -150,7 +156,6 @@ require (
github.com/hashicorp/go-getter v1.7.1 // indirect
github.com/hashicorp/go-hclog v1.5.0 // indirect
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/go-metrics v0.5.1 // indirect
github.com/hashicorp/go-plugin v1.5.2 // indirect
github.com/hashicorp/go-safetemp v1.0.0 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
Expand Down Expand Up @@ -225,7 +230,6 @@ require (
github.com/smarty/assertions v1.15.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.10.0 // indirect
github.com/spf13/viper v1.17.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect
github.com/tendermint/go-amino v0.16.0 // indirect
Expand Down
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ cosmossdk.io/math v1.2.0 h1:8gudhTkkD3NxOP2YyyJIYYmt6dQ55ZfJkDOaxXpy7Ig=
cosmossdk.io/math v1.2.0/go.mod h1:l2Gnda87F0su8a/7FEKJfFdJrM0JZRXQaohlgJeyQh0=
cosmossdk.io/store v1.0.0 h1:6tnPgTpTSIskaTmw/4s5C9FARdgFflycIc9OX8i1tOI=
cosmossdk.io/store v1.0.0/go.mod h1:ABMprwjvx6IpMp8l06TwuMrj6694/QP5NIW+X6jaTYc=
cosmossdk.io/tools/confix v0.1.0 h1:2OOZTtQsDT5e7P3FM5xqM0bPfluAxZlAwxqaDmYBE+E=
cosmossdk.io/tools/confix v0.1.0/go.mod h1:TdXKVYs4gEayav5wM+JHT+kTU2J7fozFNqoVaN+8CdY=
cosmossdk.io/x/circuit v0.1.0 h1:IAej8aRYeuOMritczqTlljbUVHq1E85CpBqaCTwYgXs=
cosmossdk.io/x/circuit v0.1.0/go.mod h1:YDzblVE8+E+urPYQq5kq5foRY/IzhXovSYXb4nwd39w=
cosmossdk.io/x/evidence v0.1.0 h1:J6OEyDl1rbykksdGynzPKG5R/zm6TacwW2fbLTW4nCk=
Expand Down Expand Up @@ -276,8 +278,6 @@ github.com/apache/thrift v0.13.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
github.com/armon/go-metrics v0.4.1 h1:hR91U9KYmb6bLBYLQjyM+3j+rcd/UhE+G78SFnF8gJA=
github.com/armon/go-metrics v0.4.1/go.mod h1:E6amYzXo6aW1tqzoZGT755KkbgrJsSdpwZ+3JqfkOG4=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
Expand Down Expand Up @@ -416,6 +416,10 @@ github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwc
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.3 h1:qMCsGGgs+MAzDFyp9LpAe1Lqy/fY/qCovCm0qnXZOBM=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creachadair/atomicfile v0.3.1 h1:yQORkHjSYySh/tv5th1dkKcn02NEW5JleB84sjt+W4Q=
github.com/creachadair/atomicfile v0.3.1/go.mod h1:mwfrkRxFKwpNAflYZzytbSwxvbK6fdGRRlp0KEQc0qU=
github.com/creachadair/tomledit v0.0.24 h1:5Xjr25R2esu1rKCbQEmjZYlrhFkDspoAbAKb6QKQDhQ=
github.com/creachadair/tomledit v0.0.24/go.mod h1:9qHbShRWQzSCcn617cMzg4eab1vbLCOjOshAWSzWr8U=
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/danieljoos/wincred v1.1.2 h1:QLdCxFs1/Yl4zduvBdcHB8goaYk9RARS2SgLLRuAyr0=
Expand Down
22 changes: 17 additions & 5 deletions x/mint/abci.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mint

import (
"context"
"time"

"github.com/cosmos/cosmos-sdk/telemetry"
Expand All @@ -11,22 +12,30 @@ import (
)

// BeginBlocker mints new tokens for the previous block.
func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
func BeginBlocker(ctx context.Context, k keeper.Keeper) error {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker)

// fetch stored params
params := k.GetParams(ctx)
params, err := k.Params.Get(ctx)
if err != nil {
return err
}

// recalculate inflation rate
totalSupply := k.TokenSupply(ctx, params.MintDenom)
bondedRatio := k.BondedRatio(ctx)
bondedRatio, err := k.BondedRatio(ctx)
if err != nil {
return err
}

minter, err := types.NewMinterWithInflationCoef(params.InflationCoef, bondedRatio, totalSupply)
if err != nil {
panic(err)
}

k.SetMinter(ctx, minter)
if err = k.Minter.Set(ctx, minter); err != nil {
return err
}

// mint coins, update supply
mintedCoin := minter.BlockProvision(params)
Expand All @@ -47,7 +56,8 @@ func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
defer telemetry.ModuleSetGauge(types.ModuleName, float32(mintedCoin.Amount.Int64()), "minted_tokens")
}

ctx.EventManager().EmitEvent(
sdkCtx := sdk.UnwrapSDKContext(ctx)
sdkCtx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeMint,
sdk.NewAttribute(types.AttributeKeyBondedRatio, bondedRatio.String()),
Expand All @@ -56,4 +66,6 @@ func BeginBlocker(ctx sdk.Context, k keeper.Keeper) {
sdk.NewAttribute(sdk.AttributeKeyAmount, mintedCoin.Amount.String()),
),
)

return nil
}
31 changes: 20 additions & 11 deletions x/mint/keeper/genesis.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
package keeper

import (
errorsmod "cosmossdk.io/errors"

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

"github.com/okp4/okp4d/x/mint/types"
)

// InitGenesis new mint genesis.
func (k Keeper) InitGenesis(ctx sdk.Context, ak types.AccountKeeper, data *types.GenesisState) {
k.SetMinter(ctx, data.Minter)
err := k.SetParams(ctx, data.Params)
if err != nil {
panic(errorsmod.Wrapf(err, "error setting params"))
// InitGenesis new mint genesis
func (keeper Keeper) InitGenesis(ctx sdk.Context, ak types.AccountKeeper, data *types.GenesisState) {
if err := keeper.Minter.Set(ctx, data.Minter); err != nil {
panic(err)
}

if err := keeper.Params.Set(ctx, data.Params); err != nil {
panic(err)
}

ak.GetModuleAccount(ctx, types.ModuleName)
}

// ExportGenesis returns a GenesisState for a given context and keeper.
func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
minter := k.GetMinter(ctx)
params := k.GetParams(ctx)
func (keeper Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
minter, err := keeper.Minter.Get(ctx)
if err != nil {
panic(err)
}

params, err := keeper.Params.Get(ctx)
if err != nil {
panic(err)
}

return types.NewGenesisState(minter, params)
}
6 changes: 3 additions & 3 deletions x/mint/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/suite"

storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
Expand Down Expand Up @@ -58,10 +58,10 @@ func (s *GenesisTestSuite) SetupTest() {

func (s *GenesisTestSuite) TestImportExportGenesis() {
genesisState := types.DefaultGenesisState()
genesisState.Minter = types.NewMinter(sdk.OneDec(), sdk.NewDecWithPrec(20, 2))
genesisState.Minter = types.NewMinter(sdk.OneDec(), math.LegacyNewDecWithPrec(20, 2))
genesisState.Params = types.NewParams(
"testDenom",
sdk.NewDecWithPrec(69, 2),
math.LegacyNewDecWithPrec(69, 2),
uint64(60*60*8766/5),
)

Expand Down
36 changes: 24 additions & 12 deletions x/mint/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,47 @@ package keeper
import (
"context"

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

"github.com/okp4/okp4d/x/mint/types"
)

var _ types.QueryServer = Keeper{}
var _ types.QueryServer = queryServer{}

func NewQueryServerImpl(k Keeper) types.QueryServer {
return queryServer{k}
}

type queryServer struct {
k Keeper
}

// Params returns params of the mint module.
func (k Keeper) Params(c context.Context, _ *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
params := k.GetParams(ctx)
func (q queryServer) Params(c context.Context, _ *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
params, err := q.k.Params.Get(c)
if err != nil {
return nil, err
}

return &types.QueryParamsResponse{Params: params}, nil
}

// Inflation returns minter.Inflation of the mint module.
func (k Keeper) Inflation(c context.Context, _ *types.QueryInflationRequest) (*types.QueryInflationResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
minter := k.GetMinter(ctx)
func (q queryServer) Inflation(c context.Context, _ *types.QueryInflationRequest) (*types.QueryInflationResponse, error) {
minter, err := q.k.Minter.Get(c)
if err != nil {
return nil, err
}

return &types.QueryInflationResponse{Inflation: minter.Inflation}, nil
}

// AnnualProvisions returns minter.AnnualProvisions of the mint module.
func (k Keeper) AnnualProvisions(c context.Context,
func (q queryServer) AnnualProvisions(c context.Context,
_ *types.QueryAnnualProvisionsRequest,
) (*types.QueryAnnualProvisionsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
minter := k.GetMinter(ctx)
minter, err := q.k.Minter.Get(c)
if err != nil {
return nil, err
}

return &types.QueryAnnualProvisionsResponse{AnnualProvisions: minter.AnnualProvisions}, nil
}
28 changes: 19 additions & 9 deletions x/mint/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import (
gocontext "context"
"testing"

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

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/suite"

storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -30,8 +33,8 @@ type MintTestSuite struct {

func (suite *MintTestSuite) SetupTest() {
encCfg := moduletestutil.MakeTestEncodingConfig(mint.AppModuleBasic{})
key := sdk.NewKVStoreKey(types.StoreKey)
testCtx := testutil.DefaultContextWithDB(suite.T(), key, sdk.NewTransientStoreKey("transient_test"))
key := storetypes.NewKVStoreKey(types.StoreKey)
testCtx := testutil.DefaultContextWithDB(suite.T(), key, storetypes.NewTransientStoreKey("transient_test"))
suite.ctx = testCtx.Ctx

// gomock initializations
Expand All @@ -44,36 +47,43 @@ func (suite *MintTestSuite) SetupTest() {

suite.mintKeeper = keeper.NewKeeper(
encCfg.Codec,
key,
runtime.NewKVStoreService(key),
stakingKeeper,
accountKeeper,
bankKeeper,
authtypes.FeeCollectorName,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)

err := suite.mintKeeper.SetParams(suite.ctx, types.DefaultParams())
err := suite.mintKeeper.Params.Set(suite.ctx, types.DefaultParams())
suite.Require().NoError(err)
err = suite.mintKeeper.Minter.Set(suite.ctx, types.DefaultInitialMinter())
suite.Require().NoError(err)
suite.mintKeeper.SetMinter(suite.ctx, types.DefaultInitialMinter())

queryHelper := baseapp.NewQueryServerTestHelper(testCtx.Ctx, encCfg.InterfaceRegistry)
types.RegisterQueryServer(queryHelper, suite.mintKeeper)
types.RegisterQueryServer(queryHelper, keeper.NewQueryServerImpl(suite.mintKeeper))

suite.queryClient = types.NewQueryClient(queryHelper)
}

func (suite *MintTestSuite) TestGRPCParams() {
params, err := suite.queryClient.Params(gocontext.Background(), &types.QueryParamsRequest{})
suite.Require().NoError(err)
suite.Require().Equal(params.Params, suite.mintKeeper.GetParams(suite.ctx))
kParams, err := suite.mintKeeper.Params.Get(suite.ctx)
suite.Require().NoError(err)
suite.Require().Equal(params.Params, kParams)

inflation, err := suite.queryClient.Inflation(gocontext.Background(), &types.QueryInflationRequest{})
suite.Require().NoError(err)
suite.Require().Equal(inflation.Inflation, suite.mintKeeper.GetMinter(suite.ctx).Inflation)
minter, err := suite.mintKeeper.Minter.Get(suite.ctx)
suite.Require().NoError(err)
suite.Require().Equal(inflation.Inflation, minter)

annualProvisions, err := suite.queryClient.AnnualProvisions(gocontext.Background(), &types.QueryAnnualProvisionsRequest{})
suite.Require().NoError(err)
suite.Require().Equal(annualProvisions.AnnualProvisions, suite.mintKeeper.GetMinter(suite.ctx).AnnualProvisions)
minter, err = suite.mintKeeper.Minter.Get(suite.ctx)
suite.Require().NoError(err)
suite.Require().Equal(annualProvisions.AnnualProvisions, minter)
}

func TestMintTestSuite(t *testing.T) {
Expand Down
Loading

0 comments on commit ca000b2

Please sign in to comment.