-
Notifications
You must be signed in to change notification settings - Fork 172
/
minter.go
106 lines (88 loc) · 2.6 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package types
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// NewMinter returns a new Minter object with the given inflation and annual
// provisions values.
func NewMinter(inflation, annualProvisions sdk.Dec, phase, startPhaseBlock uint64, targetSupply sdk.Int) Minter {
return Minter{
Inflation: inflation,
AnnualProvisions: annualProvisions,
Phase: phase,
StartPhaseBlock: startPhaseBlock,
TargetSupply: targetSupply,
}
}
// InitialMinter returns an initial Minter object with a given inflation value.
func InitialMinter(inflation sdk.Dec) Minter {
return NewMinter(
inflation,
sdk.NewDec(0),
0,
0,
sdk.NewInt(0),
)
}
// DefaultInitialMinter returns a default initial Minter object for a new chain
// which uses an inflation rate of 13%.
func DefaultInitialMinter() Minter {
return InitialMinter(
sdk.NewDecWithPrec(13, 2),
)
}
// validate minter
func ValidateMinter(minter Minter) error {
if minter.Inflation.IsNegative() {
return fmt.Errorf("mint parameter Inflation should be positive, is %s",
minter.Inflation.String())
}
return nil
}
// PhaseInflationRate returns the inflation rate by phase.
func (m Minter) PhaseInflationRate(phase uint64) sdk.Dec {
switch {
case phase > 12:
return sdk.ZeroDec()
case phase == 1:
return sdk.NewDecWithPrec(40, 2)
case phase == 2:
return sdk.NewDecWithPrec(20, 2)
case phase == 3:
return sdk.NewDecWithPrec(10, 2)
default:
// Phase4: 9%
// Phase5: 8%
// Phase6: 7%
// ...
// Phase12: 1%
return sdk.NewDecWithPrec(13-int64(phase), 2)
}
}
// NextPhase returns the new phase.
func (m Minter) NextPhase(params Params, currentSupply sdk.Int) uint64 {
nonePhase := m.Phase == 0
if nonePhase {
return 1
}
if currentSupply.LT(m.TargetSupply) {
return m.Phase
}
return m.Phase + 1
}
// NextAnnualProvisions returns the annual provisions based on current total
// supply and inflation rate.
func (m Minter) NextAnnualProvisions(_ Params, totalSupply sdk.Int) sdk.Dec {
return m.Inflation.MulInt(totalSupply)
}
// BlockProvision returns the provisions for a block based on the annual
// provisions rate.
func (m Minter) BlockProvision(params Params, totalSupply sdk.Int) sdk.Coin {
provisionAmt := m.AnnualProvisions.QuoInt(sdk.NewInt(int64(params.BlocksPerYear)))
// Because of rounding, we might mint too many tokens in this phase, let's limit it
futureSupply := totalSupply.Add(provisionAmt.TruncateInt())
if futureSupply.GT(m.TargetSupply) {
return sdk.NewCoin(params.MintDenom, m.TargetSupply.Sub(totalSupply))
}
return sdk.NewCoin(params.MintDenom, provisionAmt.TruncateInt())
}