github.com/Finschia/finschia-sdk@v0.48.1/x/mint/types/minter.go (about)

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