Skip to content
Open
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
3 changes: 1 addition & 2 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,12 +331,11 @@ func NewSimApp(
app.MintKeeper = mintkeeper.NewKeeper(
appCodec,
runtime.NewKVStoreService(keys[minttypes.StoreKey]),
app.StakingKeeper,
app.AccountKeeper,
app.BankKeeper,
authtypes.FeeCollectorName,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
// mintkeeper.WithMintFn(mintkeeper.DefaultMintFn(minttypes.DefaultInflationCalculationFn)), custom mintFn can be added here
mintkeeper.DefaultMintFn(app.StakingKeeper, minttypes.DefaultInflationCalculationFn),
)

app.ProtocolPoolKeeper = protocolpoolkeeper.NewKeeper(
Expand Down
2 changes: 1 addition & 1 deletion testutil/integration/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func Example() {

// here bankkeeper and staking keeper is nil because we are not testing them
// subspace is nil because we don't test params (which is legacy anyway)
mintKeeper := mintkeeper.NewKeeper(encodingCfg.Codec, runtime.NewKVStoreService(keys[minttypes.StoreKey]), nil, accountKeeper, nil, authtypes.FeeCollectorName, authority)
mintKeeper := mintkeeper.NewKeeper(encodingCfg.Codec, runtime.NewKVStoreService(keys[minttypes.StoreKey]), accountKeeper, nil, authtypes.FeeCollectorName, authority, mintkeeper.DefaultMintFn(nil, minttypes.DefaultInflationCalculationFn))
mintModule := mint.NewAppModule(encodingCfg.Codec, mintKeeper, accountKeeper, nil, nil)

// create the application and register all the modules from the previous step
Expand Down
2 changes: 1 addition & 1 deletion x/mint/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (s *GenesisTestSuite) SetupTest() {
accountKeeper.EXPECT().GetModuleAddress(minterAcc.Name).Return(minterAcc.GetAddress())
accountKeeper.EXPECT().GetModuleAccount(s.sdkCtx, minterAcc.Name).Return(minterAcc)

s.keeper = keeper.NewKeeper(s.cdc, runtime.NewKVStoreService(key), stakingKeeper, accountKeeper, bankKeeper, "", "")
s.keeper = keeper.NewKeeper(s.cdc, runtime.NewKVStoreService(key), accountKeeper, bankKeeper, "", "", keeper.DefaultMintFn(stakingKeeper, types.DefaultInflationCalculationFn))
}

func (s *GenesisTestSuite) TestImportExportGenesis() {
Expand Down
2 changes: 1 addition & 1 deletion x/mint/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ func (suite *MintTestSuite) SetupTest() {
suite.mintKeeper = keeper.NewKeeper(
encCfg.Codec,
storeService,
stakingKeeper,
accountKeeper,
bankKeeper,
authtypes.FeeCollectorName,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
keeper.DefaultMintFn(stakingKeeper, types.DefaultInflationCalculationFn),
)

err := suite.mintKeeper.Params.Set(suite.ctx, types.DefaultParams())
Expand Down
33 changes: 2 additions & 31 deletions x/mint/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"cosmossdk.io/collections"
storetypes "cosmossdk.io/core/store"
"cosmossdk.io/log"
"cosmossdk.io/math"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -18,7 +17,6 @@ import (
type Keeper struct {
cdc codec.BinaryCodec
storeService storetypes.KVStoreService
stakingKeeper types.StakingKeeper
bankKeeper types.BankKeeper
feeCollectorName string

Expand All @@ -34,28 +32,18 @@ type Keeper struct {
mintFn MintFn
}

type InitOption func(*Keeper)

// WithMintFn sets a custom minting function for the x/mint keeper.
func WithMintFn(mintFn MintFn) InitOption {
return func(k *Keeper) {
k.mintFn = mintFn
}
}

// NewKeeper creates a new mint Keeper instance.
//
// The mint keeper is always initialized with the DefaultMintFn but this can be overridden with the
// WithMintFn option.
func NewKeeper(
cdc codec.BinaryCodec,
storeService storetypes.KVStoreService,
sk types.StakingKeeper,
ak types.AccountKeeper,
bk types.BankKeeper,
feeCollectorName string,
authority string,
opts ...InitOption,
mintFn MintFn,
) Keeper {
// ensure mint module account is set
if addr := ak.GetModuleAddress(types.ModuleName); addr == nil {
Expand All @@ -66,13 +54,12 @@ func NewKeeper(
k := Keeper{
cdc: cdc,
storeService: storeService,
stakingKeeper: sk,
bankKeeper: bk,
feeCollectorName: feeCollectorName,
authority: authority,
Params: collections.NewItem(sb, types.ParamsKey, "params", codec.CollValue[types.Params](cdc)),
Minter: collections.NewItem(sb, types.MinterKey, "minter", codec.CollValue[types.Minter](cdc)),
mintFn: DefaultMintFn(types.DefaultInflationCalculationFn),
mintFn: mintFn,
}

schema, err := sb.Build()
Expand All @@ -81,10 +68,6 @@ func NewKeeper(
}
k.Schema = schema

for _, opt := range opts {
opt(&k)
}

return k
}

Expand All @@ -99,18 +82,6 @@ func (k Keeper) Logger(ctx context.Context) log.Logger {
return sdkCtx.Logger().With("module", "x/"+types.ModuleName)
}

// StakingTokenSupply implements an alias call to the underlying staking keeper's
// StakingTokenSupply to be used in BeginBlocker.
func (k Keeper) StakingTokenSupply(ctx context.Context) (math.Int, error) {
return k.stakingKeeper.StakingTokenSupply(ctx)
}

// BondedRatio implements an alias call to the underlying staking keeper's
// BondedRatio to be used in BeginBlocker.
func (k Keeper) BondedRatio(ctx context.Context) (math.LegacyDec, error) {
return k.stakingKeeper.BondedRatio(ctx)
}

// MintCoins implements an alias call to the underlying supply keeper's
// MintCoins to be used in BeginBlocker.
func (k Keeper) MintCoins(ctx context.Context, newCoins sdk.Coins) error {
Expand Down
14 changes: 1 addition & 13 deletions x/mint/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ func (s *IntegrationTestSuite) SetupTest() {
s.mintKeeper = keeper.NewKeeper(
encCfg.Codec,
storeService,
stakingKeeper,
accountKeeper,
bankKeeper,
authtypes.FeeCollectorName,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
keeper.DefaultMintFn(stakingKeeper, types.DefaultInflationCalculationFn),
)
s.stakingKeeper = stakingKeeper
s.bankKeeper = bankKeeper
Expand All @@ -73,18 +73,6 @@ func (s *IntegrationTestSuite) SetupTest() {
}

func (s *IntegrationTestSuite) TestAliasFunctions() {
stakingTokenSupply := math.NewIntFromUint64(100000000000)
s.stakingKeeper.EXPECT().StakingTokenSupply(s.ctx).Return(stakingTokenSupply, nil)
tokenSupply, err := s.mintKeeper.StakingTokenSupply(s.ctx)
s.Require().NoError(err)
s.Require().Equal(tokenSupply, stakingTokenSupply)

bondedRatio := math.LegacyNewDecWithPrec(15, 2)
s.stakingKeeper.EXPECT().BondedRatio(s.ctx).Return(bondedRatio, nil)
ratio, err := s.mintKeeper.BondedRatio(s.ctx)
s.Require().NoError(err)
s.Require().Equal(ratio, bondedRatio)

coins := sdk.NewCoins(sdk.NewCoin("stake", math.NewInt(1000000)))
s.bankKeeper.EXPECT().MintCoins(s.ctx, types.ModuleName, coins).Return(nil)
s.Require().Equal(s.mintKeeper.MintCoins(s.ctx, sdk.NewCoins()), nil)
Expand Down
6 changes: 3 additions & 3 deletions x/mint/keeper/mint.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (k *Keeper) MintFn(ctx sdk.Context) error {

// DefaultMintFn returns a default mint function.
// The default MintFn has a requirement on staking as it uses bond to calculate inflation.
func DefaultMintFn(ic types.InflationCalculationFn) MintFn {
func DefaultMintFn(sk types.StakingKeeper, ic types.InflationCalculationFn) MintFn {
return func(ctx sdk.Context, k *Keeper) error {
// fetch stored minter & params
minter, err := k.Minter.Get(ctx)
Expand All @@ -30,12 +30,12 @@ func DefaultMintFn(ic types.InflationCalculationFn) MintFn {
}

// recalculate inflation rate
totalStakingSupply, err := k.StakingTokenSupply(ctx)
totalStakingSupply, err := sk.StakingTokenSupply(ctx)
if err != nil {
return err
}

bondedRatio, err := k.BondedRatio(ctx)
bondedRatio, err := sk.BondedRatio(ctx)
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions x/mint/keeper/mint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ func (s *MintFnTestSuite) SetupTest() {
s.mintKeeper = keeper.NewKeeper(
encCfg.Codec,
storeService,
s.stakingKeeper,
accountKeeper,
s.bankKeeper,
authtypes.FeeCollectorName,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
keeper.DefaultMintFn(s.stakingKeeper, types.DefaultInflationCalculationFn),
)

// Set default parameters.
Expand Down Expand Up @@ -168,12 +168,11 @@ func (s *MintFnTestSuite) TestCustomMintFn() {
s.mintKeeper = keeper.NewKeeper(
encCfg.Codec,
storeService,
s.stakingKeeper,
accountKeeper,
s.bankKeeper,
authtypes.FeeCollectorName,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
keeper.WithMintFn(customMintFn),
customMintFn,
)

// Set default parameters and initial minter.
Expand Down
9 changes: 1 addition & 8 deletions x/mint/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ type ModuleInputs struct {

AccountKeeper types.AccountKeeper
BankKeeper types.BankKeeper
StakingKeeper types.StakingKeeper
}

type ModuleOutputs struct {
Expand All @@ -235,20 +234,14 @@ func ProvideModule(in ModuleInputs) ModuleOutputs {
panic("inflation calculation function argument must be nil as it is no longer used. This argument will be removed in a future release of the Cosmos SDK. To set a custom inflation calculation function, while using depinject ")
}

var opts []keeper.InitOption
if in.MintFn != nil {
opts = append(opts, keeper.WithMintFn(in.MintFn))
}

k := keeper.NewKeeper(
in.Cdc,
in.StoreService,
in.StakingKeeper,
in.AccountKeeper,
in.BankKeeper,
feeCollectorName,
authority.String(),
opts...,
in.MintFn,
)

// when no inflation calculation function is provided it will use the default types.DefaultInflationCalculationFn
Expand Down
Loading