-
Notifications
You must be signed in to change notification settings - Fork 286
/
Copy pathkeeper.go
389 lines (313 loc) · 10.7 KB
/
keeper.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package keeper
import (
"fmt"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
core "github.com/classic-terra/core/types"
"github.com/tendermint/tendermint/libs/log"
"github.com/classic-terra/core/x/treasury/types"
)
// TaxPowerUpgradeHeight is when taxes are allowed to go into effect
// This will still need a parameter change proposal, but can be activated
// anytime after this height
const TaxPowerUpgradeHeight = 9346889
// Keeper of the treasury store
type Keeper struct {
storeKey sdk.StoreKey
cdc codec.BinaryCodec
paramSpace paramstypes.Subspace
accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
marketKeeper types.MarketKeeper
stakingKeeper types.StakingKeeper
distrKeeper types.DistributionKeeper
oracleKeeper types.OracleKeeper
distributionModuleName string
}
// NewKeeper creates a new treasury Keeper instance
func NewKeeper(cdc codec.BinaryCodec, storeKey sdk.StoreKey,
paramSpace paramstypes.Subspace,
accountKeeper types.AccountKeeper,
bankKeeper types.BankKeeper,
marketKeeper types.MarketKeeper,
oracleKeeper types.OracleKeeper,
stakingKeeper types.StakingKeeper,
distrKeeper types.DistributionKeeper,
distributionModuleName string,
) Keeper {
// ensure treasury module account is set
if addr := accountKeeper.GetModuleAddress(types.ModuleName); addr == nil {
panic(fmt.Sprintf("%s module account has not been set", types.ModuleName))
}
// ensure burn module account is set
if addr := accountKeeper.GetModuleAddress(types.BurnModuleName); addr == nil {
panic(fmt.Sprintf("%s module account has not been set", types.BurnModuleName))
}
// set KeyTable if it has not already been set
if !paramSpace.HasKeyTable() {
paramSpace = paramSpace.WithKeyTable(types.ParamKeyTable())
}
return Keeper{
cdc: cdc,
storeKey: storeKey,
paramSpace: paramSpace,
accountKeeper: accountKeeper,
bankKeeper: bankKeeper,
marketKeeper: marketKeeper,
oracleKeeper: oracleKeeper,
stakingKeeper: stakingKeeper,
distrKeeper: distrKeeper,
distributionModuleName: distributionModuleName,
}
}
// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}
// GetTaxRate loads the tax rate
func (k Keeper) GetTaxRate(ctx sdk.Context) sdk.Dec {
store := ctx.KVStore(k.storeKey)
b := store.Get(types.TaxRateKey)
if b == nil {
return types.DefaultTaxRate
}
dp := sdk.DecProto{}
k.cdc.MustUnmarshal(b, &dp)
return dp.Dec
}
// SetTaxRate sets the tax rate
func (k Keeper) SetTaxRate(ctx sdk.Context, taxRate sdk.Dec) {
store := ctx.KVStore(k.storeKey)
b := k.cdc.MustMarshal(&sdk.DecProto{Dec: taxRate})
store.Set(types.TaxRateKey, b)
}
// GetRewardWeight loads the reward weight
func (k Keeper) GetRewardWeight(ctx sdk.Context) sdk.Dec {
store := ctx.KVStore(k.storeKey)
b := store.Get(types.RewardWeightKey)
if b == nil {
return types.DefaultRewardWeight
}
dp := sdk.DecProto{}
k.cdc.MustUnmarshal(b, &dp)
return dp.Dec
}
// SetRewardWeight sets the reward weight
func (k Keeper) SetRewardWeight(ctx sdk.Context, rewardWeight sdk.Dec) {
store := ctx.KVStore(k.storeKey)
b := k.cdc.MustMarshal(&sdk.DecProto{Dec: rewardWeight})
store.Set(types.RewardWeightKey, b)
}
// SetTaxCap sets the tax cap denominated in integer units of the reference {denom}
func (k Keeper) SetTaxCap(ctx sdk.Context, denom string, cap sdk.Int) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&sdk.IntProto{Int: cap})
store.Set(types.GetTaxCapKey(denom), bz)
}
// GetTaxCap gets the tax cap denominated in integer units of the reference {denom}
func (k Keeper) GetTaxCap(ctx sdk.Context, denom string) sdk.Int {
currHeight := ctx.BlockHeight()
// Allow tax cap for uluna
if denom == core.MicroLunaDenom && currHeight < TaxPowerUpgradeHeight {
return sdk.ZeroInt()
}
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetTaxCapKey(denom))
if bz == nil {
// if no tax-cap registered, return SDR tax-cap
return k.TaxPolicy(ctx).Cap.Amount
}
ip := sdk.IntProto{}
k.cdc.MustUnmarshal(bz, &ip)
return ip.Int
}
// IterateTaxCap iterates all tax cap
func (k Keeper) IterateTaxCap(ctx sdk.Context, handler func(denom string, taxCap sdk.Int) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.TaxCapKey)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
denom := string(iter.Key()[len(types.TaxCapKey):])
var ip sdk.IntProto
k.cdc.MustUnmarshal(iter.Value(), &ip)
if handler(denom, ip.Int) {
break
}
}
}
// RecordEpochTaxProceeds adds tax proceeds that have been added this epoch
func (k Keeper) RecordEpochTaxProceeds(ctx sdk.Context, delta sdk.Coins) {
if delta.IsZero() {
return
}
proceeds := k.PeekEpochTaxProceeds(ctx)
proceeds = proceeds.Add(delta...)
k.SetEpochTaxProceeds(ctx, proceeds)
}
// SetEpochTaxProceeds stores tax proceeds for the given epoch
func (k Keeper) SetEpochTaxProceeds(ctx sdk.Context, taxProceeds sdk.Coins) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&types.EpochTaxProceeds{TaxProceeds: taxProceeds})
store.Set(types.TaxProceedsKey, bz)
}
// PeekEpochTaxProceeds peeks the total amount of taxes that have been collected in the given epoch.
func (k Keeper) PeekEpochTaxProceeds(ctx sdk.Context) sdk.Coins {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.TaxProceedsKey)
taxProceeds := types.EpochTaxProceeds{}
if bz == nil {
taxProceeds.TaxProceeds = sdk.Coins{}
} else {
k.cdc.MustUnmarshal(bz, &taxProceeds)
}
return taxProceeds.TaxProceeds
}
// RecordEpochInitialIssuance updates epoch initial issuance from supply keeper
func (k Keeper) RecordEpochInitialIssuance(ctx sdk.Context) {
whitelist := k.oracleKeeper.Whitelist(ctx)
totalSupply := make(sdk.Coins, len(whitelist)+1)
totalSupply[0] = k.bankKeeper.GetSupply(ctx, core.MicroLunaDenom)
for i, denom := range whitelist {
totalSupply[i+1] = k.bankKeeper.GetSupply(ctx, denom.Name)
}
k.SetEpochInitialIssuance(ctx, totalSupply.Sort())
}
// SetEpochInitialIssuance stores epoch initial issuance
func (k Keeper) SetEpochInitialIssuance(ctx sdk.Context, issuance sdk.Coins) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&types.EpochInitialIssuance{Issuance: issuance})
store.Set(types.EpochInitialIssuanceKey, bz)
}
// GetEpochInitialIssuance returns epoch initial issuance
func (k Keeper) GetEpochInitialIssuance(ctx sdk.Context) sdk.Coins {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.EpochInitialIssuanceKey)
initialIssuance := types.EpochInitialIssuance{}
if bz == nil {
initialIssuance.Issuance = sdk.Coins{}
} else {
k.cdc.MustUnmarshal(bz, &initialIssuance)
}
return initialIssuance.Issuance
}
// PeekEpochSeigniorage returns epoch seigniorage
func (k Keeper) PeekEpochSeigniorage(ctx sdk.Context) sdk.Int {
epochIssuance := k.bankKeeper.GetSupply(ctx, core.MicroLunaDenom).Amount
preEpochIssuance := k.GetEpochInitialIssuance(ctx).AmountOf(core.MicroLunaDenom)
epochSeigniorage := preEpochIssuance.Sub(epochIssuance)
if epochSeigniorage.IsNegative() {
return sdk.ZeroInt()
}
return epochSeigniorage
}
// GetTR returns the tax rewards for the epoch
func (k Keeper) GetTR(ctx sdk.Context, epoch int64) sdk.Dec {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetTRKey(epoch))
dp := sdk.DecProto{}
if bz == nil {
dp.Dec = sdk.ZeroDec()
} else {
k.cdc.MustUnmarshal(bz, &dp)
}
return dp.Dec
}
// SetTR stores the tax rewards for the epoch
func (k Keeper) SetTR(ctx sdk.Context, epoch int64, tr sdk.Dec) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&sdk.DecProto{Dec: tr})
store.Set(types.GetTRKey(epoch), bz)
}
// ClearTRs delete all tax rewards from the store
func (k Keeper) ClearTRs(ctx sdk.Context) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.TRKey)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
store.Delete(iter.Key())
}
}
// GetSR returns the seigniorage rewards for the epoch
func (k Keeper) GetSR(ctx sdk.Context, epoch int64) sdk.Dec {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetSRKey(epoch))
dp := sdk.DecProto{}
if bz == nil {
dp.Dec = sdk.ZeroDec()
} else {
k.cdc.MustUnmarshal(bz, &dp)
}
return dp.Dec
}
// SetSR stores the seigniorage rewards for the epoch
func (k Keeper) SetSR(ctx sdk.Context, epoch int64, sr sdk.Dec) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&sdk.DecProto{Dec: sr})
store.Set(types.GetSRKey(epoch), bz)
}
// ClearSRs delete all seigniorage rewards from the store
func (k Keeper) ClearSRs(ctx sdk.Context) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.SRKey)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
store.Delete(iter.Key())
}
}
// GetTSL returns the total staked luna for the epoch
func (k Keeper) GetTSL(ctx sdk.Context, epoch int64) sdk.Int {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetTSLKey(epoch))
ip := sdk.IntProto{}
if bz == nil {
ip.Int = sdk.ZeroInt()
} else {
k.cdc.MustUnmarshal(bz, &ip)
}
return ip.Int
}
// SetTSL stores the total staked luna for the epoch
func (k Keeper) SetTSL(ctx sdk.Context, epoch int64, tsl sdk.Int) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&sdk.IntProto{Int: tsl})
store.Set(types.GetTSLKey(epoch), bz)
}
// ClearTSLs delete all the total staked luna from the store
func (k Keeper) ClearTSLs(ctx sdk.Context) {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStorePrefixIterator(store, types.TSLKey)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
store.Delete(iter.Key())
}
}
// Burn tax exemption list
func (k Keeper) AddBurnTaxExemptionAddress(ctx sdk.Context, address string) {
if _, err := sdk.AccAddressFromBech32(address); err != nil {
panic(err)
}
sub := prefix.NewStore(ctx.KVStore(k.storeKey), types.BurnTaxExemptionListPrefix)
sub.Set([]byte(address), []byte{0x01})
}
func (k Keeper) RemoveBurnTaxExemptionAddress(ctx sdk.Context, address string) error {
if _, err := sdk.AccAddressFromBech32(address); err != nil {
panic(err)
}
sub := prefix.NewStore(ctx.KVStore(k.storeKey), types.BurnTaxExemptionListPrefix)
if !sub.Has([]byte(address)) {
return types.ErrNoSuchBurnTaxExemptionAddress.Wrapf("address = %s", address)
}
sub.Delete([]byte(address))
return nil
}
func (k Keeper) HasBurnTaxExemptionAddress(ctx sdk.Context, addresses ...string) bool {
sub := prefix.NewStore(ctx.KVStore(k.storeKey), types.BurnTaxExemptionListPrefix)
for _, address := range addresses {
if !sub.Has([]byte(address)) {
return false
}
}
return true
}