github.com/cosmos/cosmos-sdk@v0.50.10/x/mint/types/minter.go (about)

     1  package types
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"cosmossdk.io/math"
     7  
     8  	sdk "github.com/cosmos/cosmos-sdk/types"
     9  )
    10  
    11  // NewMinter returns a new Minter object with the given inflation and annual
    12  // provisions values.
    13  func NewMinter(inflation, annualProvisions math.LegacyDec) Minter {
    14  	return Minter{
    15  		Inflation:        inflation,
    16  		AnnualProvisions: annualProvisions,
    17  	}
    18  }
    19  
    20  // InitialMinter returns an initial Minter object with a given inflation value.
    21  func InitialMinter(inflation math.LegacyDec) Minter {
    22  	return NewMinter(
    23  		inflation,
    24  		math.LegacyNewDec(0),
    25  	)
    26  }
    27  
    28  // DefaultInitialMinter returns a default initial Minter object for a new chain
    29  // which uses an inflation rate of 13%.
    30  func DefaultInitialMinter() Minter {
    31  	return InitialMinter(
    32  		math.LegacyNewDecWithPrec(13, 2),
    33  	)
    34  }
    35  
    36  // ValidateMinter does a basic validation on minter.
    37  func ValidateMinter(minter Minter) error {
    38  	if minter.Inflation.IsNegative() {
    39  		return fmt.Errorf("mint parameter Inflation should be positive, is %s",
    40  			minter.Inflation.String())
    41  	}
    42  	return nil
    43  }
    44  
    45  // NextInflationRate returns the new inflation rate for the next block.
    46  func (m Minter) NextInflationRate(params Params, bondedRatio math.LegacyDec) math.LegacyDec {
    47  	// The target annual inflation rate is recalculated for each block. The inflation
    48  	// is also subject to a rate change (positive or negative) depending on the
    49  	// distance from the desired ratio (67%). The maximum rate change possible is
    50  	// defined to be 13% per year, however the annual inflation is capped as between
    51  	// 7% and 20%.
    52  
    53  	// (1 - bondedRatio/GoalBonded) * InflationRateChange
    54  	inflationRateChangePerYear := math.LegacyOneDec().
    55  		Sub(bondedRatio.Quo(params.GoalBonded)).
    56  		Mul(params.InflationRateChange)
    57  	inflationRateChange := inflationRateChangePerYear.Quo(math.LegacyNewDec(int64(params.BlocksPerYear)))
    58  
    59  	// adjust the new annual inflation for this next block
    60  	inflation := m.Inflation.Add(inflationRateChange) // note inflationRateChange may be negative
    61  	if inflation.GT(params.InflationMax) {
    62  		inflation = params.InflationMax
    63  	}
    64  	if inflation.LT(params.InflationMin) {
    65  		inflation = params.InflationMin
    66  	}
    67  
    68  	return inflation
    69  }
    70  
    71  // NextAnnualProvisions returns the annual provisions based on current total
    72  // supply and inflation rate.
    73  func (m Minter) NextAnnualProvisions(_ Params, totalSupply math.Int) math.LegacyDec {
    74  	return m.Inflation.MulInt(totalSupply)
    75  }
    76  
    77  // BlockProvision returns the provisions for a block based on the annual
    78  // provisions rate.
    79  func (m Minter) BlockProvision(params Params) sdk.Coin {
    80  	provisionAmt := m.AnnualProvisions.QuoInt(math.NewInt(int64(params.BlocksPerYear)))
    81  	return sdk.NewCoin(params.MintDenom, provisionAmt.TruncateInt())
    82  }