-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from unification-com/throttle
Mint Throttle
- Loading branch information
Showing
31 changed files
with
1,551 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package mint | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/unification-com/mainchain-cosmos/x/enterprise" | ||
"github.com/unification-com/mainchain-cosmos/x/mint/internal/types" | ||
) | ||
|
||
// BeginBlocker mints new tokens for the previous block. | ||
func BeginBlocker(ctx sdk.Context, k Keeper, keeper enterprise.Keeper) { | ||
// fetch stored minter & params | ||
minter := k.GetMinter(ctx) | ||
params := k.GetParams(ctx) | ||
|
||
// recalculate inflation rate | ||
totalUNDSupply := keeper.GetTotalUndSupply(ctx) | ||
totalLockedUND := keeper.GetTotalLockedUnd(ctx) | ||
liquidUND := totalUNDSupply.Sub(totalLockedUND) | ||
|
||
bondedRatio := k.BondedRatio(ctx) | ||
minter.Inflation = minter.NextInflationRate(params, bondedRatio) | ||
minter.AnnualProvisions = minter.NextAnnualProvisions(params, liquidUND.Amount) | ||
k.SetMinter(ctx, minter) | ||
|
||
// mint coins, update supply | ||
mintedCoin := minter.BlockProvision(params) | ||
mintedCoins := sdk.NewCoins(mintedCoin) | ||
|
||
err := k.MintCoins(ctx, mintedCoins) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// send the minted coins to the fee collector account | ||
err = k.AddCollectedFees(ctx, mintedCoins) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
ctx.EventManager().EmitEvent( | ||
sdk.NewEvent( | ||
types.EventTypeMint, | ||
sdk.NewAttribute(types.AttributeKeyBondedRatio, bondedRatio.String()), | ||
sdk.NewAttribute(types.AttributeKeyInflation, minter.Inflation.String()), | ||
sdk.NewAttribute(types.AttributeKeyAnnualProvisions, minter.AnnualProvisions.String()), | ||
sdk.NewAttribute(sdk.AttributeKeyAmount, mintedCoin.Amount.String()), | ||
), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// nolint | ||
// autogenerated code using github.com/rigelrozanski/multitool | ||
// aliases generated for the following subdirectories: | ||
// ALIASGEN: github.com/unification-com/mainchain-cosmos/x/mint/internal/keeper | ||
// ALIASGEN: github.com/unification-com/mainchain-cosmos/x/mint/internal/types | ||
package mint | ||
|
||
import ( | ||
"github.com/unification-com/mainchain-cosmos/x/mint/internal/keeper" | ||
"github.com/unification-com/mainchain-cosmos/x/mint/internal/types" | ||
) | ||
|
||
const ( | ||
ModuleName = types.ModuleName | ||
DefaultParamspace = types.DefaultParamspace | ||
StoreKey = types.StoreKey | ||
QuerierRoute = types.QuerierRoute | ||
QueryParameters = types.QueryParameters | ||
QueryInflation = types.QueryInflation | ||
QueryAnnualProvisions = types.QueryAnnualProvisions | ||
) | ||
|
||
var ( | ||
// functions aliases | ||
NewKeeper = keeper.NewKeeper | ||
NewQuerier = keeper.NewQuerier | ||
NewGenesisState = types.NewGenesisState | ||
DefaultGenesisState = types.DefaultGenesisState | ||
ValidateGenesis = types.ValidateGenesis | ||
NewMinter = types.NewMinter | ||
InitialMinter = types.InitialMinter | ||
DefaultInitialMinter = types.DefaultInitialMinter | ||
ValidateMinter = types.ValidateMinter | ||
ParamKeyTable = types.ParamKeyTable | ||
NewParams = types.NewParams | ||
DefaultParams = types.DefaultParams | ||
ValidateParams = types.ValidateParams | ||
|
||
// variable aliases | ||
ModuleCdc = types.ModuleCdc | ||
MinterKey = types.MinterKey | ||
KeyMintDenom = types.KeyMintDenom | ||
KeyInflationRateChange = types.KeyInflationRateChange | ||
KeyInflationMax = types.KeyInflationMax | ||
KeyInflationMin = types.KeyInflationMin | ||
KeyGoalBonded = types.KeyGoalBonded | ||
KeyBlocksPerYear = types.KeyBlocksPerYear | ||
) | ||
|
||
type ( | ||
Keeper = keeper.Keeper | ||
GenesisState = types.GenesisState | ||
Minter = types.Minter | ||
Params = types.Params | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/cosmos/cosmos-sdk/client/context" | ||
"github.com/cosmos/cosmos-sdk/codec" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/unification-com/mainchain-cosmos/x/mint/internal/types" | ||
) | ||
|
||
// GetQueryCmd returns the cli query commands for the minting module. | ||
func GetQueryCmd(cdc *codec.Codec) *cobra.Command { | ||
mintingQueryCmd := &cobra.Command{ | ||
Use: types.ModuleName, | ||
Short: "Querying commands for the minting module", | ||
DisableFlagParsing: true, | ||
SuggestionsMinimumDistance: 2, | ||
RunE: client.ValidateCmd, | ||
} | ||
|
||
mintingQueryCmd.AddCommand( | ||
client.GetCommands( | ||
GetCmdQueryParams(cdc), | ||
GetCmdQueryInflation(cdc), | ||
GetCmdQueryAnnualProvisions(cdc), | ||
)..., | ||
) | ||
|
||
return mintingQueryCmd | ||
} | ||
|
||
// GetCmdQueryParams implements a command to return the current minting | ||
// parameters. | ||
func GetCmdQueryParams(cdc *codec.Codec) *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "params", | ||
Short: "Query the current minting parameters", | ||
Args: cobra.NoArgs, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
cliCtx := context.NewCLIContext().WithCodec(cdc) | ||
|
||
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryParameters) | ||
res, _, err := cliCtx.QueryWithData(route, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var params types.Params | ||
if err := cdc.UnmarshalJSON(res, ¶ms); err != nil { | ||
return err | ||
} | ||
|
||
return cliCtx.PrintOutput(params) | ||
}, | ||
} | ||
} | ||
|
||
// GetCmdQueryInflation implements a command to return the current minting | ||
// inflation value. | ||
func GetCmdQueryInflation(cdc *codec.Codec) *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "inflation", | ||
Short: "Query the current minting inflation value", | ||
Args: cobra.NoArgs, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
cliCtx := context.NewCLIContext().WithCodec(cdc) | ||
|
||
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryInflation) | ||
res, _, err := cliCtx.QueryWithData(route, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var inflation sdk.Dec | ||
if err := cdc.UnmarshalJSON(res, &inflation); err != nil { | ||
return err | ||
} | ||
|
||
return cliCtx.PrintOutput(inflation) | ||
}, | ||
} | ||
} | ||
|
||
// GetCmdQueryAnnualProvisions implements a command to return the current minting | ||
// annual provisions value. | ||
func GetCmdQueryAnnualProvisions(cdc *codec.Codec) *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "annual-provisions", | ||
Short: "Query the current minting annual provisions value", | ||
Args: cobra.NoArgs, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
cliCtx := context.NewCLIContext().WithCodec(cdc) | ||
|
||
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryAnnualProvisions) | ||
res, _, err := cliCtx.QueryWithData(route, nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var inflation sdk.Dec | ||
if err := cdc.UnmarshalJSON(res, &inflation); err != nil { | ||
return err | ||
} | ||
|
||
return cliCtx.PrintOutput(inflation) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package rest | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
|
||
"github.com/cosmos/cosmos-sdk/client/context" | ||
"github.com/cosmos/cosmos-sdk/types/rest" | ||
"github.com/unification-com/mainchain-cosmos/x/mint/internal/types" | ||
) | ||
|
||
func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) { | ||
r.HandleFunc( | ||
"/minting/parameters", | ||
queryParamsHandlerFn(cliCtx), | ||
).Methods("GET") | ||
|
||
r.HandleFunc( | ||
"/minting/inflation", | ||
queryInflationHandlerFn(cliCtx), | ||
).Methods("GET") | ||
|
||
r.HandleFunc( | ||
"/minting/annual-provisions", | ||
queryAnnualProvisionsHandlerFn(cliCtx), | ||
).Methods("GET") | ||
} | ||
|
||
func queryParamsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryParameters) | ||
|
||
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) | ||
if !ok { | ||
return | ||
} | ||
|
||
res, height, err := cliCtx.QueryWithData(route, nil) | ||
if err != nil { | ||
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
|
||
cliCtx = cliCtx.WithHeight(height) | ||
rest.PostProcessResponse(w, cliCtx, res) | ||
} | ||
} | ||
|
||
func queryInflationHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryInflation) | ||
|
||
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) | ||
if !ok { | ||
return | ||
} | ||
|
||
res, height, err := cliCtx.QueryWithData(route, nil) | ||
if err != nil { | ||
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
|
||
cliCtx = cliCtx.WithHeight(height) | ||
rest.PostProcessResponse(w, cliCtx, res) | ||
} | ||
} | ||
|
||
func queryAnnualProvisionsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryAnnualProvisions) | ||
|
||
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) | ||
if !ok { | ||
return | ||
} | ||
|
||
res, height, err := cliCtx.QueryWithData(route, nil) | ||
if err != nil { | ||
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
|
||
cliCtx = cliCtx.WithHeight(height) | ||
rest.PostProcessResponse(w, cliCtx, res) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package rest | ||
|
||
import ( | ||
"github.com/gorilla/mux" | ||
|
||
"github.com/cosmos/cosmos-sdk/client/context" | ||
) | ||
|
||
// RegisterRoutes registers minting module REST handlers on the provided router. | ||
func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router) { | ||
registerQueryRoutes(cliCtx, r) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package mint | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
// InitGenesis new mint genesis | ||
func InitGenesis(ctx sdk.Context, keeper Keeper, data GenesisState) { | ||
keeper.SetMinter(ctx, data.Minter) | ||
keeper.SetParams(ctx, data.Params) | ||
} | ||
|
||
// ExportGenesis returns a GenesisState for a given context and keeper. | ||
func ExportGenesis(ctx sdk.Context, keeper Keeper) GenesisState { | ||
minter := keeper.GetMinter(ctx) | ||
params := keeper.GetParams(ctx) | ||
return NewGenesisState(minter, params) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package keeper_test | ||
|
||
import ( | ||
abci "github.com/tendermint/tendermint/abci/types" | ||
|
||
"github.com/cosmos/cosmos-sdk/simapp" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/unification-com/mainchain-cosmos/x/mint/internal/types" | ||
) | ||
|
||
// returns context and an app with updated mint keeper | ||
func createTestApp(isCheckTx bool) (*simapp.SimApp, sdk.Context) { | ||
app := simapp.Setup(isCheckTx) | ||
|
||
ctx := app.BaseApp.NewContext(isCheckTx, abci.Header{}) | ||
app.MintKeeper.SetParams(ctx, types.DefaultParams()) | ||
app.MintKeeper.SetMinter(ctx, types.DefaultInitialMinter()) | ||
|
||
return app, ctx | ||
} |
Oops, something went wrong.