-
Notifications
You must be signed in to change notification settings - Fork 833
/
inflation.go
171 lines (142 loc) · 4.72 KB
/
inflation.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
ethermint "github.com/evmos/ethermint/types"
evmos "github.com/evmos/evmos/v7/types"
incentivestypes "github.com/evmos/evmos/v7/x/incentives/types"
"github.com/evmos/evmos/v7/x/inflation/types"
)
// 200M token at year 4 allocated to the team
var teamAlloc = sdk.NewInt(200_000_000).Mul(ethermint.PowerReduction)
// MintAndAllocateInflation performs inflation minting and allocation
func (k Keeper) MintAndAllocateInflation(
ctx sdk.Context,
coin sdk.Coin,
) (
staking, incentives, communityPool sdk.Coins,
err error,
) {
// Mint coins for distribution
if err := k.MintCoins(ctx, coin); err != nil {
return nil, nil, nil, err
}
// Allocate minted coins according to allocation proportions (staking, usage
// incentives, community pool)
return k.AllocateExponentialInflation(ctx, coin)
}
// MintCoins implements an alias call to the underlying supply keeper's
// MintCoins to be used in BeginBlocker.
func (k Keeper) MintCoins(ctx sdk.Context, coin sdk.Coin) error {
coins := sdk.NewCoins(coin)
// skip as no coins need to be minted
if coins.Empty() {
return nil
}
return k.bankKeeper.MintCoins(ctx, types.ModuleName, coins)
}
// AllocateExponentialInflation allocates coins from the inflation to external
// modules according to allocation proportions:
// - staking rewards -> sdk `auth` module fee collector
// - usage incentives -> `x/incentives` module
// - community pool -> `sdk `distr` module community pool
func (k Keeper) AllocateExponentialInflation(
ctx sdk.Context,
mintedCoin sdk.Coin,
) (
staking, incentives, communityPool sdk.Coins,
err error,
) {
params := k.GetParams(ctx)
proportions := params.InflationDistribution
// Allocate staking rewards into fee collector account
staking = sdk.NewCoins(k.GetProportions(ctx, mintedCoin, proportions.StakingRewards))
err = k.bankKeeper.SendCoinsFromModuleToModule(
ctx,
types.ModuleName,
k.feeCollectorName,
staking,
)
if err != nil {
return nil, nil, nil, err
}
// Allocate usage incentives to incentives module account
incentives = sdk.NewCoins(k.GetProportions(ctx, mintedCoin, proportions.UsageIncentives))
err = k.bankKeeper.SendCoinsFromModuleToModule(
ctx,
types.ModuleName,
incentivestypes.ModuleName,
incentives,
)
if err != nil {
return nil, nil, nil, err
}
// Allocate community pool amount (remaining module balance) to community
// pool address
moduleAddr := k.accountKeeper.GetModuleAddress(types.ModuleName)
communityPool = k.bankKeeper.GetAllBalances(ctx, moduleAddr)
err = k.distrKeeper.FundCommunityPool(
ctx,
communityPool,
moduleAddr,
)
if err != nil {
return nil, nil, nil, err
}
return staking, incentives, communityPool, nil
}
// GetAllocationProportion calculates the proportion of coins that is to be
// allocated during inflation for a given distribution.
func (k Keeper) GetProportions(
ctx sdk.Context,
coin sdk.Coin,
distribution sdk.Dec,
) sdk.Coin {
return sdk.NewCoin(
coin.Denom,
coin.Amount.ToDec().Mul(distribution).TruncateInt(),
)
}
// BondedRatio the fraction of the staking tokens which are currently bonded
// It doesn't consider team allocation for inflation
func (k Keeper) BondedRatio(ctx sdk.Context) sdk.Dec {
stakeSupply := k.stakingKeeper.StakingTokenSupply(ctx)
isMainnet := evmos.IsMainnet(ctx.ChainID())
if !stakeSupply.IsPositive() || (isMainnet && stakeSupply.LTE(teamAlloc)) {
return sdk.ZeroDec()
}
// don't count team allocation in bonded ratio's stake supple
if isMainnet {
stakeSupply = stakeSupply.Sub(teamAlloc)
}
return k.stakingKeeper.TotalBondedTokens(ctx).ToDec().QuoInt(stakeSupply)
}
// GetCirculatingSupply returns the bank supply of the mintDenom excluding the
// team allocation in the first year
func (k Keeper) GetCirculatingSupply(ctx sdk.Context) sdk.Dec {
mintDenom := k.GetParams(ctx).MintDenom
circulatingSupply := k.bankKeeper.GetSupply(ctx, mintDenom).Amount.ToDec()
teamAllocation := teamAlloc.ToDec()
// Consider team allocation only on mainnet chain id
if evmos.IsMainnet(ctx.ChainID()) {
circulatingSupply = circulatingSupply.Sub(teamAllocation)
}
return circulatingSupply
}
// GetInflationRate returns the inflation rate for the current period.
func (k Keeper) GetInflationRate(ctx sdk.Context) sdk.Dec {
epochMintProvision, _ := k.GetEpochMintProvision(ctx)
if epochMintProvision.IsZero() {
return sdk.ZeroDec()
}
epp := k.GetEpochsPerPeriod(ctx)
if epp == 0 {
return sdk.ZeroDec()
}
epochsPerPeriod := sdk.NewDec(epp)
circulatingSupply := k.GetCirculatingSupply(ctx)
if circulatingSupply.IsZero() {
return sdk.ZeroDec()
}
// EpochMintProvision * 365 / circulatingSupply * 100
return epochMintProvision.Mul(epochsPerPeriod).Quo(circulatingSupply).Mul(sdk.NewDec(100))
}