2018-06-26 19:00:12 -07:00
|
|
|
package keeper
|
2018-02-23 15:57:31 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-06-26 19:00:12 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/stake/types"
|
2018-02-23 15:57:31 -08:00
|
|
|
)
|
|
|
|
|
2018-03-22 10:10:25 -07:00
|
|
|
const (
|
2018-06-01 02:51:38 -07:00
|
|
|
hrsPerYr = 8766 // as defined by a julian year of 365.25 days
|
2018-06-26 19:00:12 -07:00
|
|
|
precision = 100000000000 // increased to this precision for accuracy
|
2018-03-22 10:10:25 -07:00
|
|
|
)
|
|
|
|
|
2018-06-26 19:00:12 -07:00
|
|
|
var hrsPerYrRat = sdk.NewRat(hrsPerYr)
|
2018-03-22 10:10:25 -07:00
|
|
|
|
2018-02-23 15:57:31 -08:00
|
|
|
// process provisions for an hour period
|
2018-06-26 19:00:12 -07:00
|
|
|
func (k Keeper) ProcessProvisions(ctx sdk.Context) types.Pool {
|
2018-02-23 15:57:31 -08:00
|
|
|
|
2018-03-22 10:10:25 -07:00
|
|
|
pool := k.GetPool(ctx)
|
2018-06-26 19:00:12 -07:00
|
|
|
pool.Inflation = k.NextInflation(ctx)
|
2018-02-23 15:57:31 -08:00
|
|
|
|
2018-07-02 08:57:33 -07:00
|
|
|
provisions := pool.Inflation.Mul(sdk.NewRat(pool.TokenSupply())).Quo(hrsPerYrRat).RoundInt64()
|
2018-06-13 00:12:57 -07:00
|
|
|
|
|
|
|
// TODO add to the fees provisions
|
2018-06-26 19:00:12 -07:00
|
|
|
pool.LooseTokens += provisions
|
2018-03-29 05:27:35 -07:00
|
|
|
return pool
|
2018-02-23 15:57:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// get the next inflation rate for the hour
|
2018-06-26 19:00:12 -07:00
|
|
|
func (k Keeper) NextInflation(ctx sdk.Context) (inflation sdk.Rat) {
|
2018-02-23 15:57:31 -08:00
|
|
|
|
2018-03-22 10:10:25 -07:00
|
|
|
params := k.GetParams(ctx)
|
|
|
|
pool := k.GetPool(ctx)
|
2018-02-23 15:57:31 -08:00
|
|
|
// The target annual inflation rate is recalculated for each previsions cycle. The
|
2018-06-01 02:51:38 -07:00
|
|
|
// inflation is also subject to a rate change (positive or negative) depending on
|
2018-02-23 15:57:31 -08:00
|
|
|
// the distance from the desired ratio (67%). The maximum rate change possible is
|
|
|
|
// defined to be 13% per year, however the annual inflation is capped as between
|
|
|
|
// 7% and 20%.
|
|
|
|
|
|
|
|
// (1 - bondedRatio/GoalBonded) * InflationRateChange
|
2018-06-26 19:00:12 -07:00
|
|
|
inflationRateChangePerYear := sdk.OneRat().Sub(pool.BondedRatio().Quo(params.GoalBonded)).Mul(params.InflationRateChange)
|
2018-03-22 10:10:25 -07:00
|
|
|
inflationRateChange := inflationRateChangePerYear.Quo(hrsPerYrRat)
|
2018-02-23 15:57:31 -08:00
|
|
|
|
|
|
|
// increase the new annual inflation for this next cycle
|
2018-03-22 10:10:25 -07:00
|
|
|
inflation = pool.Inflation.Add(inflationRateChange)
|
2018-02-23 15:57:31 -08:00
|
|
|
if inflation.GT(params.InflationMax) {
|
|
|
|
inflation = params.InflationMax
|
|
|
|
}
|
|
|
|
if inflation.LT(params.InflationMin) {
|
|
|
|
inflation = params.InflationMin
|
|
|
|
}
|
|
|
|
|
2018-03-30 13:19:21 -07:00
|
|
|
return inflation.Round(precision)
|
2018-02-23 15:57:31 -08:00
|
|
|
}
|