-
Notifications
You must be signed in to change notification settings - Fork 262
/
minter.go
74 lines (64 loc) · 2.53 KB
/
minter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package types
import (
"fmt"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
)
const DefaultBondDenom = "utia"
// NewMinter returns a new Minter object.
func NewMinter(inflationRate sdk.Dec, annualProvisions sdk.Dec, bondDenom string) Minter {
return Minter{
InflationRate: inflationRate,
AnnualProvisions: annualProvisions,
BondDenom: bondDenom,
}
}
// DefaultMinter returns a Minter object with default values.
func DefaultMinter() Minter {
inflationRate := InitialInflationRateAsDec()
annualProvisions := sdk.NewDec(0)
return NewMinter(inflationRate, annualProvisions, DefaultBondDenom)
}
// Validate returns an error if the minter is invalid.
func (m Minter) Validate() error {
if m.InflationRate.IsNegative() {
return fmt.Errorf("inflation rate %v should be positive", m.InflationRate.String())
}
if m.AnnualProvisions.IsNegative() {
return fmt.Errorf("annual provisions %v should be positive", m.AnnualProvisions.String())
}
if m.BondDenom == "" {
return fmt.Errorf("bond denom should not be empty string")
}
return nil
}
// CalculateInflationRate returns the inflation rate for the current year depending on
// the current block height in context. The inflation rate is expected to
// decrease every year according to the schedule specified in the README.
func (m Minter) CalculateInflationRate(ctx sdk.Context, genesis time.Time) sdk.Dec {
years := yearsSinceGenesis(genesis, ctx.BlockTime())
initialInflationRate := InitialInflationRateAsDec()
disinflationRate := DisinflationRateAsDec()
inflationRate := initialInflationRate.Mul(sdk.OneDec().Sub(disinflationRate).Power(uint64(years)))
targetInflationRate := TargetInflationRateAsDec()
if inflationRate.LT(targetInflationRate) {
return targetInflationRate
}
return inflationRate
}
// CalculateBlockProvision returns the total number of coins that should be
// minted due to inflation for the current block.
func (m Minter) CalculateBlockProvision(current time.Time, previous time.Time) sdk.Coin {
timeElapsed := current.Sub(previous).Nanoseconds()
portionOfYear := sdk.NewDec(int64(timeElapsed)).Quo(sdk.NewDec(int64(NanosecondsPerYear)))
blockProvision := m.AnnualProvisions.Mul(portionOfYear)
return sdk.NewCoin(m.BondDenom, blockProvision.TruncateInt())
}
// yearsSinceGenesis returns the number of years that have passed between
// genesis and current (rounded down).
func yearsSinceGenesis(genesis time.Time, current time.Time) (years int64) {
if current.Before(genesis) {
return 0
}
return current.Sub(genesis).Nanoseconds() / int64(NanosecondsPerYear)
}