This repository has been archived by the owner on Nov 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
keeper.go
80 lines (68 loc) · 2.71 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
package supply
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
distr "github.com/cosmos/cosmos-sdk/x/distribution"
"github.com/cosmos/cosmos-sdk/x/supply"
"github.com/tendermint/tendermint/libs/log"
)
// WrappedSupplyKeeper encapsulates the underlying supply keeper and overrides
// its BurnCoins function to send the coins to the community pool instead of
// just destroying them.
//
// Note that distrKeeper keeps the reference to the distr module keeper.
// Due to the circular dependency between supply-distr, distrKeeper
// cannot be initialized when the struct is created. Rather, SetDistrKeeper
// is expected to be called to set `distrKeeper`.
type WrappedSupplyKeeper struct {
supply.Keeper
distrKeeper *distr.Keeper
}
// WrapSupplyKeeperBurnToCommunityPool creates a new instance of WrappedSupplyKeeper
// with its distrKeeper member set to nil.
func WrapSupplyKeeperBurnToCommunityPool(sk supply.Keeper) WrappedSupplyKeeper {
return WrappedSupplyKeeper{sk, nil}
}
// SetDistrKeeper sets distr module keeper for this WrappedSupplyKeeper instance.
func (k *WrappedSupplyKeeper) SetDistrKeeper(distrKeeper *distr.Keeper) {
k.distrKeeper = distrKeeper
}
// Logger returns a module-specific logger.
func (k WrappedSupplyKeeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprint("x/wrappedSupply"))
}
// BurnCoins moves the specified amount of coins from the given module name to
// the community pool. The total supply of the coins will not change.
func (k WrappedSupplyKeeper) BurnCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error {
// If distrKeeper is not set OR we want to burn coins in distr itself, we will
// just use the original BurnCoins function.
if k.distrKeeper == nil || moduleName == distr.ModuleName {
return k.Keeper.BurnCoins(ctx, moduleName, amt)
}
// Create the account if it doesn't yet exist.
acc := k.GetModuleAccount(ctx, moduleName)
if acc == nil {
panic(sdkerrors.Wrapf(
sdkerrors.ErrUnknownAddress,
"module account %s does not exist", moduleName,
))
}
if !acc.HasPermission(supply.Burner) {
panic(sdkerrors.Wrapf(
sdkerrors.ErrUnauthorized,
"module account %s does not have permissions to burn tokens",
moduleName,
))
}
// Instead of burning coins, we send them to the community pool.
k.SendCoinsFromModuleToModule(ctx, moduleName, distr.ModuleName, amt)
feePool := k.distrKeeper.GetFeePool(ctx)
feePool.CommunityPool = feePool.CommunityPool.Add(sdk.NewDecCoinsFromCoins(amt...)...)
k.distrKeeper.SetFeePool(ctx, feePool)
logger := k.Logger(ctx)
logger.Info(fmt.Sprintf(
"sent %s from %s module account to community pool", amt.String(), moduleName,
))
return nil
}