|
| 1 | +package keeper |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "cosmossdk.io/log" |
| 7 | + "cosmossdk.io/math" |
| 8 | + storetypes "cosmossdk.io/store/types" |
| 9 | + "github.com/cosmos/cosmos-sdk/codec" |
| 10 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 11 | + paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" |
| 12 | + "github.com/scrtlabs/SecretNetwork/x/mint/types" |
| 13 | +) |
| 14 | + |
| 15 | +// Keeper of the mint store |
| 16 | +type Keeper struct { |
| 17 | + cdc codec.BinaryCodec |
| 18 | + storeService storetypes.KVStoreService |
| 19 | + paramSpace paramtypes.Subspace |
| 20 | + stakingKeeper types.StakingKeeper |
| 21 | + bankKeeper types.BankKeeper |
| 22 | + feeCollectorName string |
| 23 | + authority string |
| 24 | +} |
| 25 | + |
| 26 | +// NewKeeper creates a new mint Keeper instance |
| 27 | +func NewKeeper( |
| 28 | + cdc codec.BinaryCodec, |
| 29 | + storeService storetypes.KVStoreService, |
| 30 | + sk types.StakingKeeper, |
| 31 | + ak types.AccountKeeper, |
| 32 | + bk types.BankKeeper, |
| 33 | + feeCollectorName string, |
| 34 | + authority string, |
| 35 | +) Keeper { |
| 36 | + return Keeper{ |
| 37 | + cdc: cdc, |
| 38 | + storeService: storeService, |
| 39 | + stakingKeeper: sk, |
| 40 | + bankKeeper: bk, |
| 41 | + feeCollectorName: feeCollectorName, |
| 42 | + authority: authority, |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// SetLegacyParamSubspace sets the param subspace for migration purposes |
| 47 | +func (k *Keeper) SetLegacyParamSubspace(ps paramtypes.Subspace) { |
| 48 | + k.paramSpace = ps |
| 49 | +} |
| 50 | + |
| 51 | +// Logger returns a module-specific logger. |
| 52 | +func (k Keeper) Logger(ctx context.Context) log.Logger { |
| 53 | + sdkCtx := sdk.UnwrapSDKContext(ctx) |
| 54 | + return sdkCtx.Logger().With("module", "x/"+types.ModuleName) |
| 55 | +} |
| 56 | + |
| 57 | +// GetMinter returns the minter |
| 58 | +func (k Keeper) GetMinter(ctx context.Context) (minter types.Minter, err error) { |
| 59 | + sdkCtx := sdk.UnwrapSDKContext(ctx) |
| 60 | + store := sdkCtx.KVStore(k.storeService.OpenKVStore(ctx)) |
| 61 | + b := store.Get([]byte(types.MinterKey)) |
| 62 | + if b == nil { |
| 63 | + return minter, nil |
| 64 | + } |
| 65 | + |
| 66 | + k.cdc.MustUnmarshal(b, &minter) |
| 67 | + return minter, nil |
| 68 | +} |
| 69 | + |
| 70 | +// SetMinter sets the minter |
| 71 | +func (k Keeper) SetMinter(ctx context.Context, minter types.Minter) error { |
| 72 | + sdkCtx := sdk.UnwrapSDKContext(ctx) |
| 73 | + store := sdkCtx.KVStore(k.storeService.OpenKVStore(ctx)) |
| 74 | + b := k.cdc.MustMarshal(&minter) |
| 75 | + store.Set([]byte(types.MinterKey), b) |
| 76 | + return nil |
| 77 | +} |
| 78 | + |
| 79 | +// GetParams returns the total set of minting parameters. |
| 80 | +func (k Keeper) GetParams(ctx context.Context) (params types.Params, err error) { |
| 81 | + sdkCtx := sdk.UnwrapSDKContext(ctx) |
| 82 | + if k.paramSpace.HasKeyTable() { |
| 83 | + k.paramSpace.GetParamSet(sdkCtx, ¶ms) |
| 84 | + return params, nil |
| 85 | + } |
| 86 | + return params, nil |
| 87 | +} |
| 88 | + |
| 89 | +// SetParams sets the total set of minting parameters. |
| 90 | +func (k Keeper) SetParams(ctx context.Context, params types.Params) error { |
| 91 | + sdkCtx := sdk.UnwrapSDKContext(ctx) |
| 92 | + k.paramSpace.SetParamSet(sdkCtx, ¶ms) |
| 93 | + return nil |
| 94 | +} |
| 95 | + |
| 96 | +// StakingTokenSupply implements an alias call to the underlying staking keeper's |
| 97 | +// StakingTokenSupply to be used in BeginBlocker. |
| 98 | +func (k Keeper) StakingTokenSupply(ctx context.Context) (math.Int, error) { |
| 99 | + sdkCtx := sdk.UnwrapSDKContext(ctx) |
| 100 | + return k.stakingKeeper.StakingTokenSupply(sdkCtx), nil |
| 101 | +} |
| 102 | + |
| 103 | +// BondedRatio implements an alias call to the underlying staking keeper's |
| 104 | +// BondedRatio to be used in BeginBlocker. |
| 105 | +func (k Keeper) BondedRatio(ctx context.Context) (math.LegacyDec, error) { |
| 106 | + sdkCtx := sdk.UnwrapSDKContext(ctx) |
| 107 | + return k.stakingKeeper.BondedRatio(sdkCtx), nil |
| 108 | +} |
| 109 | + |
| 110 | +// MintCoins implements an alias call to the underlying supply keeper's |
| 111 | +// MintCoins to be used in BeginBlocker. |
| 112 | +func (k Keeper) MintCoins(ctx context.Context, newCoins sdk.Coins) error { |
| 113 | + sdkCtx := sdk.UnwrapSDKContext(ctx) |
| 114 | + if newCoins.Empty() { |
| 115 | + // skip as no coins need to be minted |
| 116 | + return nil |
| 117 | + } |
| 118 | + |
| 119 | + return k.bankKeeper.MintCoins(sdkCtx, types.ModuleName, newCoins) |
| 120 | +} |
| 121 | + |
| 122 | +// AddCollectedFees implements an alias call to the underlying supply keeper's |
| 123 | +// SendCoinsFromModuleToModule to be used in BeginBlocker. |
| 124 | +func (k Keeper) AddCollectedFees(ctx context.Context, fees sdk.Coins) error { |
| 125 | + sdkCtx := sdk.UnwrapSDKContext(ctx) |
| 126 | + return k.bankKeeper.SendCoinsFromModuleToModule(sdkCtx, types.ModuleName, k.feeCollectorName, fees) |
| 127 | +} |
0 commit comments